Skip to content

Commit 90e7c43

Browse files
committed
[Savestates] Update Gui usage during savestate loading/saving
- Add SubtickGuiScreen to use a GUI while in tickrate 0 - [Networking] Add writeEnum to Bufferbuilder and remove special TASmodBufferBuilder cases involving enums
1 parent b070d48 commit 90e7c43

28 files changed

+209
-177
lines changed

src/main/java/com/minecrafttas/mctcommon/networking/ByteBufferBuilder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ public ByteBufferBuilder writeByteArray(byte[] value) {
135135
return this;
136136
}
137137

138+
public ByteBufferBuilder writeEnum(Enum<?> state) {
139+
this.writeShort((short) state.ordinal());
140+
return this;
141+
}
142+
138143
/**
139144
* Unlocks the buffer from the pool making it available for other uses
140145
*/
@@ -201,6 +206,12 @@ public static byte[] readByteArray(ByteBuffer buf) {
201206
return array;
202207
}
203208

209+
public static <T extends Enum<?>> T readEnum(Class<T> clazz, ByteBuffer buf) {
210+
if (!clazz.isEnum())
211+
throw new IllegalArgumentException("Class is not an enum");
212+
return clazz.getEnumConstants()[buf.getShort()];
213+
}
214+
204215
/**
205216
* Debug packetname
206217
*

src/main/java/com/minecrafttas/tasmod/TASmod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.minecrafttas.tasmod.commands.CommandSaveTAS;
2424
import com.minecrafttas.tasmod.commands.CommandSavestate;
2525
import com.minecrafttas.tasmod.commands.CommandTickrate;
26-
import com.minecrafttas.tasmod.commands.TabCompletionUtils;
2726
import com.minecrafttas.tasmod.handlers.PlayUntilHandler;
2827
import com.minecrafttas.tasmod.playback.PlaybackControllerServer;
2928
import com.minecrafttas.tasmod.playback.metadata.builtin.StartpositionMetadataExtension;
@@ -37,6 +36,7 @@
3736
import com.minecrafttas.tasmod.ticksync.TickSyncServer;
3837
import com.minecrafttas.tasmod.util.LoggerMarkers;
3938
import com.minecrafttas.tasmod.util.Scheduler;
39+
import com.minecrafttas.tasmod.util.TabCompletionUtils;
4040

4141
import net.fabricmc.api.ModInitializer;
4242
import net.fabricmc.loader.api.FabricLoader;

src/main/java/com/minecrafttas/tasmod/commands/CommandFullPlay.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.minecrafttas.tasmod.networking.TASmodBufferBuilder;
55
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TASstate;
66
import com.minecrafttas.tasmod.registries.TASmodPackets;
7+
import com.minecrafttas.tasmod.savestates.SavestateHandlerServer.SavestateCallback;
78
import com.minecrafttas.tasmod.savestates.SavestateHandlerServer.SavestateFlags;
89
import com.minecrafttas.tasmod.savestates.exceptions.LoadstateException;
910

@@ -28,8 +29,17 @@ public String getUsage(ICommandSender sender) {
2829

2930
@Override
3031
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
32+
33+
SavestateCallback cb = (paths) -> {
34+
try {
35+
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.SAVESTATE_CLEAR_SCREEN));
36+
} catch (Exception e) {
37+
TASmod.LOGGER.catching(e);
38+
}
39+
};
40+
3141
try {
32-
TASmod.savestateHandlerServer.loadState(0, null, SavestateFlags.BLOCK_CHANGE_INDEX, SavestateFlags.BLOCK_PAUSE_TICKRATE);
42+
TASmod.savestateHandlerServer.loadState(0, cb, SavestateFlags.BLOCK_CHANGE_INDEX, SavestateFlags.BLOCK_PAUSE_TICKRATE);
3343
} catch (LoadstateException e) {
3444
sender.sendMessage(new TextComponentString(TextFormatting.RED + "Failed to load a savestate: " + e.getMessage()));
3545
return;

src/main/java/com/minecrafttas/tasmod/commands/CommandFullRecord.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.minecrafttas.tasmod.networking.TASmodBufferBuilder;
55
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TASstate;
66
import com.minecrafttas.tasmod.registries.TASmodPackets;
7+
import com.minecrafttas.tasmod.savestates.SavestateHandlerServer.SavestateCallback;
78
import com.minecrafttas.tasmod.savestates.SavestateHandlerServer.SavestateFlags;
89
import com.minecrafttas.tasmod.savestates.exceptions.SavestateException;
910

@@ -28,15 +29,20 @@ public String getUsage(ICommandSender sender) {
2829

2930
@Override
3031
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
32+
33+
SavestateCallback cb = (paths) -> {
34+
try {
35+
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.SAVESTATE_CLEAR_SCREEN));
36+
} catch (Exception e) {
37+
TASmod.LOGGER.catching(e);
38+
}
39+
};
40+
3141
try {
32-
TASmod.savestateHandlerServer.saveState(0, null, SavestateFlags.BLOCK_PAUSE_TICKRATE);
42+
TASmod.savestateHandlerServer.saveState(0, cb, SavestateFlags.BLOCK_PAUSE_TICKRATE);
3343
} catch (SavestateException e) {
3444
sender.sendMessage(new TextComponentString(TextFormatting.RED + "Failed to create a savestate: " + e.getMessage()));
3545
return;
36-
} catch (Exception e) {
37-
e.printStackTrace();
38-
sender.sendMessage(new TextComponentString(TextFormatting.RED + "Failed to create a savestate: " + e.getCause().toString()));
39-
return;
4046
} finally {
4147
TASmod.savestateHandlerServer.resetState();
4248
}

src/main/java/com/minecrafttas/tasmod/commands/CommandSavestate.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -413,27 +413,18 @@ private void infoAll(ICommandSender sender) {
413413
private void saveNew(ICommandSender sender) {
414414
TASmod.LOGGER.trace(LoggerMarkers.Savestate, "Command SaveNew");
415415

416-
SavestateCallback doneSavingCallback = (paths -> {
417-
if (sender instanceof EntityPlayerMP) {
418-
try {
419-
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.SAVESTATE_RENAME_SCREEN).writeInt(paths.getSavestate().getIndex()).writeString(sender.getName()));
420-
} catch (Exception e) {
421-
onFailure(sender, e);
422-
}
423-
}
424-
});
425-
426416
try {
427-
TASmod.savestateHandlerServer.saveState(doneSavingCallback);
417+
TASmod.savestateHandlerServer.saveState(createCallback(sender));
428418
} catch (SavestateException e) {
429419
onFailure(sender, e);
430420
}
431421
}
432422

433423
private void saveIndex(ICommandSender sender, int index) {
434424
TASmod.LOGGER.trace(LoggerMarkers.Savestate, "Command SaveIndex {}", index);
425+
435426
try {
436-
TASmod.savestateHandlerServer.saveState(index, null);
427+
TASmod.savestateHandlerServer.saveState(index, createCallback(sender));
437428
} catch (SavestateException e) {
438429
onFailure(sender, e);
439430
}
@@ -442,7 +433,7 @@ private void saveIndex(ICommandSender sender, int index) {
442433
private void saveIndexName(ICommandSender sender, int index, String name) {
443434
TASmod.LOGGER.trace(LoggerMarkers.Savestate, "Command SaveNameIndex {}|{}", index, name);
444435
try {
445-
TASmod.savestateHandlerServer.saveState(index, name, null);
436+
TASmod.savestateHandlerServer.saveState(index, name, createCallback(sender));
446437
} catch (SavestateException e) {
447438
onFailure(sender, e);
448439
}
@@ -451,7 +442,7 @@ private void saveIndexName(ICommandSender sender, int index, String name) {
451442
private void saveName(ICommandSender sender, String name) {
452443
TASmod.LOGGER.trace(LoggerMarkers.Savestate, "Command SaveName {}", name);
453444
try {
454-
TASmod.savestateHandlerServer.saveState(name, null);
445+
TASmod.savestateHandlerServer.saveState(name, createCallback(sender));
455446
} catch (SavestateException e) {
456447
onFailure(sender, e);
457448
}
@@ -460,7 +451,7 @@ private void saveName(ICommandSender sender, String name) {
460451
private void loadRecent(ICommandSender sender) {
461452
TASmod.LOGGER.trace(LoggerMarkers.Savestate, "Command LoadRecent");
462453
try {
463-
TASmod.savestateHandlerServer.loadState(null);
454+
TASmod.savestateHandlerServer.loadState(createCallback(sender));
464455
} catch (LoadstateException e) {
465456
onFailure(sender, e);
466457
}
@@ -469,7 +460,7 @@ private void loadRecent(ICommandSender sender) {
469460
private void loadIndex(ICommandSender sender, int index) {
470461
TASmod.LOGGER.trace(LoggerMarkers.Savestate, "Command LoadIndex {}", index);
471462
try {
472-
TASmod.savestateHandlerServer.loadState(index, null);
463+
TASmod.savestateHandlerServer.loadState(index, createCallback(sender));
473464
} catch (LoadstateException e) {
474465
onFailure(sender, e);
475466
}
@@ -479,7 +470,7 @@ private void delete(ICommandSender sender, int index) {
479470
TASmod.LOGGER.trace(LoggerMarkers.Savestate, "Command Delete {}", index);
480471

481472
SavestateCallback cb = (paths) -> {
482-
sender.sendMessage(Component.translatable("msg.lotaslight.savestate.delete", paths.getSavestate().getIndex()).withStyle(TextFormatting.GREEN).build());
473+
sender.sendMessage(Component.translatable("msg.tasmod.savestate.delete", paths.getSavestate().getIndex()).withStyle(TextFormatting.GREEN).build());
483474
};
484475

485476
try {
@@ -600,4 +591,16 @@ private static void onFailure(ICommandSender sender, Throwable e) {
600591
TASmod.LOGGER.catching(e);
601592
TASmod.savestateHandlerServer.resetState();
602593
}
594+
595+
private SavestateCallback createCallback(ICommandSender sender) {
596+
return (paths -> {
597+
if (sender instanceof EntityPlayerMP) {
598+
try {
599+
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.SAVESTATE_CLEAR_SCREEN));
600+
} catch (Exception e) {
601+
onFailure(sender, e);
602+
}
603+
}
604+
});
605+
}
603606
}

src/main/java/com/minecrafttas/tasmod/handlers/LoadingScreenHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void onSetCameraAngle() {
9595
*/
9696
@Override
9797
public void onPlayerJoinedClientSide(EntityPlayerSP player) {
98-
TASmodClient.virtual.clearKeys();
98+
TASmodClient.virtual.clearNext();
9999
}
100100

