Update modding docs

This commit is contained in:
2026-09-29 23:29:14 -03:00
parent f0cf4e4eeb
commit 1ac7a5c943
50 changed files with 2672 additions and 1924 deletions
+106
View File
@@ -0,0 +1,106 @@
# Getting Started
This section covers making a new mod, checking it, and running it on a local server.
## Prerequisites
- Familiarity with TypeScript (or JavaScript). Scripts can be `.ts` or `.js`, and the mod API is typed.
- [Deno](https://deno.com). Every tool in this repository runs through `deno task`.
- A clone of the bworld repository. Mods live in its `mods/` folder while you work on them.
bworld is always played on a server, with no single player mode. A mod is installed **on the server** only. Players who
join download its client code, data and textures from that server automatically, so they never install anything
themselves.
## Creating a Mod
```sh
deno task new-mod copper_tools "Copper Tools"
```
This copies `templates/mod` to `mods/copper_tools` and fills in the id and name. The template has one of everything: a
block with a custom component, an item, a shaped recipe, two textures, and a server, client and worldgen script. Delete
what you don't need, along with its entry in `manifest.json`.
The first argument is the mod's id, its [namespace](../concepts/registries.md#identifiers). It has to match the folder
name.
> [!TIP]
> The mods already in the repository are the best reference. `mods/bworld` is the base game itself, written against the
> same API as any other mod. `mods/farmersdelight` adds crops, a stove, a cooking pot and a cutting board, and
> `mods/recipe_viewer` is a client-only mod that draws over inventory screens.
## Checking Your Mod
```sh
deno task check-mods
```
This checks every mod in `mods/`:
- the manifest and every data file, against the formats in these docs,
- that every block, item, model and entity model a mod refers to exists,
- that textures exist (a missing one is a warning, since it shows as the missing texture instead of failing),
- that a mod lists the mods whose ids it uses in its `dependencies` (a warning),
- that ids are in the mod's own namespace, and that no two mods register the same id,
- that the scripts typecheck against the API types.
Folders in `mods/` starting with `_` or `.` are skipped, which is handy for parking a mod you aren't working on.
## Building and Running
```sh
deno task build # packs every mod in mods/ into server_mods/<id>.bmod, and builds the client
deno task server # starts a server with every .bmod in server_mods/
```
`build` refuses to pack a mod that has errors. The server checks every `.bmod` again when it starts, and a problem in
any mod stops it from starting, naming the mod and what's wrong. The game never runs with some mods missing.
Open the server's address in a browser to play. To try a change, rebuild and restart the server, then rejoin. Leaving a
server reloads the page, so no old mod code stays loaded.
### Useful Commands In Game
| Command | What it does |
| ---------------------- | ------------------------------------------------------- |
| `/give <item> [count]` | Gives you an item |
| `/summon <entity>` | Spawns an entity where you stand |
| `/time set <when>` | Jumps to `day`, `noon`, `sunset`, `night` or `midnight` |
| `/time add <ticks>` | Moves time forward, running every morning it passes |
| `/help` | Lists every command |
See [Commands](../misc/commands.md) for the rest, and for adding your own.
## Testing
`deno task test` runs the repository's tests. `tests/helpers.ts` has `test_game`, which starts a real game server with
the mods in a folder and lets a test join as a player and send what a client would:
```ts
import { assertEquals } from "@std/assert";
import { test_game } from "./helpers.ts";
Deno.test("a hoe turns grass into farmland", async () => {
const { game, join, send, take } = await test_game("mods");
join(1, "alice");
game.set_block(1, 100, 1, "bworld:grass");
send(1, { type: "move", x: 2.5, y: 101, z: 2.5, yaw: 0, pitch: 0 });
send(1, { type: "chat", text: "/give bworld:wood_hoe" });
const inventory = take(1).filter((m) => m.type === "container" && m.container === "inventory").at(-1).items;
send(1, {
type: "select_slot",
slot: inventory.findIndex((i: { id: string } | null) => i?.id === "bworld:wood_hoe"),
});
send(1, { type: "use_block", x: 1, y: 100, z: 1, face: "top" });
assertEquals(game.world.get_block_id(1, 100, 1), "bworld:hoed_dirt");
});
```
`tests/farming_test.ts`, `tests/cooking_test.ts` and `tests/cutting_board_test.ts` test mods this way.
## Next Steps
- [Mod Files](modfiles.md): the manifest, the folder layout, and `.bmod` files.
- [Sides](../concepts/sides.md): which script runs where, and why it matters.
- [Blocks](../blocks/index.md) and [Items](../items/index.md): most mods start here.
+123
View File
@@ -0,0 +1,123 @@
# Mod Files
A mod is a folder with a `manifest.json`, some data files, textures, and up to three 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
recipes/*.json
worldgen/ores.json
textures/*.png
scripts/
server.ts # game logic, never sent to players
client.ts # 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` or `recipe`, depending on the folder. File names don't matter, the `id`
inside does.
## `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"
},
"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. Not checked yet. |
| `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` 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` and
`bworld/worldgen`:
```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
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).