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 216c1c2..355dd31 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 @@ -28,18 +28,18 @@ public interface ServerImplementation { * Paper: Synced with the server main thread * Spigot: Synced with the server main thread * @param consumer Task to run - * @return Future when the task is completed + * @return Future when the task is completed, run on the same thread as the task */ - CompletableFuture runNextTick(@NotNull Consumer consumer); + @NotNull CompletableFuture runNextTick(@NotNull Consumer consumer); /** * Folia: Async * Paper: Async * Spigot: Async * @param consumer Task to run - * @return Future when the task is completed + * @return Future when the task is completed, run on the same thread as the task */ - CompletableFuture runAsync(@NotNull Consumer consumer); + @NotNull CompletableFuture runAsync(@NotNull Consumer consumer); // ----- Run Later ----- @@ -59,8 +59,9 @@ public interface ServerImplementation { * Spigot: Synced with the server main thread * @param consumer Task to run * @param delay Delay before execution in ticks + * @return Future when the task is completed, run on the same thread as the task */ - void runLater(@NotNull Consumer consumer, long delay); + @NotNull CompletableFuture runLater(@NotNull Consumer consumer, long delay); /** * Folia: Synced with the server daylight cycle tick @@ -80,8 +81,9 @@ public interface ServerImplementation { * @param consumer Task to run * @param delay Delay before execution * @param unit Time unit + * @return Future when the task is completed, run on the same thread as the task */ - void runLater(@NotNull Consumer consumer, long delay, TimeUnit unit); + @NotNull CompletableFuture runLater(@NotNull Consumer consumer, long delay, TimeUnit unit); /** * Folia: Async @@ -99,8 +101,9 @@ public interface ServerImplementation { * Spigot: Async * @param consumer Task to run * @param delay Delay before execution in ticks + * @return Future when the task is completed, run on the same thread as the task */ - void runLaterAsync(@NotNull Consumer consumer, long delay); + @NotNull CompletableFuture runLaterAsync(@NotNull Consumer consumer, long delay); /** * Folia: Async @@ -120,8 +123,9 @@ public interface ServerImplementation { * @param consumer Task to run * @param delay Delay before execution * @param unit Time unit + * @return Future when the task is completed, run on the same thread as the task */ - void runLaterAsync(@NotNull Consumer consumer, long delay, TimeUnit unit); + @NotNull CompletableFuture runLaterAsync(@NotNull Consumer consumer, long delay, TimeUnit unit); // ----- Global Timers ----- @@ -222,9 +226,9 @@ public interface ServerImplementation { * Spigot: Synced with the server main thread * @param location Location to run the task at * @param consumer Task to run - * @return Future when the task is completed + * @return Future when the task is completed, run on the same thread as the task */ - CompletableFuture runAtLocation(Location location, @NotNull Consumer consumer); + @NotNull CompletableFuture runAtLocation(Location location, @NotNull Consumer consumer); /** * Folia: Synced with the tick of the region of the chunk of the location @@ -244,8 +248,9 @@ public interface ServerImplementation { * @param location Location to run the task at * @param consumer Task to run * @param delay Delay before execution in ticks + * @return Future when the task is completed, run on the same thread as the task */ - void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay); + @NotNull CompletableFuture runAtLocationLater(Location location, @NotNull Consumer consumer, long delay); /** * Folia: Synced with the tick of the region of the chunk of the location @@ -267,8 +272,9 @@ public interface ServerImplementation { * @param consumer Task to run * @param delay Delay before execution * @param unit Time unit + * @return Future when the task is completed, run on the same thread as the task */ - void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay, TimeUnit unit); + @NotNull CompletableFuture runAtLocationLater(Location location, @NotNull Consumer consumer, long delay, TimeUnit unit); /** * Folia: Synced with the tick of the region of the chunk of the location @@ -327,9 +333,9 @@ public interface ServerImplementation { * Spigot: Synced with the server main thread * @param entity Entity to run the task at * @param consumer Task to run - * @return Future when the task is completed + * @return Future when the task is completed, run on the same thread as the task */ - CompletableFuture runAtEntity(Entity entity, @NotNull Consumer consumer); + @NotNull CompletableFuture runAtEntity(Entity entity, @NotNull Consumer consumer); /** * Folia: Synced with the tick of the region of the entity (even if the entity moves) @@ -337,9 +343,9 @@ public interface ServerImplementation { * Spigot: Synced with the server main thread * @param entity Entity to run the task at * @param consumer Task to run - * @return Future when the task is completed + * @return Future when the task is completed, run on the same thread as the task */ - CompletableFuture runAtEntityWithFallback(Entity entity, @NotNull Consumer consumer, @Nullable Runnable fallback); + @NotNull 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) @@ -371,8 +377,9 @@ public interface ServerImplementation { * @param entity Entity to run the task at * @param consumer Task to run * @param delay Delay before execution in ticks + * @return Future when the task is completed, run on the same thread as the task */ - void runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay); + @NotNull CompletableFuture runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay); /** * Folia: Synced with the tick of the region of the entity (even if the entity moves) @@ -382,8 +389,9 @@ public interface ServerImplementation { * @param consumer Task to run * @param fallback Fallback task to run when the entity is removed * @param delay Delay before execution in ticks + * @return Future when the task is completed, run on the same thread as the task */ - void runAtEntityLater(Entity entity, @NotNull Consumer consumer, Runnable fallback, long delay); + @NotNull CompletableFuture 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) @@ -405,8 +413,9 @@ public interface ServerImplementation { * @param consumer Task to run * @param delay Delay before execution * @param unit Time unit + * @return Future when the task is completed, run on the same thread as the task */ - void runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay, TimeUnit unit); + @NotNull CompletableFuture 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) 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 7c02493..a74a077 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(@NotNull Consumer consumer) { + public @NotNull CompletableFuture runNextTick(@NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); this.globalRegionScheduler.run(plugin, task -> { @@ -53,7 +53,7 @@ public CompletableFuture runNextTick(@NotNull Consumer consum } @Override - public CompletableFuture runAsync(@NotNull Consumer consumer) { + public @NotNull CompletableFuture runAsync(@NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); this.asyncScheduler.runNow(plugin, task -> { @@ -74,12 +74,19 @@ public WrappedTask runLater(@NotNull Runnable runnable, long delay) { } @Override - public void runLater(@NotNull Consumer consumer, long delay) { + public @NotNull CompletableFuture runLater(@NotNull Consumer consumer, long delay) { + CompletableFuture future = new CompletableFuture<>(); + if (delay <= 0) { InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), delay); delay = 1; } - this.globalRegionScheduler.runDelayed(plugin, task -> consumer.accept(this.wrapTask(task)), delay); + this.globalRegionScheduler.runDelayed(plugin, task -> { + consumer.accept(this.wrapTask(task)); + future.complete(null); + }, delay); + + return future; } @Override @@ -88,8 +95,8 @@ public WrappedTask runLater(@NotNull Runnable runnable, long delay, TimeUnit uni } @Override - public void runLater(@NotNull Consumer consumer, long delay, TimeUnit unit) { - this.runLater(consumer, TimeConverter.toTicks(delay, unit)); + public @NotNull CompletableFuture runLater(@NotNull Consumer consumer, long delay, TimeUnit unit) { + return this.runLater(consumer, TimeConverter.toTicks(delay, unit)); } @Override @@ -98,8 +105,8 @@ public WrappedTask runLaterAsync(@NotNull Runnable runnable, long delay) { } @Override - public void runLaterAsync(@NotNull Consumer consumer, long delay) { - this.runLaterAsync(consumer, TimeConverter.toMillis(delay), TimeUnit.MILLISECONDS); + public @NotNull CompletableFuture runLaterAsync(@NotNull Consumer consumer, long delay) { + return this.runLaterAsync(consumer, TimeConverter.toMillis(delay), TimeUnit.MILLISECONDS); } @Override @@ -110,8 +117,15 @@ public WrappedTask runLaterAsync(@NotNull Runnable runnable, long delay, TimeUni } @Override - public void runLaterAsync(@NotNull Consumer consumer, long delay, TimeUnit unit) { - this.asyncScheduler.runDelayed(plugin, task -> consumer.accept(this.wrapTask(task)), delay, unit); + public @NotNull CompletableFuture runLaterAsync(@NotNull Consumer consumer, long delay, TimeUnit unit) { + CompletableFuture future = new CompletableFuture<>(); + + this.asyncScheduler.runDelayed(plugin, task -> { + consumer.accept(this.wrapTask(task)); + future.complete(null); + }, delay, unit); + + return future; } @Override @@ -179,7 +193,7 @@ public void runTimerAsync(@NotNull Consumer consumer, long delay, l } @Override - public CompletableFuture runAtLocation(Location location, @NotNull Consumer consumer) { + public @NotNull CompletableFuture runAtLocation(Location location, @NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); this.plugin.getServer().getRegionScheduler().run(plugin, location, task -> { @@ -202,12 +216,19 @@ public WrappedTask runAtLocationLater(Location location, @NotNull Runnable runna } @Override - public void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay) { + public @NotNull CompletableFuture runAtLocationLater(Location location, @NotNull Consumer consumer, long delay) { + CompletableFuture future = new CompletableFuture<>(); + if (delay <= 0) { InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), delay); delay = 1; } - this.plugin.getServer().getRegionScheduler().runDelayed(plugin, location, task -> consumer.accept(this.wrapTask(task)), delay); + this.plugin.getServer().getRegionScheduler().runDelayed(plugin, location, task -> { + consumer.accept(this.wrapTask(task)); + future.complete(null); + }, delay); + + return future; } @Override @@ -216,8 +237,8 @@ public WrappedTask runAtLocationLater(Location location, @NotNull Runnable runna } @Override - public void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay, TimeUnit unit) { - this.runAtLocationLater(location, consumer, TimeConverter.toTicks(delay, unit)); + public @NotNull CompletableFuture runAtLocationLater(Location location, @NotNull Consumer consumer, long delay, TimeUnit unit) { + return this.runAtLocationLater(location, consumer, TimeConverter.toTicks(delay, unit)); } @Override @@ -259,7 +280,7 @@ public void runAtLocationTimer(Location location, @NotNull Consumer } @Override - public CompletableFuture runAtEntity(Entity entity, @NotNull Consumer consumer) { + public @NotNull CompletableFuture runAtEntity(Entity entity, @NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); ScheduledTask scheduledTask = entity.getScheduler().run(this.plugin, task -> { @@ -275,7 +296,7 @@ public CompletableFuture runAtEntity(Entity entity, @NotNull C } @Override - public CompletableFuture runAtEntityWithFallback(Entity entity, @NotNull Consumer consumer, Runnable fallback) { + public @NotNull CompletableFuture runAtEntityWithFallback(Entity entity, @NotNull Consumer consumer, Runnable fallback) { CompletableFuture future = new CompletableFuture<>(); ScheduledTask scheduledTask = entity.getScheduler().run(this.plugin, task -> { @@ -308,17 +329,33 @@ public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, R } @Override - public void runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay) { - this.runAtEntityLater(entity, consumer, null, delay); + public @NotNull CompletableFuture runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay) { + return this.runAtEntityLater(entity, consumer, null, delay); } @Override - public void runAtEntityLater(Entity entity, @NotNull Consumer consumer, Runnable fallback, long delay) { + public @NotNull CompletableFuture runAtEntityLater(Entity entity, @NotNull Consumer consumer, Runnable fallback, long delay) { + CompletableFuture future = new CompletableFuture<>(); + + // Wrap the fallback so we can complete the future + if (fallback != null) { + final Runnable finalFallback = fallback; + fallback = () -> { + finalFallback.run(); + future.complete(null); + }; + } + if (delay <= 0) { InvalidTickDelayNotifier.notifyOnce(plugin.getLogger(), delay); delay = 1; } - entity.getScheduler().runDelayed(plugin, task -> consumer.accept(this.wrapTask(task)), fallback, delay); + entity.getScheduler().runDelayed(plugin, task -> { + consumer.accept(this.wrapTask(task)); + future.complete(null); + }, fallback, delay); + + return future; } @Override @@ -327,8 +364,8 @@ public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, l } @Override - public void runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay, TimeUnit unit) { - this.runAtEntityLater(entity, consumer, TimeConverter.toTicks(delay, unit)); + public @NotNull CompletableFuture runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay, TimeUnit unit) { + return this.runAtEntityLater(entity, consumer, TimeConverter.toTicks(delay, unit)); } @Override 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 12fa4aa..599c775 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 @@ -37,7 +37,7 @@ public LegacySpigotImplementation(FoliaLib foliaLib) { } @Override - public CompletableFuture runNextTick(@NotNull Consumer consumer) { + public @NotNull CompletableFuture runNextTick(@NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); WrappedTask[] taskReference = new WrappedTask[1]; @@ -50,7 +50,7 @@ public CompletableFuture runNextTick(@NotNull Consumer consum } @Override - public CompletableFuture runAsync(@NotNull Consumer consumer) { + public @NotNull CompletableFuture runAsync(@NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); WrappedTask[] taskReference = new WrappedTask[1]; @@ -68,12 +68,16 @@ public WrappedTask runLater(@NotNull Runnable runnable, long delay) { } @Override - public void runLater(@NotNull Consumer consumer, long delay) { + public @NotNull CompletableFuture runLater(@NotNull Consumer consumer, long delay) { + CompletableFuture future = new CompletableFuture<>(); WrappedTask[] taskReference = new WrappedTask[1]; taskReference[0] = this.wrapTask(this.scheduler.runTaskLater(plugin, () -> { consumer.accept(taskReference[0]); + future.complete(null); }, delay)); + + return future; } @Override @@ -82,8 +86,8 @@ public WrappedTask runLater(@NotNull Runnable runnable, long delay, TimeUnit uni } @Override - public void runLater(@NotNull Consumer consumer, long delay, TimeUnit unit) { - this.runLater(consumer, TimeConverter.toTicks(delay, unit)); + public @NotNull CompletableFuture runLater(@NotNull Consumer consumer, long delay, TimeUnit unit) { + return this.runLater(consumer, TimeConverter.toTicks(delay, unit)); } @Override @@ -92,12 +96,16 @@ public WrappedTask runLaterAsync(@NotNull Runnable runnable, long delay) { } @Override - public void runLaterAsync(@NotNull Consumer consumer, long delay) { + public @NotNull CompletableFuture runLaterAsync(@NotNull Consumer consumer, long delay) { + CompletableFuture future = new CompletableFuture<>(); WrappedTask[] taskReference = new WrappedTask[1]; taskReference[0] = this.wrapTask(this.scheduler.runTaskLaterAsynchronously(plugin, () -> { consumer.accept(taskReference[0]); + future.complete(null); }, delay)); + + return future; } @Override @@ -106,8 +114,8 @@ public WrappedTask runLaterAsync(@NotNull Runnable runnable, long delay, TimeUni } @Override - public void runLaterAsync(@NotNull Consumer consumer, long delay, TimeUnit unit) { - this.runLaterAsync(consumer, TimeConverter.toTicks(delay, unit)); + public @NotNull CompletableFuture runLaterAsync(@NotNull Consumer consumer, long delay, TimeUnit unit) { + return this.runLaterAsync(consumer, TimeConverter.toTicks(delay, unit)); } @Override @@ -159,7 +167,7 @@ public void runTimerAsync(@NotNull Consumer consumer, long delay, l } @Override - public CompletableFuture runAtLocation(Location location, @NotNull Consumer consumer) { + public @NotNull CompletableFuture runAtLocation(Location location, @NotNull Consumer consumer) { return this.runNextTick(consumer); } @@ -169,12 +177,16 @@ public WrappedTask runAtLocationLater(Location location, @NotNull Runnable runna } @Override - public void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay) { + public @NotNull CompletableFuture runAtLocationLater(Location location, @NotNull Consumer consumer, long delay) { + CompletableFuture future = new CompletableFuture<>(); WrappedTask[] taskReference = new WrappedTask[1]; taskReference[0] = this.wrapTask(this.scheduler.runTaskLater(plugin, () -> { consumer.accept(taskReference[0]); + future.complete(null); }, delay)); + + return future; } @Override @@ -183,8 +195,8 @@ public WrappedTask runAtLocationLater(Location location, @NotNull Runnable runna } @Override - public void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay, TimeUnit unit) { - this.runAtLocationLater(location, consumer, TimeConverter.toTicks(delay, unit)); + public @NotNull CompletableFuture runAtLocationLater(Location location, @NotNull Consumer consumer, long delay, TimeUnit unit) { + return this.runAtLocationLater(location, consumer, TimeConverter.toTicks(delay, unit)); } @Override @@ -212,7 +224,7 @@ public void runAtLocationTimer(Location location, @NotNull Consumer } @Override - public CompletableFuture runAtEntity(Entity entity, @NotNull Consumer consumer) { + public @NotNull CompletableFuture runAtEntity(Entity entity, @NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); WrappedTask[] taskReference = new WrappedTask[1]; @@ -225,7 +237,7 @@ public CompletableFuture runAtEntity(Entity entity, @NotNull C } @Override - public CompletableFuture runAtEntityWithFallback(Entity entity, @NotNull Consumer consumer, Runnable fallback) { + public @NotNull CompletableFuture runAtEntityWithFallback(Entity entity, @NotNull Consumer consumer, Runnable fallback) { CompletableFuture future = new CompletableFuture<>(); WrappedTask[] taskReference = new WrappedTask[1]; @@ -257,20 +269,28 @@ public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, @ } @Override - public void runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay) { - this.runAtEntityLater(entity, consumer, null, delay); + public @NotNull CompletableFuture runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay) { + return this.runAtEntityLater(entity, consumer, null, delay); } @Override - public void runAtEntityLater(Entity entity, @NotNull Consumer consumer, Runnable fallback, long delay) { + public @NotNull CompletableFuture runAtEntityLater(Entity entity, @NotNull Consumer consumer, Runnable fallback, long delay) { + CompletableFuture future = new CompletableFuture<>(); + if (!entity.isValid()) { - if (fallback != null) fallback.run(); + if (fallback != null) { + fallback.run(); + future.complete(null); + } } WrappedTask[] taskReference = new WrappedTask[1]; taskReference[0] = this.wrapTask(this.scheduler.runTaskLater(plugin, () -> { consumer.accept(taskReference[0]); + future.complete(null); }, delay)); + + return future; } @Override @@ -279,8 +299,8 @@ public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, l } @Override - public void runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay, TimeUnit unit) { - this.runAtEntityLater(entity, consumer, TimeConverter.toTicks(delay, unit)); + public @NotNull CompletableFuture runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay, TimeUnit unit) { + return this.runAtEntityLater(entity, consumer, TimeConverter.toTicks(delay, unit)); } @Override 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 ae2a9e5..a76f929 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 @@ -35,7 +35,7 @@ public SpigotImplementation(FoliaLib foliaLib) { } @Override - public CompletableFuture runNextTick(@NotNull Consumer consumer) { + public @NotNull CompletableFuture runNextTick(@NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); this.scheduler.runTask(plugin, (task) -> { @@ -47,7 +47,7 @@ public CompletableFuture runNextTick(@NotNull Consumer consum } @Override - public CompletableFuture runAsync(@NotNull Consumer consumer) { + public @NotNull CompletableFuture runAsync(@NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); this.scheduler.runTaskAsynchronously(plugin, (task) -> { @@ -64,8 +64,15 @@ public WrappedTask runLater(@NotNull Runnable runnable, long delay) { } @Override - public void runLater(@NotNull Consumer consumer, long delay) { - this.scheduler.runTaskLater(plugin, task -> consumer.accept(this.wrapTask(task)), delay); + public @NotNull CompletableFuture runLater(@NotNull Consumer consumer, long delay) { + CompletableFuture future = new CompletableFuture<>(); + + this.scheduler.runTaskLater(plugin, task -> { + consumer.accept(this.wrapTask(task)); + future.complete(null); + }, delay); + + return future; } @Override @@ -74,8 +81,8 @@ public WrappedTask runLater(@NotNull Runnable runnable, long delay, TimeUnit uni } @Override - public void runLater(@NotNull Consumer consumer, long delay, TimeUnit unit) { - this.runLater(consumer, TimeConverter.toTicks(delay, unit)); + public @NotNull CompletableFuture runLater(@NotNull Consumer consumer, long delay, TimeUnit unit) { + return this.runLater(consumer, TimeConverter.toTicks(delay, unit)); } @Override @@ -84,8 +91,15 @@ public WrappedTask runLaterAsync(@NotNull Runnable runnable, long delay) { } @Override - public void runLaterAsync(@NotNull Consumer consumer, long delay) { - this.scheduler.runTaskLaterAsynchronously(plugin, task -> consumer.accept(this.wrapTask(task)), delay); + public @NotNull CompletableFuture runLaterAsync(@NotNull Consumer consumer, long delay) { + CompletableFuture future = new CompletableFuture<>(); + + this.scheduler.runTaskLaterAsynchronously(plugin, task -> { + consumer.accept(this.wrapTask(task)); + future.complete(null); + }, delay); + + return future; } @Override @@ -94,8 +108,8 @@ public WrappedTask runLaterAsync(@NotNull Runnable runnable, long delay, TimeUni } @Override - public void runLaterAsync(@NotNull Consumer consumer, long delay, TimeUnit unit) { - this.runLaterAsync(consumer, TimeConverter.toTicks(delay, unit)); + public @NotNull CompletableFuture runLaterAsync(@NotNull Consumer consumer, long delay, TimeUnit unit) { + return this.runLaterAsync(consumer, TimeConverter.toTicks(delay, unit)); } @Override @@ -139,7 +153,7 @@ public void runTimerAsync(@NotNull Consumer consumer, long delay, l } @Override - public CompletableFuture runAtLocation(Location location, @NotNull Consumer consumer) { + public @NotNull CompletableFuture runAtLocation(Location location, @NotNull Consumer consumer) { return this.runNextTick(consumer); } @@ -149,8 +163,8 @@ public WrappedTask runAtLocationLater(Location location, @NotNull Runnable runna } @Override - public void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay) { - this.runLater(consumer, delay); + public @NotNull CompletableFuture runAtLocationLater(Location location, @NotNull Consumer consumer, long delay) { + return this.runLater(consumer, delay); } @Override @@ -159,8 +173,8 @@ public WrappedTask runAtLocationLater(Location location, @NotNull Runnable runna } @Override - public void runAtLocationLater(Location location, @NotNull Consumer consumer, long delay, TimeUnit unit) { - this.runAtLocationLater(location, consumer, TimeConverter.toTicks(delay, unit)); + public @NotNull CompletableFuture runAtLocationLater(Location location, @NotNull Consumer consumer, long delay, TimeUnit unit) { + return this.runAtLocationLater(location, consumer, TimeConverter.toTicks(delay, unit)); } @Override @@ -184,7 +198,7 @@ public void runAtLocationTimer(Location location, @NotNull Consumer } @Override - public CompletableFuture runAtEntity(Entity entity, @NotNull Consumer consumer) { + public @NotNull CompletableFuture runAtEntity(Entity entity, @NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); this.scheduler.runTask(plugin, (task) -> { @@ -196,7 +210,7 @@ public CompletableFuture runAtEntity(Entity entity, @NotNull C } @Override - public CompletableFuture runAtEntityWithFallback(Entity entity, @NotNull Consumer consumer, Runnable fallback) { + public @NotNull CompletableFuture runAtEntityWithFallback(Entity entity, @NotNull Consumer consumer, Runnable fallback) { CompletableFuture future = new CompletableFuture<>(); this.scheduler.runTask(plugin, (task) -> { @@ -227,16 +241,28 @@ public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, R } @Override - public void runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay) { - this.runAtEntityLater(entity, consumer, null, delay); + public @NotNull CompletableFuture runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay) { + return this.runAtEntityLater(entity, consumer, null, delay); } @Override - public void runAtEntityLater(Entity entity, @NotNull Consumer consumer, @Nullable Runnable fallback, long delay) { + public @NotNull CompletableFuture runAtEntityLater(Entity entity, @NotNull Consumer consumer, @Nullable Runnable fallback, long delay) { + CompletableFuture future = new CompletableFuture<>(); + if (!entity.isValid()) { - if (fallback != null) fallback.run(); + if (fallback != null) { + fallback.run(); + future.complete(null); + } } - else this.runAtEntityLater(entity, consumer, delay); + else { + this.scheduler.runTaskLater(plugin, task -> { + consumer.accept(this.wrapTask(task)); + future.complete(null); + }, delay); + } + + return future; } @Override @@ -245,8 +271,8 @@ public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, l } @Override - public void runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay, TimeUnit unit) { - this.runAtEntityLater(entity, consumer, TimeConverter.toTicks(delay, unit)); + public @NotNull CompletableFuture runAtEntityLater(Entity entity, @NotNull Consumer consumer, long delay, TimeUnit unit) { + return this.runAtEntityLater(entity, consumer, TimeConverter.toTicks(delay, unit)); } @Override