-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
346c7be
commit 365fc0b
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
litecommands-sponge/src/dev/rollczi/litecommands/sponge/SpongeScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters