diff --git a/src/main/java/cn/lunadeer/colorfulmap/utils/Scheduler.java b/src/main/java/cn/lunadeer/colorfulmap/utils/Scheduler.java index 136ed9e..f21f1c7 100644 --- a/src/main/java/cn/lunadeer/colorfulmap/utils/Scheduler.java +++ b/src/main/java/cn/lunadeer/colorfulmap/utils/Scheduler.java @@ -26,6 +26,74 @@ public class Scheduler { } } + /** + * Run a task later + * + * @param task The task to run + * @param delay The delay in ticks (20 ticks = 1 second) + * @return The scheduled task + */ + public static void runTaskLater(Runnable task, long delay) { + if (delay <= 0) { + runTask(task); + return; + } + if (instance.isPaper) { + instance.plugin.getServer().getGlobalRegionScheduler().runDelayed(instance.plugin, (plugin) -> task.run(), delay); + } else { + instance.plugin.getServer().getScheduler().runTaskLater(instance.plugin, task, delay); + } + } + + /** + * Run a task + * + * @param task The task to run + * @return The scheduled task + */ + public static void runTask(Runnable task) { + if (instance.isPaper) { + instance.plugin.getServer().getGlobalRegionScheduler().run(instance.plugin, (plugin) -> task.run()); + } else { + instance.plugin.getServer().getScheduler().runTask(instance.plugin, task); + } + } + + /** + * Run a task repeatedly + * + * @param task The task to run + * @param delay The delay in ticks (20 ticks = 1 second) + * @param period The period in ticks (20 ticks = 1 second) + * @return The scheduled task + */ + public static void runTaskRepeat(Runnable task, long delay, long period) { + if (instance.isPaper) { + instance.plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(instance.plugin, (plugin) -> task.run(), delay, period); + } else { + instance.plugin.getServer().getScheduler().runTaskTimer(instance.plugin, task, delay, period); + } + } + + /** + * Run a task later asynchronously + * + * @param task The task to run + * @param delay The delay in milliseconds + * @return The scheduled task + */ + public static void runTaskLaterAsync(Runnable task, long delay) { + if (delay <= 0) { + runTaskAsync(task); + return; + } + if (instance.isPaper) { + instance.plugin.getServer().getAsyncScheduler().runDelayed(instance.plugin, (plugin) -> task.run(), delay * 50, TimeUnit.MILLISECONDS); + } else { + instance.plugin.getServer().getScheduler().runTaskLaterAsynchronously(instance.plugin, task, delay); + } + } + /** * Run a task asynchronously * @@ -39,4 +107,20 @@ public class Scheduler { instance.plugin.getServer().getScheduler().runTask(instance.plugin, task); } } + + /** + * Run a task repeatedly asynchronously + * + * @param task The task to run + * @param delay The delay in milliseconds + * @param period The period in milliseconds + * @return The scheduled task + */ + public static void runTaskRepeatAsync(Runnable task, long delay, long period) { + if (instance.isPaper) { + instance.plugin.getServer().getAsyncScheduler().runAtFixedRate(instance.plugin, (plugin) -> task.run(), delay * 50, period * 50, TimeUnit.MILLISECONDS); + } else { + instance.plugin.getServer().getScheduler().runTaskTimerAsynchronously(instance.plugin, task, delay, period); + } + } }