Skip to content

Commit 6700c78

Browse files
committed
Safecall all player recorder functions
1 parent c24f5df commit 6700c78

File tree

10 files changed

+59
-45
lines changed

10 files changed

+59
-45
lines changed

src/main/java/net/jasper/mod/automation/Commands.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import net.jasper.mod.gui.PlayerAutomaMenuScreen;
99
import net.jasper.mod.gui.RecordingSelectorScreen;
1010
import net.jasper.mod.gui.RecordingStorerScreen;
11+
import net.jasper.mod.util.PlayerAutomaExceptionHandler;
1112
import net.minecraft.client.MinecraftClient;
1213
import net.minecraft.client.gui.screen.Screen;
1314
import net.minecraft.text.Text;
@@ -31,19 +32,19 @@ public static void register() {
3132
literal("record")
3233
.then(literal("start")
3334
.executes(context -> {
34-
PlayerRecorder.startRecord();
35+
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::startRecord);
3536
return 1;
3637
})
3738
)
3839
.then(literal("stop")
3940
.executes(context -> {
40-
PlayerRecorder.stopRecord();
41+
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::stopRecord);
4142
return 1;
4243
})
4344
)
4445
.then(literal("clear")
4546
.executes(context -> {
46-
PlayerRecorder.clearRecord();
47+
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::clearRecord);
4748
return 1;
4849
})
4950
)
@@ -98,7 +99,7 @@ public static void register() {
9899
})
99100
.executes(context -> {
100101
String name = StringArgumentType.getString(context, "name");
101-
PlayerRecorder.loadRecord(name);
102+
PlayerAutomaExceptionHandler.callSafe(() -> PlayerRecorder.loadRecord(name));
102103
return 1;
103104
})
104105
)
@@ -111,24 +112,24 @@ public static void register() {
111112
dispatcher.register(literal("replay")
112113
.then(literal("start")
113114
.executes(context -> {
114-
PlayerRecorder.startReplay(false);
115+
PlayerAutomaExceptionHandler.callSafe(() -> PlayerRecorder.startReplay(false));
115116
return 1;
116117
})
117118
)
118119
.then(literal("stop")
119120
.executes(context -> {
120-
PlayerRecorder.stopReplay();
121+
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::stopReplay);
121122
return 1;
122123
})
123124
)
124125
.then(literal("togglepause")
125126
.executes(context -> {
126-
PlayerRecorder.togglePauseReplay();
127+
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::togglePauseReplay);
127128
return 1;
128129
})
129130
).then(literal("loop")
130131
.executes(context -> {
131-
PlayerRecorder.startReplay(true);
132+
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::startReplay);
132133
return 1;
133134
})
134135
)

src/main/java/net/jasper/mod/automation/PlayerRecorder.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,6 @@ public static void reset() {
128128
}
129129

