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

View File

@@ -343,7 +343,7 @@ public class ServerControl implements ApplicationListener{
handler.register("stop", "Stop hosting the server.", arg -> { handler.register("stop", "Stop hosting the server.", arg -> {
net.closeServer(); net.closeServer();
if(lastTask != null) lastTask.cancel(); cancelPlayTask();
state.set(State.menu); state.set(State.menu);
info("Stopped server."); info("Stopped server.");
}); });
@@ -354,7 +354,7 @@ public class ServerControl implements ApplicationListener{
return; return;
} }
if(lastTask != null) lastTask.cancel(); cancelPlayTask();
Gamemode preset = Gamemode.survival; Gamemode preset = Gamemode.survival;
@@ -1063,6 +1063,13 @@ public class ServerControl implements ApplicationListener{
maps.setNextMapOverride(map); 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. * Resets the world state, starts a new game.
* @param run What task to run to load a new world. * @param run What task to run to load a new world.
@@ -1078,11 +1085,11 @@ public class ServerControl implements ApplicationListener{
*/ */
public void play(boolean wait, Runnable run){ public void play(boolean wait, Runnable run){
inGameOverWait = true; inGameOverWait = true;
if(lastTask != null) lastTask.cancel(); cancelPlayTask();
Runnable r = () -> { Runnable reload = () -> {
try{
WorldReloader reloader = new WorldReloader(); WorldReloader reloader = new WorldReloader();
reloader.begin(); reloader.begin();
run.run(); run.run();
@@ -1092,24 +1099,16 @@ public class ServerControl implements ApplicationListener{
reloader.end(); reloader.end();
inGameOverWait = false; inGameOverWait = false;
};
if(wait){
lastTask = new Task(){
@Override
public void run(){
try{
r.run();
}catch(MapException e){ }catch(MapException e){
err("@: @", e.map.plainName(), e.getMessage()); err("@: @", e.map.plainName(), e.getMessage());
net.closeServer(); net.closeServer();
} }
}
}; };
Timer.schedule(lastTask, Config.roundExtraTime.num()); if(wait){
lastTask = Timer.schedule(reload, Config.roundExtraTime.num());
}else{ }else{
r.run(); reload.run();
} }
} }