← Back to Roadmap

Advanced Patterns

30 min

Async operations, persistence, and performance tips.

Advanced Patterns

Async Operations

Never block the main thread. Use async for I/O:

java
// Async database query
CompletableFuture.runAsync(() -> {
    // Database query here
    return loadPlayerData(uuid);
}).thenAccept(data -> {
    // Back on main thread
    HytaleServer.SCHEDULED_EXECUTOR.execute(() -> {
        applyPlayerData(player, data);
    });
});

Scheduled Tasks

java
// Run later (20 ticks = 1 second)
getServer().getScheduler().runTaskLater(this, () -> {
    // Delayed code
}, 20);

// Run repeatedly getServer().getScheduler().runTaskTimer(this, () -> { // Repeating code }, 0, 20); // delay, period

Data Persistence

Store player data across sessions:

java
public class PlayerDataManager {
    private final Map<UUID, PlayerData> cache =
        new ConcurrentHashMap<>();

public void save(UUID uuid) { PlayerData data = cache.get(uuid); if (data != null) { CompletableFuture.runAsync(() -> { // Save to database/file }); } }

public CompletableFuture<PlayerData> load(UUID uuid) { return CompletableFuture.supplyAsync(() -> { // Load from database/file return new PlayerData(); }); } }

Performance Tips

  • Cache component lookups - store ComponentType references
  • Batch operations - use CommandBuffer for multiple changes
  • Avoid allocations in tick - reuse objects
  • Profile first - don't optimize without data