101101
/**

src/main/java/com/minecrafttas/tasmod/networking/TASmodBufferBuilder.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99

1010
import com.minecrafttas.mctcommon.networking.ByteBufferBuilder;
1111
import com.minecrafttas.mctcommon.networking.interfaces.PacketID;
12-
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TASstate;
1312
import com.minecrafttas.tasmod.savestates.storage.builtin.ClientMotionStorage.MotionData;
14-
import com.minecrafttas.tasmod.tickratechanger.TickrateChangerServer.TickratePauseState;
1513

1614
import net.minecraft.nbt.CompressedStreamTools;
1715
import net.minecraft.nbt.NBTTagCompound;
@@ -30,11 +28,6 @@ public TASmodBufferBuilder(ByteBuffer buf) {
3028
super(buf);
3129
}
3230

33-
public TASmodBufferBuilder writeTASState(TASstate state) {
34-
this.writeShort((short) state.ordinal());
35-
return this;
36-
}
37-
3831
public TASmodBufferBuilder writeNBTTagCompound(NBTTagCompound compound) {
3932

4033
ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -59,11 +52,6 @@ public TASmodBufferBuilder writeNBTTagCompound(NBTTagCompound compound) {
5952
return this;
6053
}
6154

62-
public TASmodBufferBuilder writeTickratePauseState(TickratePauseState state) {
63-
writeShort((short) state.ordinal());
64-
return this;
65-
}
66-
6755
public TASmodBufferBuilder writeMotionData(MotionData data) {
6856
writeDouble(data.getClientX());
6957
writeDouble(data.getClientY());
@@ -76,10 +64,6 @@ public TASmodBufferBuilder writeMotionData(MotionData data) {
7664
return this;
7765
}
7866

79-
public static TASstate readTASState(ByteBuffer buf) {
80-
return TASstate.values()[buf.getShort()];
81-
}
82-
8367
public static NBTTagCompound readNBTTagCompound(ByteBuffer buf) throws IOException {
8468
ByteArrayInputStream input = new ByteArrayInputStream(readByteArray(buf));
8569

@@ -93,10 +77,6 @@ public static NBTTagCompound readNBTTagCompound(ByteBuffer buf) throws IOExcepti
9377
return compound;
9478
}
9579

96-
public static TickratePauseState readTickratePauseState(ByteBuffer buf) {
97-
return TickratePauseState.values()[buf.getShort()];
98-
}
99-
10080
public static MotionData readMotionData(ByteBuffer buf) {
10181
double x = TASmodBufferBuilder.readDouble(buf);
10282
double y = TASmodBufferBuilder.readDouble(buf);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public PlaybackControllerClient() {
187187
*/
188188
public void setTASState(TASstate stateIn) {
189189
try {
190-
TASmodClient.client.send(new TASmodBufferBuilder(PLAYBACK_STATE).writeTASState(stateIn));
190+
TASmodClient.client.send(new TASmodBufferBuilder(PLAYBACK_STATE).writeEnum(stateIn));
191191
} catch (Exception e) {
192192
logger.catching(e);
193193
}
@@ -269,7 +269,7 @@ public String setTASStateClient(TASstate stateIn, boolean verbose) {
269269
LOGGER.debug(LoggerMarkers.Playback, "Pausing a playback");
270270
state = TASstate.PAUSED;
271271
stateAfterPause = TASstate.PLAYBACK;
272-
TASmodClient.virtual.clearKeys();
272+
TASmodClient.virtual.clearNext();
273273
return verbose ? TextFormatting.GREEN + "Pausing a playback" : "";
274274
case NONE:
275275
stopPlayback(true);
@@ -315,7 +315,7 @@ private void startRecording() {
315315

316316
private void stopRecording() {
317317
LOGGER.debug(LoggerMarkers.Playback, "Stopping a recording");
318-
TASmodClient.virtual.clearKeys();
318+
TASmodClient.virtual.clearNext();
319319
}
320320

321321
private void startPlayback() {
@@ -329,7 +329,7 @@ private void stopPlayback(boolean clearInputs) {
329329
LOGGER.debug(LoggerMarkers.Playback, "Stopping a playback");
330330
Minecraft.getMinecraft().gameSettings.chatLinks = true;
331331
if (clearInputs) {
332-
TASmodClient.virtual.clearKeys();
332+
TASmodClient.virtual.clearNext();
333333
}
334334
}
335335

@@ -1035,7 +1035,7 @@ public void onClientPacket(PacketID id, ByteBuffer buf, String username) throws
10351035
throw new WrongSideException(packet, Side.CLIENT);
10361036

10371037
case PLAYBACK_STATE:
1038-
TASstate networkState = TASmodBufferBuilder.readTASState(buf);
1038+
TASstate networkState = TASmodBufferBuilder.readEnum(TASstate.class, buf);
10391039
boolean verbose = TASmodBufferBuilder.readBoolean(buf);
10401040
Task task = () -> {
10411041
PlaybackControllerClient container = TASmodClient.controller;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void onServerPacket(PacketID id, ByteBuffer buf, String username) throws
5656
switch (packet) {
5757

5858
case PLAYBACK_STATE:
59-
TASstate networkState = TASmodBufferBuilder.readTASState(buf);
59+
TASstate networkState = TASmodBufferBuilder.readEnum(TASstate.class, buf);
6060
/* TODO Permissions */
6161
setState(networkState);
6262
break;
@@ -80,7 +80,7 @@ public void onServerPacket(PacketID id, ByteBuffer buf, String username) throws
8080
public void setState(TASstate stateIn) {
8181
setServerState(stateIn);
8282
try {
83-
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.PLAYBACK_STATE).writeTASState(state).writeBoolean(true));
83+
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.PLAYBACK_STATE).writeEnum(state).writeBoolean(true));
8484
} catch (Exception e) {
8585
e.printStackTrace();
8686
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
import com.minecrafttas.tasmod.TASmodClient;
88
import com.minecrafttas.tasmod.networking.TASmodBufferBuilder;
99
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TASstate;
10-
import com.minecrafttas.tasmod.savestates.gui.GuiSavestateRename;
1110
import com.minecrafttas.tasmod.virtual.VirtualKeybindings;
1211

1312
import net.minecraft.client.Minecraft;
1413
import net.minecraft.client.settings.KeyBinding;
15-
import net.minecraft.util.text.TextComponentString;
1614

1715
public enum TASmodKeybinds {
1816
TICKRATE_0("Tickrate 0 Key", "TASmod", Keyboard.KEY_F8, () -> TASmodClient.tickratechanger.togglePause(), VirtualKeybindings::isKeyDown),
@@ -47,7 +45,6 @@ public enum TASmodKeybinds {
4745
TASmodClient.virtual.CAMERA_ANGLE.updateNextCameraAngle(0, 45);
4846
}),
4947
TEST1("Various Testing", "TASmod", Keyboard.KEY_F12, () -> {
50-
Minecraft.getMinecraft().displayGuiScreen(new GuiSavestateRename(new TextComponentString("Test"), 1));
5148
}, VirtualKeybindings::isKeyDown),
5249
TEST2("Various Testing2", "TASmod", Keyboard.KEY_F7, () -> {
5350
}, VirtualKeybindings::isKeyDown);

0 commit comments

Comments
 (0)