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
+11 -13
View File
@@ -4,20 +4,18 @@ A bworld mod. `MODS.md` at the root of the bworld repo links to the modding docs
## Files
| Path | What it's for |
| --------------------- | ------------------------------------------------------------------------------------ |
| `manifest.json` | The mod's id, name, version, dependencies and scripts. |
| `blocks/*.json` | One block per file. Blocks get an item form with the same id unless `"item": false`. |
| `items/*.json` | Items that aren't a block's item form. |
| `recipes/*.json` | `shaped` crafting, `furnace` smelting and `fuel`. |
| `textures/*.png` | 16×16 textures, named `example_mod:<file name>`. |
| `worldgen/ores.json` | Ores to generate (optional, not included here). |
| `scripts/server.ts` | Game logic: block components, commands, events. Never sent to players. |
| `scripts/client.ts` | Sent to players: menu screens, screens, HUD, keybinds. |
| `scripts/worldgen.ts` | Terrain features. Runs on the server and every client and must be deterministic. |
| `CREDITS.md` | Licenses and thanks, shown on the About page. |
| Path | What it's for |
| -------------------- | ----------------------------------------------------------------------------------------------------------- |
| `manifest.json` | The mod's id, name, version, dependencies and script. |
| `blocks/*.json` | One block per file. Blocks get an item form with the same id unless `"item": false`. |
| `items/*.json` | Items that aren't a block's item form. |
| `recipes/*.json` | `shaped` crafting, `furnace` smelting and `fuel`. |
| `textures/*.png` | 16×16 textures, named `example_mod:<file name>`. |
| `worldgen/ores.json` | Ores to generate (optional, not included here). |
| `scripts/main.ts` | Its exports are phases: `server` for game logic, `client` for screens and the HUD, `worldgen` for features. |
| `CREDITS.md` | Licenses and thanks, shown on the About page. |
Delete what you don't use, including its entry in `manifest.json`.
Delete what you don't use. A phase you don't need is an export you leave out.
## Checking it
+1 -5
View File
@@ -9,10 +9,6 @@
"dependencies": [
{ "id": "bworld", "version": ">=0.1.0" }
],
"scripts": {
"server": "scripts/server.ts",
"client": "scripts/client.ts",
"worldgen": "scripts/worldgen.ts"
},
"main": "scripts/main.ts",
"credits": "CREDITS.md"
}
-14
View File
@@ -1,14 +0,0 @@
import type { ClientContext } from "cubi/client";
// called on every player's client after the mod is downloaded, before the world shows up.
// everything here is visible to players, so keep secrets and game rules in server.ts
export function setup(ctx: ClientContext) {
ctx.log("loaded");
// a HUD element, drawn every frame
// ctx.hud.register("example_mod:hint", {
// on_render(g) {
// g.text("example mod is here", 8, 8);
// },
// });
}
+65
View File
@@ -0,0 +1,65 @@
import type { ClientContext } from "cubi/client";
import type { ServerContext } from "cubi/server";
import type { WorldgenContext } from "cubi/worldgen";
// the mod's script. each export is a phase, called where and when it runs. this file goes to the server and to every
// player, and each runs only its own phases, so keep the top level free of anything only one side has
// called once when the server starts. players never run it, so game rules belong here
export function server(ctx: ServerContext) {
// used by blocks/example_block.json, which passes { message } as params
ctx.components.register_block<{ message: string }>("example_mod:announce", {
// block data is saved with the world. on_tick and on_second only run for blocks that have some
on_create(block) {
block.data = { age: 0 };
},
// on_tick runs 20 times a second, on_second once, while a player is nearby
on_second(block) {
block.data.age += 1;
},
on_interact(block, params, player) {
player.send_message(`${params.message} It's been here for ${block.data?.age ?? 0} seconds.`);
// handled, so right clicking it doesn't place a block
return true;
},
});
// /example_mod:hello, or /hello when no other mod uses that name
ctx.commands.register("hello", {
description: "Say hello",
usage: "/hello",
run(_args, player) {
player.send_message(`Hello ${player.name}!`);
},
});
ctx.events.after.player_join.subscribe(({ player }) => {
ctx.log(`${player.name} joined`);
});
}
// called on every player's client after the mod is downloaded, before the world shows up
export function client(ctx: ClientContext) {
ctx.log("loaded");
// a HUD element, drawn every frame
// ctx.hud.register("example_mod:hint", {
// on_render(g) {
// g.text("example mod is here", 8, 8);
// },
// });
}
// runs wherever the server generates chunks, which must always give the same world. only use chunk.rng and the noise
// helpers, never Math.random or the time
export function worldgen(gen: WorldgenContext) {
// a boulder on one chunk in ten
gen.register_feature("example_mod:boulders", (chunk) => {
if (chunk.rng.next() > 0.1) {
return;
}
const x = chunk.x * 16 + Math.floor(chunk.rng.next() * 16);
const z = chunk.z * 16 + Math.floor(chunk.rng.next() * 16);
chunk.set_block(x, chunk.height_at(x, z) + 1, z, "bworld:stone");
});
}
-34
View File
@@ -1,34 +0,0 @@
import type { ServerContext } from "cubi/server";
// called once when the server starts
export function setup(ctx: ServerContext) {
// used by blocks/example_block.json, which passes { message } as params
ctx.components.register_block<{ message: string }>("example_mod:announce", {
// block data is saved with the world. on_tick and on_second only run for blocks that have some
on_create(block) {
block.data = { age: 0 };
},
// on_tick runs 20 times a second, on_second once, while a player is nearby
on_second(block) {
block.data.age += 1;
},
on_interact(block, params, player) {
player.send_message(`${params.message} It's been here for ${block.data?.age ?? 0} seconds.`);
// handled, so right clicking it doesn't place a block
return true;
},
});
// /example_mod:hello, or /hello when no other mod uses that name
ctx.commands.register("hello", {
description: "Say hello",
usage: "/hello",
run(_args, player) {
player.send_message(`Hello ${player.name}!`);
},
});
ctx.events.after.player_join.subscribe(({ player }) => {
ctx.log(`${player.name} joined`);
});
}
-15
View File
@@ -1,15 +0,0 @@
import type { WorldgenContext } from "cubi/worldgen";
// runs in chunk workers on the server and every client, which must all generate the same world.
// only use chunk.rng and the noise helpers, never Math.random or the time
export function setup(gen: WorldgenContext) {
// a boulder on one chunk in ten
gen.register_feature("example_mod:boulders", (chunk) => {
if (chunk.rng.next() > 0.1) {
return;
}
const x = chunk.x * 16 + Math.floor(chunk.rng.next() * 16);
const z = chunk.z * 16 + Math.floor(chunk.rng.next() * 16);
chunk.set_block(x, chunk.height_at(x, z) + 1, z, "bworld:stone");
});
}