# 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`, `y`, a turn around the vertical axis of 0, 90, 180 or 270 degrees, clockwise seen from above, and [`shape`](../resources/models.md#shapes). - **Each of them 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.