@@ -83,6 +83,10 @@ public SavestateHandler(MinecraftServer server) {
8383 public void saveState () throws SavestateException , IOException {
8484 saveState (-1 , true );
8585 }
86+
87+ public void saveState (int savestateIndex , boolean tickrate0 ) throws SavestateException , IOException {
88+ saveState (savestateIndex , tickrate0 , true );
89+ }
8690
8791 /**
8892 * Creates a copy of the world that is currently being played and saves it in
@@ -94,10 +98,11 @@ public void saveState() throws SavestateException, IOException {
9498 * index<0 if it should save it in the next index from
9599 * the currentindex
96100 * @param tickrate0 When true: Set's the game to tickrate 0 after creating a savestate
101+ * @param changeIndex When true: Changes the index to the savestateIndex
97102 * @throws SavestateException
98103 * @throws IOException
99104 */
100- public void saveState (int savestateIndex , boolean tickrate0 ) throws SavestateException , IOException {
105+ public void saveState (int savestateIndex , boolean tickrate0 , boolean changeIndex ) throws SavestateException , IOException {
101106 if (state == SavestateState .SAVING ) {
102107 throw new SavestateException ("A savestating operation is already being carried out" );
103108 }
@@ -127,19 +132,19 @@ public void saveState(int savestateIndex, boolean tickrate0) throws SavestateExc
127132 refresh ();
128133
129134 // Setting the current index depending on the savestateIndex.
135+ int indexToSave =savestateIndex ;
130136 if (savestateIndex < 0 ) {
131- setCurrentIndex (currentIndex + 1 ); // If the savestateIndex <= 0, create a savestate at currentIndex+1
132- } else {
133- setCurrentIndex (savestateIndex );
134- }
137+ indexToSave =currentIndex + 1 ; // If the savestateIndex <= 0, create a savestate at currentIndex+1
138+ }
139+ setCurrentIndex (indexToSave , changeIndex );
135140
136141 // Get the current and target directory for copying
137142 String worldname = server .getFolderName ();
138143 File currentfolder = new File (savestateDirectory , ".." + File .separator + worldname );
139- File targetfolder = getSavestateFile (currentIndex );
144+ File targetfolder = getSavestateFile (indexToSave );
140145
141146 if (targetfolder .exists ()) {
142- TASmod .logger .warn ("WARNING! Overwriting the savestate with the index {}" , currentIndex );
147+ TASmod .logger .warn ("WARNING! Overwriting the savestate with the index {}" , indexToSave );
143148 FileUtils .deleteDirectory (targetfolder );
144149 }
145150
@@ -152,7 +157,7 @@ public void saveState(int savestateIndex, boolean tickrate0) throws SavestateExc
152157 * Send the name of the world to all players. This will make a savestate of the
153158 * recording on the client with that name
154159 */
155- CommonProxy .NETWORK .sendToAll (new InputSavestatesPacket (true , getSavestateName (currentIndex )));
160+ CommonProxy .NETWORK .sendToAll (new InputSavestatesPacket (true , getSavestateName (indexToSave )));
156161 }
157162
158163 // Wait for the chunkloader to save the game
@@ -173,7 +178,7 @@ public void saveState(int savestateIndex, boolean tickrate0) throws SavestateExc
173178 saveCurrentIndexToFile ();
174179
175180 // Send a notification that the savestate has been loaded
176- server .getPlayerList ().sendMessage (new TextComponentString (TextFormatting .GREEN + "Savestate " + currentIndex + " saved" ));
181+ server .getPlayerList ().sendMessage (new TextComponentString (TextFormatting .GREEN + "Savestate " + indexToSave + " saved" ));
177182
178183 // Close the GuiSavestateScreen on the client
179184 CommonProxy .NETWORK .sendToAll (new SavestatePacket ());
@@ -198,6 +203,18 @@ public void saveState(int savestateIndex, boolean tickrate0) throws SavestateExc
198203 public void loadState () throws LoadstateException , IOException {
199204 loadState (-1 , true );
200205 }
206+
207+ /**
208+ *
209+ * @param savestateIndex
210+ * @param tickrate0
211+ *
212+ * @throws LoadstateException
213+ * @throws IOException
214+ */
215+ public void loadState (int savestateIndex , boolean tickrate0 ) throws LoadstateException , IOException {
216+ loadState (savestateIndex , tickrate0 , true );
217+ }
201218
202219 /**
203220 * Loads the latest savestate it can find in
@@ -208,10 +225,11 @@ public void loadState() throws LoadstateException, IOException {
208225 * @param savestateIndex The index where the mod will load the savestate.
209226 * index<0 if it should load the currentindex
210227 * @param tickrate0 When true: Set's the game to tickrate 0 after creating a savestate
228+ * @param changeIndex When true: Changes the index to the savestateIndex
211229 * @throws LoadstateException
212230 * @throws IOException
213231 */
214- public void loadState (int savestateIndex , boolean tickrate0 ) throws LoadstateException , IOException {
232+ public void loadState (int savestateIndex , boolean tickrate0 , boolean changeIndex ) throws LoadstateException , IOException {
215233 if (state == SavestateState .SAVING ) {
216234 throw new LoadstateException ("A savestating operation is already being carried out" );
217235 }
@@ -234,16 +252,16 @@ public void loadState(int savestateIndex, boolean tickrate0) throws LoadstateExc
234252
235253 int indexToLoad = savestateIndex < 0 ? currentIndex : savestateIndex ;
236254
237- if (! getSavestateFile (indexToLoad ).exists ()) {
238- throw new LoadstateException ( "Savestate " + indexToLoad + " doesn't exist" );
255+ if (getSavestateFile (indexToLoad ).exists ()) {
256+ setCurrentIndex ( indexToLoad , changeIndex );
239257 } else {
240- setCurrentIndex ( indexToLoad );
258+ throw new LoadstateException ( "Savestate " + indexToLoad + " doesn't exist" );
241259 }
242260
243261 // Get the current and target directory for copying
244262 String worldname = server .getFolderName ();
245263 File currentfolder = new File (savestateDirectory , ".." + File .separator + worldname );
246- File targetfolder = getSavestateFile (currentIndex );
264+ File targetfolder = getSavestateFile (indexToLoad );
247265
248266 /*
249267 * Prevents loading an InputSavestate when loading index 0 (Index 0 is the
@@ -252,7 +270,7 @@ public void loadState(int savestateIndex, boolean tickrate0) throws LoadstateExc
252270 */
253271 if (savestateIndex != 0 ) {
254272 // Load savestate on the client
255- CommonProxy .NETWORK .sendToAll (new InputSavestatesPacket (false , getSavestateName (currentIndex )));
273+ CommonProxy .NETWORK .sendToAll (new InputSavestatesPacket (false , getSavestateName (indexToLoad )));
256274 }
257275
258276 // Disabeling level saving for all worlds in case the auto save kicks in during
@@ -293,7 +311,7 @@ public void loadState(int savestateIndex, boolean tickrate0) throws LoadstateExc
293311 saveCurrentIndexToFile ();
294312
295313 // Send a notification that the savestate has been loaded
296- server .getPlayerList ().sendMessage (new TextComponentString (TextFormatting .GREEN + "Savestate " + currentIndex + " loaded" ));
314+ server .getPlayerList ().sendMessage (new TextComponentString (TextFormatting .GREEN + "Savestate " + indexToLoad + " loaded" ));
297315
298316 WorldServer [] worlds = DimensionManager .getWorlds ();
299317
@@ -404,7 +422,7 @@ public void deleteSavestate(int index) throws SavestateDeleteException {
404422 }
405423 refresh ();
406424 if (!indexList .contains (currentIndex )) {
407- setCurrentIndex (latestIndex );
425+ setCurrentIndex (latestIndex , true );
408426 }
409427 // Send a notification that the savestate has been deleted
410428 server .getPlayerList ().sendMessage (new TextComponentString (TextFormatting .GREEN + "Savestate " + index + " deleted" ));
@@ -496,16 +514,20 @@ public void loadCurrentIndexFromFile() {
496514 }
497515 }
498516 }
499- setCurrentIndex (index );
517+ setCurrentIndex (index , true );
500518 }
501519
502- private void setCurrentIndex (int index ) {
503- if (index < 0 ) {
504- currentIndex = latestIndex ;
520+ private void setCurrentIndex (int index , boolean changeIndex ) {
521+ if (changeIndex ) {
522+ if (index < 0 ) {
523+ currentIndex = latestIndex ;
524+ } else {
525+ currentIndex = index ;
526+ }
527+ TASmod .logger .info ("Setting the savestate index to {}" , currentIndex );
505528 } else {
506- currentIndex = index ;
529+ TASmod . logger . warn ( "Keeping the savestate index at {}" , currentIndex ) ;
507530 }
508- TASmod .logger .info ("Setting the savestate index to {}" , currentIndex );
509531 }
510532
511533 public int getCurrentIndex () {
0 commit comments