Skip to content

Commit 365fc0b

Browse files
committed
implement Sponge scheduler
1 parent 346c7be commit 365fc0b

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

litecommands-sponge/src/dev/rollczi/litecommands/sponge/LiteSpongeFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public static <B extends LiteCommandsBuilder<CommandCause, LiteSpongeSettings, B
3636
.argument(ServerPlayer.class, new ServerPlayerArgument(game, messageRegistry))
3737
.context(ServerPlayer.class, new ServerPlayerOnlyContext(messageRegistry))
3838

39+
.scheduler(new SpongeScheduler(plugin, game))
40+
3941
.extension(new LiteAdventureExtension<>(invocation -> invocation.sender().audience()));
4042
});
4143
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package dev.rollczi.litecommands.sponge;
2+
3+
import dev.rollczi.litecommands.scheduler.Scheduler;
4+
import dev.rollczi.litecommands.scheduler.SchedulerPoll;
5+
import dev.rollczi.litecommands.shared.ThrowingSupplier;
6+
import org.spongepowered.api.Game;
7+
import org.spongepowered.api.scheduler.Task;
8+
import org.spongepowered.plugin.PluginContainer;
9+
10+
import java.time.Duration;
11+
import java.util.concurrent.CompletableFuture;
12+
13+
public class SpongeScheduler implements Scheduler {
14+
15+
private final PluginContainer plugin;
16+
private final Game game;
17+
18+
public SpongeScheduler(PluginContainer plugin, Game game) {
19+
this.plugin = plugin;
20+
this.game = game;
21+
}
22+
23+
@Override
24+
public <T> CompletableFuture<T> supplyLater(SchedulerPoll type, Duration delay, ThrowingSupplier<T, Throwable> supplier) {
25+
CompletableFuture<T> future = new CompletableFuture<>();
26+
27+
Task.Builder builder = Task.builder().plugin(plugin).execute(() -> {
28+
try {
29+
future.complete(supplier.get());
30+
} catch (Throwable e) {
31+
future.completeExceptionally(e);
32+
if (type.isLogging()) {
33+
plugin.logger().error("Error completing command future:", e);
34+
}
35+
}
36+
});
37+
38+
if (delay.isPositive()) {
39+
builder.delay(delay);
40+
}
41+
42+
if (type.resolve(SchedulerPoll.MAIN, SchedulerPoll.ASYNCHRONOUS).equals(SchedulerPoll.ASYNCHRONOUS)) {
43+
game.asyncScheduler().submit(builder.build());
44+
} else {
45+
game.server().scheduler().submit(builder.build());
46+
}
47+
48+
return future;
49+
}
50+
51+
@Override
52+
public void shutdown() {
53+
}
54+
}

settings.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ include(":litecommands-sponge", VERSION_21, tests = false)
3939
include(":litecommands-fabric", VERSION_17, tests = false)
4040

4141
// examples
42+
/*
4243
include(":examples:bukkit", tests = false)
4344
include(":examples:bukkit-adventure-platform", tests = false)
4445
include(":examples:bukkit-chatgpt", VERSION_11, tests = false)
@@ -47,6 +48,8 @@ include(":examples:velocity", VERSION_11, tests = false)
4748
include(":examples:sponge", VERSION_21, tests = false)
4849
include(":examples:fabric", VERSION_17, tests = false)
4950
51+
*/
52+
5053
fun include(project: String, java: JavaVersion = VERSION_1_8, tests: Boolean = true) {
5154
compatibleWith("including $project", java, tests, {
5255
settings.include(project)

0 commit comments

Comments
 (0)