diff --git a/src/main/java/com/bgsoftware/wildloaders/handlers/NPCHandler.java b/src/main/java/com/bgsoftware/wildloaders/handlers/NPCHandler.java index f2d25ba..4622c84 100644 --- a/src/main/java/com/bgsoftware/wildloaders/handlers/NPCHandler.java +++ b/src/main/java/com/bgsoftware/wildloaders/handlers/NPCHandler.java @@ -5,6 +5,7 @@ import com.bgsoftware.wildloaders.api.managers.NPCManager; import com.bgsoftware.wildloaders.api.npc.ChunkLoaderNPC; import com.bgsoftware.wildloaders.utils.ServerVersion; import com.bgsoftware.wildloaders.utils.locations.LocationUtils; +import com.bgsoftware.wildloaders.utils.threads.Executor; import com.google.common.collect.Maps; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -105,7 +106,7 @@ public final class NPCHandler implements NPCManager { @SuppressWarnings("ResultOfMethodCallIgnored") private void saveUUIDs(){ if(Bukkit.isPrimaryThread()){ - Bukkit.getScheduler().runTaskAsynchronously(plugin, this::saveUUIDs); + Executor.async(this::saveUUIDs); return; } diff --git a/src/main/java/com/bgsoftware/wildloaders/loaders/WChunkLoader.java b/src/main/java/com/bgsoftware/wildloaders/loaders/WChunkLoader.java index 6b8f5c6..5cf7d36 100644 --- a/src/main/java/com/bgsoftware/wildloaders/loaders/WChunkLoader.java +++ b/src/main/java/com/bgsoftware/wildloaders/loaders/WChunkLoader.java @@ -4,6 +4,7 @@ import com.bgsoftware.wildloaders.WildLoadersPlugin; import com.bgsoftware.wildloaders.api.loaders.ChunkLoader; import com.bgsoftware.wildloaders.api.loaders.LoaderData; import com.bgsoftware.wildloaders.api.npc.ChunkLoaderNPC; +import com.bgsoftware.wildloaders.utils.threads.Executor; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; @@ -74,7 +75,7 @@ public final class WChunkLoader implements ChunkLoader { @Override public void remove() { if(!Bukkit.isPrimaryThread()){ - Bukkit.getScheduler().runTask(plugin, this::remove); + Executor.sync(this::remove); return; } diff --git a/src/main/java/com/bgsoftware/wildloaders/utils/threads/Executor.java b/src/main/java/com/bgsoftware/wildloaders/utils/threads/Executor.java new file mode 100644 index 0000000..30e55b7 --- /dev/null +++ b/src/main/java/com/bgsoftware/wildloaders/utils/threads/Executor.java @@ -0,0 +1,74 @@ +package com.bgsoftware.wildloaders.utils.threads; + +import com.bgsoftware.wildloaders.WildLoadersPlugin; +import com.google.common.util.concurrent.ThreadFactoryBuilder; +import org.bukkit.Bukkit; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +public final class Executor { + + private static final ExecutorService dataService = Executors.newFixedThreadPool(3, new ThreadFactoryBuilder().setNameFormat("WildChests DB Thread - #%d").build()); + private static final WildLoadersPlugin plugin = WildLoadersPlugin.getPlugin(); + private static boolean shutdown = false; + + private Executor() {} + + public static void sync(Runnable runnable){ + if(shutdown) + return; + + sync(runnable, 0L); + } + + public static void sync(Runnable runnable, long delay){ + if(shutdown) + return; + + Bukkit.getScheduler().runTaskLater(plugin, runnable, delay); + } + + public static void async(Runnable runnable){ + if(shutdown) + return; + + if(Bukkit.isPrimaryThread()){ + Bukkit.getScheduler().runTaskAsynchronously(plugin, runnable); + } + else{ + runnable.run(); + } + } + + public static void async(Runnable runnable, long delay){ + if(shutdown) + return; + + if(Bukkit.isPrimaryThread()){ + Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, runnable, delay); + } + else{ + runnable.run(); + } + } + + public static void data(Runnable runnable){ + if(shutdown) + return; + + dataService.execute(runnable); + } + + public static void stop(){ + try{ + shutdown = true; + dataService.shutdown(); + dataService.awaitTermination(1, TimeUnit.MINUTES); + }catch (Exception ex){ + ex.printStackTrace(); + } + } + +}