Files
bworld/docs/modding/blocks/components.md
T
2026-09-29 23:29:14 -03:00

6.3 KiB
Raw Blame History

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:

"components": {
	"bworld:storage": { "rows": 3 }
}

A block can list several. Their handlers run in the listed order.

Registering a Component

In the server script:

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);
			}
		},
	});
}
"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. 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. 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 Grows a stage every morning its farmland was watered.
bworld:crop_top see Crops 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.