Skip to content

Commit

Permalink
implement Sponge scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackBaroness committed Nov 13, 2024
1 parent 346c7be commit 365fc0b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public static <B extends LiteCommandsBuilder<CommandCause, LiteSpongeSettings, B
.argument(ServerPlayer.class, new ServerPlayerArgument(game, messageRegistry))
.context(ServerPlayer.class, new ServerPlayerOnlyContext(messageRegistry))

.scheduler(new SpongeScheduler(plugin, game))

.extension(new LiteAdventureExtension<>(invocation -> invocation.sender().audience()));
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package dev.rollczi.litecommands.sponge;

import dev.rollczi.litecommands.scheduler.Scheduler;
import dev.rollczi.litecommands.scheduler.SchedulerPoll;
import dev.rollczi.litecommands.shared.ThrowingSupplier;
import org.spongepowered.api.Game;
import org.spongepowered.api.scheduler.Task;
import org.spongepowered.plugin.PluginContainer;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;

public class SpongeScheduler implements Scheduler {

private final PluginContainer plugin;
private final Game game;

public SpongeScheduler(PluginContainer plugin, Game game) {
this.plugin = plugin;
this.game = game;
}

@Override
public <T> CompletableFuture<T> supplyLater(SchedulerPoll type, Duration delay, ThrowingSupplier<T, Throwable> supplier) {
CompletableFuture<T> future = new CompletableFuture<>();

Task.Builder builder = Task.builder().plugin(plugin).execute(() -> {
try {
future.complete(supplier.get());
} catch (Throwable e) {
future.completeExceptionally(e);
if (type.isLogging()) {
plugin.logger().error("Error completing command future:", e);
}
}
});

if (delay.isPositive()) {
builder.delay(delay);
}

if (type.resolve(SchedulerPoll.MAIN, SchedulerPoll.ASYNCHRONOUS).equals(SchedulerPoll.ASYNCHRONOUS)) {
game.asyncScheduler().submit(builder.build());
} else {
game.server().scheduler().submit(builder.build());
}

return future;
}

@Override
public void shutdown() {
}
}
3 changes: 3 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ include(":litecommands-sponge", VERSION_21, tests = false)
include(":litecommands-fabric", VERSION_17, tests = false)

// examples
/*
include(":examples:bukkit", tests = false)
include(":examples:bukkit-adventure-platform", tests = false)
include(":examples:bukkit-chatgpt", VERSION_11, tests = false)
Expand All @@ -47,6 +48,8 @@ include(":examples:velocity", VERSION_11, tests = false)
include(":examples:sponge", VERSION_21, tests = false)
include(":examples:fabric", VERSION_17, tests = false)
*/

fun include(project: String, java: JavaVersion = VERSION_1_8, tests: Boolean = true) {
compatibleWith("including $project", java, tests, {
settings.include(project)
Expand Down

0 comments on commit 365fc0b

Please sign in to comment.