130130
public static void startRecord() {
131-
int i = 0;
132-
System.out.println(10 / i);
133131
if (state.isReplaying() || state.isPausedReplaying()) {
134132
ClientHelpers.writeToActionBar(Text.translatable("playerautoma.messages.error.cannotStartRecordingWhileReplaying"));
135133
return;

src/main/java/net/jasper/mod/gui/PlayerAutomaMenuScreen.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import net.jasper.mod.automation.PlayerRecorder;
44
import net.jasper.mod.gui.option.PlayerAutomaOptionsScreen;
5+
import net.jasper.mod.util.PlayerAutomaExceptionHandler;
56
import net.minecraft.client.MinecraftClient;
67
import net.minecraft.client.gui.screen.Screen;
78
import net.minecraft.client.gui.tooltip.Tooltip;
@@ -30,22 +31,22 @@ public PlayerAutomaMenuScreen(Screen parent) {
3031
// Player Recorder
3132
public ButtonWidget START_RECORDING = ButtonWidget.builder(Text.translatable("playerautoma.screens.menu.startRecording"), button -> {
3233
Objects.requireNonNull(MinecraftClient.getInstance().currentScreen).close();
33-
PlayerRecorder.startRecord();
34+
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::startRecord);
3435
}).tooltip(Tooltip.of(Text.translatable("playerautoma.screens.menu.tooltip.startRecording"))).build();
3536

3637
public ButtonWidget STOP_RECORDING = ButtonWidget.builder(Text.translatable("playerautoma.screens.menu.stopRecording"), button -> {
3738
Objects.requireNonNull(MinecraftClient.getInstance().currentScreen).close();
38-
PlayerRecorder.stopRecord();
39+
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::stopRecord);
3940
}).tooltip(Tooltip.of(Text.translatable("playerautoma.screens.menu.tooltip.stopRecording"))).build();
4041

4142
public ButtonWidget START_REPLAY = ButtonWidget.builder(Text.translatable("playerautoma.screens.menu.startReplay"), button -> {
4243
Objects.requireNonNull(MinecraftClient.getInstance().currentScreen).close();
43-
PlayerRecorder.startReplay(false);
44+
PlayerAutomaExceptionHandler.callSafe(() -> PlayerRecorder.startReplay(false));
4445
}).tooltip(Tooltip.of(Text.translatable("playerautoma.screens.menu.tooltip.startReplay"))).build();
4546

4647
public ButtonWidget START_LOOP = ButtonWidget.builder(Text.translatable("playerautoma.screens.menu.startLoop"), button -> {
4748
Objects.requireNonNull(MinecraftClient.getInstance().currentScreen).close();
48-
PlayerRecorder.startLoop();
49+
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::startLoop);
4950
}).tooltip(Tooltip.of(Text.translatable("playerautoma.screens.menu.tooltip.startLoop"))).build();
5051

5152
public ButtonWidget STORE_RECORDING = ButtonWidget.builder(Text.translatable("playerautoma.screens.menu.storeRecording"),

src/main/java/net/jasper/mod/gui/QuickMenu.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.jasper.mod.gui.option.PlayerAutomaOptionsScreen;
66
import net.jasper.mod.mixins.accessors.ScreenAccessor;
77
import net.jasper.mod.util.ColorHelpers;
8+
import net.jasper.mod.util.PlayerAutomaExceptionHandler;
89
import net.jasper.mod.util.Textures;
910
import net.minecraft.client.MinecraftClient;
1011
import net.minecraft.client.gui.DrawContext;
@@ -64,7 +65,7 @@ public class QuickMenu extends Screen {
6465
if (PlayerRecorder.state.isRecording() || PlayerRecorder.state.isPausedRecording()) {
6566
PlayerRecorder.togglePauseRecord();
6667
} else {
67-
PlayerRecorder.startRecord();
68+
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::startRecord);
6869
}
6970
}
7071
).size(BUTTON_DIMENSIONS, BUTTON_DIMENSIONS).build();
@@ -73,7 +74,7 @@ public class QuickMenu extends Screen {
7374
Text.of(""),
7475
b -> {
7576
loopCount = 0;
76-
PlayerRecorder.stopRecord();
77+
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::stopRecord);
7778
}
7879
).size(BUTTON_DIMENSIONS, BUTTON_DIMENSIONS).build();
7980

@@ -83,9 +84,9 @@ public class QuickMenu extends Screen {
8384
b -> {
8485
loopCount = 0;
8586
if (PlayerRecorder.state.isReplaying() || PlayerRecorder.state.isPausedReplaying()) {
86-
PlayerRecorder.togglePauseReplay();
87+
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::togglePauseReplay);
8788
} else {
88-
PlayerRecorder.startReplay(false);
89+
PlayerAutomaExceptionHandler.callSafe(() -> PlayerRecorder.startReplay(false));
8990
}
9091
}
9192
).size(BUTTON_DIMENSIONS, BUTTON_DIMENSIONS).build();
@@ -94,7 +95,7 @@ public class QuickMenu extends Screen {
9495
Text.of(""),
9596
b -> {
9697
loopCount = 0;
97-
PlayerRecorder.stopReplay();
98+
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::stopReplay);
9899
}
99100
).size(BUTTON_DIMENSIONS, BUTTON_DIMENSIONS).build();
100101

