Single module modding

This commit is contained in:
2026-11-14 12:18:41 -03:00
parent 605008f584
commit 313098892e
134 changed files with 919 additions and 924 deletions
+2 -2
View File
@@ -19,8 +19,8 @@ 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`.
block with a custom component, an item, a shaped recipe, two textures, and a script with a server, client and worldgen
phase. Delete what you don't need: a phase you don't need is an export you leave out.
The first argument is the mod's id, its [namespace](../concepts/registries.md#identifiers). It has to match the folder
name.
+20 -33
View File
@@ -1,7 +1,7 @@
# Mod Files
A mod is a folder with a `manifest.json`, some data files, textures, and up to five scripts. The build packs it into a
single `.cubi` file, which is what servers load.
A mod is a folder with a `manifest.json`, some data files, textures, and a script. The build packs it into a single
`.cubi` file, which is what servers load.
## Layout
@@ -32,11 +32,8 @@ mods/
textures/*.png
textures/*.json # animations, see resources/textures.md
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
content.ts # blocks made in code, run by the server while it loads mods, never sent to players
main.ts # the mod's script, its exports are its phases. sent to players, see concepts/sides.md
*.ts # whatever else it imports
```
Data folders can have subfolders. Every data file holds one definition, wrapped with the format version:
@@ -63,13 +60,7 @@ matter, the `id` inside does, except for [tags](../resources/tags.md), whose id
"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",
"content": "scripts/content.ts"
},
"main": "scripts/main.ts",
"credits": "CREDITS.md"
}
```
@@ -83,29 +74,29 @@ matter, the `id` inside does, except for [tags](../resources/tags.md), whose id
| `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 `cubi/engine` and hook the engine. See [Engine Access](../misc/engine.md). |
| `engine_access` | no | `true` lets the script import `cubi/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`, `worldgen` and `content` scripts. See [Sides](../concepts/sides.md). |
| `main` | no | The path of the mod's script, whose exports are its phases. 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
## The Script
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.
A mod's script is an ES module whose exports are its phases: `content`, `shared`, `server`, `client` and `worldgen`,
each a function called with its context where and when it runs. See [Sides](../concepts/sides.md). The build bundles it
with everything it imports into one module, so it can import other files and packages.
> [!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.
> The whole script is sent to every player who joins, its server phase too. Don't put secrets in it.
### 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
When a mod's folder has a `deno.json`, its script is 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 `cubi/server`, `cubi/client`, `cubi/shared`,
`cubi/worldgen` and `cubi/content`, and [`cubi/engine`](../misc/engine.md):
this repository. That config maps the API types, which the script imports as `cubi/server`, `cubi/client`,
`cubi/shared`, `cubi/worldgen` and `cubi/content`, and [`cubi/engine`](../misc/engine.md):
```ts
import type { ServerContext } from "cubi/server";
@@ -123,13 +114,9 @@ deno task build # packs every mod in mods/ into server_mo
```
copper_tools.cubi
manifest.json # with scripts and credits pointing into the zip
manifest.json # with main and credits pointing into the zip
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
scripts/shared.js
scripts/content.js
script.js # the script bundled into one module
textures/<name>.png
textures/<name>.json # for animated textures
credits.md
@@ -139,6 +126,6 @@ copper_tools.cubi
- Packing the same mod twice gives the same bytes, so the file only changes when the mod does.
- `deno task build` leaves other `.cubi` files in `server_mods/` alone, so mods from elsewhere can sit next to the ones
it builds.
- `data.json` doesn't hold the blocks `scripts/content.js` registers. The server runs it when it loads the `.cubi`.
- Players get a copy **without** `scripts/server.js`, `scripts/worldgen.js`, `scripts/content.js` and the config, but
with the blocks the content script registered in its `data.json`. See [Networking](../networking/index.md).
- `data.json` doesn't hold the blocks the content phase registers. The server runs it when it loads the `.cubi`.
- Players get a copy **without** the terrain's data and the config, but with the blocks the content phase registered in
its `data.json`. See [Networking](../networking/index.md).