# 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("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" } } ``` Items have components the same way, and so do [status effects](../entities/effects.md#effect-components). 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. See [Ticking](#ticking) for where. | | `block` is `{ id, x, y, z, biome, season, data }`, where `data` is the block's [tile data](tiledata.md) and `biome` and `season` are its column's (see [Seasons](../worldgen/biomes.md#seasons)). `params` is what the block's JSON lists for this component, and `dt` is in seconds. ## Rules A component's rules decide a block's states from where it's placed, what's around it and being right clicked. They're registered in the mod's [shared script](../concepts/sides.md#shared-scripts) instead, under the same id, because clients run them too: that's how a player sees a fence join its neighbors the moment they place it, instead of a moment later when the server answers. A component can have rules, handlers, or both, and a block can list one that only has rules. ```ts import type { SharedContext } from "bworld/shared"; export function setup(ctx: SharedContext) { ctx.components.register_block("copper_tools:pipe", { state_for_placement(placement, params) {/* ... */}, on_neighbor_change(block, params, neighbor) {/* ... */}, on_interact(block, params, interaction) {/* ... */}, }); } ``` | Rule | Called when | Returns | | --------------------------------------------- | ---------------------------------------------------------------------- | -------------------------- | | `state_for_placement(placement, params)` | A player is about to place it. | States to place it with | | `on_neighbor_change(block, params, neighbor)` | A block on one of its 6 sides is placed, broken or changes its states. | | | `on_interact(block, params, interaction)` | A player right clicks it. | `true` if it did something | Here `block` is `{ id, x, y, z }`, without tile data, which clients never have. `ctx.world` in a shared script has `get_block`, `get_state`, `set_state`, `is_face_sturdy`, `biome_at` and `season_at` (see [Seasons](../worldgen/biomes.md#seasons)), and only works inside rules. ### Neighbors `on_neighbor_change` is how a block keeps up with what's around it, like Minecraft's `updateShape`: `neighbor` is `{ x, y, z, id }` of the block that changed. A fence sets its `north`, `east`, `south` and `west` states from it. Setting a state to what it already is changes nothing and tells no one, so blocks answering each other this way settle. `ctx.world.is_face_sturdy(x, y, z, face)` says whether a block's side is something to attach to, like Minecraft's `isFaceSturdy`: its [collision shape](../resources/models.md#collision-shapes) covers the whole side. Stone's sides are, a slab's sides and plants aren't. ### Placing `state_for_placement` picks the states a block is placed with, like Minecraft's `getStateForPlacement`. `placement` is `{ x, y, z, face, hit, facing }`: where it's going, the face of the block that was clicked, where on that block the click was (from 0 to 1 on each axis), and the way the player looks along the ground. It returns states by name, and the rest keep their defaults, except that a 2 bit `facing` already faces the player. It runs before the server checks that no one is in the way, so that's checked against the shape the block will really have. A trapdoor clicked onto the upper half of a block's side hangs in the top half: ```ts state_for_placement(placement): Record { if (placement.face !== "top" && placement.face !== "bottom") { return { facing: ["north", "east", "south", "west"].indexOf(placement.face), half: placement.hit[1] > 0.5 ? 1 : 0 }; } return { half: placement.face === "top" ? 0 : 1 }; }, ``` A fence's placement looks at its neighbors, so it's joined from the start, and its `on_neighbor_change` keeps it joined after. Both are in `mods/bworld/scripts/shared.ts`. ### Interacting When a player right clicks a block, every component's `on_interact` runs: the rules' first, then the handlers'. 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. A rule's `on_interact` also runs on the clicking player's client, so they see what it does right away instead of waiting for the server, like a trapdoor opening. It's for clicks that only change states with `ctx.world.set_state`, like Minecraft's `useWithoutItem`: anything that needs the server, like opening a screen, giving items or tile data, goes in the handler. `interaction` is `{ face, hit, facing }`: the face that was clicked, where on it (from 0 to 1 on each axis), and the way the player looks along the ground. The trapdoor's is in `mods/bworld/scripts/shared.ts`: ```ts on_interact(block) { const open = ctx.world.get_state(block.x, block.y, block.z, "open") ?? 0; return ctx.world.set_state(block.x, block.y, block.z, "open", open ? 0 : 1); }, ``` 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. Blocks without tile data, like the cacti world generation makes, run it too, but only within 6 chunks of a player, like the blocks Minecraft random ticks. Blocks one of them places that morning, like a cactus growing a block taller, wait for the next one. 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:shipping_bin` | `rows` | Like `bworld:storage`, and every morning at 06:00 sells what's in it with a `base_sell_value` for coins. | | `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` | `loot_table`? | The fishing rod's hook floats on it and fish bite there when it lands in it. What's caught is `loot_table`, `bworld:gameplay/fishing` when not given, built from the fish whose `liquid` is this block. | | `bworld:trapdoor` | none | Opens and closes when right clicked, and is placed like Minecraft's (both are rules, in the shared script). Needs `facing` (2 bits), `half` and `open` states, see `blocks/oak_trapdoor.json`. | | `bworld:fence` | `connects_to` | Joins the blocks in the tag `connects_to` and sturdy sides of other blocks, except leaves. Needs the `north`, `east`, `south` and `west` states. | Item components work the same way, see [Items](../items/index.md#item-components).