Skip to content

Commit e83b5ae

Browse files
ScribbleScribble
authored andcommitted
Added a state to savestatehandler
1 parent 67ff57f commit e83b5ae

File tree

6 files changed

+28
-26
lines changed

6 files changed

+28
-26
lines changed

src/main/java/de/scribble/lp/tasmod/mixin/MixinMinecraftServer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import de.scribble.lp.tasmod.CommonProxy;
1414
import de.scribble.lp.tasmod.TASmod;
1515
import de.scribble.lp.tasmod.savestates.server.SavestateHandler;
16+
import de.scribble.lp.tasmod.savestates.server.SavestateState;
1617
import de.scribble.lp.tasmod.tickratechanger.TickrateChangerServer;
1718
import de.scribble.lp.tasmod.ticksync.TickSyncPackage;
1819
import de.scribble.lp.tasmod.ticksync.TickSyncServer;
@@ -33,8 +34,8 @@ public long modifyMSPT(long fiftyLong) {
3334
@Redirect(method = "run", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;tick()V", ordinal = 1))
3435
public void redirectTick(MinecraftServer server) {
3536
this.tick();
36-
if (SavestateHandler.wasLoading) {
37-
SavestateHandler.wasLoading = false;
37+
if (SavestateHandler.state==SavestateState.WASLOADING) {
38+
SavestateHandler.state = SavestateState.NONE;
3839
SavestateHandler.playerLoadSavestateEventServer();
3940
}
4041

src/main/java/de/scribble/lp/tasmod/savestates/server/LoadstatePacketHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public IMessage onMessage(LoadstatePacket message, MessageContext ctx) {
3030
player.sendMessage(new TextComponentString(TextFormatting.RED+"Failed to load a savestate: "+e.getCause().toString()));
3131
e.printStackTrace();
3232
} finally {
33-
SavestateHandler.isLoading=false;
33+
SavestateHandler.state=SavestateState.NONE;
3434
}
3535
});
3636
}else {

src/main/java/de/scribble/lp/tasmod/savestates/server/SavestateHandler.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ public class SavestateHandler {
3939
private static MinecraftServer server=TASmod.getServerInstance();
4040
private static File savestateDirectory;
4141

42-
public static boolean isSaving=false;
43-
44-
public static boolean isLoading=false;
45-
public static boolean wasLoading=false;
42+
public static SavestateState state=SavestateState.NONE;
4643

4744
/**
4845
* Creates a copy of the currently played world and saves it in .minecraft/saves/savestates/worldname <br>
@@ -53,14 +50,14 @@ public class SavestateHandler {
5350
* @throws IOException
5451
*/
5552
public static void saveState() throws SavestateException, IOException {
56-
if(isSaving) {
53+
if(state==SavestateState.SAVING) {
5754
throw new SavestateException("A savestating operation is already being carried out");
5855
}
59-
if(isLoading) {
56+
if(state==SavestateState.LOADING) {
6057
throw new SavestateException("A loadstate operation is being carried out");
6158
}
6259
//Lock savestating and loadstating
63-
isSaving=true;
60+
state=SavestateState.SAVING;
6461

6562
//Create a directory just in case
6663
createSavestateDirectory();
@@ -107,7 +104,7 @@ public static void saveState() throws SavestateException, IOException {
107104
CommonProxy.NETWORK.sendToAll(new SavestatePacket());
108105

109106
//Unlock savestating
110-
isSaving=false;
107+
state=SavestateState.NONE;
111108
}
112109

113110
/**
@@ -121,10 +118,8 @@ private static File getNextSaveFolderLocation(String worldname) throws Savestate
121118
int i = 1;
122119
int limit=300;
123120
File targetsavefolder=null;
124-
isSaving=true;
125121
while (i <= limit) {
126122
if (i >= limit) {
127-
isSaving = false;
128123
throw new SavestateException("Savestatecount is greater or equal than "+limit);
129124
}
130125
targetsavefolder = new File(savestateDirectory,worldname + "-Savestate" + Integer.toString(i)+File.separator);
@@ -171,14 +166,14 @@ private static String nameWhenSaving(String worldname) {
171166
* @throws IOException
172167
*/
173168
public static void loadState() throws LoadstateException, IOException {
174-
if(isSaving) {
169+
if(state==SavestateState.SAVING) {
175170
throw new LoadstateException("A savestating operation is already being carried out");
176171
}
177-
if(isLoading) {
172+
if(state==SavestateState.LOADING) {
178173
throw new LoadstateException("A loadstate operation is being carried out");
179174
}
180175
//Lock savestating and loadstating
181-
isLoading=true;
176+
state=SavestateState.LOADING;
182177

183178
//Create a directory just in case
184179
createSavestateDirectory();
@@ -246,8 +241,7 @@ public static void loadState() throws LoadstateException, IOException {
246241
}
247242

248243
//Unlock loadstating
249-
isLoading=false;
250-
wasLoading=true;
244+
state=SavestateState.WASLOADING;
251245
}
252246

253247
/**

src/main/java/de/scribble/lp/tasmod/savestates/server/SavestatePacketHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public IMessage onMessage(SavestatePacket message, MessageContext ctx) {
3838
e.printStackTrace();
3939
player.sendMessage(new TextComponentString(TextFormatting.RED+"Failed to create a savestate: "+ e.getCause().toString()));
4040
} finally {
41-
SavestateHandler.isSaving=false;
41+
SavestateHandler.state=SavestateState.NONE;
4242
}
4343
});
4444
}else {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package de.scribble.lp.tasmod.savestates.server;
2+
3+
public enum SavestateState {
4+
SAVING,
5+
LOADING,
6+
WASLOADING,
7+
NONE
8+
}

src/main/java/de/scribble/lp/tasmod/tickratechanger/TickrateChangerServer.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package de.scribble.lp.tasmod.tickratechanger;
22

33
import de.scribble.lp.tasmod.CommonProxy;
4+
import de.scribble.lp.tasmod.TASmod;
45
import net.minecraft.entity.player.EntityPlayerMP;
56
import net.minecraft.server.MinecraftServer;
67

@@ -13,11 +14,6 @@ public class TickrateChangerServer {
1314
public static int cooldownKeyPause;
1415
public static int cooldownKeyAdvance;
1516

16-
private static MinecraftServer serverInstance;
17-
18-
public TickrateChangerServer(MinecraftServer server) {
19-
serverInstance=server;
20-
}
2117
public static void changeClientTickrate(float tickrate) {
2218
CommonProxy.NETWORK.sendToAll(new TickratePacket(false, tickrate, false));
2319
}
@@ -59,8 +55,11 @@ public static void advanceTick() {
5955
*/
6056
public static void leaveServer(EntityPlayerMP player) {
6157
//TODO Test this in multiplayer
62-
if (TickrateChangerServer.TICKS_PER_SECOND == 0) {
63-
TickrateChangerServer.changeServerTickrate(20F);
58+
if(TASmod.getServerInstance().getPlayerList().getCurrentPlayerCount()==1) {
59+
if (TickrateChangerServer.TICKS_PER_SECOND == 0 || ADVANCE_TICK) {
60+
TASmod.logger.info("Changing tickrate to 20 since the last player left the server");
61+
TickrateChangerServer.changeServerTickrate(20F);
62+
}
6463
}
6564
}
6665
}

0 commit comments

Comments
 (0)