diff options
| author | Nahuel Dolores <contact@nahu.me> | 2023-07-24 10:46:57 -0300 |
|---|---|---|
| committer | Nahuel Dolores <contact@nahu.me> | 2023-07-24 10:46:57 -0300 |
| commit | 481773de1670c5dac5a2186252e5dccd5991404d (patch) | |
| tree | 9de34f620c9094dbad7a57677a844ba85a8aec59 /bukkit | |
| parent | 63d8c6b4d9c5c70bb0d71ddb541bfc121b142ec1 (diff) | |
Add missing scheduler wrapper methods for future usage
Diffstat (limited to 'bukkit')
3 files changed, 191 insertions, 7 deletions
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; @@ -18,6 +19,60 @@ public interface ServerScheduler extends com.leonardobishop.quests.common.schedu void cancelTask(@NotNull WrappedTask wrappedTask); /** + * Run a new task. + * <p> + * Folia: Synced with the server daylight cycle tick. + * <p> + * 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. + * <p> + * Folia: Run in the dedicated async thread. + * <p> + * 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. + * <p> + * Folia: Synced with the tick of the region of the entity (even if the entity moves). + * <p> + * 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. + * <p> + * Folia: Synced with the tick of the region of the chunk of the location. + * <p> + * 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. * <p> * Folia: Synced with the server daylight cycle tick. @@ -48,6 +103,38 @@ public interface ServerScheduler extends com.leonardobishop.quests.common.schedu WrappedTask runTaskTimerAsynchronously(@NotNull Runnable runnable, long delay, long period); /** + * Run a new task timer. + * <p> + * Folia: Synced with the tick of the region of the entity (even if the entity moves). + * <p> + * 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. + * <p> + * Folia: Synced with the tick of the region of the chunk of the location. + * <p> + * 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. * <p> * Folia: Synced with the server daylight cycle tick. @@ -82,6 +169,7 @@ public interface ServerScheduler extends com.leonardobishop.quests.common.schedu * <p> * 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. @@ -90,6 +178,37 @@ public interface ServerScheduler extends com.leonardobishop.quests.common.schedu WrappedTask runTaskLaterAtEntity(@NotNull Entity entity, @NotNull Runnable runnable, long delay); /** + * Run a new task later. + * <p> + * Folia: Synced with the tick of the region of the chunk of the location. + * <p> + * 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. * * @return {@link String} 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; @@ -28,6 +29,26 @@ public class BukkitServerSchedulerAdapter implements ServerScheduler { } @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)); } @@ -38,6 +59,16 @@ public class BukkitServerSchedulerAdapter implements ServerScheduler { } @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)); } @@ -53,6 +84,11 @@ public class BukkitServerSchedulerAdapter implements ServerScheduler { } @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 @@ -51,6 +55,26 @@ public class FoliaServerScheduler implements ServerScheduler { } @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 -> { if (runnable != null) runnable.run(); @@ -63,6 +87,16 @@ public class FoliaServerScheduler implements ServerScheduler { } @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)); } } |