@@ -134,9 +135,9 @@ public void close() {
134135
wasClosed = true;
135136
// -1 == Infinity
136137
if (loopCount < 0) {
137-
PlayerRecorder.startLoop();
138+
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::startLoop);
138139
} else if (loopCount > 0) {
139-
PlayerRecorder.startReplay(loopCount);
140+
PlayerAutomaExceptionHandler.callSafe(() -> PlayerRecorder.startReplay(loopCount));
140141
}
141142
loopCount = 0;
142143

src/main/java/net/jasper/mod/gui/RecordingSelectorScreen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private void onRefresh() {
144144
private void onDone() {
145145
RecordingSelectionListWidget.RecordingEntry recEntry = this.recordingSelectionList.getSelectedOrNull();
146146
if (recEntry != null) {
147-
PlayerRecorder.loadRecord(recEntry.file);
147+
PlayerAutomaExceptionHandler.callSafe(() -> PlayerRecorder.loadRecord(recEntry.file));
148148
}
149149
this.client.setScreen(null);
150150
}

src/main/java/net/jasper/mod/gui/RecordingStorerScreen.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.jasper.mod.gui.components.FilteredTextFieldWidget;
55
import net.jasper.mod.gui.option.OptionButton;
66
import net.jasper.mod.util.IOHelpers;
7+
import net.jasper.mod.util.PlayerAutomaExceptionHandler;
78
import net.minecraft.client.MinecraftClient;
89
import net.minecraft.client.gui.DrawContext;
910
import net.minecraft.client.gui.screen.Screen;
@@ -84,7 +85,8 @@ protected void init() {
8485
// Append correct file ending if necessary
8586
String file_ending = useJSON.getValue() ? ".json" : ".rec";
8687
name += file_ending;
87-
PlayerRecorder.storeRecord(name);
88+
String finalName = name;
89+
PlayerAutomaExceptionHandler.callSafe(() -> PlayerRecorder.storeRecord(finalName));
8890
this.close();
8991
}).dimensions(this.width / 2 - 100, this.height / 2 + 10, 150, 20)
9092
.tooltip(Tooltip.of(Text.translatable("playerautoma.screens.fileSelector.tooltip.save")))

src/main/java/net/jasper/mod/mixins/KeyboardMixin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.jasper.mod.automation.PlayerRecorder;
55
import net.jasper.mod.mixins.accessors.KeyBindingAccessor;
66
import net.jasper.mod.util.ClientHelpers;
7+
import net.jasper.mod.util.PlayerAutomaExceptionHandler;
78
import net.jasper.mod.util.keybinds.Constants;
89
import net.minecraft.client.Keyboard;
910
import net.minecraft.text.Text;
@@ -27,7 +28,7 @@ private void injected(long window, int key, int scancode, int action, int modifi
2728

2829
int startReplayKeyCode = ((KeyBindingAccessor) Constants.START_REPLAY).getBoundKey().getCode();
2930
if (PlayerRecorder.state.isReplaying() && key != startReplayKeyCode) {
30-
PlayerRecorder.stopReplay();
31+
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::stopReplay);
3132
ClientHelpers.writeToActionBar(Text.literal("Replay stopped due to manual input"));
3233
}
3334
}

