Skip to content

Commit 596c0b3

Browse files
authored
[Savestates] Various fixes (#275)
- **[Savestates] Various fixes** - **[Savestates] Fix client savestates being stored in the wrong folder** - **[Savestates] Group client savestates into folders like server savestates** - **[Savestates] Turn off rename gui**
2 parents 4978472 + 0f9d059 commit 596c0b3

File tree

12 files changed

+106
-42
lines changed

12 files changed

+106
-42
lines changed

src/main/java/com/minecrafttas/mctcommon/file/AbstractDataFile.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ protected AbstractDataFile(Path file, String name, String comment) {
5454
this.name = name;
5555
this.comment = comment;
5656
this.properties = new Properties();
57-
58-
try {
59-
createDirectory(file.getParent());
60-
} catch (IOException e) {
61-
MCTCommon.LOGGER.catching(e);
62-
}
6357
}
6458

6559
/**
@@ -97,6 +91,11 @@ protected void saveToProperties() {
9791
}
9892

9993
protected void saveToProperties(Path file) {
94+
try {
95+
createDirectory(file.getParent());
96+
} catch (IOException e) {
97+
MCTCommon.LOGGER.catching(e);
98+
}
10099
try {
101100
OutputStream fos = Files.newOutputStream(file);
102101
properties.store(fos, comment);
@@ -118,6 +117,11 @@ protected void saveToXML() {
118117
* @param file The file to save the {@link #properties} to
119118
*/
120119
protected void saveToXML(Path file) {
120+
try {
121+
createDirectory(file.getParent());
122+
} catch (IOException e) {
123+
MCTCommon.LOGGER.catching(e);
124+
}
121125
try {
122126
OutputStream fos = Files.newOutputStream(file);
123127
properties.storeToXML(fos, comment, "UTF-8");
@@ -132,6 +136,11 @@ protected void saveToJson() {
132136
}
133137

134138
protected void saveToJson(Path file) {
139+
try {
140+
createDirectory(file.getParent());
141+
} catch (IOException e) {
142+
MCTCommon.LOGGER.catching(e);
143+
}
135144
//@formatter:off
136145
Gson json = new GsonBuilder()
137146
.registerTypeAdapter(Properties.class, new PropertiesSerializer())

src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinMinecraft.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.minecrafttas.tasmod.mixin.playbackhooks;
22

3+
import org.lwjgl.input.Keyboard;
4+
import org.lwjgl.input.Mouse;
35
import org.spongepowered.asm.mixin.Mixin;
46
import org.spongepowered.asm.mixin.Shadow;
57
import org.spongepowered.asm.mixin.injection.At;
@@ -8,6 +10,7 @@
810
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
911

1012
import com.minecrafttas.tasmod.TASmodClient;
13+
import com.minecrafttas.tasmod.virtual.SubtickGuiScreen;
1114
import com.minecrafttas.tasmod.virtual.VirtualInput;
1215
import com.minecrafttas.tasmod.virtual.VirtualInput.VirtualKeyboardInput;
1316
import com.minecrafttas.tasmod.virtual.VirtualInput.VirtualMouseInput;
@@ -16,6 +19,7 @@
1619

1720
import net.minecraft.client.Minecraft;
1821
import net.minecraft.client.gui.GuiScreen;
22+
import net.minecraft.client.settings.KeyBinding;
1923

2024
@Mixin(Minecraft.class)
2125
public class MixinMinecraft {
@@ -158,4 +162,29 @@ public int playback_redirectGetEventDWheel() {
158162
return TASmodClient.virtual.MOUSE.getEventMouseScrollWheel();
159163
}
160164

165+
// ============================ DisplayGui
166+
167+
@Redirect(method = "displayGuiScreen", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/settings/KeyBinding;unPressAllKeys()V"))
168+
private static void playback_unpressAllKeys(GuiScreen guiScreen) {
169+
if (guiScreen instanceof SubtickGuiScreen) {
170+
return;
171+
}
172+
KeyBinding.unPressAllKeys();
173+
}
174+
175+
@Redirect(method = "displayGuiScreen", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Mouse;next()Z"))
176+
private static boolean playback_mouseNext(GuiScreen guiScreen) {
177+
if (guiScreen instanceof SubtickGuiScreen) {
178+
return false;
179+
}
180+
return Mouse.next();
181+
}
182+
183+
@Redirect(method = "displayGuiScreen", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;next()Z"))
184+
private static boolean playback_keyboardNext(GuiScreen guiScreen) {
185+
if (guiScreen instanceof SubtickGuiScreen) {
186+
return false;
187+
}
188+
return Keyboard.next();
189+
}
161190
}

src/main/java/com/minecrafttas/tasmod/playback/PlaybackControllerClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ public void onClientInit(Minecraft mc) {
10741074
// Execute /restartandplay. Load the file to start from the config. If it exists load the playback file on start.
10751075
String fileOnStart = TASmodClient.config.get(TASmodConfig.FileToOpen);
10761076
if (fileOnStart.isEmpty()) {
1077-
fileOnStart = null;
1077+
return;
10781078
} else {
10791079
TASmodClient.config.reset(TASmodConfig.FileToOpen);
10801080
}

src/main/java/com/minecrafttas/tasmod/registries/TASmodKeybinds.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public enum TASmodKeybinds implements KeybindID {
4646
TASmodClient.virtual.CAMERA_ANGLE.updateNextCameraAngle(0, 45);
4747
}),
4848
TEST1("Various Testing", "TASmod", Keyboard.KEY_F12, () -> {
49+
Minecraft.getMinecraft().displayGuiScreen(null);
4950
}, VirtualKeybindings::isKeyDown),
5051
TEST2("Various Testing2", "TASmod", Keyboard.KEY_F7, () -> {
5152
}, VirtualKeybindings::isKeyDown);

src/main/java/com/minecrafttas/tasmod/registries/TASmodPackets.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.minecrafttas.mctcommon.networking.Client.Side;
55
import com.minecrafttas.mctcommon.networking.CompactPacketHandler;
66
import com.minecrafttas.mctcommon.networking.interfaces.PacketID;
7+
import com.minecrafttas.tasmod.TASmod;
78
import com.minecrafttas.tasmod.TASmodClient;
89
import com.minecrafttas.tasmod.playback.PlaybackControllerClient;
910
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TASstate;
@@ -106,7 +107,7 @@ public enum TASmodPackets implements PacketID {
106107
SAVESTATE_CLEAR_SCREEN(Side.CLIENT, (buf, clientID) -> {
107108
Minecraft mc = Minecraft.getMinecraft();
108109
if (mc.currentScreen instanceof GuiSavestate || mc.currentScreen instanceof GuiDownloadTerrain) {
109-
mc.addScheduledTask(() -> {
110+
TASmod.gameLoopSchedulerServer.add(() -> {
110111
mc.displayGuiScreen(null);
111112
});
112113
}

src/main/java/com/minecrafttas/tasmod/savestates/SavestateHandlerClient.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,16 @@ public static void savestate(String nameOfSavestate) throws SavestateException,
118118
return;
119119
}
120120

121-
createClientSavestateDirectory();
121+
Path targetfile = clientSavestateDirectory.resolve(nameOfSavestate + ".mctas").normalize();
122122

123-
Path targetfile = clientSavestateDirectory.resolve(nameOfSavestate + ".mctas");
123+
if (!targetfile.startsWith(clientSavestateDirectory)) {
124+
LOGGER.error("Could not create client savestate: Savestate won't be saved in savestate folder {}", targetfile);
125+
return;
126+
}
127+
128+
Path targetParentDir = targetfile.getParent();
129+
if (!Files.exists(targetParentDir))
130+
Files.createDirectories(targetParentDir);
124131

125132
PlaybackControllerClient container = TASmodClient.controller;
126133
if (container.isRecording()) {
@@ -146,6 +153,13 @@ public static void loadstate(String nameOfSavestate) throws Exception {
146153
return;
147154
}
148155

156+
Path targetfile = clientSavestateDirectory.resolve(nameOfSavestate + ".mctas").normalize();
157+
158+
if (!targetfile.startsWith(clientSavestateDirectory)) {
159+
LOGGER.error("Could not load client savestate: Savestate won't be saved in savestate folder {}", targetfile);
160+
return;
161+
}
162+
149163
PlaybackControllerClient controller = TASmodClient.controller;
150164

151165
TASstate state = controller.getState();
@@ -161,8 +175,6 @@ public static void loadstate(String nameOfSavestate) throws Exception {
161175
state = controller.getStateAfterPause();
162176
}
163177

164-
Path targetfile = clientSavestateDirectory.resolve(nameOfSavestate + ".mctas");
165-
166178
BigArrayList<InputContainer> savestateContainerList;
167179

168180
if (Files.exists(targetfile)) {
@@ -254,11 +266,6 @@ else if (state == TASstate.PLAYBACK) {
254266
});
255267
}
256268

257-
private static void createClientSavestateDirectory() throws IOException {
258-
LOGGER.trace(LoggerMarkers.Savestate, "Creating savestate directory at {}", clientSavestateDirectory);
259-
Files.createDirectories(clientSavestateDirectory);
260-
}
261-
262269
private static void preload(BigArrayList<InputContainer> containerList, long index) {
263270
LOGGER.trace(LoggerMarkers.Savestate, "Preloading container at index {}", index);
264271
InputContainer containerToPreload = containerList.get(index);

src/main/java/com/minecrafttas/tasmod/savestates/SavestateHandlerServer.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,12 @@ private void savestateInner(SavestatePaths paths, SavestateCallback cb, Savestat
177177
* Send the name of the world to all players. This will make a savestate of the
178178
* recording on the client with that name
179179
*/
180+
Path folder = paths.getSavestate().folder;
181+
Path savestateDir = folder.getParent().getParent();
182+
Path relativeFolder = savestateDir.relativize(folder);
180183
try {
181184
// savestate inputs client
182-
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.SAVESTATE_SAVE).writeString(paths.getSavestate().folder.toString()));
185+
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.SAVESTATE_SAVE).writeString(relativeFolder.toString()));
183186
} catch (Exception e) {
184187
e.printStackTrace();
185188
}
@@ -286,9 +289,12 @@ private void loadStateInner(SavestatePaths paths, SavestateCallback cb, Savestat
286289
* InputSavestate)
287290
*/
288291
if (!SavestateFlags.BLOCK_CLIENT_SAVESTATE.isBlocked(flags)) {
292+
Path folder = paths.getSavestate().folder;
293+
Path savestateDir = folder.getParent().getParent();
294+
Path relativeFolder = savestateDir.relativize(folder);
289295
try {
290296
// loadstate inputs client
291-
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.SAVESTATE_LOAD).writeString(paths.getSavestate().folder.toString()));
297+
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.SAVESTATE_LOAD).writeString(relativeFolder.toString()));
292298
} catch (Exception e) {
293299
e.printStackTrace();
294300
}
@@ -451,7 +457,8 @@ public void onServerPacket(PacketID id, ByteBuffer buf, String username) throws
451457
* Once the player is done renaming the savestate, the screens are cleared for all players.
452458
*/
453459
try {
454-
TASmod.server.sendTo(player, new TASmodBufferBuilder(TASmodPackets.SAVESTATE_RENAME_SCREEN).writeInt(paths.getSavestate().index));
460+
// TODO Currently desyncs the savestates... Bigger investigation necessary
461+
// TASmod.server.sendTo(player, new TASmodBufferBuilder(TASmodPackets.SAVESTATE_RENAME_SCREEN).writeInt(paths.getSavestate().index));
455462
} catch (Exception e) {
456463
LOGGER.catching(e);
457464
}

src/main/java/com/minecrafttas/tasmod/savestates/SavestateIndexer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public SavestatePaths loadSavestate(int index, boolean changeIndex) throws Loads
204204
throw new LoadstateException("msg.tasmod.savestate.error.filenoexist", missingFile);
205205
}
206206

207-
SavestatePaths out = SavestatePaths.of(currentSavestate.clone(), sourceDir, targetDir);
207+
SavestatePaths out = SavestatePaths.of(savestateToLoad.clone(), sourceDir, targetDir);
208208

209209
trackerfile.increaseLoadstateCount();
210210

src/main/java/com/minecrafttas/tasmod/savestates/gui/GuiSavestateRename.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public GuiSavestateRename(ITextComponent msg, int index) {
2727

2828
@Override
2929
public void initGui() {
30-
TASmodClient.virtual.clearNext();
3130
this.buttonList.clear();
3231
int boxWidth = 200;
3332
buttonList.add(new GuiButton(1, width / 2 - (boxWidth / 2) - 1, height / 2 + 62, boxWidth + 3, 20, new TextComponentTranslation("gui.tasmod.savestate.save.rename.button").getFormattedText()));
@@ -76,8 +75,6 @@ private void renameAndExit() {
7675
} catch (Exception e) {
7776
TASmod.LOGGER.catching(e);
7877
}
79-
TASmodClient.virtual.clearNext();
80-
mc.displayGuiScreen(null);
8178
}
8279

8380
@Override
@@ -87,6 +84,12 @@ public void updateScreen() {
8784

8885
@Override
8986
public void onGuiClosed() {
87+
TASmodClient.virtual.clearNext();
9088
Keyboard.enableRepeatEvents(false);
9189
}
90+
91+
@Override
92+
public boolean doesGuiPauseGame() {
93+
return false;
94+
}
9295
}

src/main/java/com/minecrafttas/tasmod/savestates/handlers/SavestateGuiHandlerClient.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.minecrafttas.mctcommon.networking.exception.WrongSideException;
1111
import com.minecrafttas.mctcommon.networking.interfaces.ClientPacketHandler;
1212
import com.minecrafttas.mctcommon.networking.interfaces.PacketID;
13+
import com.minecrafttas.tasmod.TASmodClient;
1314
import com.minecrafttas.tasmod.networking.TASmodBufferBuilder;
1415
import com.minecrafttas.tasmod.registries.TASmodPackets;
1516
import com.minecrafttas.tasmod.savestates.SavestateHandlerServer.SavestateState;
@@ -44,20 +45,18 @@ public void onClientPacket(PacketID id, ByteBuffer buf, String username) throws
4445
case SAVESTATE_LOADING_SCREEN:
4546
// Open Savestate screen
4647
SavestateState state = TASmodBufferBuilder.readEnum(SavestateState.class, buf);
47-
mc.addScheduledTask(() -> {
4848

49-
String msg = "";
50-
if (state == SavestateState.SAVING)
51-
msg = "gui.tasmod.savestate.save.start";
52-
else if (state == SavestateState.LOADING)
53-
msg = "gui.tasmod.savestate.load.start";
49+
String msg = "";
50+
if (state == SavestateState.SAVING)
51+
msg = "gui.tasmod.savestate.save.start";
52+
else if (state == SavestateState.LOADING)
53+
msg = "gui.tasmod.savestate.load.start";
5454

55-
mc.displayGuiScreen(new GuiSavestate(Component.translatable(msg).withStyle(TextFormatting.YELLOW).build()));
56-
});
55+
mc.displayGuiScreen(new GuiSavestate(Component.translatable(msg).withStyle(TextFormatting.YELLOW).build()));
5756
break;
5857
case SAVESTATE_RENAME_SCREEN:
5958
int index = TASmodBufferBuilder.readInt(buf);
60-
mc.addScheduledTask(() -> {
59+
TASmodClient.tickSchedulerClient.add(() -> {
6160
displayGuiRename(index);
6261
});
6362
break;

0 commit comments

Comments
 (0)