Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void> runNextTick(@NotNull Consumer<WrappedTask> consumer);
@NotNull CompletableFuture<Void> runNextTick(@NotNull Consumer<WrappedTask> 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<Void> runAsync(@NotNull Consumer<WrappedTask> consumer);
@NotNull CompletableFuture<Void> runAsync(@NotNull Consumer<WrappedTask> consumer);

// ----- Run Later -----

Expand All @@ -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<WrappedTask> consumer, long delay);
@NotNull CompletableFuture<Void> runLater(@NotNull Consumer<WrappedTask> consumer, long delay);

/**
* Folia: Synced with the server daylight cycle tick
Expand All @@ -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<WrappedTask> consumer, long delay, TimeUnit unit);
@NotNull CompletableFuture<Void> runLater(@NotNull Consumer<WrappedTask> consumer, long delay, TimeUnit unit);

/**
* Folia: Async
Expand All @@ -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<WrappedTask> consumer, long delay);
@NotNull CompletableFuture<Void> runLaterAsync(@NotNull Consumer<WrappedTask> consumer, long delay);

/**
* Folia: Async
Expand All @@ -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<WrappedTask> consumer, long delay, TimeUnit unit);
@NotNull CompletableFuture<Void> runLaterAsync(@NotNull Consumer<WrappedTask> consumer, long delay, TimeUnit unit);

// ----- Global Timers -----

Expand Down Expand Up @@ -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<Void> runAtLocation(Location location, @NotNull Consumer<WrappedTask> consumer);
@NotNull CompletableFuture<Void> runAtLocation(Location location, @NotNull Consumer<WrappedTask> consumer);

/**
* Folia: Synced with the tick of the region of the chunk of the location
Expand All @@ -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<WrappedTask> consumer, long delay);
@NotNull CompletableFuture<Void> runAtLocationLater(Location location, @NotNull Consumer<WrappedTask> consumer, long delay);

/**
* Folia: Synced with the tick of the region of the chunk of the location
Expand All @@ -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<WrappedTask> consumer, long delay, TimeUnit unit);
@NotNull CompletableFuture<Void> runAtLocationLater(Location location, @NotNull Consumer<WrappedTask> consumer, long delay, TimeUnit unit);

/**
* Folia: Synced with the tick of the region of the chunk of the location
Expand Down Expand Up @@ -327,19 +333,19 @@ 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<EntityTaskResult> runAtEntity(Entity entity, @NotNull Consumer<WrappedTask> consumer);
@NotNull CompletableFuture<EntityTaskResult> runAtEntity(Entity entity, @NotNull Consumer<WrappedTask> consumer);

/**
* 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
* @return Future when the task is completed
* @return Future when the task is completed, run on the same thread as the task
*/
CompletableFuture<EntityTaskResult> runAtEntityWithFallback(Entity entity, @NotNull Consumer<WrappedTask> consumer, @Nullable Runnable fallback);
@NotNull CompletableFuture<EntityTaskResult> runAtEntityWithFallback(Entity entity, @NotNull Consumer<WrappedTask> consumer, @Nullable Runnable fallback);