src/main/java/net/jasper/mod/mixins/ScreenMixin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.jasper.mod.automation.MenuPrevention;
44
import net.jasper.mod.automation.PlayerRecorder;
55
import net.jasper.mod.mixins.accessors.KeyBindingAccessor;
6+
import net.jasper.mod.util.PlayerAutomaExceptionHandler;
67
import net.jasper.mod.util.keybinds.Constants;
78
import net.minecraft.client.gui.DrawContext;
89
import net.minecraft.client.gui.screen.Screen;
@@ -38,7 +39,7 @@ private void renderPlayerRecorderIcon(DrawContext context, int mouseX, int mouse
3839
@Inject(method="keyPressed", at=@At("HEAD"))
3940
private void stopReplay(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable<Boolean> cir) {
4041
if (STOP_REPLAY.matchesKey(keyCode, scanCode)) {
41-
PlayerRecorder.stopReplay();
42+
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::stopReplay);
4243
}
4344
}
4445

src/main/java/net/jasper/mod/util/PlayerAutomaExceptionHandler.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,23 @@ public static void handleException(Exception exception) {
8484
// Reset into functional state
8585
PlayerRecorder.reset();
8686
}
87+
88+
public static void callSafe(Runnable action) {
89+
try {
90+
action.run();
91+
} catch (Exception e) {
92+
handleException(e);
93+
}
94+
}
95+
96+
public static Runnable produceSafeCall(Runnable run) {
97+
return () -> {
98+
try {
99+
run.run();
100+
} catch (Exception exception) {
101+
handleException(exception);
102+
}
103+
};
104+
}
105+
87106
}

src/main/java/net/jasper/mod/util/keybinds/Constants.java

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,17 @@ public class Constants {
6161
public static final KeyBinding QUICK_MENU = BINDINGS[10];
6262
public static final KeyBinding START_REPLAY = BINDINGS[2];
6363

64-
private static Runnable exceptionSaveKeybindCallback(Runnable run) {
65-
return () -> {
66-
try {
67-
run.run();
68-
} catch (Exception exception) {
69-
PlayerAutomaExceptionHandler.handleException(exception);
70-
}
71-
};
72-
}
73-
7464
private static final Runnable[] callbackMethods = {
75-
exceptionSaveKeybindCallback(PlayerRecorder::startRecord),
76-
exceptionSaveKeybindCallback(PlayerRecorder::stopRecord),
77-
exceptionSaveKeybindCallback(PlayerRecorder::startReplay),
78-
exceptionSaveKeybindCallback(PlayerRecorder::stopReplay),
79-
exceptionSaveKeybindCallback(PlayerRecorder::startLoop),
80-
exceptionSaveKeybindCallback(RecordingStorerScreen::open),
81-
exceptionSaveKeybindCallback(RecordingSelectorScreen::open),
82-
exceptionSaveKeybindCallback(PlayerRecorder::togglePause),
83-
exceptionSaveKeybindCallback(PlayerAutomaMenuScreen::open),
84-
exceptionSaveKeybindCallback(MenuPrevention::toggleBackgroundPrevention),
65+
PlayerAutomaExceptionHandler.produceSafeCall(PlayerRecorder::startRecord),
66+
PlayerAutomaExceptionHandler.produceSafeCall(PlayerRecorder::stopRecord),
67+
PlayerAutomaExceptionHandler.produceSafeCall(PlayerRecorder::startReplay),
68+
PlayerAutomaExceptionHandler.produceSafeCall(PlayerRecorder::stopReplay),
69+
PlayerAutomaExceptionHandler.produceSafeCall(PlayerRecorder::startLoop),
70+
PlayerAutomaExceptionHandler.produceSafeCall(RecordingStorerScreen::open),
71+
PlayerAutomaExceptionHandler.produceSafeCall(RecordingSelectorScreen::open),
72+
PlayerAutomaExceptionHandler.produceSafeCall(PlayerRecorder::togglePause),
73+
PlayerAutomaExceptionHandler.produceSafeCall(PlayerAutomaMenuScreen::open),
74+
PlayerAutomaExceptionHandler.produceSafeCall(MenuPrevention::toggleBackgroundPrevention),
8575
() -> {} // Do nothing! The quickMenu opens onPress and closes onRelease and needs to be handled differently
8676
};
8777

0 commit comments

Comments
 (0)