From 481773de1670c5dac5a2186252e5dccd5991404d Mon Sep 17 00:00:00 2001 From: Nahuel Dolores Date: Mon, 24 Jul 2023 10:46:57 -0300 Subject: Add missing scheduler wrapper methods for future usage --- .../quests/bukkit/scheduler/ServerScheduler.java | 119 +++++++++++++++++++++ .../bukkit/BukkitServerSchedulerAdapter.java | 36 +++++++ .../scheduler/folia/FoliaServerScheduler.java | 43 ++++++-- 3 files changed, 191 insertions(+), 7 deletions(-) (limited to 'bukkit/src') diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/scheduler/ServerScheduler.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/scheduler/ServerScheduler.java index 79fc9306..0dc82414 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/scheduler/ServerScheduler.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/scheduler/ServerScheduler.java @@ -1,5 +1,6 @@ package com.leonardobishop.quests.bukkit.scheduler; +import org.bukkit.Location; import org.bukkit.entity.Entity; import org.jetbrains.annotations.NotNull; @@ -17,6 +18,60 @@ public interface ServerScheduler extends com.leonardobishop.quests.common.schedu */ void cancelTask(@NotNull WrappedTask wrappedTask); + /** + * Run a new task. + *

+ * Folia: Synced with the server daylight cycle tick. + *

+ * Paper: Synced with the server main thread. + * + * @param runnable Runnable to run. + * @return {@link WrappedTask} task reference. + */ + @NotNull + WrappedTask runTask(@NotNull Runnable runnable); + + /** + * Run a new task. + *

+ * Folia: Run in the dedicated async thread. + *

+ * Paper: Run in the dedicated async thread. + * + * @param runnable Runnable to run. + * @return {@link WrappedTask} task reference. + */ + @NotNull + WrappedTask runTaskAsynchronously(@NotNull Runnable runnable); + + /** + * Run a new task. + *

+ * Folia: Synced with the tick of the region of the entity (even if the entity moves). + *

+ * Paper: Synced with the server main thread. + * + * @param entity Entity to run the task at. + * @param runnable Runnable to run. + * @return {@link WrappedTask} task reference. + */ + @NotNull + WrappedTask runTaskAtEntity(@NotNull Entity entity, @NotNull Runnable runnable); + + /** + * Run a new task. + *

+ * Folia: Synced with the tick of the region of the chunk of the location. + *

+ * Paper: Synced with the server main thread. + * + * @param location Location to run the task at. + * @param runnable Runnable to run. + * @return {@link WrappedTask} task reference. + */ + @NotNull + WrappedTask runTaskAtLocation(@NotNull Location location, @NotNull Runnable runnable); + /** * Run a new task timer. *

@@ -47,6 +102,38 @@ public interface ServerScheduler extends com.leonardobishop.quests.common.schedu @NotNull WrappedTask runTaskTimerAsynchronously(@NotNull Runnable runnable, long delay, long period); + /** + * Run a new task timer. + *

+ * Folia: Synced with the tick of the region of the entity (even if the entity moves). + *

+ * Paper: Synced with the server main thread. + * + * @param entity Entity to run the task at. + * @param runnable Runnable to run. + * @param delay Delay before first execution. Must be greater than zero. + * @param period Delay between executions. Must be greater than zero. + * @return {@link WrappedTask} task reference. + */ + @NotNull + WrappedTask runTaskTimerAtEntity(@NotNull Entity entity, @NotNull Runnable runnable, long delay, long period); + + /** + * Run a new task timer. + *

+ * Folia: Synced with the tick of the region of the chunk of the location. + *

+ * Paper: Synced with the server main thread. + * + * @param location Location to run the task at. + * @param runnable Runnable to run. + * @param delay Delay before first execution. Must be greater than zero. + * @param period Delay between executions. Must be greater than zero. + * @return {@link WrappedTask} task reference. + */ + @NotNull + WrappedTask runTaskTimerAtLocation(@NotNull Location location, @NotNull Runnable runnable, long delay, long period); + /** * Run a new task later. *

@@ -82,6 +169,7 @@ public interface ServerScheduler extends com.leonardobishop.quests.common.schedu *

* Paper: Synced with the server main thread. * + * @param entity Entity to run the task at. * @param runnable Runnable to run. * @param delay Delay before first execution. Must be greater than zero. * @return {@link WrappedTask} task reference. @@ -89,6 +177,37 @@ public interface ServerScheduler extends com.leonardobishop.quests.common.schedu @NotNull WrappedTask runTaskLaterAtEntity(@NotNull Entity entity, @NotNull Runnable runnable, long delay); + /** + * Run a new task later. + *

+ * Folia: Synced with the tick of the region of the chunk of the location. + *

+ * Paper: Synced with the server main thread. + * + * @param location Location to run the task at. + * @param runnable Runnable to run. + * @param delay Delay before first execution. Must be greater than zero. + * @return {@link WrappedTask} task reference. + */ + @NotNull + WrappedTask runTaskLaterAtLocation(@NotNull Location location, @NotNull Runnable runnable, long delay); + + /** + * {@inheritDoc} + */ + @Override + default void doAsync(Runnable runnable) { + runTaskAsynchronously(runnable); + } + + /** + * {@inheritDoc} + */ + @Override + default void doSync(Runnable runnable) { + runTask(runnable); + } + /** * Get the server scheduler name. * diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/scheduler/bukkit/BukkitServerSchedulerAdapter.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/scheduler/bukkit/BukkitServerSchedulerAdapter.java index bc261a8d..d087e74e 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/scheduler/bukkit/BukkitServerSchedulerAdapter.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/scheduler/bukkit/BukkitServerSchedulerAdapter.java @@ -3,6 +3,7 @@ package com.leonardobishop.quests.bukkit.scheduler.bukkit; import com.leonardobishop.quests.bukkit.BukkitQuestsPlugin; import com.leonardobishop.quests.bukkit.scheduler.ServerScheduler; import com.leonardobishop.quests.bukkit.scheduler.WrappedTask; +import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.scheduler.BukkitScheduler; import org.jetbrains.annotations.NotNull; @@ -27,6 +28,26 @@ public class BukkitServerSchedulerAdapter implements ServerScheduler { wrappedTask.cancel(); } + @Override + public @NotNull WrappedTask runTask(@NotNull Runnable runnable) { + return new BukkitWrappedTask(bukkitScheduler.runTask(plugin, runnable)); + } + + @Override + public @NotNull WrappedTask runTaskAsynchronously(@NotNull Runnable runnable) { + return new BukkitWrappedTask(bukkitScheduler.runTaskAsynchronously(plugin, runnable)); + } + + @Override + public @NotNull WrappedTask runTaskAtEntity(@NotNull Entity entity, @NotNull Runnable runnable) { + return runTask(runnable); + } + + @Override + public @NotNull WrappedTask runTaskAtLocation(@NotNull Location location, @NotNull Runnable runnable) { + return runTask(runnable); + } + @Override public @NotNull WrappedTask runTaskTimer(@NotNull Runnable runnable, long delay, long period) { return new BukkitWrappedTask(bukkitScheduler.runTaskTimer(plugin, runnable, delay, period)); @@ -37,6 +58,16 @@ public class BukkitServerSchedulerAdapter implements ServerScheduler { return new BukkitWrappedTask(bukkitScheduler.runTaskTimerAsynchronously(plugin, runnable, delay, period)); } + @Override + public @NotNull WrappedTask runTaskTimerAtEntity(@NotNull Entity entity, @NotNull Runnable runnable, long delay, long period) { + return runTaskTimer(runnable, delay, period); + } + + @Override + public @NotNull WrappedTask runTaskTimerAtLocation(@NotNull Location location, @NotNull Runnable runnable, long delay, long period) { + return runTaskTimer(runnable, delay, period); + } + @Override public @NotNull WrappedTask runTaskLater(@NotNull Runnable runnable, long delay) { return new BukkitWrappedTask(bukkitScheduler.runTaskLater(plugin, runnable, delay)); @@ -52,6 +83,11 @@ public class BukkitServerSchedulerAdapter implements ServerScheduler { return runTaskLater(runnable, delay); } + @Override + public @NotNull WrappedTask runTaskLaterAtLocation(@NotNull Location location, @NotNull Runnable runnable, long delay) { + return runTaskLater(runnable, delay); + } + @Override public void doSync(Runnable runnable) { bukkitScheduler.runTask(plugin, runnable); diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/scheduler/folia/FoliaServerScheduler.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/scheduler/folia/FoliaServerScheduler.java index ca62fb0d..0bc24794 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/scheduler/folia/FoliaServerScheduler.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/scheduler/folia/FoliaServerScheduler.java @@ -5,6 +5,8 @@ import com.leonardobishop.quests.bukkit.scheduler.ServerScheduler; import com.leonardobishop.quests.bukkit.scheduler.WrappedTask; import io.papermc.paper.threadedregions.scheduler.AsyncScheduler; import io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler; +import io.papermc.paper.threadedregions.scheduler.RegionScheduler; +import org.bukkit.Location; import org.bukkit.Server; import org.bukkit.entity.Entity; import org.jetbrains.annotations.NotNull; @@ -30,6 +32,7 @@ public class FoliaServerScheduler implements ServerScheduler { private final GlobalRegionScheduler globalRegionScheduler; private final AsyncScheduler asyncScheduler; + private final RegionScheduler regionScheduler; public FoliaServerScheduler(BukkitQuestsPlugin plugin) { this.plugin = plugin; @@ -37,6 +40,7 @@ public class FoliaServerScheduler implements ServerScheduler { final Server server = plugin.getServer(); this.globalRegionScheduler = server.getGlobalRegionScheduler(); this.asyncScheduler = server.getAsyncScheduler(); + this.regionScheduler = server.getRegionScheduler(); } @Override @@ -50,6 +54,26 @@ public class FoliaServerScheduler implements ServerScheduler { wrappedTask.cancel(); } + @Override + public @NotNull WrappedTask runTask(@NotNull Runnable runnable) { + return new FoliaWrappedTask(globalRegionScheduler.run(plugin, task -> runnable.run())); + } + + @Override + public @NotNull WrappedTask runTaskAsynchronously(@NotNull Runnable runnable) { + return new FoliaWrappedTask(asyncScheduler.runNow(plugin, task -> runnable.run())); + } + + @Override + public @NotNull WrappedTask runTaskAtEntity(@NotNull Entity entity, @NotNull Runnable runnable) { + return new FoliaWrappedTask(Objects.requireNonNull(entity.getScheduler().run(plugin, task -> runnable.run(), () -> {}))); + } + + @Override + public @NotNull WrappedTask runTaskAtLocation(@NotNull Location location, @NotNull Runnable runnable) { + return new FoliaWrappedTask(regionScheduler.run(plugin, location, task -> runnable.run())); + } + @Override public @NotNull WrappedTask runTaskTimer(@NotNull Runnable runnable, long delay, long period) { return new FoliaWrappedTask(globalRegionScheduler.runAtFixedRate(plugin, task -> { @@ -62,6 +86,16 @@ public class FoliaServerScheduler implements ServerScheduler { return new FoliaWrappedTask(asyncScheduler.runAtFixedRate(plugin, task -> runnable.run(), delay * 50L, period * 50L, TimeUnit.MILLISECONDS)); } + @Override + public @NotNull WrappedTask runTaskTimerAtEntity(@NotNull Entity entity, @NotNull Runnable runnable, long delay, long period) { + return new FoliaWrappedTask(Objects.requireNonNull(entity.getScheduler().runAtFixedRate(plugin, task -> runnable.run(), () -> {}, delay, period))); + } + + @Override + public @NotNull WrappedTask runTaskTimerAtLocation(@NotNull Location location, @NotNull Runnable runnable, long delay, long period) { + return new FoliaWrappedTask(regionScheduler.runAtFixedRate(plugin, location, task -> runnable.run(), delay, period)); + } + @Override public @NotNull WrappedTask runTaskLater(@NotNull Runnable runnable, long delay) { return new FoliaWrappedTask(globalRegionScheduler.runDelayed(plugin, task -> runnable.run(), delay)); @@ -78,12 +112,7 @@ public class FoliaServerScheduler implements ServerScheduler { } @Override - public void doSync(Runnable runnable) { - globalRegionScheduler.run(plugin, task -> runnable.run()); - } - - @Override - public void doAsync(Runnable runnable) { - asyncScheduler.runNow(plugin, task -> runnable.run()); + public @NotNull WrappedTask runTaskLaterAtLocation(@NotNull Location location, @NotNull Runnable runnable, long delay) { + return new FoliaWrappedTask(regionScheduler.runDelayed(plugin, location, task -> runnable.run(), delay)); } } -- cgit v1.2.3-70-g09d2