Skip to content

Commit

Permalink
[1.2.2] Fix Scheduler swallowing Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
3arthqu4ke committed Mar 28, 2023
1 parent 378d8de commit 7cd7dca
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'me.earth.headlessmc'
version '1.2.1'
version '1.2.2'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public MinecraftContext(HeadlessMc ctx, Minecraft mc) {
@Override
protected void executeCommand(Command cmd, String... args) {
if (cmd instanceof ScheduledCommand) {
mc.schedule(() -> super.executeCommand(cmd, args));
mc.scheduleEx(() -> super.executeCommand(cmd, args));
} else {
super.executeCommand(cmd, args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void execute(String... args) {
ctx.log("Quitting Minecraft...");
mc.quit();
} else {
mc.schedule(() -> {
mc.scheduleEx(() -> {
ctx.log("Quitting Minecraft...");
mc.quit();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public interface SchedulesTasks {
* immediately.
*
* @param task the task to run on the main thread.
* @return a Future representing the completion of the task.
*/
Future<?> schedule(Runnable task);

Expand All @@ -31,8 +32,29 @@ public interface SchedulesTasks {
*/
default <T> Future<T> schedule(Supplier<T> supplier) {
AtomicReference<T> result = new AtomicReference<>();
Future<?> future = schedule(() -> result.set(supplier.get()));
Future<?> future = scheduleEx(() -> result.set(supplier.get()));
return new DelegatingFuture<>(future, result::get);
}

/**
* Same as {@link #schedule(Runnable)} but the runnable will be wrapped in
* a try- and catch- which will print the StackTrace of any caught Throwable
* and then immediately rethrows it. The reason for this is that sometimes
* Exceptions just get swallowed by the Scheduler implementation (E.g. this
* seems to be the case on Minecraft 1.19.4)
*
* @param task the task to run on the main thread.
* @return a Future representing the completion of the task.
*/
default Future<?> scheduleEx(Runnable task) {
return schedule(() -> {
try {
task.run();
} catch (Throwable t) {
t.printStackTrace();
throw t;
}
});
}

}

0 comments on commit 7cd7dca

Please sign in to comment.