Mod config
This commit is contained in:
@@ -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 } } });
|
||||
```
|
||||
Reference in New Issue
Block a user