Skip to content

Commit 054f6cb

Browse files
ScribbleScribble
authored andcommitted
Loading savestate 0 won't change savestate index anymore
1 parent d2e3049 commit 054f6cb

File tree

5 files changed

+109
-48
lines changed

5 files changed

+109
-48
lines changed

src/main/java/de/scribble/lp/tasmod/commands/fullplay/CommandFullPlay.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public String getUsage(ICommandSender sender) {
2727
@Override
2828
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
2929
try {
30-
TASmod.savestateHandler.loadState(0, false);
30+
TASmod.savestateHandler.loadState(0, false, false);
3131
} catch (LoadstateException e) {
3232
sender.sendMessage(new TextComponentString(TextFormatting.RED+"Failed to load a savestate: "+e.getMessage()));
3333
return;

src/main/java/de/scribble/lp/tasmod/commands/fullrecord/FullRecordPacket.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.scribble.lp.tasmod.commands.fullrecord;
22

3+
import de.scribble.lp.tasmod.ClientProxy;
34
import de.scribble.lp.tasmod.events.OpenGuiEvents;
45
import de.scribble.lp.tasmod.util.TASstate;
56
import io.netty.buffer.ByteBuf;
@@ -43,6 +44,7 @@ public IMessage onMessage(FullRecordPacket message, MessageContext ctx) {
4344
@SideOnly(Side.CLIENT)
4445
private void workaround(Minecraft mc) {
4546
OpenGuiEvents.stateWhenOpened = TASstate.RECORDING;
47+
ClientProxy.virtual.getContainer().clear();
4648
mc.world.sendQuittingDisconnectingPacket();
4749
mc.loadWorld((WorldClient) null);
4850
mc.displayGuiScreen(new net.minecraft.client.gui.GuiMainMenu());

src/main/java/de/scribble/lp/tasmod/inputcontainer/InputContainer.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -399,13 +399,17 @@ public Map<Integer, List<Pair<String, String[]>>> getControlBytes() {
399399
return controls;
400400
}
401401

402-
public void setIndex(int index) {
403-
this.index = index;
404-
if (state == TASstate.PLAYBACK) {
405-
TickInputContainer tickcontainer = inputs.get(index);
406-
this.keyboard = tickcontainer.getKeyboard();
407-
this.mouse = tickcontainer.getMouse();
408-
this.subticks = tickcontainer.getSubticks();
402+
public void setIndex(int index) throws IndexOutOfBoundsException{
403+
if(index<=size()) {
404+
this.index = index;
405+
if (state == TASstate.PLAYBACK) {
406+
TickInputContainer tickcontainer = inputs.get(index);
407+
this.keyboard = tickcontainer.getKeyboard();
408+
this.mouse = tickcontainer.getMouse();
409+
this.subticks = tickcontainer.getSubticks();
410+
}
411+
}else {
412+
throw new IndexOutOfBoundsException("Index is bigger than the container");
409413
}
410414
}
411415

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

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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() {

src/main/java/de/scribble/lp/tasmod/virtual/VirtualInput.java

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.util.Iterator;
77
import java.util.List;
88

9-
import de.scribble.lp.killtherng.repack.org.msgpack.core.annotations.Nullable;
109
import de.scribble.lp.tasmod.ClientProxy;
1110
import de.scribble.lp.tasmod.TASmod;
1211
import de.scribble.lp.tasmod.events.OpenGuiEvents;
@@ -125,7 +124,7 @@ public VirtualKeyboard getNextKeyboard() {
125124
* Loads the inputs and starts a TAS on initialize
126125
* @param fileToLoad (Nullable) Loads this filename and starts playing back the TAS
127126
*/
128-
public VirtualInput(@Nullable String fileToLoad) {
127+
public VirtualInput(String fileToLoad) {
129128
if (fileToLoad != null) {
130129
try {
131130
loadInputs(fileToLoad);
@@ -447,32 +446,66 @@ public void setContainer(InputContainer container) {
447446
* Loads and preloads the inputs from the new InputContainer to
448447
* {@link #container}
449448
*
449+
* Saving a savestate is done via {@linkplain de.scribble.lp.tasmod.util.ContainerSerialiser#saveToFileV1(File, InputContainer)} in {@linkplain de.scribble.lp.tasmod.savestates.client.InputSavestatesHandler#savestate(String)}
450+
*
450451
* @param savestatecontainer The container that should be loaded.
451452
*/
452-
public void loadSavestate(InputContainer savestatecontainer) {
453-
454-
if (this.container.isPlayingback()) {
455-
preloadInput(this.container, savestatecontainer.size() - 1); // Preloading from the current container and from the second to last index of
456-
// the savestatecontainer. Since this is executed during playback,
457-
// we will only load the position of the savestate container and not replace the
458-
// container itself
459-
460-
this.container.setIndex(savestatecontainer.size()); // Set the "playback" index of the current container to the latest index of the
461-
// savestatecontainer. Meaning this index will be played next
453+
public void loadClientSavestate(InputContainer savestatecontainer) {
454+
455+
if (container.isPlayingback()) {
456+
preloadInput(container, savestatecontainer.size() - 1); // Preloading from the current container and
457+
// from the second to last index of
458+
// the savestatecontainer. Since this is
459+
// executed during playback,
460+
// we will only load the position of the
461+
// savestate container and not replace the
462+
// container itself. This is due to the fact
463+
// that the playback would immediately end
464+
// when you replace the container.
465+
466+
if (container.size() >= savestatecontainer.size()) { // Check if the current container is bigger than the
467+
// savestated one.
468+
469+
try {
470+
container.setIndex(savestatecontainer.size()); // Set the "playback" index of the current
471+
// container to the latest index of the
472+
// savestatecontainer. Meaning this index will
473+
// be played next
474+
} catch (IndexOutOfBoundsException e) {
475+
e.printStackTrace();
476+
}
477+
} else {
478+
String start = savestatecontainer.getStartLocation();
479+
savestatecontainer.setStartLocation("");
480+
481+
try {
482+
savestatecontainer.setIndex(savestatecontainer.size() - 1);
483+
} catch (IndexOutOfBoundsException e) {
484+
e.printStackTrace();
485+
}
486+
savestatecontainer.setTASState(TASstate.PLAYBACK);
487+
savestatecontainer.setStartLocation(start);
488+
container = savestatecontainer;
489+
}
462490

463-
} else if (this.container.isRecording()) {
464-
String start = savestatecontainer.getStartLocation(); // TODO Another start location thing to keep in mind
491+
} else if (container.isRecording()) {
492+
String start = savestatecontainer.getStartLocation();
465493
preloadInput(savestatecontainer, savestatecontainer.size() - 1); // Preload the input of the savestate
466494

467495
nextKeyboard = new VirtualKeyboard(); // Unpress the nextKeyboard and mouse to get rid of the preloaded inputs in the
468496
// next keyboard. Note that these inputs are still loaded in the current
469497
// keyboard
470498
nextMouse = new VirtualMouse();
471499

472-
savestatecontainer.setIndex(savestatecontainer.size());
500+
try {
501+
savestatecontainer.setIndex(savestatecontainer.size());
502+
} catch(IndexOutOfBoundsException e) {
503+
e.printStackTrace();
504+
}
505+
473506
savestatecontainer.setTASState(TASstate.RECORDING);
474-
savestatecontainer.setStartLocation(start); // TODO Another one
475-
this.container = savestatecontainer; // Replace the current container with the savestated container
507+
savestatecontainer.setStartLocation(start);
508+
container = savestatecontainer; // Replace the current container with the savestated container
476509
}
477510
}
478511

0 commit comments

Comments
 (0)