diff --git a/build.gradle b/build.gradle index 470d96b..237ba64 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { allprojects { apply plugin: 'java' - version = '0.3.3' + version = '0.3.4' repositories { 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 a1d459a..c502f47 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 @@ -5,6 +5,8 @@ import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.UUID; @@ -24,7 +26,7 @@ public interface ServerImplementation { * @param consumer Task to run * @return Future when the task is completed */ - CompletableFuture runNextTick(Consumer consumer); + CompletableFuture runNextTick(@NotNull Consumer consumer); /** * Folia: Async @@ -33,7 +35,7 @@ public interface ServerImplementation { * @param consumer Task to run * @return Future when the task is completed */ - CompletableFuture runAsync(Consumer consumer); + CompletableFuture runAsync(@NotNull Consumer consumer); // ----- Run Later ----- @@ -45,7 +47,7 @@ public interface ServerImplementation { * @param delay Delay before execution in ticks * @return WrappedTask instance */ - WrappedTask runLater(Runnable runnable, long delay); + WrappedTask runLater(@NotNull Runnable runnable, long delay); /** * Folia: Synced with the server daylight cycle tick @@ -54,7 +56,7 @@ public interface ServerImplementation { * @param consumer Task to run * @param delay Delay before execution in ticks */ - void runLater(Consumer consumer, long delay); + void runLater(@NotNull Consumer consumer, long delay); /** * Folia: Synced with the server daylight cycle tick @@ -65,7 +67,7 @@ public interface ServerImplementation { * @param unit Time unit * @return WrappedTask instance */ - WrappedTask runLater(Runnable runnable, long delay, TimeUnit unit); + WrappedTask runLater(@NotNull Runnable runnable, long delay, TimeUnit unit); /** * Folia: Synced with the server daylight cycle tick @@ -75,7 +77,7 @@ public interface ServerImplementation { * @param delay Delay before execution * @param unit Time unit */ - void runLater(Consumer consumer, long delay, TimeUnit unit); + void runLater(@NotNull Consumer consumer, long delay, TimeUnit unit); /** * Folia: Async @@ -85,7 +87,7 @@ public interface ServerImplementation { * @param delay Delay before execution in ticks * @return WrappedTask instance */ - WrappedTask runLaterAsync(Runnable runnable, long delay); + WrappedTask runLaterAsync(@NotNull Runnable runnable, long delay); /** * Folia: Async @@ -94,7 +96,7 @@ public interface ServerImplementation { * @param consumer Task to run * @param delay Delay before execution in ticks */ - void runLaterAsync(Consumer consumer, long delay); + void runLaterAsync(@NotNull Consumer consumer, long delay); /** * Folia: Async @@ -105,7 +107,7 @@ public interface ServerImplementation { * @param unit Time unit * @return WrappedTask instance */ - WrappedTask runLaterAsync(Runnable runnable, long delay, TimeUnit unit); + WrappedTask runLaterAsync(@NotNull Runnable runnable, long delay, TimeUnit unit); /** * Folia: Async @@ -115,7 +117,7 @@ public interface ServerImplementation { * @param delay Delay before execution * @param unit Time unit */ - void runLaterAsync(Consumer consumer, long delay, TimeUnit unit); + void runLaterAsync(@NotNull Consumer consumer, long delay, TimeUnit unit); // ----- Global Timers ----- @@ -128,7 +130,7 @@ public interface ServerImplementation { * @param period Delay between executions in ticks * @return WrappedTask instance */ - WrappedTask runTimer(Runnable runnable, long delay, long period); + WrappedTask runTimer(@NotNull Runnable runnable, long delay, long period); /** * Folia: Synced with the server daylight cycle tick @@ -138,7 +140,7 @@ public interface ServerImplementation { * @param delay Delay before first execution in ticks * @param period Delay between executions in ticks */ - void runTimer(Consumer consumer, long delay, long period); + void runTimer(@NotNull Consumer consumer, long delay, long period); /** * Folia: Synced with the server daylight cycle tick @@ -150,7 +152,7 @@ public interface ServerImplementation { * @param unit Time unit * @return WrappedTask instance */ - WrappedTask runTimer(Runnable runnable, long delay, long period, TimeUnit unit); + WrappedTask runTimer(@NotNull Runnable runnable, long delay, long period, TimeUnit unit); /** * Folia: Synced with the server daylight cycle tick @@ -161,7 +163,7 @@ public interface ServerImplementation { * @param period Delay between executions * @param unit Time unit */ - void runTimer(Consumer consumer, long delay, long period, TimeUnit unit); + void runTimer(@NotNull Consumer consumer, long delay, long period, TimeUnit unit); /** * Folia: Async @@ -172,7 +174,7 @@ public interface ServerImplementation { * @param period Delay between executions in ticks * @return WrappedTask instance */ - WrappedTask runTimerAsync(Runnable runnable, long delay, long period); + WrappedTask runTimerAsync(@NotNull Runnable runnable, long delay, long period); /** * Folia: Async @@ -182,7 +184,7 @@ public interface ServerImplementation { * @param delay Delay before first execution in ticks * @param period Delay between executions in ticks */ - void runTimerAsync(Consumer consumer, long delay, long period); + void runTimerAsync(@NotNull Consumer consumer, long delay, long period); /** * Folia: Async @@ -194,7 +196,7 @@ public interface ServerImplementation { * @param unit Time unit * @return WrappedTask instance */ - WrappedTask runTimerAsync(Runnable runnable, long delay, long period, TimeUnit unit); + WrappedTask runTimerAsync(@NotNull Runnable runnable, long delay, long period, TimeUnit unit); /** * Folia: Async @@ -205,7 +207,7 @@ public interface ServerImplementation { * @param period Delay between executions * @param unit Time unit */ - void runTimerAsync(Consumer consumer, long delay, long period, TimeUnit unit); + void runTimerAsync(@NotNull Consumer consumer, long delay, long period, TimeUnit unit); // ----- Location/Region based ----- @@ -218,7 +220,7 @@ public interface ServerImplementation { * @param consumer Task to run * @return Future when the task is completed */ - CompletableFuture runAtLocation(Location location, Consumer consumer); + CompletableFuture runAtLocation(Location location, @NotNull Consumer consumer); /** * Folia: Synced with the tick of the region of the chunk of the location @@ -229,7 +231,7 @@ public interface ServerImplementation { * @param delay Delay before execution in ticks * @return WrappedTask instance */ - WrappedTask runAtLocationLater(Location location, Runnable runnable, long delay); + WrappedTask runAtLocationLater(Location location, @NotNull Runnable runnable, long delay); /** * Folia: Synced with the tick of the region of the chunk of the location @@ -239,7 +241,7 @@ public interface ServerImplementation { * @param consumer Task to run * @param delay Delay before execution in ticks */ - void runAtLocationLater(Location location, Consumer consumer, long delay); + void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay); /** * Folia: Synced with the tick of the region of the chunk of the location @@ -251,7 +253,7 @@ public interface ServerImplementation { * @param unit Time unit * @return WrappedTask instance */ - WrappedTask runAtLocationLater(Location location, Runnable runnable, long delay, TimeUnit unit); + 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 @@ -262,7 +264,7 @@ public interface ServerImplementation { * @param delay Delay before execution * @param unit Time unit */ - void runAtLocationLater(Location location, Consumer consumer, long delay, TimeUnit unit); + 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 @@ -274,7 +276,7 @@ public interface ServerImplementation { * @param period Delay between executions in ticks * @return WrappedTask instance */ - WrappedTask runAtLocationTimer(Location location, Runnable runnable, long delay, long period); + 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 @@ -285,7 +287,7 @@ public interface ServerImplementation { * @param delay Delay before first execution in ticks * @param period Delay between executions in ticks */ - void runAtLocationTimer(Location location, Consumer consumer, long delay, long period); + 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 @@ -298,7 +300,7 @@ public interface ServerImplementation { * @param unit Time unit * @return WrappedTask instance */ - WrappedTask runAtLocationTimer(Location location, Runnable runnable, long delay, long period, TimeUnit unit); + 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 @@ -310,7 +312,7 @@ public interface ServerImplementation { * @param period Delay between executions * @param unit Time unit */ - void runAtLocationTimer(Location location, Consumer consumer, long delay, long period, TimeUnit unit); + void runAtLocationTimer(Location location, @NotNull Consumer consumer, long delay, long period, TimeUnit unit); // ----- Entity based ----- @@ -323,7 +325,7 @@ public interface ServerImplementation { * @param consumer Task to run * @return Future when the task is completed */ - CompletableFuture runAtEntity(Entity entity, Consumer consumer); + CompletableFuture runAtEntity(Entity entity, @NotNull Consumer consumer); /** * Folia: Synced with the tick of the region of the entity (even if the entity moves) @@ -333,7 +335,7 @@ public interface ServerImplementation { * @param consumer Task to run * @return Future when the task is completed */ - CompletableFuture runAtEntityWithFallback(Entity entity, Consumer consumer, Runnable fallback); + CompletableFuture runAtEntityWithFallback(Entity entity, @NotNull Consumer consumer, @Nullable Runnable fallback); /** * Folia: Synced with the tick of the region of the entity (even if the entity moves) @@ -344,7 +346,19 @@ public interface ServerImplementation { * @param delay Delay before execution in ticks * @return WrappedTask instance */ - WrappedTask runAtEntityLater(Entity entity, Runnable runnable, long delay); + WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, long delay); + + /** + * Folia: Synced with the tick of the region of the entity (even if the entity moves) + * Paper: Synced with the server main thread + * Spigot: Synced with the server main thread + * @param entity Entity to run the task at + * @param runnable Task to run + * @param fallback Fallback task to run when the entity is removed + * @param delay Delay before execution in ticks + * @return WrappedTask instance + */ + WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, @Nullable Runnable fallback, long delay); /** * Folia: Synced with the tick of the region of the entity (even if the entity moves) @@ -354,7 +368,18 @@ public interface ServerImplementation { * @param consumer Task to run * @param delay Delay before execution in ticks */ - void runAtEntityLater(Entity entity, Consumer consumer, long delay); + void runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay); + + /** + * Folia: Synced with the tick of the region of the entity (even if the entity moves) + * Paper: Synced with the server main thread + * Spigot: Synced with the server main thread + * @param entity Entity to run the task at + * @param consumer Task to run + * @param fallback Fallback task to run when the entity is removed + * @param delay Delay before execution in ticks + */ + void runAtEntityLater(Entity entity, @NotNull Consumer consumer, Runnable fallback, long delay); /** * Folia: Synced with the tick of the region of the entity (even if the entity moves) @@ -366,7 +391,7 @@ public interface ServerImplementation { * @param unit Time unit * @return WrappedTask instance */ - WrappedTask runAtEntityLater(Entity entity, Runnable runnable, long delay, TimeUnit unit); + WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, long delay, TimeUnit unit); /** * Folia: Synced with the tick of the region of the entity (even if the entity moves) @@ -377,7 +402,19 @@ public interface ServerImplementation { * @param delay Delay before execution * @param unit Time unit */ - void runAtEntityLater(Entity entity, Consumer consumer, long delay, TimeUnit unit); + void runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay, TimeUnit unit); + + /** + * Folia: Synced with the tick of the region of the entity (even if the entity moves) + * Paper: Synced with the server main thread + * Spigot: Synced with the server main thread + * @param entity Entity 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 runAtEntityTimer(Entity entity, @NotNull Runnable runnable, long delay, long period); /** * Folia: Synced with the tick of the region of the entity (even if the entity moves) @@ -389,7 +426,18 @@ public interface ServerImplementation { * @param period Delay between executions in ticks * @return WrappedTask instance */ - WrappedTask runAtEntityTimer(Entity entity, Runnable runnable, long delay, long period); + WrappedTask runAtEntityTimer(Entity entity, @NotNull Runnable runnable, Runnable fallback, long delay, long period); + + /** + * Folia: Synced with the tick of the region of the entity (even if the entity moves) + * Paper: Synced with the server main thread + * Spigot: Synced with the server main thread + * @param entity Entity 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 runAtEntityTimer(Entity entity, @NotNull Consumer consumer, long delay, long period); /** * Folia: Synced with the tick of the region of the entity (even if the entity moves) @@ -400,7 +448,7 @@ public interface ServerImplementation { * @param delay Delay before first execution in ticks * @param period Delay between executions in ticks */ - void runAtEntityTimer(Entity entity, Consumer consumer, long delay, long period); + void runAtEntityTimer(Entity entity, @NotNull Consumer consumer, Runnable fallback, long delay, long period); /** * Folia: Synced with the tick of the region of the entity (even if the entity moves) @@ -413,7 +461,7 @@ public interface ServerImplementation { * @param unit Time unit * @return WrappedTask instance */ - WrappedTask runAtEntityTimer(Entity entity, Runnable runnable, long delay, long period, TimeUnit unit); + WrappedTask runAtEntityTimer(Entity entity, @NotNull Runnable runnable, long delay, long period, TimeUnit unit); /** * Folia: Synced with the tick of the region of the entity (even if the entity moves) @@ -425,7 +473,7 @@ public interface ServerImplementation { * @param period Delay between executions * @param unit Time unit */ - void runAtEntityTimer(Entity entity, Consumer consumer, long delay, long period, TimeUnit unit); + void runAtEntityTimer(Entity entity, @NotNull Consumer consumer, long delay, long period, TimeUnit unit); /** * Cancel a task 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 c1e3183..af68938 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 @@ -41,7 +41,7 @@ public FoliaImplementation(FoliaLib foliaLib) { } @Override - public CompletableFuture runNextTick(Consumer consumer) { + public CompletableFuture runNextTick(@NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); this.globalRegionScheduler.run(plugin, task -> { @@ -53,7 +53,7 @@ public CompletableFuture runNextTick(Consumer consumer) { } @Override - public CompletableFuture runAsync(Consumer consumer) { + public CompletableFuture runAsync(@NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); this.asyncScheduler.runNow(plugin, task -> { @@ -65,7 +65,7 @@ public CompletableFuture runAsync(Consumer consumer) { } @Override - public WrappedTask runLater(Runnable runnable, long delay) { + public WrappedTask runLater(@NotNull Runnable runnable, long delay) { if (delay <= 0) { InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), delay); delay = 1; @@ -74,7 +74,7 @@ public WrappedTask runLater(Runnable runnable, long delay) { } @Override - public void runLater(Consumer consumer, long delay) { + public void runLater(@NotNull Consumer consumer, long delay) { if (delay <= 0) { InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), delay); delay = 1; @@ -83,39 +83,39 @@ public void runLater(Consumer consumer, long delay) { } @Override - public WrappedTask runLater(Runnable runnable, long delay, TimeUnit unit) { + public WrappedTask runLater(@NotNull Runnable runnable, long delay, TimeUnit unit) { return this.runLater(runnable, TimeConverter.toTicks(delay, unit)); } @Override - public void runLater(Consumer consumer, long delay, TimeUnit unit) { + public void runLater(@NotNull Consumer consumer, long delay, TimeUnit unit) { this.runLater(consumer, TimeConverter.toTicks(delay, unit)); } @Override - public WrappedTask runLaterAsync(Runnable runnable, long delay) { + public WrappedTask runLaterAsync(@NotNull Runnable runnable, long delay) { return this.runLaterAsync(runnable, TimeConverter.toMillis(delay), TimeUnit.MILLISECONDS); } @Override - public void runLaterAsync(Consumer consumer, long delay) { + public void runLaterAsync(@NotNull Consumer consumer, long delay) { this.runLaterAsync(consumer, TimeConverter.toMillis(delay), TimeUnit.MILLISECONDS); } @Override - public WrappedTask runLaterAsync(Runnable runnable, long delay, TimeUnit unit) { + public WrappedTask runLaterAsync(@NotNull Runnable runnable, long delay, TimeUnit unit) { return this.wrapTask( this.asyncScheduler.runDelayed(plugin, task -> runnable.run(), delay, unit) ); } @Override - public void runLaterAsync(Consumer consumer, long delay, TimeUnit unit) { + public void runLaterAsync(@NotNull Consumer consumer, long delay, TimeUnit unit) { this.asyncScheduler.runDelayed(plugin, task -> consumer.accept(this.wrapTask(task)), delay, unit); } @Override - public WrappedTask runTimer(Runnable runnable, long delay, long period) { + public WrappedTask runTimer(@NotNull Runnable runnable, long delay, long period) { if (delay <= 0) { InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), delay); delay = 1; @@ -130,7 +130,7 @@ public WrappedTask runTimer(Runnable runnable, long delay, long period) { } @Override - public void runTimer(Consumer consumer, long delay, long period) { + public void runTimer(@NotNull Consumer consumer, long delay, long period) { if (delay <= 0) { InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), delay); delay = 1; @@ -143,43 +143,43 @@ public void runTimer(Consumer consumer, long delay, long period) { } @Override - public WrappedTask runTimer(Runnable runnable, long delay, long period, TimeUnit unit) { + public WrappedTask runTimer(@NotNull Runnable runnable, long delay, long period, TimeUnit unit) { return this.runTimer(runnable, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } @Override - public void runTimer(Consumer consumer, long delay, long period, TimeUnit unit) { + public void runTimer(@NotNull Consumer consumer, long delay, long period, TimeUnit unit) { this.runTimer(consumer, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } @Override - public WrappedTask runTimerAsync(Runnable runnable, long delay, long period) { + public WrappedTask runTimerAsync(@NotNull Runnable runnable, long delay, long period) { return this.runTimerAsync( runnable, TimeConverter.toMillis(delay), TimeConverter.toMillis(period), TimeUnit.MILLISECONDS ); } @Override - public void runTimerAsync(Consumer consumer, long delay, long period) { + public void runTimerAsync(@NotNull Consumer consumer, long delay, long period) { this.runTimerAsync( consumer, TimeConverter.toMillis(delay), TimeConverter.toMillis(period), TimeUnit.MILLISECONDS ); } @Override - public WrappedTask runTimerAsync(Runnable runnable, long delay, long period, TimeUnit unit) { + public WrappedTask runTimerAsync(@NotNull Runnable runnable, long delay, long period, TimeUnit unit) { return this.wrapTask( this.asyncScheduler.runAtFixedRate(plugin, task -> runnable.run(), delay, period, unit) ); } @Override - public void runTimerAsync(Consumer consumer, long delay, long period, TimeUnit unit) { + public void runTimerAsync(@NotNull Consumer consumer, long delay, long period, TimeUnit unit) { this.asyncScheduler.runAtFixedRate(plugin, task -> consumer.accept(this.wrapTask(task)), delay, period, unit); } @Override - public CompletableFuture runAtLocation(Location location, Consumer consumer) { + public CompletableFuture runAtLocation(Location location, @NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); this.plugin.getServer().getRegionScheduler().run(plugin, location, task -> { @@ -191,7 +191,7 @@ public CompletableFuture runAtLocation(Location location, Consumer consumer, long delay) { + public void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay) { if (delay <= 0) { InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), delay); delay = 1; @@ -211,17 +211,17 @@ public void runAtLocationLater(Location location, Consumer consumer } @Override - public WrappedTask runAtLocationLater(Location location, Runnable runnable, long delay, TimeUnit unit) { + public WrappedTask runAtLocationLater(Location location, @NotNull Runnable runnable, long delay, TimeUnit unit) { return this.runAtLocationLater(location, runnable, TimeConverter.toTicks(delay, unit)); } @Override - public void runAtLocationLater(Location location, Consumer consumer, long delay, TimeUnit unit) { + public void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay, TimeUnit unit) { this.runAtLocationLater(location, consumer, TimeConverter.toTicks(delay, unit)); } @Override - public WrappedTask runAtLocationTimer(Location location, Runnable runnable, long delay, long period) { + public WrappedTask runAtLocationTimer(Location location, @NotNull Runnable runnable, long delay, long period) { if (delay <= 0) { InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), delay); delay = 1; @@ -236,7 +236,7 @@ public WrappedTask runAtLocationTimer(Location location, Runnable runnable, long } @Override - public void runAtLocationTimer(Location location, Consumer consumer, long delay, long period) { + public void runAtLocationTimer(Location location, @NotNull Consumer consumer, long delay, long period) { if (delay <= 0) { InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), delay); delay = 1; @@ -249,17 +249,17 @@ public void runAtLocationTimer(Location location, Consumer consumer } @Override - public WrappedTask runAtLocationTimer(Location location, Runnable runnable, long delay, long period, TimeUnit unit) { + 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 void runAtLocationTimer(Location location, Consumer consumer, long delay, long period, TimeUnit unit) { + 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 CompletableFuture runAtEntity(Entity entity, Consumer consumer) { + public CompletableFuture runAtEntity(Entity entity, @NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); ScheduledTask scheduledTask = entity.getScheduler().run(this.plugin, task -> { @@ -275,7 +275,7 @@ public CompletableFuture runAtEntity(Entity entity, Consumer runAtEntityWithFallback(Entity entity, Consumer consumer, Runnable fallback) { + public CompletableFuture runAtEntityWithFallback(Entity entity, @NotNull Consumer consumer, Runnable fallback) { CompletableFuture future = new CompletableFuture<>(); ScheduledTask scheduledTask = entity.getScheduler().run(this.plugin, task -> { @@ -294,35 +294,50 @@ public CompletableFuture runAtEntityWithFallback(Entity entity } @Override - public WrappedTask runAtEntityLater(Entity entity, Runnable runnable, long delay) { + public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, long delay) { + return this.runAtEntityLater(entity, runnable, null, delay); + } + + @Override + public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, Runnable fallback, long delay) { if (delay <= 0) { InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), delay); delay = 1; } - return this.wrapTask(entity.getScheduler().runDelayed(plugin, task -> runnable.run(), null, delay)); + return this.wrapTask(entity.getScheduler().runDelayed(plugin, task -> runnable.run(), fallback, delay)); } @Override - public void runAtEntityLater(Entity entity, Consumer consumer, long delay) { + public void runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay) { + this.runAtEntityLater(entity, consumer, null, delay); + } + + @Override + public void runAtEntityLater(Entity entity, @NotNull Consumer consumer, Runnable fallback, long delay) { if (delay <= 0) { InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), delay); delay = 1; } - entity.getScheduler().runDelayed(plugin, task -> consumer.accept(this.wrapTask(task)), null, delay); + entity.getScheduler().runDelayed(plugin, task -> consumer.accept(this.wrapTask(task)), fallback, delay); } @Override - public WrappedTask runAtEntityLater(Entity entity, Runnable runnable, long delay, TimeUnit unit) { + public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, long delay, TimeUnit unit) { return this.runAtEntityLater(entity, runnable, TimeConverter.toTicks(delay, unit)); } @Override - public void runAtEntityLater(Entity entity, Consumer consumer, long delay, TimeUnit unit) { + public void runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay, TimeUnit unit) { this.runAtEntityLater(entity, consumer, TimeConverter.toTicks(delay, unit)); } @Override - public WrappedTask runAtEntityTimer(Entity entity, Runnable runnable, long delay, long period) { + public WrappedTask runAtEntityTimer(Entity entity, @NotNull Runnable runnable, long delay, long period) { + return this.runAtEntityTimer(entity, runnable, null, delay, period); + } + + @Override + public WrappedTask runAtEntityTimer(Entity entity, @NotNull Runnable runnable, Runnable fallback, long delay, long period) { if (delay <= 0) { InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), delay); delay = 1; @@ -332,12 +347,17 @@ public WrappedTask runAtEntityTimer(Entity entity, Runnable runnable, long delay period = 1; } return this.wrapTask( - entity.getScheduler().runAtFixedRate(plugin, task -> runnable.run(), null, delay, period) + entity.getScheduler().runAtFixedRate(plugin, task -> runnable.run(), fallback, delay, period) ); } - @Override - public void runAtEntityTimer(Entity entity, Consumer consumer, long delay, long period) { + @Override + public void runAtEntityTimer(Entity entity, @NotNull Consumer consumer, long delay, long period) { + this.runAtEntityTimer(entity, consumer, null, delay, period); + } + + @Override + public void runAtEntityTimer(Entity entity, @NotNull Consumer consumer, Runnable fallback, long delay, long period) { if (delay <= 0) { InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), delay); delay = 1; @@ -346,16 +366,16 @@ public void runAtEntityTimer(Entity entity, Consumer consumer, long InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), period); period = 1; } - entity.getScheduler().runAtFixedRate(plugin, task -> consumer.accept(this.wrapTask(task)), null, delay, period); + entity.getScheduler().runAtFixedRate(plugin, task -> consumer.accept(this.wrapTask(task)), fallback, delay, period); } - @Override - public WrappedTask runAtEntityTimer(Entity entity, Runnable runnable, long delay, long period, TimeUnit unit) { + @Override + public WrappedTask runAtEntityTimer(Entity entity, @NotNull Runnable runnable, long delay, long period, TimeUnit unit) { return this.runAtEntityTimer(entity, runnable, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } @Override - public void runAtEntityTimer(Entity entity, Consumer consumer, long delay, long period, TimeUnit unit) { + public void runAtEntityTimer(Entity entity, @NotNull Consumer consumer, long delay, long period, TimeUnit unit) { this.runAtEntityTimer(entity, consumer, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } @@ -460,8 +480,12 @@ public CompletableFuture teleportAsync(Player player, Location location @Override public WrappedTask wrapTask(Object nativeTask) { + if (nativeTask == null) { + return null; + } + if (!(nativeTask instanceof ScheduledTask)) { - String nativeTaskClassName = nativeTask == null ? null : nativeTask.getClass().getName(); + String nativeTaskClassName = nativeTask.getClass().getName(); throw new IllegalArgumentException("The nativeTask provided must be a ScheduledTask. Got: " + nativeTaskClassName + " instead."); } 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 bdea96e..3e29cdb 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 @@ -14,6 +14,7 @@ import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitTask; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.UUID; @@ -36,7 +37,7 @@ public LegacySpigotImplementation(FoliaLib foliaLib) { } @Override - public CompletableFuture runNextTick(Consumer consumer) { + public CompletableFuture runNextTick(@NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); WrappedTask[] taskReference = new WrappedTask[1]; @@ -49,7 +50,7 @@ public CompletableFuture runNextTick(Consumer consumer) { } @Override - public CompletableFuture runAsync(Consumer consumer) { + public CompletableFuture runAsync(@NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); WrappedTask[] taskReference = new WrappedTask[1]; @@ -62,12 +63,12 @@ public CompletableFuture runAsync(Consumer consumer) { } @Override - public WrappedTask runLater(Runnable runnable, long delay) { + public WrappedTask runLater(@NotNull Runnable runnable, long delay) { return this.wrapTask(this.scheduler.runTaskLater(plugin, runnable, delay)); } @Override - public void runLater(Consumer consumer, long delay) { + public void runLater(@NotNull Consumer consumer, long delay) { WrappedTask[] taskReference = new WrappedTask[1]; taskReference[0] = this.wrapTask(this.scheduler.runTaskLater(plugin, () -> { @@ -76,22 +77,22 @@ public void runLater(Consumer consumer, long delay) { } @Override - public WrappedTask runLater(Runnable runnable, long delay, TimeUnit unit) { + public WrappedTask runLater(@NotNull Runnable runnable, long delay, TimeUnit unit) { return this.runLater(runnable, TimeConverter.toTicks(delay, unit)); } @Override - public void runLater(Consumer consumer, long delay, TimeUnit unit) { + public void runLater(@NotNull Consumer consumer, long delay, TimeUnit unit) { this.runLater(consumer, TimeConverter.toTicks(delay, unit)); } @Override - public WrappedTask runLaterAsync(Runnable runnable, long delay) { + public WrappedTask runLaterAsync(@NotNull Runnable runnable, long delay) { return this.wrapTask(this.scheduler.runTaskLaterAsynchronously(plugin, runnable, delay)); } @Override - public void runLaterAsync(Consumer consumer, long delay) { + public void runLaterAsync(@NotNull Consumer consumer, long delay) { WrappedTask[] taskReference = new WrappedTask[1]; taskReference[0] = this.wrapTask(this.scheduler.runTaskLaterAsynchronously(plugin, () -> { @@ -100,22 +101,22 @@ public void runLaterAsync(Consumer consumer, long delay) { } @Override - public WrappedTask runLaterAsync(Runnable runnable, long delay, TimeUnit unit) { + public WrappedTask runLaterAsync(@NotNull Runnable runnable, long delay, TimeUnit unit) { return this.runLaterAsync(runnable, TimeConverter.toTicks(delay, unit)); } @Override - public void runLaterAsync(Consumer consumer, long delay, TimeUnit unit) { + public void runLaterAsync(@NotNull Consumer consumer, long delay, TimeUnit unit) { this.runLaterAsync(consumer, TimeConverter.toTicks(delay, unit)); } @Override - public WrappedTask runTimer(Runnable runnable, long delay, long period) { + public WrappedTask runTimer(@NotNull Runnable runnable, long delay, long period) { return this.wrapTask(this.scheduler.runTaskTimer(plugin, runnable, delay, period)); } @Override - public void runTimer(Consumer consumer, long delay, long period) { + public void runTimer(@NotNull Consumer consumer, long delay, long period) { WrappedTask[] taskReference = new WrappedTask[1]; taskReference[0] = this.wrapTask(this.scheduler.runTaskTimer(plugin, () -> { @@ -124,22 +125,22 @@ public void runTimer(Consumer consumer, long delay, long period) { } @Override - public WrappedTask runTimer(Runnable runnable, long delay, long period, TimeUnit unit) { + public WrappedTask runTimer(@NotNull Runnable runnable, long delay, long period, TimeUnit unit) { return this.runTimer(runnable, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } @Override - public void runTimer(Consumer consumer, long delay, long period, TimeUnit unit) { + public void runTimer(@NotNull Consumer consumer, long delay, long period, TimeUnit unit) { this.runTimer(consumer, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } @Override - public WrappedTask runTimerAsync(Runnable runnable, long delay, long period) { + public WrappedTask runTimerAsync(@NotNull Runnable runnable, long delay, long period) { return this.wrapTask(this.scheduler.runTaskTimerAsynchronously(plugin, runnable, delay, period)); } @Override - public void runTimerAsync(Consumer consumer, long delay, long period) { + public void runTimerAsync(@NotNull Consumer consumer, long delay, long period) { WrappedTask[] taskReference = new WrappedTask[1]; taskReference[0] = this.wrapTask(this.scheduler.runTaskTimerAsynchronously(plugin, () -> { @@ -148,27 +149,27 @@ public void runTimerAsync(Consumer consumer, long delay, long perio } @Override - public WrappedTask runTimerAsync(Runnable runnable, long delay, long period, TimeUnit unit) { + public WrappedTask runTimerAsync(@NotNull Runnable runnable, long delay, long period, TimeUnit unit) { return this.runTimerAsync(runnable, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } @Override - public void runTimerAsync(Consumer consumer, long delay, long period, TimeUnit unit) { + public void runTimerAsync(@NotNull Consumer consumer, long delay, long period, TimeUnit unit) { this.runTimerAsync(consumer, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } @Override - public CompletableFuture runAtLocation(Location location, Consumer consumer) { + public CompletableFuture runAtLocation(Location location, @NotNull Consumer consumer) { return this.runNextTick(consumer); } @Override - public WrappedTask runAtLocationLater(Location location, Runnable runnable, long delay) { + public WrappedTask runAtLocationLater(Location location, @NotNull Runnable runnable, long delay) { return this.wrapTask(this.scheduler.runTaskLater(plugin, runnable, delay)); } @Override - public void runAtLocationLater(Location location, Consumer consumer, long delay) { + public void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay) { WrappedTask[] taskReference = new WrappedTask[1]; taskReference[0] = this.wrapTask(this.scheduler.runTaskLater(plugin, () -> { @@ -177,22 +178,22 @@ public void runAtLocationLater(Location location, Consumer consumer } @Override - public WrappedTask runAtLocationLater(Location location, Runnable runnable, long delay, TimeUnit unit) { + public WrappedTask runAtLocationLater(Location location, @NotNull Runnable runnable, long delay, TimeUnit unit) { return this.runAtLocationLater(location, runnable, TimeConverter.toTicks(delay, unit)); } @Override - public void runAtLocationLater(Location location, Consumer consumer, long delay, TimeUnit unit) { + public void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay, TimeUnit unit) { this.runAtLocationLater(location, consumer, TimeConverter.toTicks(delay, unit)); } @Override - public WrappedTask runAtLocationTimer(Location location, Runnable runnable, long delay, long period) { + public WrappedTask runAtLocationTimer(Location location, @NotNull Runnable runnable, long delay, long period) { return this.wrapTask(this.scheduler.runTaskTimer(plugin, runnable, delay, period)); } @Override - public void runAtLocationTimer(Location location, Consumer consumer, long delay, long period) { + public void runAtLocationTimer(Location location, @NotNull Consumer consumer, long delay, long period) { WrappedTask[] taskReference = new WrappedTask[1]; taskReference[0] = this.wrapTask(this.scheduler.runTaskTimer(plugin, () -> { @@ -201,17 +202,17 @@ public void runAtLocationTimer(Location location, Consumer consumer } @Override - public WrappedTask runAtLocationTimer(Location location, Runnable runnable, long delay, long period, TimeUnit unit) { + 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 void runAtLocationTimer(Location location, Consumer consumer, long delay, long period, TimeUnit unit) { + 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 CompletableFuture runAtEntity(Entity entity, Consumer consumer) { + public CompletableFuture runAtEntity(Entity entity, @NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); WrappedTask[] taskReference = new WrappedTask[1]; @@ -224,7 +225,7 @@ public CompletableFuture runAtEntity(Entity entity, Consumer runAtEntityWithFallback(Entity entity, Consumer consumer, Runnable fallback) { + public CompletableFuture runAtEntityWithFallback(Entity entity, @NotNull Consumer consumer, Runnable fallback) { CompletableFuture future = new CompletableFuture<>(); WrappedTask[] taskReference = new WrappedTask[1]; @@ -242,12 +243,29 @@ public CompletableFuture runAtEntityWithFallback(Entity entity } @Override - public WrappedTask runAtEntityLater(Entity entity, Runnable runnable, long delay) { + public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, long delay) { + return this.runAtEntityLater(entity, runnable, null, delay); + } + + @Override + public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, @Nullable Runnable fallback, long delay) { + if (!entity.isValid()) { + if (fallback != null) fallback.run(); + return null; + } return this.wrapTask(this.scheduler.runTaskLater(plugin, runnable, delay)); } @Override - public void runAtEntityLater(Entity entity, Consumer consumer, long delay) { + public void runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay) { + this.runAtEntityLater(entity, consumer, null, delay); + } + + @Override + public void runAtEntityLater(Entity entity, @NotNull Consumer consumer, Runnable fallback, long delay) { + if (!entity.isValid()) { + if (fallback != null) fallback.run(); + } WrappedTask[] taskReference = new WrappedTask[1]; taskReference[0] = this.wrapTask(this.scheduler.runTaskLater(plugin, () -> { @@ -256,22 +274,40 @@ public void runAtEntityLater(Entity entity, Consumer consumer, long } @Override - public WrappedTask runAtEntityLater(Entity entity, Runnable runnable, long delay, TimeUnit unit) { + public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, long delay, TimeUnit unit) { return this.runAtEntityLater(entity, runnable, TimeConverter.toTicks(delay, unit)); } @Override - public void runAtEntityLater(Entity entity, Consumer consumer, long delay, TimeUnit unit) { + public void runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay, TimeUnit unit) { this.runAtEntityLater(entity, consumer, TimeConverter.toTicks(delay, unit)); } @Override - public WrappedTask runAtEntityTimer(Entity entity, Runnable runnable, long delay, long period) { + public WrappedTask runAtEntityTimer(Entity entity, @NotNull Runnable runnable, long delay, long period) { + return this.runAtEntityTimer(entity, runnable, null, delay, period); + } + + @Override + public WrappedTask runAtEntityTimer(Entity entity, @NotNull Runnable runnable, Runnable fallback, long delay, long period) { + if (!entity.isValid()) { + if (fallback != null) fallback.run(); + return null; + } return this.wrapTask(this.scheduler.runTaskTimer(plugin, runnable, delay, period)); } @Override - public void runAtEntityTimer(Entity entity, Consumer consumer, long delay, long period) { + public void runAtEntityTimer(Entity entity, @NotNull Consumer consumer, long delay, long period) { + this.runAtEntityTimer(entity, consumer, null, delay, period); + } + + @Override + public void runAtEntityTimer(Entity entity, @NotNull Consumer consumer, Runnable fallback, long delay, long period) { + if (!entity.isValid()) { + if (fallback != null) fallback.run(); + } + WrappedTask[] taskReference = new WrappedTask[1]; taskReference[0] = this.wrapTask(this.scheduler.runTaskTimer(plugin, () -> { @@ -280,12 +316,12 @@ public void runAtEntityTimer(Entity entity, Consumer consumer, long } @Override - public WrappedTask runAtEntityTimer(Entity entity, Runnable runnable, long delay, long period, TimeUnit unit) { + public WrappedTask runAtEntityTimer(Entity entity, @NotNull Runnable runnable, long delay, long period, TimeUnit unit) { return this.runAtEntityTimer(entity, runnable, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } @Override - public void runAtEntityTimer(Entity entity, Consumer consumer, long delay, long period, TimeUnit unit) { + public void runAtEntityTimer(Entity entity, @NotNull Consumer consumer, long delay, long period, TimeUnit unit) { this.runAtEntityTimer(entity, consumer, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } @@ -359,6 +395,7 @@ public WrappedTask wrapTask(Object nativeTask) { /** * Internal util to get a player regardless of the calling thread + * * @param playerSupplier The supplier to get the player * @return Player or null if not found */ diff --git a/platform/spigot/src/main/java/com/tcoded/folialib/impl/SpigotImplementation.java b/platform/spigot/src/main/java/com/tcoded/folialib/impl/SpigotImplementation.java index 3cf4e7e..9512b4e 100644 --- a/platform/spigot/src/main/java/com/tcoded/folialib/impl/SpigotImplementation.java +++ b/platform/spigot/src/main/java/com/tcoded/folialib/impl/SpigotImplementation.java @@ -12,6 +12,7 @@ import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitTask; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.UUID; @@ -34,7 +35,7 @@ public SpigotImplementation(FoliaLib foliaLib) { } @Override - public CompletableFuture runNextTick(Consumer consumer) { + public CompletableFuture runNextTick(@NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); this.scheduler.runTask(plugin, (task) -> { @@ -46,7 +47,7 @@ public CompletableFuture runNextTick(Consumer consumer) { } @Override - public CompletableFuture runAsync(Consumer consumer) { + public CompletableFuture runAsync(@NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); this.scheduler.runTaskAsynchronously(plugin, (task) -> { @@ -58,132 +59,132 @@ public CompletableFuture runAsync(Consumer consumer) { } @Override - public WrappedTask runLater(Runnable runnable, long delay) { + public WrappedTask runLater(@NotNull Runnable runnable, long delay) { return this.wrapTask(this.scheduler.runTaskLater(plugin, runnable, delay)); } @Override - public void runLater(Consumer consumer, long delay) { + public void runLater(@NotNull Consumer consumer, long delay) { this.scheduler.runTaskLater(plugin, task -> consumer.accept(this.wrapTask(task)), delay); } @Override - public WrappedTask runLater(Runnable runnable, long delay, TimeUnit unit) { + public WrappedTask runLater(@NotNull Runnable runnable, long delay, TimeUnit unit) { return runLater(runnable, TimeConverter.toTicks(delay, unit)); } @Override - public void runLater(Consumer consumer, long delay, TimeUnit unit) { + public void runLater(@NotNull Consumer consumer, long delay, TimeUnit unit) { this.runLater(consumer, TimeConverter.toTicks(delay, unit)); } @Override - public WrappedTask runLaterAsync(Runnable runnable, long delay) { + public WrappedTask runLaterAsync(@NotNull Runnable runnable, long delay) { return this.wrapTask(this.scheduler.runTaskLaterAsynchronously(plugin, runnable, delay)); } @Override - public void runLaterAsync(Consumer consumer, long delay) { + public void runLaterAsync(@NotNull Consumer consumer, long delay) { this.scheduler.runTaskLaterAsynchronously(plugin, task -> consumer.accept(this.wrapTask(task)), delay); } @Override - public WrappedTask runLaterAsync(Runnable runnable, long delay, TimeUnit unit) { + public WrappedTask runLaterAsync(@NotNull Runnable runnable, long delay, TimeUnit unit) { return this.runLaterAsync(runnable, TimeConverter.toTicks(delay, unit)); } @Override - public void runLaterAsync(Consumer consumer, long delay, TimeUnit unit) { + public void runLaterAsync(@NotNull Consumer consumer, long delay, TimeUnit unit) { this.runLaterAsync(consumer, TimeConverter.toTicks(delay, unit)); } @Override - public WrappedTask runTimer(Runnable runnable, long delay, long period) { + public WrappedTask runTimer(@NotNull Runnable runnable, long delay, long period) { return this.wrapTask(this.scheduler.runTaskTimer(plugin, runnable, delay, period)); } @Override - public void runTimer(Consumer consumer, long delay, long period) { + public void runTimer(@NotNull Consumer consumer, long delay, long period) { this.scheduler.runTaskTimer(plugin, task -> consumer.accept(this.wrapTask(task)), delay, period); } @Override - public WrappedTask runTimer(Runnable runnable, long delay, long period, TimeUnit unit) { + public WrappedTask runTimer(@NotNull Runnable runnable, long delay, long period, TimeUnit unit) { return this.runTimer(runnable, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } @Override - public void runTimer(Consumer consumer, long delay, long period, TimeUnit unit) { + public void runTimer(@NotNull Consumer consumer, long delay, long period, TimeUnit unit) { this.runTimer(consumer, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } @Override - public WrappedTask runTimerAsync(Runnable runnable, long delay, long period) { + public WrappedTask runTimerAsync(@NotNull Runnable runnable, long delay, long period) { return this.wrapTask(this.scheduler.runTaskTimerAsynchronously(plugin, runnable, delay, period)); } @Override - public void runTimerAsync(Consumer consumer, long delay, long period) { + public void runTimerAsync(@NotNull Consumer consumer, long delay, long period) { this.scheduler.runTaskTimerAsynchronously(plugin, task -> consumer.accept(this.wrapTask(task)), delay, period); } @Override - public WrappedTask runTimerAsync(Runnable runnable, long delay, long period, TimeUnit unit) { + public WrappedTask runTimerAsync(@NotNull Runnable runnable, long delay, long period, TimeUnit unit) { return this.runTimerAsync(runnable, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } @Override - public void runTimerAsync(Consumer consumer, long delay, long period, TimeUnit unit) { + public void runTimerAsync(@NotNull Consumer consumer, long delay, long period, TimeUnit unit) { this.runTimerAsync(consumer, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } @Override - public CompletableFuture runAtLocation(Location location, Consumer consumer) { + public CompletableFuture runAtLocation(Location location, @NotNull Consumer consumer) { return this.runNextTick(consumer); } @Override - public WrappedTask runAtLocationLater(Location location, Runnable runnable, long delay) { + public WrappedTask runAtLocationLater(Location location, @NotNull Runnable runnable, long delay) { return this.runLater(runnable, delay); } @Override - public void runAtLocationLater(Location location, Consumer consumer, long delay) { + public void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay) { this.runLater(consumer, delay); } @Override - public WrappedTask runAtLocationLater(Location location, Runnable runnable, long delay, TimeUnit unit) { + public WrappedTask runAtLocationLater(Location location, @NotNull Runnable runnable, long delay, TimeUnit unit) { return this.runAtLocationLater(location, runnable, TimeConverter.toTicks(delay, unit)); } @Override - public void runAtLocationLater(Location location, Consumer consumer, long delay, TimeUnit unit) { + public void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay, TimeUnit unit) { this.runAtLocationLater(location, consumer, TimeConverter.toTicks(delay, unit)); } @Override - public WrappedTask runAtLocationTimer(Location location, Runnable runnable, long delay, long period) { + public WrappedTask runAtLocationTimer(Location location, @NotNull Runnable runnable, long delay, long period) { return this.runTimer(runnable, delay, period); } @Override - public void runAtLocationTimer(Location location, Consumer consumer, long delay, long period) { + public void runAtLocationTimer(Location location, @NotNull Consumer consumer, long delay, long period) { this.runTimer(consumer, delay, period); } @Override - public WrappedTask runAtLocationTimer(Location location, Runnable runnable, long delay, long period, TimeUnit unit) { + 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 void runAtLocationTimer(Location location, Consumer consumer, long delay, long period, TimeUnit unit) { + 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 CompletableFuture runAtEntity(Entity entity, Consumer consumer) { + public CompletableFuture runAtEntity(Entity entity, @NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); this.scheduler.runTask(plugin, (task) -> { @@ -195,7 +196,7 @@ public CompletableFuture runAtEntity(Entity entity, Consumer runAtEntityWithFallback(Entity entity, Consumer consumer, Runnable fallback) { + public CompletableFuture runAtEntityWithFallback(Entity entity, @NotNull Consumer consumer, Runnable fallback) { CompletableFuture future = new CompletableFuture<>(); this.scheduler.runTask(plugin, (task) -> { @@ -212,42 +213,77 @@ public CompletableFuture runAtEntityWithFallback(Entity entity } @Override - public WrappedTask runAtEntityLater(Entity entity, Runnable runnable, long delay) { - return this.wrapTask(this.scheduler.runTaskLater(plugin, runnable, delay)); + public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, long delay) { + return this.runAtEntityLater(entity, runnable, null, delay); } @Override - public void runAtEntityLater(Entity entity, Consumer consumer, long delay) { - this.scheduler.runTaskLater(plugin, task -> consumer.accept(this.wrapTask(task)), delay); + public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, Runnable fallback, long delay) { + if (!entity.isValid()) { + if (fallback != null) fallback.run(); + return null; + } + return this.runAtEntityLater(entity, runnable, delay); + } + + @Override + public void runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay) { + this.runAtEntityLater(entity, consumer, null, delay); + } + + @Override + public void runAtEntityLater(Entity entity, @NotNull Consumer consumer, @Nullable Runnable fallback, long delay) { + if (!entity.isValid()) { + if (fallback != null) fallback.run(); + } + else this.runAtEntityLater(entity, consumer, delay); } @Override - public WrappedTask runAtEntityLater(Entity entity, Runnable runnable, long delay, TimeUnit unit) { + public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, long delay, TimeUnit unit) { return this.runAtEntityLater(entity, runnable, TimeConverter.toTicks(delay, unit)); } @Override - public void runAtEntityLater(Entity entity, Consumer consumer, long delay, TimeUnit unit) { + public void runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay, TimeUnit unit) { this.runAtEntityLater(entity, consumer, TimeConverter.toTicks(delay, unit)); } @Override - public WrappedTask runAtEntityTimer(Entity entity, Runnable runnable, long delay, long period) { + public WrappedTask runAtEntityTimer(Entity entity, @NotNull Runnable runnable, long delay, long period) { + return this.runAtEntityTimer(entity, runnable, null, delay, period); + } + + @Override + public WrappedTask runAtEntityTimer(Entity entity, @NotNull Runnable runnable, @Nullable Runnable fallback, long delay, long period) { + if (!entity.isValid()) { + if (fallback != null) fallback.run(); + return null; + } return this.wrapTask(this.scheduler.runTaskTimer(plugin, runnable, delay, period)); } @Override - public void runAtEntityTimer(Entity entity, Consumer consumer, long delay, long period) { + public void runAtEntityTimer(Entity entity, @NotNull Consumer consumer, long delay, long period) { + this.runAtEntityTimer(entity, consumer, null, delay, period); + } + + @Override + public void runAtEntityTimer(Entity entity, @NotNull Consumer consumer, Runnable fallback, long delay, long period) { + if (!entity.isValid()) { + if (fallback != null) fallback.run(); + return; + } this.scheduler.runTaskTimer(plugin, task -> consumer.accept(this.wrapTask(task)), delay, period); } @Override - public WrappedTask runAtEntityTimer(Entity entity, Runnable runnable, long delay, long period, TimeUnit unit) { + public WrappedTask runAtEntityTimer(Entity entity, @NotNull Runnable runnable, long delay, long period, TimeUnit unit) { return this.runAtEntityTimer(entity, runnable, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } @Override - public void runAtEntityTimer(Entity entity, Consumer consumer, long delay, long period, TimeUnit unit) { + public void runAtEntityTimer(Entity entity, @NotNull Consumer consumer, long delay, long period, TimeUnit unit) { this.runAtEntityTimer(entity, consumer, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit)); } diff --git a/platform/unsupported/src/main/java/com/tcoded/folialib/impl/UnsupportedImplementation.java b/platform/unsupported/src/main/java/com/tcoded/folialib/impl/UnsupportedImplementation.java index d07a6ac..904db70 100644 --- a/platform/unsupported/src/main/java/com/tcoded/folialib/impl/UnsupportedImplementation.java +++ b/platform/unsupported/src/main/java/com/tcoded/folialib/impl/UnsupportedImplementation.java @@ -18,7 +18,7 @@ public UnsupportedImplementation(FoliaLib foliaLib) { String.format("\n" + "---------------------------------------------------------------------\n" + "FoliaLib does not support this server software! (%s)\n" + - "FoliaLib will attempt to use the default spigot implementation.\n" + + "FoliaLib will attempt to use the legacy spigot implementation.\n" + "---------------------------------------------------------------------\n", plugin.getServer().getVersion() )