diff --git a/core/assets/contributors b/core/assets/contributors index a6536f4f0a..61f5ef7943 100644 --- a/core/assets/contributors +++ b/core/assets/contributors @@ -148,3 +148,4 @@ SMOLKEYS 1stvaliduser(SUS) GlennFolker BlackDeluxeCat +zenonet diff --git a/core/src/mindustry/game/EventType.java b/core/src/mindustry/game/EventType.java index 6efcc19e04..faaad3d6be 100644 --- a/core/src/mindustry/game/EventType.java +++ b/core/src/mindustry/game/EventType.java @@ -554,6 +554,7 @@ public class EventType{ } } + /** Called before a player leaves the game. */ public static class PlayerLeave{ public final Player player; diff --git a/core/src/mindustry/net/Administration.java b/core/src/mindustry/net/Administration.java index 7982dce937..e6ed620551 100644 --- a/core/src/mindustry/net/Administration.java +++ b/core/src/mindustry/net/Administration.java @@ -487,7 +487,8 @@ public class Administration{ autosaveAmount = new Config("autosaveAmount", "The maximum amount of autosaves. Older ones get replaced.", 10), autosaveSpacing = new Config("autosaveSpacing", "Spacing between autosaves in seconds.", 60 * 5), debug = new Config("debug", "Enable debug logging", false, () -> Log.level = debug() ? LogLevel.debug : LogLevel.info), - snapshotInterval = new Config("snapshotInterval", "Client entity snapshot interval in ms.", 200); + snapshotInterval = new Config("snapshotInterval", "Client entity snapshot interval in ms.", 200), + autoPause = new Config("autoPause", "Whether the game should pause when nobody is online.", false); public final Object defaultValue; public final String name, key, description; diff --git a/server/src/mindustry/server/ServerControl.java b/server/src/mindustry/server/ServerControl.java index 6efe7f7ede..5440f775e1 100644 --- a/server/src/mindustry/server/ServerControl.java +++ b/server/src/mindustry/server/ServerControl.java @@ -64,6 +64,7 @@ public class ServerControl implements ApplicationListener{ private ServerSocket serverSocket; private PrintWriter socketOutput; private String suggested; + private boolean autoPaused = false; public ServerControl(String[] args){ setup(args); @@ -269,6 +270,23 @@ public class ServerControl implements ApplicationListener{ info("Server loaded. Type @ for help.", "'help'"); }); + + Events.on(PlayerJoin.class, e -> { + if(state.serverPaused && autoPaused && Config.autoPause.bool()){ + state.serverPaused = false; + autoPaused = false; + } + }); + + Events.on(PlayerLeave.class, e -> { + // The player list length is compared with 1 and not 0 here, + // because when PlayerLeave gets fired, the player hasn't been removed from the player list yet + if(!state.serverPaused && Config.autoPause.bool() && Groups.player.size() == 1){ + state.serverPaused = true; + autoPaused = true; + } + }); + } protected void registerCommands(){ @@ -352,6 +370,11 @@ public class ServerControl implements ApplicationListener{ info("Map loaded."); netServer.openServer(); + + if(Config.autoPause.bool()){ + state.serverPaused = true; + autoPaused = true; + } }catch(MapException e){ err(e.map.name() + ": " + e.getMessage()); } @@ -467,6 +490,7 @@ public class ServerControl implements ApplicationListener{ handler.register("pause", "", "Pause or unpause the game.", arg -> { boolean pause = arg[0].equals("on"); + autoPaused = false; state.serverPaused = pause; info(pause ? "Game paused." : "Game unpaused."); });