Skip to content

Commit

Permalink
Fixed errors when disabling the plugin due to running new tasks (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Dec 21, 2024
1 parent faa1fde commit 3e68804
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/main/java/com/bgsoftware/wildloaders/WildLoadersPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand All @@ -44,6 +45,7 @@ public final class WildLoadersPlugin extends JavaPlugin implements WildLoaders {
@Override
public void onLoad() {
plugin = this;
Scheduler.initialize();

DependenciesManager.inject(this);

Expand Down Expand Up @@ -93,11 +95,13 @@ public void onEnable() {

@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() {
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/bgsoftware/wildloaders/scheduler/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import org.bukkit.entity.Entity;

public class Scheduler {

private static final ScheduledTask NULL_TASK = () -> {};

private static final ISchedulerImplementation IMP = initializeSchedulerImplementation();

private static ISchedulerImplementation initializeSchedulerImplementation() {
Expand All @@ -24,6 +27,8 @@ private static ISchedulerImplementation initializeSchedulerImplementation() {
}
}

private static boolean isEnabled = true;

private Scheduler() {

}
Expand All @@ -32,23 +37,43 @@ public static void initialize() {
// 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);
}

Expand Down Expand Up @@ -84,4 +109,9 @@ public static ScheduledTask runTaskAsync(Runnable task) {
return runTaskAsync(task, 0L);
}

private static ScheduledTask executeNow(Runnable task) {
task.run();
return NULL_TASK;
}

}

0 comments on commit 3e68804

Please sign in to comment.