More ServerControl improvements (#9038)

* More ServerControl improvements

* Update server/src/mindustry/server/ServerControl.java

---------

Co-authored-by: Anuken <arnukren@gmail.com>
This commit is contained in:
Darkness 2023-09-09 22:53:24 +03:00 committed by GitHub
parent 2b1557832d
commit b0cbe06e08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -343,7 +343,7 @@ public class ServerControl implements ApplicationListener{
handler.register("stop", "Stop hosting the server.", arg -> {
net.closeServer();
if(lastTask != null) lastTask.cancel();
cancelPlayTask();
state.set(State.menu);
info("Stopped server.");
});
@ -354,7 +354,7 @@ public class ServerControl implements ApplicationListener{
return;
}
if(lastTask != null) lastTask.cancel();
cancelPlayTask();
Gamemode preset = Gamemode.survival;
@ -1063,6 +1063,13 @@ public class ServerControl implements ApplicationListener{
maps.setNextMapOverride(map);
}
/**
* Cancels the world load timer task, if it is scheduled. Can be useful for stopping a server or hosting a new game.
*/
public void cancelPlayTask(){
if(lastTask != null) lastTask.cancel();
}
/**
* Resets the world state, starts a new game.
* @param run What task to run to load a new world.
@ -1078,38 +1085,30 @@ public class ServerControl implements ApplicationListener{
*/
public void play(boolean wait, Runnable run){
inGameOverWait = true;
if(lastTask != null) lastTask.cancel();
cancelPlayTask();
Runnable r = () -> {
WorldReloader reloader = new WorldReloader();
Runnable reload = () -> {
try{
WorldReloader reloader = new WorldReloader();
reloader.begin();
reloader.begin();
run.run();
run.run();
state.rules = state.map.applyRules(lastMode);
logic.play();
state.rules = state.map.applyRules(lastMode);
logic.play();
reloader.end();
inGameOverWait = false;
reloader.end();
inGameOverWait = false;
}catch(MapException e){
err("@: @", e.map.plainName(), e.getMessage());
net.closeServer();
}
};
if(wait){
lastTask = new Task(){
@Override
public void run(){
try{
r.run();
}catch(MapException e){
err("@: @", e.map.plainName(), e.getMessage());
net.closeServer();
}
}
};
Timer.schedule(lastTask, Config.roundExtraTime.num());
lastTask = Timer.schedule(reload, Config.roundExtraTime.num());
}else{
r.run();
reload.run();
}
}