mirror of
https://github.com/BG-Software-LLC/WildLoaders.git
synced 2024-11-21 11:46:46 +01:00
Added executor for easier interactions with Bukkit's Scheduler
This commit is contained in:
parent
9cae8e9d5a
commit
50d0f2b85a
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user