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
+109
View File
@@ -0,0 +1,109 @@
# Block Components
A block's JSON says what it is. Components say what it _does_. A component is a set of handlers a server script
registers under an id. Any block that lists that id in its `components` gets that behavior, with its own parameters.
This is how every block that does something works, including the base game's chests, furnaces and crops. One component
can serve many blocks: every block with `bworld:storage` is a chest, with its own number of rows.
## Using a Component
List it in the block's `components`, with its parameters:
```json
"components": {
"bworld:storage": { "rows": 3 }
}
```
A block can list several. Their handlers run in the listed order.
## Registering a Component
In the server script:
```ts
import type { ServerContext } from "bworld/server";
interface OxidizesParams {
seconds: number;
into: string;
}
export function setup(ctx: ServerContext) {
ctx.components.register_block<OxidizesParams>("copper_tools:oxidizes", {
on_create(block) {
block.data = { age: 0 };
},
on_second(block, params) {
block.data.age += 1;
if (block.data.age >= params.seconds) {
ctx.world.set_block(block.x, block.y, block.z, params.into);
}
},
});
}
```
```json
"components": {
"copper_tools:oxidizes": { "seconds": 600, "into": "copper_tools:oxidized_copper_block" }
}
```
Components can only be registered during `setup`, and only in the mod's own namespace. Once every mod's `setup` has run,
a block or item that lists a component nobody registered stops the server from starting, naming the block and the
component.
## Handlers
| Handler | Called when | Returns |
| ------------------------------------ | ----------------------------------------------------------------- | -------------------------- |
| `on_create(block, params)` | The block is placed or set, by a player or a script. | |
| `on_break(block, params, player?)` | The block is broken or replaced. `player` when a player broke it. | |
| `on_click(block, params, player)` | A player starts hitting it. | |
| `on_interact(block, params, player)` | A player right clicks it. | `true` if it did something |
| `on_tick(block, params, dt)` | Every tick, 20 times a second, near a player. | |
| `on_second(block, params, dt)` | Every second, near a player. | |
| `on_new_day(block, params, day)` | Every morning at 06:00, anywhere in the world. | |
`block` is `{ id, x, y, z, data }`, where `data` is the block's [tile data](tiledata.md). `params` is what the block's
JSON lists for this component, and `dt` is in seconds.
### Interacting
When a player right clicks a block, every component's `on_interact` runs. If any of them returns `true`, the click was
handled and nothing else happens. Otherwise the held item is used or placed, as if the block had no components.
Give a block `"interactive": true` when right clicking it always does something, like opening a screen. Otherwise
clients guess that the click places the held block and show it for a moment before the server corrects them.
### Ticking
`on_tick` and `on_second` only run for blocks that **have tile data**, within 6 chunks of a player. So a component that
ticks from the start sets `block.data` in `on_create`, like `copper_tools:oxidizes` above.
`on_new_day` runs for every block with tile data in the whole world, near a player or not, so crops keep growing while
nobody's around. When time jumps forward past several mornings, like with `/time add`, each of them runs, up to the
last 64.
The server runs a fixed 20 ticks a second. When a tick takes too long, up to ten of the next ones run back to back to
catch up. If it's further behind than that, it skips the rest and logs a warning. `/tps` shows how it's keeping up.
## The Base Game's Components
`mods/bworld/scripts/server.ts` registers these, and any mod that depends on `bworld` can use them:
| Component | Params | What it does |
| ----------------------- | --------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| `bworld:storage` | `rows` | A chest: `rows` × 9 slots, dropped when it's broken. |
| `bworld:furnace` | none | Smelts with `furnace` and `fuel` [recipes](../resources/recipes.md). Needs `lit` and `facing` states, see `blocks/furnace.json`. |
| `bworld:smithing_table` | none | Upgrades tools with `smithing` recipes. |
| `bworld:hoeable` | `tool`, `into` | Right clicking with a tool of type `tool` turns it into `into`, like a hoe on grass. |
| `bworld:farmland` | none | Watered with the watering can, dries out every morning. |
| `bworld:crop` | see [Crops](crops.md) | Grows a stage every morning its farmland was watered. |
| `bworld:crop_top` | see [Crops](crops.md) | The top half of a two block crop, like rice. |
| `bworld:water_source` | none | Refills the watering can. |
| `bworld:fishable` | none | Can be fished in with the fishing rod. |
Item components work the same way, see [Items](../items/index.md#item-components).
+120
View File
@@ -0,0 +1,120 @@
# Crops
A crop is a block with an `age` state, the base game's `bworld:crop` [component](components.md), and a seeds item that
places it. Adding one takes no code.
Crops grow on Stardew Valley's clock with Minecraft's rules: every morning at 06:00, a crop whose farmland was watered
the day before grows a day, then all farmland dries out. The crop shows the stage for how far along it is.
`mods/farmersdelight` adds its cabbages, onions, tomatoes and rice this way.
## A Basic Crop
`blocks/cabbages.json`, with one texture per stage through [variants](states.md#variants):
```json
{
"format_version": 1,
"block": {
"id": "farmersdelight:cabbages",
"model": "engine:cross",
"textures": { "cross": "farmersdelight:cabbages_stage0" },
"variants": {
"age=1": { "textures": { "cross": "farmersdelight:cabbages_stage1" } },
"age=2": { "textures": { "cross": "farmersdelight:cabbages_stage2" } }
},
"render_layer": "cutout",
"collision": false,
"mining": { "toughness": 0 },
"item": false,
"place_on": ["bworld:hoed_dirt"],
"states": [{ "name": "age", "bits": 3, "default": 0 }],
"components": {
"bworld:crop": {
"days": 12,
"stages": 8,
"fertilizer": "bworld:bone_meal",
"drops": {
"young": "farmersdelight:cabbage_seeds",
"mature": "farmersdelight:cabbage",
"bonus": "farmersdelight:cabbage_seeds"
}
}
}
}
}
```
`"item": false` because players plant the seeds, not the crop. `items/cabbage_seeds.json`:
```json
{
"format_version": 1,
"item": {
"id": "farmersdelight:cabbage_seeds",
"texture": "farmersdelight:cabbage_seeds",
"places": "farmersdelight:cabbages"
}
}
```
`place_on` keeps it on farmland, and it pops off when the farmland is dug up.
## Parameters
| Param | Meaning |
| ------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `days` | Watered mornings from planting to fully grown. |
| `stages` | How many ages it shows, 0 to `stages - 1`, spread evenly over `days`. The `age` state must fit them. |
| `drops` | What it drops when broken: `young` before it's grown, `mature` once it is, and `bonus`, 1 to 4 of an item when grown (Minecraft's 1 plus 3 tries at 57%, like wheat's seeds). `young` and `mature` can be one id or a list. |
| `fertilizer` | An item that grows it a day right away when used on it, like bone meal. |
| `water` | `"farmland"` (the default) needs watered farmland under it. `"always"` grows every morning. |
| `regrow_days`, `harvest` | Right clicking it once grown picks it, dropping `harvest.item` (`count` is `[min, max]`), and it grows back after `regrow_days`. |
| `upper` | `{ block, stages, supporting_age }`: its last stages grow a second block on top, see below. |
| `leaves` | What the spot becomes when it's broken, like the water rice was planted in. |
## Harvesting By Hand
Tomatoes are picked instead of broken:
```json
"bworld:crop": {
"days": 11,
"stages": 8,
"regrow_days": 4,
"harvest": { "item": "farmersdelight:tomato", "count": [1, 2] },
"drops": {
"young": "farmersdelight:tomato_seeds",
"mature": ["farmersdelight:tomato", "farmersdelight:tomato_seeds"]
}
}
```
## Two Block Crops
Rice grows in water and, once it's tall enough, grows panicles on top. The crop stays at `supporting_age` while the top
block goes through its own `stages`:
```json
"place_in": ["bworld:water"],
"place_on": ["bworld:dirt", "bworld:grass", "bworld:hoed_dirt"],
"components": {
"bworld:crop": {
"days": 6,
"stages": 4,
"water": "always",
"upper": { "block": "farmersdelight:rice_panicles", "stages": 4, "supporting_age": 4 },
"leaves": "bworld:water",
"drops": { "young": "farmersdelight:rice", "mature": "farmersdelight:rice" }
}
}
```
The top block uses `bworld:crop_top` with its own `stages` and `drops`. Breaking either half harvests the whole plant:
```json
"components": {
"bworld:crop_top": { "stages": 4, "drops": { "mature": "farmersdelight:rice_panicle" } }
}
```
Since water is `replaceable` and rice has `place_in`, players plant rice by clicking the water itself.
+106
View File
@@ -0,0 +1,106 @@
# Blocks
Blocks make up the world. Each kind of block is defined once, in a JSON file in the mod's `blocks/` folder, and the
world is made of references to those definitions at different positions.
A block's definition says how it looks, how it's mined and what it drops. What it _does_ comes from
[components](components.md), which server scripts register.
## A Basic Block
`blocks/copper_block.json`:
```json
{
"format_version": 1,
"block": {
"id": "copper_tools:copper_block",
"textures": "copper_tools:copper_block",
"mining": { "toughness": 5, "tool": "pickaxe", "requires_tool": true, "tier": 1 },
"drops": "copper_tools:copper_block"
}
}
```
This registers the block and an item with the same id that places it. It's a full cube with the same texture on every
side, takes 5 seconds to break by hand, breaks faster with a pickaxe, and only drops itself when broken with a pickaxe
of tier 1 (stone or copper) or better.
## Properties
### Looks
| Field | Default | Meaning |
| ---------------- | ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | required | The block's id. |
| `name` | from the id | What players see. `copper_tools:copper_block` is "Copper Block" when not set. Its item uses it too. |
| `textures` | required | One texture id for every side, `{ "top", "bottom", "side" }`, or `{ "front", "side" }` (optionally with `top` and `bottom`) for a block that faces a way. With another `model`, the model's texture variables. |
| `model` | `engine:cube` | The [block model](../resources/models.md). |
| `variants` | none | Other models, textures or rotations for some [states](states.md). |
| `render_layer` | `solid` | `solid`; `cutout` for texels that are fully opaque or fully clear, like leaves; `translucent` for blended ones, like water and glass. |
| `cull_same` | see meaning | Hide the faces between two of this block. `true` for translucent blocks, `false` otherwise. |
| `alpha` | `1` | Opacity, for translucent blocks. |
| `tint` | none | Draws its textures in a color, see [below](#tinting). |
| `light_emission` | `0` | Light it gives off, 0-15. A torch is 14. |
| `light_opacity` | see meaning | How much light passing through it loses, 0-15. `15` for solid blocks, `0` otherwise. Leaves and water use `1`. |
Only `solid` blocks hide their neighbors' faces. Blocks that aren't full cubes, like plants, should use `cutout`, or
there will be holes in the world behind them.
### Behavior
| Field | Default | Meaning |
| ---------------------- | ----------- | ----------------------------------------------------------------------------------------------------------------------------- |
| `collision` | `true` | Whether entities bump into it. Plants and water set `false`. |
| `mining.toughness` | unbreakable | Seconds to break by hand. Without `mining`, a block can't be broken. |
| `mining.tool` | none | The [tool type](../items/tools.md) that breaks it faster, like `pickaxe`, `axe` or `shovel`. |
| `mining.requires_tool` | `false` | Only drops when broken with `mining.tool`. |
| `mining.tier` | `0` | With `requires_tool`, the lowest tool tier it drops for. Iron ore is `1`, so a wood pickaxe gets nothing. |
| `drops` | nothing | The item id it drops when broken. |
| `item` | `true` | Also register an item that places it, with the same id. |
| `interactive` | `false` | Right clicking it does something, like opening a screen, so clients don't guess that it places a block. |
| `replaceable` | `false` | Placing a block into it replaces it, like water. |
| `place_in` | see meaning | Block ids it can be placed into, replacing them, like `["bworld:water"]` for rice. Air and `replaceable` blocks when not set. |
| `place_on` | anywhere | Block ids it can stand on, like `["bworld:hoed_dirt"]` for crops. See [below](#placement). |
| `states` | none | Named [state](states.md) bits, like a crop's age. |
| `components` | none | [Components](components.md) and their parameters. |
| `item_display` | none | Shows an item on top of it, see [Tile Data](tiledata.md#showing-an-item). |
A block's definition goes to every player, since clients need it to draw the block, time breaking and collide with it.
It can't hold functions.
## Mining
Breaking takes `mining.toughness` seconds by hand. A tool whose type matches `mining.tool` breaks it `tool.speed` times
faster. The client times breaking itself, and the server checks the result.
When it breaks, the block's components' `on_break` runs and it drops `drops`, unless it has `requires_tool` and the
player's tool is the wrong type or a tier below `mining.tier`.
## Placement
Players place a block by right clicking a face of another block with its item. It goes next to the face they clicked, or
into the clicked block itself when that one is `replaceable`, like water.
It's only placed when:
- the spot is one it can go into: air or a `replaceable` block, or one of `place_in`,
- the block under the spot is one of `place_on`, when the block has `place_on`,
- and, for blocks with collision, no player or mob is in the way.
A block with `place_on` also pops off (dropping what it drops) when the block under it becomes something else, like a
crop when its farmland is dug up.
Food that places a block, like potatoes, is planted when clicked somewhere it can go and eaten otherwise.
## Tinting
`tint` draws a block's textures in a color, for textures painted in greys:
```json
"tint": { "color": "grass", "textures": ["top"] }
```
`color` is `"grass"` or `"foliage"` for the biome's color, blended across biome borders like Minecraft's grass and
leaves, or a fixed color like `"#80a755"`. `textures` lists the texture variables to tint, like only the grass block's
top. Without it every texture is tinted. In inventories, where there's no biome, the block uses plains' color.
+88
View File
@@ -0,0 +1,88 @@
# Block States
A block's states are a few named numbers stored with each copy of it in the world, like a crop's growth stage or which
way a furnace faces. They're saved with the world, synced to players, and can change how the block looks through
`variants`.
States are for small values the client needs, like what to draw. For anything else a block keeps, use
[tile data](tiledata.md).
## Declaring States
```json
"states": [
{ "name": "age", "bits": 3, "default": 0 },
{ "name": "facing", "bits": 2, "default": 0 }
]
```
Each state is `bits` wide, so `age` above holds 0 to 7. A block has at most 16 bits of state in total. It starts with
the defaults when it's placed or generated.
## Reading and Changing States
Server scripts use `ctx.world`:
```ts
const age = ctx.world.get_state(x, y, z, "age") ?? 0;
ctx.world.set_state(x, y, z, "age", age + 1);
```
`get_state` is `undefined` where there's no block, and `set_state` does nothing there and returns `false`. Both throw
for a state the block doesn't have, and `set_state` for a value that doesn't fit its bits. Changes are synced to
everyone who can see the block. Unlike `set_block`, it doesn't run `on_break` or `on_create`, and the block keeps its
tile data.
## Variants
`variants` gives a block a different `model`, `textures` or rotation depending on its states. `blocks/wheat.json` shows
a texture per growth stage:
```json
{
"format_version": 1,
"block": {
"id": "bworld:wheat",
"model": "engine:crop",
"textures": { "crop": "bworld:wheat_stage_0" },
"variants": {
"age=1": { "textures": { "crop": "bworld:wheat_stage_1" } },
"age=2": { "textures": { "crop": "bworld:wheat_stage_2" } }
},
"render_layer": "cutout",
"collision": false,
"states": [{ "name": "age", "bits": 3, "default": 0 }]
}
}
```
- A key is a condition on the block's states: `"age=3"`, or `"age=3,facing=1"` when all of them have to match. `""`
matches always.
- A variant sets any of `model`, `textures` and `y`, a turn around the vertical axis of 0, 90, 180 or 270 degrees,
clockwise seen from above.
- **Each of the three comes from the first matching variant that sets it.** So one variant can turn the block and a
later one change its textures, and both apply.
- What no matching variant sets comes from the block itself.
## Facing
A block with a `facing` state of at least 2 bits faces the player who places it: `facing` is set from which way they
look, 0 to 3. Pair it with turning variants, like the furnace:
```json
"textures": { "front": "bworld:furnace_front", "side": "bworld:furnace_side", "top": "bworld:furnace_top", "bottom": "bworld:cobblestone" },
"variants": {
"facing=1": { "y": 90 },
"facing=2": { "y": 180 },
"facing=3": { "y": 270 },
"lit=1": { "textures": { "front": "bworld:furnace_front_lit", "side": "bworld:furnace_side", "top": "bworld:furnace_top", "bottom": "bworld:cobblestone" } }
},
"states": [
{ "name": "lit", "bits": 1, "default": 0 },
{ "name": "facing", "bits": 2, "default": 0 }
]
```
A lit furnace facing east matches both `facing=1` (its turn) and `lit=1` (its textures).
Turning also turns a [displayed item](tiledata.md#showing-an-item), like the knife on Farmer's Delight's cutting board.
+73
View File
@@ -0,0 +1,73 @@
# Tile Data
Tile data is extra data kept for one block in the world, like what a chest holds or how far along a furnace is. It's
bworld's version of Minecraft's block entities: a [block state](states.md) is a few bits every client knows, while tile
data is any JSON value that only the server knows.
## Reading and Writing
A [component](components.md)'s handlers get the block's data as `block.data`. It's `undefined` until a handler sets it,
and it can be any JSON value:
```ts
ctx.components.register_block("copper_tools:counter", {
on_create(block) {
block.data = { clicks: 0 };
},
on_interact(block, _params, player) {
block.data.clicks += 1;
player.send_message(`Clicked ${block.data.clicks} times`);
return true;
},
});
```
Outside a handler, `ctx.world.get_block_data<T>(x, y, z)` reads it.
- Tile data is saved with the world, and goes away when the block is broken or replaced.
- It's **never sent to clients**. To show it to a player, open a [container screen](../inventories/menus.md), or change
a block state that the block's `variants` show.
- Only blocks with tile data run `on_tick`, `on_second` and `on_new_day`.
## Blocks With Containers
A block that holds items keeps a [container](../inventories/containers.md) and stores its id in tile data. Containers
are saved until they're deleted, so the block deletes it when it's broken, after dropping what's inside:
```ts
ctx.components.register_block("copper_tools:crate", {
on_create(block) {
block.data = { container: ctx.containers.create(9).id };
},
on_break(block) {
const container = ctx.containers.get(block.data.container);
if (!container) return;
for (let slot = 0; slot < container.size; slot++) {
const item = container.get(slot);
if (item) ctx.world.drop_item(block.x, block.y, block.z, item);
}
ctx.containers.delete(container.id);
},
});
```
The base game's `bworld:storage` does exactly this, see `mods/bworld/scripts/server.ts`.
## Showing an Item
A block with `item_display` draws one item lying on top of it, like the food on Farmer's Delight's cutting board:
```json
"item_display": { "surface": 1 }
```
`surface` is the height in pixels the item lies at, 0 to 16. The server script sets the item:
```ts
ctx.world.set_block_item(x, y, z, { id: "farmersdelight:cabbage", count: 1 });
const shown = ctx.world.get_block_item(x, y, z); // a copy, or undefined
ctx.world.set_block_item(x, y, z, undefined); // shows nothing
```
Unlike the rest of tile data, the displayed item is synced to players. It's saved with the block, turns with the block's
`y` variant, and disappears with the block without dropping, so drop it in `on_break` if it should.