-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSavestateGuiHandlerClient.java
More file actions
97 lines (87 loc) · 3.41 KB
/
SavestateGuiHandlerClient.java
File metadata and controls
97 lines (87 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.minecrafttas.tasmod.savestates.handlers;
import static com.minecrafttas.tasmod.registries.TASmodPackets.SAVESTATE_LOADING_SCREEN;
import static com.minecrafttas.tasmod.registries.TASmodPackets.SAVESTATE_RENAME_SCREEN;
import java.nio.ByteBuffer;
import com.minecrafttas.mctcommon.networking.Client.Side;
import com.minecrafttas.mctcommon.networking.exception.PacketNotImplementedException;
import com.minecrafttas.mctcommon.networking.exception.WrongSideException;
import com.minecrafttas.mctcommon.networking.interfaces.ClientPacketHandler;
import com.minecrafttas.mctcommon.networking.interfaces.PacketID;
import com.minecrafttas.tasmod.TASmodClient;
import com.minecrafttas.tasmod.networking.TASmodBufferBuilder;
import com.minecrafttas.tasmod.registries.TASmodPackets;
import com.minecrafttas.tasmod.savestates.SavestateHandlerServer.SavestateState;
import com.minecrafttas.tasmod.savestates.gui.GuiSavestate;
import com.minecrafttas.tasmod.savestates.gui.GuiSavestateRename;
import com.minecrafttas.tasmod.util.Component;
import net.minecraft.client.Minecraft;
import net.minecraft.util.text.TextFormatting;
/**
* Handles displaying Gui screens for savestating on the client
*
* @author Scribble
*/
public class SavestateGuiHandlerClient implements ClientPacketHandler {
@Override
public PacketID[] getAcceptedPacketIDs() {
//@formatter:off
return new PacketID[] {
SAVESTATE_LOADING_SCREEN,
SAVESTATE_RENAME_SCREEN
};
//@formatter:on
}
@Override
public void onClientPacket(PacketID id, ByteBuffer buf, String username) throws PacketNotImplementedException, WrongSideException, Exception {
TASmodPackets packet = (TASmodPackets) id;
Minecraft mc = Minecraft.getMinecraft();
switch (packet) {
case SAVESTATE_LOADING_SCREEN:
// Open Savestate screen
SavestateState state = TASmodBufferBuilder.readEnum(SavestateState.class, buf);
TASmodClient.gameLoopSchedulerClient.add(() -> {
String msg = "";
if (state == SavestateState.SAVING)
msg = "gui.tasmod.savestate.save.start";
else if (state == SavestateState.LOADING)
msg = "gui.tasmod.savestate.load.start";
mc.displayGuiScreen(new GuiSavestate(Component.translatable(msg).withStyle(TextFormatting.YELLOW).build()));
});
break;
case SAVESTATE_RENAME_SCREEN:
int index = TASmodBufferBuilder.readInt(buf);
/*
* At the time of writing, the savestate rename screen
* is only opened when the savestate is triggered via a keybind
*
* However opening the screen would desync a running recording
* by displacing the player by a few units.
*
* The solution is to first clear the screen, then at the start of the next tick
* display the screen.
*
* Apparently showing a screen has a tiny influence on the motion of the client...
*/
mc.displayGuiScreen(null);
TASmodClient.tickSchedulerClient.add(() -> {
displayGuiRename(index);
});
break;
default:
throw new PacketNotImplementedException(packet, Side.CLIENT);
}
}
private void displayGuiRename(int index) {
Minecraft mc = Minecraft.getMinecraft();
//@formatter:off
mc.displayGuiScreen(
new GuiSavestateRename(
Component.translatable("gui.tasmod.savestate.save.rename",
Component.literal(Integer.toString(index)).withStyle(t->t.setColor(TextFormatting.AQUA))
).withStyle(t->t.setColor(TextFormatting.GREEN)).build(),
index
)
);
//@formatter:on
}
}