Updated some ChatUtil methods + some refactoring
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package fr.pandacube.lib.paper.scheduler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import fr.pandacube.lib.paper.PandaLibPaper;
|
||||
|
||||
public class AutoUpdatedObject {
|
||||
private static Plugin plugin = PandaLibPaper.getPlugin();
|
||||
|
||||
private Runnable updater;
|
||||
|
||||
private List<BukkitTask> tasks = new ArrayList<>();
|
||||
|
||||
|
||||
protected AutoUpdatedObject() { }
|
||||
public AutoUpdatedObject(Runnable updater) {
|
||||
this.updater = Objects.requireNonNull(updater);
|
||||
}
|
||||
|
||||
public synchronized void updateSync() {
|
||||
tasks.add(Bukkit.getScheduler().runTask(plugin, this::update));
|
||||
}
|
||||
|
||||
public synchronized void updateAsync() {
|
||||
tasks.add(Bukkit.getScheduler().runTaskAsynchronously(plugin, this::update));
|
||||
}
|
||||
|
||||
public synchronized void updateLater(long delay)
|
||||
throws IllegalArgumentException, IllegalStateException {
|
||||
tasks.add(Bukkit.getScheduler().runTaskLater(plugin, this::update, delay));
|
||||
}
|
||||
|
||||
public synchronized void updateLaterAsync(long delay)
|
||||
throws IllegalArgumentException, IllegalStateException {
|
||||
tasks.add(Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, this::update, delay));
|
||||
}
|
||||
|
||||
public synchronized void updateTimer(long delay, long period)
|
||||
throws IllegalArgumentException, IllegalStateException {
|
||||
tasks.add(Bukkit.getScheduler().runTaskTimer(plugin, this::update, delay, period));
|
||||
}
|
||||
|
||||
public synchronized void updateTimerAsync(long delay, long period)
|
||||
throws IllegalArgumentException, IllegalStateException {
|
||||
tasks.add(Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, this::update, delay, period));
|
||||
}
|
||||
|
||||
public synchronized void cancel() {
|
||||
tasks.forEach(t -> t.cancel());
|
||||
tasks.clear();
|
||||
}
|
||||
|
||||
public void update() {
|
||||
Objects.requireNonNull(updater, "Please use new AutoUpdatedObject(Runnable) or override the run method.");
|
||||
updater.run();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
package fr.pandacube.lib.paper.scheduler;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import fr.pandacube.lib.core.util.Log;
|
||||
import fr.pandacube.lib.paper.PandaLibPaper;
|
||||
|
||||
public class SchedulerUtil {
|
||||
|
||||
public static void runOnServerThread(Runnable task) {
|
||||
if (Bukkit.isPrimaryThread())
|
||||
task.run();
|
||||
|
||||
Bukkit.getScheduler().runTask(PandaLibPaper.getPlugin(), task);
|
||||
}
|
||||
|
||||
public static <T> T runOnServerThreadAndWait(Callable<T> task) throws Exception {
|
||||
if (Bukkit.isPrimaryThread())
|
||||
return task.call();
|
||||
|
||||
return Bukkit.getScheduler().callSyncMethod(PandaLibPaper.getPlugin(), () -> {
|
||||
try {
|
||||
return task.call();
|
||||
} catch (Exception e) {
|
||||
Log.severe("Exception while running callback code on server Thread. The source exception is:", e);
|
||||
throw e;
|
||||
}
|
||||
}).get();
|
||||
}
|
||||
|
||||
public static void runOnServerThreadAndWait(Runnable task) throws Exception {
|
||||
runOnServerThreadAndWait((Callable<Void>)() -> {
|
||||
task.run();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user