diff --git a/platform/common/src/main/java/com/tcoded/folialib/impl/ServerImplementation.java b/platform/common/src/main/java/com/tcoded/folialib/impl/ServerImplementation.java index c502f47..f5bdac9 100644 --- a/platform/common/src/main/java/com/tcoded/folialib/impl/ServerImplementation.java +++ b/platform/common/src/main/java/com/tcoded/folialib/impl/ServerImplementation.java @@ -3,6 +3,7 @@ import com.tcoded.folialib.enums.EntityTaskResult; import com.tcoded.folialib.wrapper.task.WrappedTask; import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -222,6 +223,18 @@ public interface ServerImplementation { */ CompletableFuture runAtLocation(Location location, @NotNull Consumer consumer); + /** + * Folia: Synced with the tick of the region of the chunk of the location + * Paper: Synced with the server main thread + * Spigot: Synced with the server main thread + * @param world The world to run the task at + * @param chunkX The x coordinate of the chunk to run the task at + * @param chunkZ The z coordinate of the chunk to run the task at + * @param consumer Task to run + * @return Future when the task is completed + */ + CompletableFuture runAtLocation(World world, int chunkX, int chunkZ, @NotNull Consumer consumer); + /** * Folia: Synced with the tick of the region of the chunk of the location * Paper: Synced with the server main thread @@ -233,6 +246,19 @@ public interface ServerImplementation { */ WrappedTask runAtLocationLater(Location location, @NotNull Runnable runnable, long delay); + /** + * Folia: Synced with the tick of the region of the chunk of the location + * Paper: Synced with the server main thread + * Spigot: Synced with the server main thread + * @param world The world to run the task at + * @param chunkX The x coordinate of the chunk to run the task at + * @param chunkZ The z coordinate of the chunk to run the task at + * @param runnable Task to run + * @param delay Delay before execution in ticks + * @return WrappedTask instance + */ + WrappedTask runAtLocationLater(World world, int chunkX, int chunkZ, @NotNull Runnable runnable, long delay); + /** * Folia: Synced with the tick of the region of the chunk of the location * Paper: Synced with the server main thread @@ -243,6 +269,18 @@ public interface ServerImplementation { */ void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay); + /** + * Folia: Synced with the tick of the region of the chunk of the location + * Paper: Synced with the server main thread + * Spigot: Synced with the server main thread + * @param world The world to run the task at + * @param chunkX The x coordinate of the chunk to run the task at + * @param chunkZ The z coordinate of the chunk to run the task at + * @param consumer Task to run + * @param delay Delay before execution in ticks + */ + void runAtLocationLater(World world, int chunkX, int chunkZ, @NotNull Consumer consumer, long delay); + /** * Folia: Synced with the tick of the region of the chunk of the location * Paper: Synced with the server main thread @@ -255,6 +293,20 @@ public interface ServerImplementation { */ WrappedTask runAtLocationLater(Location location, @NotNull Runnable runnable, long delay, TimeUnit unit); + /** + * Folia: Synced with the tick of the region of the chunk of the location + * Paper: Synced with the server main thread + * Spigot: Synced with the server main thread + * @param world The world to run the task at + * @param chunkX The x coordinate of the chunk to run the task at + * @param chunkZ The z coordinate of the chunk to run the task at + * @param runnable Task to run + * @param delay Delay before execution + * @param unit Time unit + * @return WrappedTask instance + */ + WrappedTask runAtLocationLater(World world, int chunkX, int chunkZ, @NotNull Runnable runnable, long delay, TimeUnit unit); + /** * Folia: Synced with the tick of the region of the chunk of the location * Paper: Synced with the server main thread @@ -266,6 +318,19 @@ public interface ServerImplementation { */ void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay, TimeUnit unit); + /** + * Folia: Synced with the tick of the region of the chunk of the location + * Paper: Synced with the server main thread + * Spigot: Synced with the server main thread + * @param world The world to run the task at + * @param chunkX The x coordinate of the chunk to run the task at + * @param chunkZ The z coordinate of the chunk to run the task at + * @param consumer Task to run + * @param delay Delay before execution + * @param unit Time unit + */ + void runAtLocationLater(World world, int chunkX, int chunkZ, @NotNull Consumer consumer, long delay, TimeUnit unit); + /** * Folia: Synced with the tick of the region of the chunk of the location * Paper: Synced with the server main thread @@ -278,6 +343,20 @@ public interface ServerImplementation { */ WrappedTask runAtLocationTimer(Location location, @NotNull Runnable runnable, long delay, long period); + /** + * Folia: Synced with the tick of the region of the chunk of the location + * Paper: Synced with the server main thread + * Spigot: Synced with the server main thread + * @param world The world to run the task at + * @param chunkX The x coordinate of the chunk to run the task at + * @param chunkZ The z coordinate of the chunk to run the task at + * @param runnable Task to run + * @param delay Delay before first execution in ticks + * @param period Delay between executions in ticks + * @return WrappedTask instance + */ + WrappedTask runAtLocationTimer(World world, int chunkX, int chunkZ, @NotNull Runnable runnable, long delay, long period); + /** * Folia: Synced with the tick of the region of the chunk of the location * Paper: Synced with the server main thread @@ -289,6 +368,19 @@ public interface ServerImplementation { */ void runAtLocationTimer(Location location, @NotNull Consumer consumer, long delay, long period); + /** + * Folia: Synced with the tick of the region of the chunk of the location + * Paper: Synced with the server main thread + * Spigot: Synced with the server main thread + * @param world The world to run the task at + * @param chunkX The x coordinate of the chunk to run the task at + * @param chunkZ The z coordinate of the chunk to run the task at + * @param consumer Task to run + * @param delay Delay before first execution in ticks + * @param period Delay between executions in ticks + */ + void runAtLocationTimer(World world, int chunkX, int chunkZ, @NotNull Consumer consumer, long delay, long period); + /** * Folia: Synced with the tick of the region of the chunk of the location * Paper: Synced with the server main thread @@ -302,6 +394,21 @@ public interface ServerImplementation { */ WrappedTask runAtLocationTimer(Location location, @NotNull Runnable runnable, long delay, long period, TimeUnit unit); + /** + * Folia: Synced with the tick of the region of the chunk of the location + * Paper: Synced with the server main thread + * Spigot: Synced with the server main thread + * @param world The world to run the task at + * @param chunkX The x coordinate of the chunk to run the task at + * @param chunkZ The z coordinate of the chunk to run the task at + * @param runnable Task to run + * @param delay Delay before first execution + * @param period Delay between executions + * @param unit Time unit + * @return WrappedTask instance + */ + WrappedTask runAtLocationTimer(World world, int chunkX, int chunkZ, @NotNull Runnable runnable, long delay, long period, TimeUnit unit); + /** * Folia: Synced with the tick of the region of the chunk of the location * Paper: Synced with the server main thread @@ -314,6 +421,19 @@ public interface ServerImplementation { */ void runAtLocationTimer(Location location, @NotNull Consumer consumer, long delay, long period, TimeUnit unit); + /** + * Folia: Synced with the tick of the region of the chunk of the location + * Paper: Synced with the server main thread + * Spigot: Synced with the server main thread + * @param world The world to run the task at + * @param chunkX The x coordinate of the chunk to run the task at + * @param chunkZ The z coordinate of the chunk to run the task at + * @param consumer Task to run + * @param delay Delay before first execution + * @param period Delay between executions + * @param unit Time unit + */ + void runAtLocationTimer(World world, int chunkX, int chunkZ, @NotNull Consumer consumer, long delay, long period, TimeUnit unit); // ----- Entity based ----- diff --git a/platform/folia/src/main/java/com/tcoded/folialib/impl/FoliaImplementation.java b/platform/folia/src/main/java/com/tcoded/folialib/impl/FoliaImplementation.java index af68938..ad64c9f 100644 --- a/platform/folia/src/main/java/com/tcoded/folialib/impl/FoliaImplementation.java +++ b/platform/folia/src/main/java/com/tcoded/folialib/impl/FoliaImplementation.java @@ -11,6 +11,7 @@ import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -187,6 +188,18 @@ public CompletableFuture runAtLocation(Location location, @NotNull Consume future.complete(null); }); + return future; + } + + @Override + public CompletableFuture runAtLocation(World world, int chunkX, int chunkZ, @NotNull Consumer consumer) { + CompletableFuture future = new CompletableFuture<>(); + + this.plugin.getServer().getRegionScheduler().run(plugin, world, chunkX, chunkZ, task -> { + consumer.accept(this.wrapTask(task)); + future.complete(null); + }); + return future; } @@ -201,6 +214,17 @@ public WrappedTask runAtLocationLater(Location location, @NotNull Runnable runna ); } + @Override + public WrappedTask runAtLocationLater(World world, int chunkX, int chunkZ, @NotNull Runnable runnable, long delay) { + if (delay <= 0) { + InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), delay); + delay = 1; + } + return this.wrapTask( + this.plugin.getServer().getRegionScheduler().runDelayed(plugin, world, chunkX, chunkZ, task -> runnable.run(), delay) + ); + } + @Override public void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay) { if (delay <= 0) { @@ -210,16 +234,35 @@ public void runAtLocationLater(Location location, @NotNull Consumer this.plugin.getServer().getRegionScheduler().runDelayed(plugin, location, task -> consumer.accept(this.wrapTask(task)), delay); } + @Override + public void runAtLocationLater(World world, int chunkX, int chunkZ, @NotNull Consumer consumer, long delay) { + if (delay <= 0) { + InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), delay); + delay = 1; + } + this.plugin.getServer().getRegionScheduler().runDelayed(plugin, world, chunkX, chunkZ, task -> consumer.accept(this.wrapTask(task)), delay); + } + @Override public WrappedTask runAtLocationLater(Location location, @NotNull Runnable runnable, long delay, TimeUnit unit) { return this.runAtLocationLater(location, runnable, TimeConverter.toTicks(delay, unit)); } + @Override + public WrappedTask runAtLocationLater(World world, int chunkX, int chunkZ, @NotNull Runnable runnable, long delay, TimeUnit unit) { + return this.runAtLocationLater(world, chunkX, chunkZ, runnable, TimeConverter.toTicks(delay, unit)); + } + @Override public void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay, TimeUnit unit) { this.runAtLocationLater(location, consumer, TimeConverter.toTicks(delay, unit)); } + @Override + public void runAtLocationLater(World world, int chunkX, int chunkZ, @NotNull Consumer consumer, long delay, TimeUnit unit) { + this.runAtLocationLater(world, chunkX, chunkZ, consumer, TimeConverter.toTicks(delay, unit)); + } + @Override public WrappedTask runAtLocationTimer(Location location, @NotNull Runnable runnable, long delay, long period) { if (delay <= 0) { @@ -235,6 +278,21 @@ public WrappedTask runAtLocationTimer(Location location, @NotNull Runnable runna ); } + @Override + public WrappedTask runAtLocationTimer(World world, int chunkX, int chunkZ, @NotNull Runnable runnable, long delay, long period) { + if (delay <= 0) { + InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), delay); + delay = 1; + } + if (period <= 0) { + InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), period); + period = 1; + } + return this.wrapTask( + this.plugin.getServer().getRegionScheduler().runAtFixedRate(plugin, world, chunkX, chunkZ, task -> runnable.run(), delay, period) + ); + } + @Override public void runAtLocationTimer(Location location, @NotNull Consumer consumer, long delay, long period) { if (delay <= 0) { @@ -248,16 +306,39 @@ public void runAtLocationTimer(Location location, @NotNull Consumer this.plugin.getServer().getRegionScheduler().runAtFixedRate(plugin, location, task -> consumer.accept(this.wrapTask(task)), delay, period); } + @Override + public void runAtLocationTimer(World world, int chunkX, int chunkZ, @NotNull Consumer consumer, long delay, long period) { + if (delay <= 0) { + InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), delay); + delay = 1; + } + if (period <= 0) { + InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), period); + period = 1; + } + this.plugin.getServer().getRegionScheduler().runAtFixedRate(plugin, world, chunkX, chunkZ, task -> consumer.accept(this.wrapTask(task)), delay, period); + } + @Override public WrappedTask runAtLocationTimer(Location location, @NotNull Runnable runnable, long delay, long period, TimeUnit unit) { return this.runAtLocationTimer(location, runnable, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } + @Override + public WrappedTask runAtLocationTimer(World world, int chunkX, int chunkZ, @NotNull Runnable runnable, long delay, long period, TimeUnit unit) { + return this.runAtLocationTimer(world, chunkX, chunkZ, runnable, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); + } + @Override public void runAtLocationTimer(Location location, @NotNull Consumer consumer, long delay, long period, TimeUnit unit) { this.runAtLocationTimer(location, consumer, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } + @Override + public void runAtLocationTimer(World world, int chunkX, int chunkZ, @NotNull Consumer consumer, long delay, long period, TimeUnit unit) { + this.runAtLocationTimer(world, chunkX, chunkZ, consumer, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); + } + @Override public CompletableFuture runAtEntity(Entity entity, @NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); diff --git a/platform/legacy-spigot/src/main/java/com/tcoded/folialib/impl/LegacySpigotImplementation.java b/platform/legacy-spigot/src/main/java/com/tcoded/folialib/impl/LegacySpigotImplementation.java index 3e29cdb..cd9c694 100644 --- a/platform/legacy-spigot/src/main/java/com/tcoded/folialib/impl/LegacySpigotImplementation.java +++ b/platform/legacy-spigot/src/main/java/com/tcoded/folialib/impl/LegacySpigotImplementation.java @@ -8,6 +8,7 @@ import com.tcoded.folialib.wrapper.task.WrappedTask; import com.tcoded.folialib.wrapper.task.WrappedLegacyBukkitTask; import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -163,18 +164,29 @@ public CompletableFuture runAtLocation(Location location, @NotNull Consume return this.runNextTick(consumer); } + @Override + public CompletableFuture runAtLocation(World world, int chunkX, int chunkZ, @NotNull Consumer consumer) { + return this.runNextTick(consumer); + } + @Override public WrappedTask runAtLocationLater(Location location, @NotNull Runnable runnable, long delay) { - return this.wrapTask(this.scheduler.runTaskLater(plugin, runnable, delay)); + return this.runLater(runnable, delay); + } + + @Override + public WrappedTask runAtLocationLater(World world, int chunkX, int chunkZ, @NotNull Runnable runnable, long delay) { + return this.runLater(runnable, delay); } @Override public void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay) { - WrappedTask[] taskReference = new WrappedTask[1]; + this.runLater(consumer, delay); + } - taskReference[0] = this.wrapTask(this.scheduler.runTaskLater(plugin, () -> { - consumer.accept(taskReference[0]); - }, delay)); + @Override + public void runAtLocationLater(World world, int chunkX, int chunkZ, @NotNull Consumer consumer, long delay) { + this.runLater(consumer, delay); } @Override @@ -182,23 +194,39 @@ public WrappedTask runAtLocationLater(Location location, @NotNull Runnable runna return this.runAtLocationLater(location, runnable, TimeConverter.toTicks(delay, unit)); } + @Override + public WrappedTask runAtLocationLater(World world, int chunkX, int chunkZ, @NotNull Runnable runnable, long delay, TimeUnit unit) { + return this.runAtLocationLater(world, chunkX, chunkZ, runnable, TimeConverter.toTicks(delay, unit)); + } + @Override public void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay, TimeUnit unit) { this.runAtLocationLater(location, consumer, TimeConverter.toTicks(delay, unit)); } + @Override + public void runAtLocationLater(World world, int chunkX, int chunkZ, @NotNull Consumer consumer, long delay, TimeUnit unit) { + this.runAtLocationLater(world, chunkX, chunkZ, consumer, TimeConverter.toTicks(delay, unit)); + } + @Override public WrappedTask runAtLocationTimer(Location location, @NotNull Runnable runnable, long delay, long period) { - return this.wrapTask(this.scheduler.runTaskTimer(plugin, runnable, delay, period)); + return this.runTimer(runnable, delay, period); + } + + @Override + public WrappedTask runAtLocationTimer(World world, int chunkX, int chunkZ, @NotNull Runnable runnable, long delay, long period) { + return this.runTimer(runnable, delay, period); } @Override public void runAtLocationTimer(Location location, @NotNull Consumer consumer, long delay, long period) { - WrappedTask[] taskReference = new WrappedTask[1]; + this.runTimer(consumer, delay, period); + } - taskReference[0] = this.wrapTask(this.scheduler.runTaskTimer(plugin, () -> { - consumer.accept(taskReference[0]); - }, delay, period)); + @Override + public void runAtLocationTimer(World world, int chunkX, int chunkZ, @NotNull Consumer consumer, long delay, long period) { + this.runTimer(consumer, delay, period); } @Override @@ -206,11 +234,21 @@ public WrappedTask runAtLocationTimer(Location location, @NotNull Runnable runna return this.runAtLocationTimer(location, runnable, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } + @Override + public WrappedTask runAtLocationTimer(World world, int chunkX, int chunkZ, @NotNull Runnable runnable, long delay, long period, TimeUnit unit) { + return this.runAtLocationTimer(world, chunkX, chunkZ, runnable, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); + } + @Override public void runAtLocationTimer(Location location, @NotNull Consumer consumer, long delay, long period, TimeUnit unit) { this.runAtLocationTimer(location, consumer, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } + @Override + public void runAtLocationTimer(World world, int chunkX, int chunkZ, @NotNull Consumer consumer, long delay, long period, TimeUnit unit) { + this.runAtLocationTimer(world, chunkX, chunkZ, consumer, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); + } + @Override public CompletableFuture runAtEntity(Entity entity, @NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>();