aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorAaron <51387595+AzureAaron@users.noreply.github.com>2025-06-05 15:22:59 -0400
committerGitHub <noreply@github.com>2025-06-05 15:22:59 -0400
commit0bf0c1b1a52530ef73d426ddd2fbe96315406207 (patch)
treeed225b6db3408b296355edfc03501945b1225064 /src/main/java
parent197b5f80c2538b253a0ae4384feec6f4e2a545ca (diff)
downloadSkyblocker-0bf0c1b1a52530ef73d426ddd2fbe96315406207.tar.gz
Skyblocker-0bf0c1b1a52530ef73d426ddd2fbe96315406207.tar.bz2
Skyblocker-0bf0c1b1a52530ef73d426ddd2fbe96315406207.zip
Improve scheduler thread safety to prevent crashes (#1306)
Note: The code can't be put in another method otherwise the stack walker probably wont work correctly.
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java b/src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java
index cca4da09..bc8abfbe 100644
--- a/src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java
+++ b/src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java
@@ -1,5 +1,6 @@
package de.hysky.skyblocker.utils.scheduler;
+import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.context.CommandContext;
import it.unimi.dsi.fastutil.ints.AbstractInt2ObjectMap;
@@ -12,6 +13,7 @@ import net.minecraft.util.profiler.Profilers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.lang.StackWalker.Option;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
@@ -54,6 +56,13 @@ public class Scheduler {
* @param multithreaded whether to run the task on the schedulers dedicated thread pool
*/
public void schedule(Runnable task, int delay, boolean multithreaded) {
+ if (!RenderSystem.isOnRenderThread() && MinecraftClient.getInstance() != null) {
+ LOGGER.warn("[Skyblocker Scheduler] Called the scheduler from the {} class on the {} thread. This will be unsupported in the future.", StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE).getCallerClass().getName(), Thread.currentThread().getName());
+ MinecraftClient.getInstance().send(() -> schedule(task, delay, multithreaded));
+
+ return;
+ }
+
if (delay >= 0) {
addTask(multithreaded ? new ScheduledTask(task, true) : task, currentTick + delay);
} else {
@@ -69,6 +78,13 @@ public class Scheduler {
* @param multithreaded whether to run the task on the schedulers dedicated thread pool
*/
public void scheduleCyclic(Runnable task, int period, boolean multithreaded) {
+ if (!RenderSystem.isOnRenderThread() && MinecraftClient.getInstance() != null) {
+ LOGGER.warn("[Skyblocker Scheduler] Called the scheduler from the {} class on the {} thread. This will be unsupported in the future.", StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE).getCallerClass().getName(), Thread.currentThread().getName());
+ MinecraftClient.getInstance().send(() -> scheduleCyclic(task, period, multithreaded));
+
+ return;
+ }
+
if (period > 0) {
addTask(new ScheduledTask(task, period, true, multithreaded), currentTick);
} else {