Skip to content

Commit 1bae8b3

Browse files
committed
[Savestates] Add warning when tickrate is set to 0
- Change registering of keybinds - Fix error some error messages
1 parent 895344d commit 1bae8b3

File tree

11 files changed

+219
-98
lines changed

11 files changed

+219
-98
lines changed
Lines changed: 73 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.minecrafttas.mctcommon;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
3+
import java.util.HashMap;
4+
import java.util.Map;
55

66
import org.apache.commons.lang3.ArrayUtils;
77

@@ -21,47 +21,7 @@ public class KeybindManager implements EventClientGameLoop {
2121

2222
private final IsKeyDownFunc defaultFunction;
2323

24-
public static class Keybind {
25-
26-
public final KeyBinding vanillaKeyBinding;
27-
private final String category;
28-
private final Runnable onKeyDown;
29-
private final IsKeyDownFunc isKeyDownFunc;
30-
31-
/**
32-
* Initialize keybind
33-
*
34-
* @param name Name of keybind
35-
* @param category Category of keybind
36-
* @param defaultKey Default key of keybind
37-
* @param onKeyDown Will be run when the keybind is pressed
38-
*/
39-
public Keybind(String name, String category, int defaultKey, Runnable onKeyDown) {
40-
this(name, category, defaultKey, onKeyDown, null);
41-
}
42-
43-
/**
44-
* Initialize keybind with a different "isKeyDown" method
45-
*
46-
* @param name Name of keybind
47-
* @param category Category of keybind
48-
* @param defaultKey Default key of keybind
49-
* @param onKeyDown Will be run when the keybind is pressed
50-
*/
51-
public Keybind(String name, String category, int defaultKey, Runnable onKeyDown, IsKeyDownFunc func) {
52-
this.vanillaKeyBinding = new KeyBinding(name, defaultKey, category);
53-
this.category = category;
54-
this.onKeyDown = onKeyDown;
55-
this.isKeyDownFunc = func;
56-
}
57-
58-
@Override
59-
public String toString() {
60-
return this.vanillaKeyBinding.getKeyDescription();
61-
}
62-
}
63-
64-
private List<Keybind> keybindings;
24+
private Map<KeybindID, Keybind> keybindings;
6525

6626
/**
6727
* Initialize keybind manage
@@ -71,15 +31,15 @@ public String toString() {
7131
*/
7232
public KeybindManager(IsKeyDownFunc defaultFunction) {
7333
this.defaultFunction = defaultFunction;
74-
this.keybindings = new ArrayList<>();
34+
this.keybindings = new HashMap<>();
7535
}
7636

7737
/**
7838
* Handle registered keybindings on game loop
7939
*/
8040
@Override
8141
public void onRunClientGameLoop(Minecraft mc) {
82-
for (Keybind keybind : this.keybindings) {
42+
for (Keybind keybind : this.keybindings.values()) {
8343
IsKeyDownFunc keyDown = keybind.isKeyDownFunc != null ? keybind.isKeyDownFunc : defaultFunction;
8444
if (keyDown.isKeyDown(keybind.vanillaKeyBinding)) {
8545
keybind.onKeyDown.run();
@@ -88,26 +48,87 @@ public void onRunClientGameLoop(Minecraft mc) {
8848

8949
}
9050

51+
public void registerKeybinds(GameSettings options, Class<? extends KeybindID> keybindIDclass) {
52+
if (keybindIDclass.isEnum())
53+
registerKeybinds(options, keybindIDclass.getEnumConstants());
54+
}
55+
56+
public void registerKeybinds(GameSettings options, KeybindID... keybind) {
57+
for (KeybindID keybindEnum : keybind) {
58+
registerKeybind(options, keybindEnum, keybindEnum.getKeybind());
59+
}
60+
}
61+
9162
/**
92-
* Register new keybind
63+
* Register a new keybind
9364
*
94-
* @param keybind Keybind to register
95-
* @param options
65+
* @param keybindID The {@link KeybindID} to register this underI
66+
* @param keybind The {@link Keybind} to register
9667
*/
97-
public void registerKeybind(Keybind keybind, GameSettings options) {
98-
this.keybindings.add(keybind);
68+
public void registerKeybind(GameSettings options, KeybindID keybindID, Keybind keybind) {
69+
this.keybindings.put(keybindID, keybind);
9970
KeyBinding keyBinding = keybind.vanillaKeyBinding;
10071

101-
if (!AccessorKeyBinding.getCategoryOrder().containsKey(keybind.category))
102-
AccessorKeyBinding.getCategoryOrder().put(keybind.category, AccessorKeyBinding.getCategoryOrder().size() + 1);
72+
Map<String, Integer> categoryOrder = AccessorKeyBinding.getCategoryOrder();
73+
74+
if (!categoryOrder.containsKey(keybind.category))
75+
categoryOrder.put(keybind.category, categoryOrder.size() + 1);
10376

10477
// add keybinding
10578
options.keyBindings = ArrayUtils.add(options.keyBindings, keyBinding);
10679
}
10780

81+
public Keybind getKeybind(KeybindID id) {
82+
return keybindings.get(id);
83+
}
84+
10885
@FunctionalInterface
10986
public static interface IsKeyDownFunc {
11087

11188
public boolean isKeyDown(KeyBinding keybind);
11289
}
90+
91+
public static interface KeybindID {
92+
public Keybind getKeybind();
93+
}
94+
95+
public static class Keybind {
96+
97+
public final KeyBinding vanillaKeyBinding;
98+
private final String category;
99+
private final Runnable onKeyDown;
100+
private final IsKeyDownFunc isKeyDownFunc;
101+
102+
/**
103+
* Initialize keybind
104+
*
105+
* @param name Name of keybind
106+
* @param category Category of keybind
107+
* @param defaultKey Default key of keybind
108+
* @param onKeyDown Will be run when the keybind is pressed
109+
*/
110+
public Keybind(String name, String category, int defaultKey, Runnable onKeyDown) {
111+
this(name, category, defaultKey, onKeyDown, null);
112+
}
113+
114+
/**
115+
* Initialize keybind with a different "isKeyDown" method
116+
*
117+
* @param name Name of keybind
118+
* @param category Category of keybind
119+
* @param defaultKey Default key of keybind
120+
* @param onKeyDown Will be run when the keybind is pressed
121+
*/
122+
public Keybind(String name, String category, int defaultKey, Runnable onKeyDown, IsKeyDownFunc func) {
123+
this.vanillaKeyBinding = new KeyBinding(name, defaultKey, category);
124+
this.category = category;
125+
this.onKeyDown = onKeyDown;
126+
this.isKeyDownFunc = func;
127+
}
128+
129+
@Override
130+
public String toString() {
131+
return this.vanillaKeyBinding.getKeyDescription();
132+
}
133+
}
113134
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ public void onInitializeClient() {
126126

127127
registerConfigValues();
128128

129-
loadConfig(Minecraft.getMinecraft());
129+
Minecraft mc = Minecraft.getMinecraft();
130+
131+
loadConfig(mc);
130132

131133
virtual = new VirtualInput(LOGGER);
132134

@@ -315,7 +317,8 @@ private void initializeCustomPacketHandler() {
315317

316318
@Override
317319
public void onOptionsInit(GameSettings options) {
318-
Arrays.stream(TASmodKeybinds.valuesKeybind()).forEach((keybind) -> keybindManager.registerKeybind(keybind, options));
320+
// Initialize keybind manager
321+
keybindManager.registerKeybinds(options, TASmodKeybinds.class);
319322
Arrays.stream(TASmodKeybinds.valuesVanillaKeybind()).forEach(VirtualKeybindings::registerBlockedKeyBinding);
320323
}
321324

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,15 @@ private void saveNew(ICommandSender sender) {
438438
SavestateCallback cb = createChatMessageCallback(sender, "msg.tasmod.savestate.save.end");
439439

440440
TASmod.gameLoopSchedulerServer.add(() -> {
441-
442441
try {
443442
TASmod.savestateHandlerServer.saveState(cb);
444443
} catch (SavestateException e) {
445444
onFailure(sender, e);
445+
try {
446+
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.TICKRATE_0_WARN));
447+
} catch (Exception e1) {
448+
TASmod.LOGGER.catching(e);
449+
}
446450
}
447451
});
448452
}
@@ -464,6 +468,11 @@ private void saveIndex(ICommandSender sender, int index) {
464468
TASmod.savestateHandlerServer.saveState(index, cb);
465469
} catch (SavestateException e) {
466470
onFailure(sender, e);
471+
try {
472+
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.TICKRATE_0_WARN));
473+
} catch (Exception e1) {
474+
TASmod.LOGGER.catching(e);
475+
}
467476
}
468477
});
469478
}
@@ -485,6 +494,11 @@ private void saveIndexName(ICommandSender sender, int index, String name) {
485494
TASmod.savestateHandlerServer.saveState(index, name, cb);
486495
} catch (SavestateException e) {
487496
onFailure(sender, e);
497+
try {
498+
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.TICKRATE_0_WARN));
499+
} catch (Exception e1) {
500+
TASmod.LOGGER.catching(e);
501+
}
488502
}
489503
});
490504
}
@@ -499,6 +513,11 @@ private void saveName(ICommandSender sender, String name) {
499513
TASmod.savestateHandlerServer.saveState(name, cb);
500514
} catch (SavestateException e) {
501515
onFailure(sender, e);
516+
try {
517+
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.TICKRATE_0_WARN));
518+
} catch (Exception e1) {
519+
TASmod.LOGGER.catching(e);
520+
}
502521
}
503522
});
504523
}
@@ -513,6 +532,11 @@ private void loadRecent(ICommandSender sender) {
513532
TASmod.savestateHandlerServer.loadState(cb);
514533
} catch (LoadstateException e) {
515534
onFailure(sender, e);
535+
try {
536+
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.TICKRATE_0_WARN));
537+
} catch (Exception e1) {
538+
TASmod.LOGGER.catching(e);
539+
}
516540
}
517541
});
518542
}
@@ -531,6 +555,11 @@ private void loadIndex(ICommandSender sender, int index) {
531555
TASmod.savestateHandlerServer.loadState(index, cb);
532556
} catch (LoadstateException e) {
533557
onFailure(sender, e);
558+
try {
559+
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.TICKRATE_0_WARN));
560+
} catch (Exception e1) {
561+
TASmod.LOGGER.catching(e);
562+
}
534563
}
535564
});
536565
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public enum TASmodConfig implements ConfigOptions {
1111
FileToOpen("fileToOpen", ""),
1212
ServerConnection("serverConnection", ""),
1313
EnabledFileCommands("enabledFileCommands", "tasmod_desyncMonitor@v1, tasmod_label@v1, tasmod_options@v1"),
14-
SAVESTATE_SHOW_CONTROLS("savestateShowControls", "true");
14+
UnpauseWarn("unpauseWarn", "true");
1515

1616
private String configKey;
1717
private String defaultValue;

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.minecrafttas.mctcommon.KeybindManager.IsKeyDownFunc;
66
import com.minecrafttas.mctcommon.KeybindManager.Keybind;
7+
import com.minecrafttas.mctcommon.KeybindManager.KeybindID;
78
import com.minecrafttas.tasmod.TASmodClient;
89
import com.minecrafttas.tasmod.networking.TASmodBufferBuilder;
910
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TASstate;
@@ -12,7 +13,7 @@
1213
import net.minecraft.client.Minecraft;
1314
import net.minecraft.client.settings.KeyBinding;
1415

15-
public enum TASmodKeybinds {
16+
public enum TASmodKeybinds implements KeybindID {
1617
TICKRATE_0("Tickrate 0 Key", "TASmod", Keyboard.KEY_F8, () -> TASmodClient.tickratechanger.togglePause(), VirtualKeybindings::isKeyDown),
1718
TICKRATE_ADVANCE("Advance Tick", "TASmod", Keyboard.KEY_F9, () -> TASmodClient.tickratechanger.advanceTick(), VirtualKeybindings::isKeyDown),
1819
TICKRATE_INCREASE("Increase Tickrate", "TASmod", Keyboard.KEY_PERIOD, () -> TASmodClient.tickratechanger.increaseTickrate(), VirtualKeybindings::isKeyDownExceptTextfield),
@@ -76,4 +77,9 @@ public static KeyBinding[] valuesVanillaKeybind() {
7677
}
7778
return keybinds;
7879
}
80+
81+
@Override
82+
public Keybind getKeybind() {
83+
return this.keybind;
84+
}
7985
}

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
package com.minecrafttas.tasmod.registries;
22

3+
import com.minecrafttas.mctcommon.KeybindManager.Keybind;
34
import com.minecrafttas.mctcommon.networking.Client.Side;
45
import com.minecrafttas.mctcommon.networking.CompactPacketHandler;
56
import com.minecrafttas.mctcommon.networking.interfaces.PacketID;
7+
import com.minecrafttas.tasmod.TASmodClient;
68
import com.minecrafttas.tasmod.playback.PlaybackControllerClient;
79
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TASstate;
810
import com.minecrafttas.tasmod.playback.filecommands.PlaybackFileCommand.PlaybackFileCommandExtension;
911
import com.minecrafttas.tasmod.playback.tasfile.flavor.SerialiserFlavorBase;
12+
import com.minecrafttas.tasmod.savestates.SavestateHandlerClient;
1013
import com.minecrafttas.tasmod.savestates.SavestateHandlerServer.SavestateState;
1114
import com.minecrafttas.tasmod.savestates.gui.GuiSavestate;
1215
import com.minecrafttas.tasmod.savestates.storage.builtin.ClientMotionStorage.MotionData;
1316
import com.minecrafttas.tasmod.tickratechanger.TickrateChangerServer.TickratePauseState;
17+
import com.minecrafttas.tasmod.util.Component;
1418
import com.minecrafttas.tasmod.util.Ducks.ScoreboardDuck;
19+
import com.minecrafttas.tasmod.virtual.VirtualKey;
1520

1621
import net.minecraft.client.Minecraft;
1722
import net.minecraft.client.gui.GuiDownloadTerrain;
1823
import net.minecraft.nbt.NBTTagCompound;
24+
import net.minecraft.util.text.ChatType;
25+
import net.minecraft.util.text.TextFormatting;
1926

2027
/**
2128
* PacketIDs and handlers specifically for TASmod
@@ -49,6 +56,18 @@ public enum TASmodPackets implements PacketID {
4956
* ARGS: None
5057
*/
5158
TICKRATE_ADVANCE,
59+
/**
60+
* <p>Displays a warning in chat that the tickrate was automatically set to 0
61+
* <p>SIDE: Client
62+
*/
63+
TICKRATE_0_WARN(Side.CLIENT, (buf, username) -> {
64+
Minecraft mc = Minecraft.getMinecraft();
65+
Keybind tickrate0Kbd = TASmodClient.keybindManager.getKeybind(TASmodKeybinds.TICKRATE_0);
66+
String keyName = VirtualKey.getName(tickrate0Kbd.vanillaKeyBinding.getKeyCode());
67+
68+
if (TASmodClient.config.getBoolean(TASmodConfig.UnpauseWarn))
69+
mc.ingameGUI.addChatMessage(ChatType.CHAT, Component.translatable("msg.tasmod.tickrate.tr0warn", Component.literal(keyName).withStyle(TextFormatting.GOLD)).withStyle(TextFormatting.GREEN).build());
70+
}),
5271
/**
5372
* <p>Creates a savestate
5473
* <p>SIDE: Both<br>
@@ -87,7 +106,9 @@ public enum TASmodPackets implements PacketID {
87106
SAVESTATE_CLEAR_SCREEN(Side.CLIENT, (buf, clientID) -> {
88107
Minecraft mc = Minecraft.getMinecraft();
89108
if (mc.currentScreen instanceof GuiSavestate || mc.currentScreen instanceof GuiDownloadTerrain) {
90-
mc.displayGuiScreen(null);
109+
mc.addScheduledTask(() -> {
110+
mc.displayGuiScreen(null);
111+
});
91112
}
92113
}),
93114
/**
@@ -116,7 +137,12 @@ public enum TASmodPackets implements PacketID {
116137
* <p>SIDE: Client<br>
117138
* ARGS: none
118139
*/
119-
SAVESTATE_UNLOAD_CHUNKS,
140+
SAVESTATE_UNLOAD_CHUNKS(Side.CLIENT, (buf, username) -> {
141+
Minecraft mc = Minecraft.getMinecraft();
142+
mc.addScheduledTask(() -> {
143+
SavestateHandlerClient.unloadAllClientChunks();
144+
});
145+
}),
120146
/**
121147
* <p>Clears the scoreboard on the client side
122148
* <p>SIDE: Client<br>

0 commit comments

Comments
 (0)