Fixed errors when disabling the plugin due to running new tasks (#100)

This commit is contained in:
OmerBenGera 2024-12-21 11:17:14 +02:00
parent faa1fde848
commit 3e68804785
2 changed files with 39 additions and 5 deletions

View File

@ -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() {

View File

@ -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;
}
}