Mod config

This commit is contained in:
2026-10-14 14:14:52 -03:00
parent d7f1b91c0a
commit cedc7725b5
20 changed files with 371 additions and 32 deletions
+1
View File
@@ -124,6 +124,7 @@ seed and chunk always give the same blocks. See [World Generation](../worldgen/i
| Item stacks and their `data` | yes | in containers the player has open or owns |
| Entities | yes | near the player |
| `ctx.storage` | yes | no |
| [Config](../misc/config.md) | yes | no |
| Server script code | yes | **never** |
| Shared script code | yes | yes, and runs it |
+2 -1
View File
@@ -31,7 +31,8 @@ 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 settings and totals. Data about one block
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](../misc/config.md), and data about one block
belongs in that block's tile data, which goes away with the block.
## What Isn't Saved
+3 -2
View File
@@ -11,6 +11,7 @@ mods/
manifest.json # required, everything else is optional
deno.json # makes the mod its own deno project, see below
CREDITS.md # shown on the Credits screen
config.json # settings servers can change, never sent to players. see misc/config.md
blocks/*.json # one block per file
items/*.json # items that aren't a block's item form
models/*.json # block models
@@ -120,7 +121,7 @@ deno task build # packs every mod in mods/ into server_mo
```
copper_tools.bmod
manifest.json # with scripts and credits pointing into the zip
data.json # every block, model, item, entity, recipe, ore, tag and loot table
data.json # every block, model, item, entity, recipe, ore, tag and loot table, and the config
scripts/server.js # each script bundled into one module
scripts/client.js
scripts/worldgen.js
@@ -134,5 +135,5 @@ copper_tools.bmod
- Packing the same mod twice gives the same bytes, so the file only changes when the mod does.
- `deno task build` leaves other `.bmod` files in `server_mods/` alone, so mods from elsewhere can sit next to the ones
it builds.
- Players get a copy **without** `scripts/server.js` and `scripts/worldgen.js`. See
- Players get a copy **without** `scripts/server.js`, `scripts/worldgen.js` and the config. See
[Networking](../networking/index.md).
+83
View File
@@ -0,0 +1,83 @@
# Config
A mod's config is its settings: the numbers and switches a server might want different without changing the mod, like
how fast a machine works, how far something reaches, or whether a feature is on. The mod lists them with their defaults
in `config.json`, a server changes the ones it wants in a file of its own, and the server script reads the result as
`ctx.config`. It's like NeoForge's server configs.
## `config.json`
In the mod's folder, next to `manifest.json`:
```json
{
"format_version": 1,
"config": {
"max_speed": 256,
"crank_hunger": 0.01,
"fan": { "push_distance": 20, "pull_distance": 20 },
"stress": { "copper_tools:press": 8, "copper_tools:mixer": 4 }
}
}
```
A setting can be any JSON value, and objects group settings that go together, like `fan` here. Every setting a server
can change has to be in this file with its default.
## Reading It
`ctx.config` in the server script holds the settings, with the server's changes made:
```ts
import type { ServerContext } from "bworld/server";
interface Config {
max_speed: number;
fan: { push_distance: number; pull_distance: number };
}
export function setup(ctx: ServerContext) {
const config: Config = ctx.config;
ctx.log(`fans push things up to ${config.fan.push_distance} blocks away`);
}
```
It's there from `setup` on, and stays the same for as long as the server runs. It's typed `any`, so give it a type of
your own like `Config` above.
Only the server script has it. The copy of a mod players download leaves its config out, since a server's settings
aren't always the defaults. When players need to see one, the server sends it, like any other
[server data](../concepts/sides.md#what-each-side-knows): in a block's [client data](../blocks/tiledata.md), an item's
`data`, or over a [channel](../networking/index.md#mod-channels).
## Changing It on a Server
A server reads its changes from `config/<mod id>.json`, next to `server_mods/`, or in the folder `CONFIG_DIR` says. The
desktop app's own server reads them from `config/` in the app's data folder. A file is shaped like the mod's
`config.json`, with only the settings to change:
```json
{
"format_version": 1,
"config": {
"fan": { "push_distance": 32 }
}
}
```
- Objects are changed setting by setting, so `fan.pull_distance` stays 20. Anything else, lists too, is replaced whole.
- A change has to be to a setting the mod has, and the same kind of value as its default: a number for a number, an
object for an object. Otherwise the server doesn't start, and says which setting is wrong, so a misspelled one can't
go unnoticed.
- A file for a mod the server doesn't have is left alone.
Settings are read when the server starts, so restart it after changing them.
## In Tests
[`test_game`](../gettingstarted/index.md#testing) takes changes by mod id as its fourth argument, made the same way as a
server's files are:
```ts
const { game } = await test_game("mods", undefined, undefined, { copper_tools: { fan: { push_distance: 4 } } });
```