Added executor for easier interactions with Bukkit's Scheduler

This commit is contained in:
OmerBenGera 2020-08-20 16:47:27 +03:00
parent 9cae8e9d5a
commit 50d0f2b85a
3 changed files with 78 additions and 2 deletions

View File

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

View File

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

View File

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