Code Patterns

Copy-paste examples for common plugin tasks. Commands, events, ECS, GUI, and more.

← Back to Patterns
plugin

Scheduled Task

Schedule repeating tasks using HytaleServer.SCHEDULED_EXECUTOR.

Example Code

java
// In plugin setup():

int intervalMinutes = 5;

HytaleServer.SCHEDULED_EXECUTOR.scheduleWithFixedDelay(() -> {
    try {
        this.getLogger().at(Level.INFO).log("Running scheduled task...");

        // Do something periodically
        Universe.get().runBackup().thenAccept(v -> {
            this.getLogger().at(Level.INFO).log("Backup completed!");
        });

    } catch (Exception e) {
        this.getLogger().at(Level.SEVERE).withCause(e).log("Task failed!");
    }
}, intervalMinutes, intervalMinutes, TimeUnit.MINUTES);

// One-time delayed task:
HytaleServer.SCHEDULED_EXECUTOR.schedule(() -> {
    // Run once after delay
}, 30, TimeUnit.SECONDS);

Thread Safety

Scheduled tasks run on executor thread, not world thread. Use world.execute() for entity modifications.

Common Mistakes

Forgetting try-catch in scheduled task (exceptions can kill the scheduler). Blocking the executor with long operations.