Skip to main content

Build a game session

A game session bundles persistent data with lifecycle and tick handlers. It can auto-start when the world loads and its data survives server restarts.

Define the session

scripts/server/<bundle>/Server.groovy
import de.luckymcdev.foundryengine.common.Common
import de.luckymcdev.foundryengine.common.game.GameData
import de.luckymcdev.foundryengine.common.game.GameSession

def data = new GameData(id("my_session"))

def session = new GameSession(id("my_session"), data)
session.autoStart(true)
session.onStarting {
println "Session starting"
}
session.onStopping {
println "Session stopping"
}
session.onServerTick { server, level ->
println "Tick ${level.getGameTime()}"
}

Common.getGameManager().register(session)

autoStart(true) starts the session automatically when the world reaches the running lifecycle. Handlers attach with fluent calls; each returns the session.

Handler reference

HandlerSignatureRuns
.onCommonTick(Level) -> voidevery tick, both sides
.onServerTick(MinecraftServer, ServerLevel) -> voidevery server tick
.onClientTick(Minecraft, ClientLevel) -> voidevery client tick
.onStarting() -> voidonce, when the session starts
.onStopping() -> voidonce, when the session stops
.onInit() -> voidonce, first time data initializes

Persistent data

GameData is an NBT-backed store identified by an Identifier. Read and write typed values:

def data = session.data()
data.putBoolean("intro_seen", true)
data.putInt("score", 42)
def score = data.getInt("score")

Save with session.save() and load with session.load(). Sessions registered for a world persist under that world's data directory.