/**
* Folia: Synced with the tick of the region of the entity (even if the entity moves)
Expand Down Expand Up @@ -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<WrappedTask> consumer, long delay);
@NotNull CompletableFuture<Void> runAtEntityLater(Entity entity, @NotNull Consumer<WrappedTask> consumer, long delay);

/**
* Folia: Synced with the tick of the region of the entity (even if the entity moves)
Expand All @@ -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<WrappedTask> consumer, Runnable fallback, long delay);
@NotNull CompletableFuture<Void> runAtEntityLater(Entity entity, @NotNull Consumer<WrappedTask> consumer, Runnable fallback, long delay);

/**
* Folia: Synced with the tick of the region of the entity (even if the entity moves)
Expand All @@ -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<WrappedTask> consumer, long delay, TimeUnit unit);
@NotNull CompletableFuture<Void> runAtEntityLater(Entity entity, @NotNull Consumer<WrappedTask> consumer, long delay, TimeUnit unit);

/**
* Folia: Synced with the tick of the region of the entity (even if the entity moves)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public FoliaImplementation(FoliaLib foliaLib) {
}

@Override
public CompletableFuture<Void> runNextTick(@NotNull Consumer<WrappedTask> consumer) {
public @NotNull CompletableFuture<Void> runNextTick(@NotNull Consumer<WrappedTask> consumer) {
CompletableFuture<Void> future = new CompletableFuture<>();

this.globalRegionScheduler.run(plugin, task -> {
Expand All @@ -53,7 +53,7 @@ public CompletableFuture<Void> runNextTick(@NotNull Consumer<WrappedTask> consum
}

@Override
public CompletableFuture<Void> runAsync(@NotNull Consumer<WrappedTask> consumer) {
public @NotNull CompletableFuture<Void> runAsync(@NotNull Consumer<WrappedTask> consumer) {
CompletableFuture<Void> future = new CompletableFuture<>();

this.asyncScheduler.runNow(plugin, task -> {
Expand All @@ -74,12 +74,19 @@ public WrappedTask runLater(@NotNull Runnable runnable, long delay) {
}

@Override
public void runLater(@NotNull Consumer<WrappedTask> consumer, long delay) {
public @NotNull CompletableFuture<Void> runLater(@NotNull Consumer<WrappedTask> consumer, long delay) {
CompletableFuture<Void> 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
Expand All @@ -88,8 +95,8 @@ public WrappedTask runLater(@NotNull Runnable runnable, long delay, TimeUnit uni
}

@Override
public void runLater(@NotNull Consumer<WrappedTask> consumer, long delay, TimeUnit unit) {
this.runLater(consumer, TimeConverter.toTicks(delay, unit));
public @NotNull CompletableFuture<Void> runLater(@NotNull Consumer<WrappedTask> consumer, long delay, TimeUnit unit) {
return this.runLater(consumer, TimeConverter.toTicks(delay, unit));
}

@Override
Expand All @@ -98,8 +105,8 @@ public WrappedTask runLaterAsync(@NotNull Runnable runnable, long delay) {
}

@Override
public void runLaterAsync(@NotNull Consumer<WrappedTask> consumer, long delay) {
this.runLaterAsync(consumer, TimeConverter.toMillis(delay), TimeUnit.MILLISECONDS);
public @NotNull CompletableFuture<Void> runLaterAsync(@NotNull Consumer<WrappedTask> consumer, long delay) {
return this.runLaterAsync(consumer, TimeConverter.toMillis(delay), TimeUnit.MILLISECONDS);
}

@Override
Expand All @@ -110,8 +117,15 @@ public WrappedTask runLaterAsync(@NotNull Runnable runnable, long delay, TimeUni
}

@Override
public void runLaterAsync(@NotNull Consumer<WrappedTask> consumer, long delay, TimeUnit unit) {
this.asyncScheduler.runDelayed(plugin, task -> consumer.accept(this.wrapTask(task)), delay, unit);
public @NotNull CompletableFuture<Void> runLaterAsync(@NotNull Consumer<WrappedTask> consumer, long delay, TimeUnit unit) {
CompletableFuture<Void> future = new CompletableFuture<>();

this.asyncScheduler.runDelayed(plugin, task -> {
consumer.accept(this.wrapTask(task));
future.complete(null);
}, delay, unit);

return future;
}

@Override
Expand Down Expand Up @@ -179,7 +193,7 @@ public void runTimerAsync(@NotNull Consumer<WrappedTask> consumer, long delay, l
}

@Override
public CompletableFuture<Void> runAtLocation(Location location, @NotNull Consumer<WrappedTask> consumer) {
public @NotNull CompletableFuture<Void> runAtLocation(Location location, @NotNull Consumer<WrappedTask> consumer) {
CompletableFuture<Void> future = new CompletableFuture<>();

this.plugin.getServer().getRegionScheduler().run(plugin, location, task -> {
Expand All @@ -202,12 +216,19 @@ public WrappedTask runAtLocationLater(Location location, @NotNull Runnable runna
}

@Override
public void runAtLocationLater(Location location, @NotNull Consumer<WrappedTask> consumer, long delay) {
public @NotNull CompletableFuture<Void> runAtLocationLater(Location location, @NotNull Consumer<WrappedTask> consumer, long delay) {
CompletableFuture<Void> 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
Expand All @@ -216,8 +237,8 @@ public WrappedTask runAtLocationLater(Location location, @NotNull Runnable runna
}

@Override
public void runAtLocationLater(Location location, @NotNull Consumer<WrappedTask> consumer, long delay, TimeUnit unit) {
this.runAtLocationLater(location, consumer, TimeConverter.toTicks(delay, unit));
public @NotNull CompletableFuture<Void> runAtLocationLater(Location location, @NotNull Consumer<WrappedTask> consumer, long delay, TimeUnit unit) {
return this.runAtLocationLater(location, consumer, TimeConverter.toTicks(delay, unit));
}

@Override
Expand Down Expand Up @@ -259,7 +280,7 @@ public void runAtLocationTimer(Location location, @NotNull Consumer<WrappedTask>
}

@Override
public CompletableFuture<EntityTaskResult> runAtEntity(Entity entity, @NotNull Consumer<WrappedTask> consumer) {
public @NotNull CompletableFuture<EntityTaskResult> runAtEntity(Entity entity, @NotNull Consumer<WrappedTask> consumer) {
CompletableFuture<EntityTaskResult> future = new CompletableFuture<>();

ScheduledTask scheduledTask = entity.getScheduler().run(this.plugin, task -> {
Expand All @@ -275,7 +296,7 @@ public CompletableFuture<EntityTaskResult> runAtEntity(Entity entity, @NotNull C
}

@Override
public CompletableFuture<EntityTaskResult> runAtEntityWithFallback(Entity entity, @NotNull Consumer<WrappedTask> consumer, Runnable fallback) {
public @NotNull CompletableFuture<EntityTaskResult> runAtEntityWithFallback(Entity entity, @NotNull Consumer<WrappedTask> consumer, Runnable fallback) {
CompletableFuture<EntityTaskResult> future = new CompletableFuture<>();

ScheduledTask scheduledTask = entity.getScheduler().run(this.plugin, task -> {
Expand Down Expand Up @@ -308,17 +329,33 @@ public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, R
}

@Override
public void runAtEntityLater(Entity entity, @NotNull Consumer<WrappedTask> consumer, long delay) {
this.runAtEntityLater(entity, consumer, null, delay);
public @NotNull CompletableFuture<Void> runAtEntityLater(Entity entity, @NotNull Consumer<WrappedTask> consumer, long delay) {
return this.runAtEntityLater(entity, consumer, null, delay);
}

@Override
public void runAtEntityLater(Entity entity, @NotNull Consumer<WrappedTask> consumer, Runnable fallback, long delay) {
public @NotNull CompletableFuture<Void> runAtEntityLater(Entity entity, @NotNull Consumer<WrappedTask> consumer, Runnable fallback, long delay) {
CompletableFuture<Void> 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
Expand All @@ -327,8 +364,8 @@ public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, l
}

@Override
public void runAtEntityLater(Entity entity, @NotNull Consumer<WrappedTask> consumer, long delay, TimeUnit unit) {
this.runAtEntityLater(entity, consumer, TimeConverter.toTicks(delay, unit));
public @NotNull CompletableFuture<Void> runAtEntityLater(Entity entity, @NotNull Consumer<WrappedTask> consumer, long delay, TimeUnit unit) {
return this.runAtEntityLater(entity, consumer, TimeConverter.toTicks(delay, unit));
}

@Override
Expand Down
Loading