Added autoPause config for servers (#7405)

* Extracted the method ServerControl.setPauseState()

* Added a comment to EventType.PlayerLeave

* Added the `pauseWithoutPlayers` config

* Revert "Extracted the method ServerControl.setPauseState()"

This reverts commit 45f5b01547.

* Renamed pauseWithoutPlayers to autoPause,
fixed manual- and automatic pausing to interfere
and made the server pause when hosting a new game

* Added a dot to the config description of `autoPause` and added my name to the contributors list

* Refactoring

* Merged remote-tracking changed I guess

* More refactoring

* More refactoring

* Removed logging about autoPause

* Removed another log I forgot in the last commit

Co-authored-by: Anuken <arnukren@gmail.com>
This commit is contained in:
zenonet 2022-08-21 16:32:58 +02:00 committed by GitHub
parent 62a3f30755
commit dde9f895cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 1 deletions

View File

@ -148,3 +148,4 @@ SMOLKEYS
1stvaliduser(SUS)
GlennFolker
BlackDeluxeCat
zenonet

View File

@ -554,6 +554,7 @@ public class EventType{
}
}
/** Called before a player leaves the game. */
public static class PlayerLeave{
public final Player player;

View File

@ -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;

View File

@ -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", "<on/off>", "Pause or unpause the game.", arg -> {
boolean pause = arg[0].equals("on");
autoPaused = false;
state.serverPaused = pause;
info(pause ? "Game paused." : "Game unpaused.");
});