2.0 KiB
2.0 KiB
Saving Data
The server phase has no file access, so everything a mod keeps goes through the world, and is saved with it. Pick the place by what the data belongs to:
| Data belongs to | Keep it in | Sent to players |
|---|---|---|
| one block | its tile data, block.data |
never |
| one block, small and visible | a block state | yes |
| a set of items | a container | while a screen shows it |
| one item stack | the stack's data |
with the stack |
| the whole mod | ctx.storage |
never |
Everything here must be plain JSON: objects, arrays, strings, numbers, booleans and null. No classes, Maps or
functions.
Mod Storage
ctx.storage is a small key value store for each mod, saved with the world:
interface ModStorage {
get<T>(key: string): T | undefined;
set(key: string, value: unknown): void; // JSON values only
delete(key: string): void;
}
const count = ctx.storage.get<number>("smelted") ?? 0;
ctx.storage.set("smelted", count + 1);
Each mod has its own, so keys don't clash between mods. It's for things like totals and settings changed in game. Settings a server picks before it starts belong in the mod's config, and data about one block belongs in that block's tile data, which goes away with the block.