Scheduler
SchedulerService (a Service) wraps BukkitScheduler with intention-revealing methods and provides Executor instances for integration with CompletableFuture.
Methods
Section titled βMethodsβ| Method | Runs on | When |
|---|---|---|
sync(Runnable) |
Main thread | Next tick |
async(Runnable) |
Async thread | Immediately |
later(Runnable, ticks) |
Main thread | After delay |
laterAsync(Runnable, ticks) |
Async thread | After delay |
repeating(Runnable, delay, period) |
Main thread | Every period ticks |
repeatingAsync(Runnable, delay, period) |
Async thread | Every period ticks |
asyncThenSync(Supplier, Consumer) |
Async β Main | Immediately |
All methods return BukkitTask (except asyncThenSync), which can be used to cancel the task.
Examples
Section titled βExamplesβSchedulerService scheduler = service(SchedulerService.class);
// Run on main thread next tickscheduler.sync(() -> player.sendMessage("Hello from main thread!"));
// Run asynchronouslyscheduler.async(() -> { // heavy I/O β do NOT touch Bukkit API here String data = fetchFromRemote(); scheduler.sync(() -> player.sendMessage(data)); // back on main thread});
// Delay (20 ticks = 1 second)scheduler.later(() -> player.sendMessage("This was delayed by 5 seconds!"), 20L * 5);
// Repeating task β auto-save every 5 minutesBukkitTask task = scheduler.repeating(() -> dataStore.save(), 0L, 20L * 60 * 5);
// Cancel when donetask.cancel();asyncThenSync bridge
Section titled βasyncThenSync bridgeβThe most common async pattern: fetch something off-thread, then apply the result on the main thread:
scheduler.asyncThenSync( () -> database.loadPlayer(uuid), // runs async record -> player.sendMessage( // runs sync ColorUtils.parse("<gold>Coins: " + record.coins())));Executors for CompletableFuture
Section titled βExecutors for CompletableFutureβThe database layer uses these executors internally. You can use them directly with CompletableFuture.supplyAsync:
CompletableFuture.supplyAsync(() -> heavyComputation(), scheduler.asyncExecutor()) .thenAcceptAsync(result -> updateHud(player, result), scheduler.mainThreadExecutor());scheduler.asyncExecutor() // Executor β scheduler.async(...)scheduler.mainThreadExecutor() // Executor β scheduler.sync(...)Tick reference
Section titled βTick referenceβ| Duration | Ticks |
|---|---|
| 1 second | 20 |
| 1 minute | 1200 |
| 5 minutes | 6000 |
| 1 hour | 72000 |