Skip to Content
MineDuels

Threads and Folia

Which thread each API call and event runs on, and how to touch players and worlds safely.

MineDuels supports Paper and Folia. The rules below keep your plugin correct on both.

The service

CallsThread rules
inMatch, queued, elo, partyOf, kits, matchOf, runningMatches, queues, parties, drainingSafe from any thread. They read concurrent maps or take a short lock, and return immutable snapshots of the moment of the call
stats, statsByName, top, historySafe from any thread. They return a CompletableFuture that completes on a MineDuels database thread

A snapshot does not change after you got it. A player can leave a match a moment later, so check again before you act.

Never call get() or join() on a future from a server thread. It would freeze that thread while the database answers.

Events

An event is fired on the thread that caused it:

  • On Paper, almost always the main thread.
  • On Folia, a region thread or a player's thread. event.isAsynchronous() is true whenever the thread is not the primary tick thread.

So a listener must not assume the main thread, must not block, and must change players and worlds through the right scheduler.

Touching players and worlds on Folia

Use the scheduler that owns the thing you change.

You changeUse
A player or any entityentity.getScheduler().run(plugin, task, retired)
A block or chunkBukkit.getRegionScheduler().execute(plugin, location, task)
Global state, such as the console or a global settingBukkit.getGlobalRegionScheduler().execute(plugin, task)
Slow or blocking workBukkit.getAsyncScheduler().runNow(plugin, task)

retired runs when the entity is gone before the task could run. It may be null.

@EventHandler
public void onEnd(MatchEndEvent event) {
    for (UUID id : event.winners()) {
        Player winner = Bukkit.getPlayer(id);
        if (winner == null) continue;
        winner.getScheduler().run(plugin,
            task -> winner.sendMessage(Component.text("You won!", NamedTextColor.GREEN)), null);
    }
}

This code is correct on Paper too, since Paper provides the same scheduler methods.

If your plugin does not support Folia, leave folia-supported out of your plugin.yml. A Folia server then refuses to load it. Plugins that use the classic Bukkit.getScheduler() do not work on Folia.

Slow work

A database call, an HTTP request or a file write inside an event handler stalls the thread that fired it, which on Paper is the whole server tick. Do slow work asynchronously and come back to the right thread for the result:

@EventHandler
public void onEnd(MatchEndEvent event) {
    UUID first = event.winners().stream().findFirst().orElse(null);
    if (first == null) return;
    Bukkit.getAsyncScheduler().runNow(plugin, task -> {
        sendToMyWebsite(first, event.kit());          // slow, off the game thread
    });
}

And a stored-stats read, back on the player's thread:

api.stats(player.getUniqueId()).thenAccept(optional -> optional.ifPresent(stats ->
    player.getScheduler().run(plugin,
        task -> player.sendMessage("Best streak: " + stats.bestStreak()), null)));

What MineDuels does itself

  • All its SQL runs on dedicated database threads, never on a game thread.
  • Match logic runs where the players are: a region or player thread on Folia.
  • The web API reads game state on the global region thread and database data on its own small pool, so it cannot stall either.
  • Arena and world work waits for the server to be ready, because worlds cannot be created from onEnable on Folia.

Checklist

  1. No get() or join() on MineDuels futures from a server thread.
  2. Players and entities through entity.getScheduler().
  3. Blocks and chunks through the region scheduler.
  4. No blocking calls inside event handlers.
  5. Re-check state in the task: the player may have left and the match may be over.

On this page