From 7cd7dca10c2ea0fc51f2684c9f1d3ded65a73ef6 Mon Sep 17 00:00:00 2001 From: 3arthqu4ke <56741599+3arthqu4ke@users.noreply.github.com> Date: Tue, 28 Mar 2023 16:20:45 +0200 Subject: [PATCH] [1.2.2] Fix Scheduler swallowing Exceptions --- build.gradle | 2 +- .../mc/commands/MinecraftContext.java | 2 +- .../headlessmc/mc/commands/QuitCommand.java | 2 +- .../mc/scheduling/SchedulesTasks.java | 24 ++++++++++++++++++- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index 1e9bc98..b062f95 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'me.earth.headlessmc' -version '1.2.1' +version '1.2.2' repositories { mavenCentral() diff --git a/src/main/java/me/earth/headlessmc/mc/commands/MinecraftContext.java b/src/main/java/me/earth/headlessmc/mc/commands/MinecraftContext.java index 1cbecba..fd068c3 100644 --- a/src/main/java/me/earth/headlessmc/mc/commands/MinecraftContext.java +++ b/src/main/java/me/earth/headlessmc/mc/commands/MinecraftContext.java @@ -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); } diff --git a/src/main/java/me/earth/headlessmc/mc/commands/QuitCommand.java b/src/main/java/me/earth/headlessmc/mc/commands/QuitCommand.java index 669cc1b..0e8f446 100644 --- a/src/main/java/me/earth/headlessmc/mc/commands/QuitCommand.java +++ b/src/main/java/me/earth/headlessmc/mc/commands/QuitCommand.java @@ -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(); }); diff --git a/src/main/java/me/earth/headlessmc/mc/scheduling/SchedulesTasks.java b/src/main/java/me/earth/headlessmc/mc/scheduling/SchedulesTasks.java index c5267f6..c1c699c 100644 --- a/src/main/java/me/earth/headlessmc/mc/scheduling/SchedulesTasks.java +++ b/src/main/java/me/earth/headlessmc/mc/scheduling/SchedulesTasks.java @@ -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); @@ -31,8 +32,29 @@ public interface SchedulesTasks { */ default Future schedule(Supplier supplier) { AtomicReference 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; + } + }); + } + }