Files
bworld/docs/modding/gettingstarted/modfiles.md
T
2026-10-06 20:27:17 -03:00

131 lines
6.4 KiB
Markdown

# Mod Files
A mod is a folder with a `manifest.json`, some data files, textures, and up to four scripts. The build packs it into a
single `.bmod` file, which is what servers load.
## Layout
```
mods/
copper_tools/
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
blocks/*.json # one block per file
items/*.json # items that aren't a block's item form
models/*.json # block models
models/*.geo.json # entity models
entities/*.json
effects/*.json # status effects, see entities/effects.md
recipes/*.json
tags/<registry>/<namespace>/*.json # tags, see resources/tags.md
worldgen/ores.json
worldgen/spawns.json # monsters that spawn on their own, see entities/index.md
textures/*.png
scripts/
server.ts # game logic, never sent to players
client.ts # sent to players
shared.ts # block rules, run on the server and sent to players
worldgen.ts # terrain features, never sent to players
```
Data folders can have subfolders. Every data file holds one definition, wrapped with the format version:
```json
{
"format_version": 1,
"block": { "id": "copper_tools:copper_block", "textures": "copper_tools:copper_block" }
}
```
The key is `block`, `item`, `model`, `entity`, `effect`, `recipe` or `tag`, depending on the folder. File names don't
matter, the `id` inside does, except for [tags](../resources/tags.md), whose id is their path.
## `manifest.json`
```json
{
"format_version": 1,
"id": "copper_tools",
"name": "Copper Tools",
"description": "Copper blocks and a copper pickaxe.",
"version": "1.0.0",
"authors": ["paula"],
"game_version": ">=0.1.0",
"dependencies": [{ "id": "bworld", "version": "*" }],
"scripts": {
"server": "scripts/server.ts",
"client": "scripts/client.ts",
"worldgen": "scripts/worldgen.ts",
"shared": "scripts/shared.ts"
},
"credits": "CREDITS.md"
}
```
| Field | Required | Meaning |
| ---------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| `format_version` | yes | Always `1`. Loaders reject versions they don't know. |
| `id` | yes | The mod's [namespace](../concepts/registries.md#identifiers). 1-32 characters of `a-z`, `0-9` and `_`, matching the folder name. |
| `name` | yes | Display name, shown to players when they join from another site and on the Credits screen. |
| `description` | no | A sentence or two. |
| `version` | yes | The mod's own version, semver like `1.0.0`. |
| `authors` | no | A list of names. |
| `game_version` | no | The bworld versions the mod works with, as a semver range. Only checked for mods with `engine_access`. |
| `engine_access` | no | `true` lets the scripts import `bworld/engine` and hook the engine. See [Engine Access](../misc/engine.md). |
| `dependencies` | no | Other mods, `{ "id", "version" }`. A missing dependency is an error, and dependencies load first. The version range isn't checked yet. |
| `scripts` | no | Paths to the `server`, `client`, `shared` and `worldgen` scripts. See [Sides](../concepts/sides.md). |
| `credits` | no | A markdown file for licenses and thanks, shown on the Credits screen. Raw HTML in it is shown as text. |
List every mod whose ids you use in `dependencies`, including `bworld` for the base game's blocks and items. It
guarantees they're loaded before yours, and `check-mods` warns when one is missing.
## Scripts
Each script is an ES module that exports a `setup` function. The build bundles every script on its own into one module,
so they can import other files and packages, but code imported by several scripts is copied into each bundle.
> [!WARNING]
> Anything the client script imports is sent to every player who joins. Don't import secrets or anti-cheat logic into
> code the client script shares with the server script.
### Mods as Deno Projects
When a mod's folder has a `deno.json`, its scripts are bundled and typechecked with it, so the mod can have its own
imports and npm or JSR packages. Mods without one use the `deno.json` of the folder the build runs in, like the mods in
this repository. That config maps the API types, which scripts import as `bworld/server`, `bworld/client`,
`bworld/shared` and `bworld/worldgen`, and [`bworld/engine`](../misc/engine.md):
```ts
import type { ServerContext } from "bworld/server";
```
## `.bmod` Files
A `.bmod` is a built mod in one zip file. Servers load every `.bmod` in their `server_mods/` folder, so installing a mod
on a server is dropping its file there and restarting.
```sh
deno task pack-mod mods/copper_tools # writes copper_tools.bmod, to share
deno task build # packs every mod in mods/ into server_mods/
```
```
copper_tools.bmod
manifest.json # with scripts and credits pointing into the zip
data.json # every block, model, item, entity, recipe and ore
scripts/server.js # each script bundled into one module
scripts/client.js
scripts/worldgen.js
scripts/shared.js
textures/<name>.png
credits.md
```
- Packing checks the mod first, like `check-mods`, and refuses one with errors.
- 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
[Networking](../networking/index.md).