periodic-leaderboard
Recomputes a top-5 leaderboard every minute by reading PlayerStats.level
from every online player in parallel, sorting, and broadcasting.
What it shows
pluginScope(this).launch { while (isActive) { delay(60.seconds); … } }— a plugin-lifetime periodic task. Cancels cleanly on shutdown viaAsync.shutdown().- Parallel
readover players viaasync { … }.awaitAll(). Eachreadswitches to its player's owning world dispatcher independently — reads on different worlds run concurrently, reads on the same world serialize. runCatching { … }around the body so a single bad read doesn't kill the loop.
This is the shape for any plugin-wide background recompute: leaderboards, periodic backups of derived state, quest sync, etc.
Build
./gradlew shadowJar
# → build/libs/periodic-leaderboard.jar
Run
- Drop in
mods/, wirePlayerStatsBinding.componentType()andonlinePlayers(). - For a faster demo while testing, change
delay(60.seconds)todelay(5.seconds)and rebuild. - Connect 2+ players. The top-5 logs every loop tick.
If you want per-world leaderboards instead, swap pluginScope(this) for
worldScope(world) and run one loop per world. Cancellation flows through
WorldScopes.cancel(uuid).