From 3e68804785112dcd3681d976c1610612b9ed849e Mon Sep 17 00:00:00 2001 From: OmerBenGera Date: Sat, 21 Dec 2024 11:17:14 +0200 Subject: [PATCH] Fixed errors when disabling the plugin due to running new tasks (#100) --- .../wildloaders/WildLoadersPlugin.java | 14 +++++---- .../wildloaders/scheduler/Scheduler.java | 30 +++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/bgsoftware/wildloaders/WildLoadersPlugin.java b/src/main/java/com/bgsoftware/wildloaders/WildLoadersPlugin.java index d34eae7..b69ce99 100644 --- a/src/main/java/com/bgsoftware/wildloaders/WildLoadersPlugin.java +++ b/src/main/java/com/bgsoftware/wildloaders/WildLoadersPlugin.java @@ -18,6 +18,7 @@ import com.bgsoftware.wildloaders.listeners.BlocksListener; import com.bgsoftware.wildloaders.listeners.ChunksListener; import com.bgsoftware.wildloaders.listeners.PlayersListener; import com.bgsoftware.wildloaders.nms.NMSAdapter; +import com.bgsoftware.wildloaders.scheduler.Scheduler; import com.bgsoftware.wildloaders.utils.database.Database; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; @@ -44,6 +45,7 @@ public final class WildLoadersPlugin extends JavaPlugin implements WildLoaders { @Override public void onLoad() { plugin = this; + Scheduler.initialize(); DependenciesManager.inject(this); @@ -93,11 +95,13 @@ public final class WildLoadersPlugin extends JavaPlugin implements WildLoaders { @Override public void onDisable() { - if (shouldEnable) { - Database.stop(); - loadersHandler.removeChunkLoaders(); - npcHandler.killAllNPCs(); - } + if (!shouldEnable) + return; + + Scheduler.disable(); + Database.stop(); + loadersHandler.removeChunkLoaders(); + npcHandler.killAllNPCs(); } private boolean loadNMSAdapter() { diff --git a/src/main/java/com/bgsoftware/wildloaders/scheduler/Scheduler.java b/src/main/java/com/bgsoftware/wildloaders/scheduler/Scheduler.java index 4df0ba8..ae85dc8 100644 --- a/src/main/java/com/bgsoftware/wildloaders/scheduler/Scheduler.java +++ b/src/main/java/com/bgsoftware/wildloaders/scheduler/Scheduler.java @@ -6,6 +6,9 @@ import org.bukkit.World; import org.bukkit.entity.Entity; public class Scheduler { + + private static final ScheduledTask NULL_TASK = () -> {}; + private static final ISchedulerImplementation IMP = initializeSchedulerImplementation(); private static ISchedulerImplementation initializeSchedulerImplementation() { @@ -24,6 +27,8 @@ public class Scheduler { } } + private static boolean isEnabled = true; + private Scheduler() { } @@ -32,23 +37,43 @@ public class Scheduler { // Do nothing, load static initializer } + public static void disable() { + isEnabled = false; + } + public static boolean isRegionScheduler() { return IMP.isRegionScheduler(); } public static ScheduledTask runTask(World world, int chunkX, int chunkZ, Runnable task, long delay) { + if (!isEnabled) { + return executeNow(task); + } + return IMP.scheduleTask(world, chunkX, chunkZ, task, delay); } public static ScheduledTask runTask(Entity entity, Runnable task, long delay) { + if (!isEnabled) { + return executeNow(task); + } + return IMP.scheduleTask(entity, task, delay); } public static ScheduledTask runTask(Runnable task, long delay) { + if (!isEnabled) { + return executeNow(task); + } + return IMP.scheduleTask(task, delay); } public static ScheduledTask runTaskAsync(Runnable task, long delay) { + if (!isEnabled) { + return executeNow(task); + } + return IMP.scheduleAsyncTask(task, delay); } @@ -84,4 +109,9 @@ public class Scheduler { return runTaskAsync(task, 0L); } + private static ScheduledTask executeNow(Runnable task) { + task.run(); + return NULL_TASK; + } + }