Files
bworld/docs/modding/blocks/index.md
T

249 lines
18 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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). |
| `parts` | none | Models drawn together depending on [states](states.md), like a fence's post and bars. See [Parts](../resources/models.md#parts). |
| `item_model` | none | The model its item is drawn with. See [Item Models](../resources/models.md#item-models). |
| `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 set `false`, and fluids default to it. |
| `shape` | its model | The boxes players point at and entities bump into, in pixels. See [Shapes](../resources/models.md#shapes). |
| `collision_shape` | `shape` | What entities bump into when it's different, up to 24 pixels high. See [Collision Shapes](../resources/models.md#collision-shapes). |
| `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, one of it. |
| `loot_table` | none | A [loot table](../resources/loot_tables.md) it drops from when broken instead, with its states and the tool. Not with `drops`. |
| `item` | `true` | Also register an item that places it, with the same id. To give that item more, see [Block Items](../items/index.md#block-items). |
| `interactive` | `false` | Right clicking it does something, like opening a screen, so clients don't guess that it places a block. |
| `scrollable` | `false` | The mouse wheel turned while looking at it goes to its components' `on_scroll` instead of the hotbar, see [Scrolling](components.md#scrolling). |
| `replaceable` | `false` | Placing a block into it replaces it, like water. Fluids default to `true`. |
| `place_in` | see meaning | Block ids or [tags](../resources/tags.md) it can be placed into, replacing them, like `["bworld:water"]` for rice. Air and `replaceable` blocks when not set. |
| `place_on` | anywhere | Block ids or [tags](../resources/tags.md) it can stand on, like `["bworld:hoed_dirt"]` for crops or `["#bworld:logs"]`. 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). |
| `fluid` | none | Makes it a fluid, like water. See [Fluids](#fluids). |
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.
### Solid Blocks
A block is solid when it's drawn in the `solid` layer and entities bump into a whole block in every state: stone,
planks, logs and furnaces are, glass, leaves, slabs, chests and fences aren't. Every variant counts, even one that no
state shows, and blocks made of `parts` never are. Scripts on every side ask with `ctx.blocks.solid(id)`, which is
`false` for air and throws for a block that doesn't exist. It's worked out once per block, so it's cheap enough to ask
for every block a walk goes through, unlike `ctx.world.is_face_sturdy`, which looks at the block at a position.
## 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` or what its `loot_table` gives, unless it
has `requires_tool` and the player's tool is the wrong type or a tier below `mining.tier`.
### Breaking From Scripts
`ctx.world.break_block(x, y, z)` breaks a block like a player with nothing in their hand, and returns what it dropped,
or `undefined` when there was nothing there. A machine that breaks blocks over time, like a drill, shows its progress
and passes options for how it breaks them:
```ts
const toughness = ctx.blocks.toughness(id); // seconds by hand, Infinity for one that can't be broken
ctx.world.set_break_progress(x, y, z, progress); // cracks, 0 to 1, that every player who sees it sees
// once it's through: drops what the right tool would, and hands them back instead of dropping them
const drops = ctx.world.break_block(x, y, z, { harvest: true, collect: true });
```
`set_break_progress` is like Minecraft's `destroyBlockProgress`. Only a change in the stage the cracks are drawn at is
sent, so setting it every tick costs little. `undefined` takes the cracks off, and so does the block being broken or
replaced. Players who load the chunk later don't see cracks set before, so keep setting it while it's being broken.
`harvest` drops what the right tool would, though nothing holds one, like Create's drills and saws: stone gives
cobblestone. `collect` returns the drops without dropping them, for a machine that puts them somewhere, like onto a
belt.
## 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 of its [shape](../resources/models.md#shapes).
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.
## Fluids
A block with `fluid` is a fluid, like water. The engine draws it like Luanti's fluids: each corner of its surface is as
high as the fluid around it, so it slopes down from a source to where it flows, and it thins out to a film at the shore.
`mods/oily/blocks/oil.json`:
```json
{
"format_version": 1,
"block": {
"id": "oily:oil",
"textures": { "still": "oily:oil_still", "flowing": "oily:oil_flowing" },
"render_layer": "translucent",
"alpha": 0.9,
"fluid": { "range": 4 }
}
}
```
- `textures` is one texture, or `{ still, flowing }`, like Minecraft's water. `still` is on its bottom and on a level
top. `flowing` is on its sides and on a sloping top, turned to run downhill. A block shows as many of `flowing`'s
pixels across as `still` has, so a 32×32 `flowing` next to a 16×16 `still` spreads over two blocks. With one texture,
it's all of that texture on every side. Either can be [animated](../resources/textures.md#animated-textures).
- `fluid.range` is how many blocks it flows from a source, 1 to 7, and `7` when not set.
- `fluid.ticks` is how many ticks each step of its flow takes, `5` when not set, like Minecraft's water. Lava's is 30.
- `fluid.renewable`, `true` when not set: a spot with two sources beside it and something solid or a source under it
becomes a source too, like Minecraft's infinite water.
- It gets a `level` [state](states.md) of its own, after any it lists: `0` is a source, a full block, and `1` up to
`range` as it flows away from one, each lower.
- `collision` defaults to `false` and `replaceable` to `true`. Entities swim in it and falling into it doesn't hurt.
- Where it flows, it pushes entities in it along, like Minecraft's water: towards lower levels of it beside them, over
edges where it pours down, and down where it falls beside a wall. Players aren't pushed while flying.
- It can't have a `model`, `variants`, `parts`, `shape`, `collision_shape` or `item_model`, since the engine draws it.
- `tint` works on the `still` and `flowing` textures.
### Flowing
Fluids flow on the server like Minecraft's water. When a block next to a fluid changes, the cells around it are looked
at again after its `ticks`, and each works out what it should be:
- A source stays a source.
- With the same fluid above, it's falling, and drawn full.
- Otherwise it's one level more than the lowest of the fluid beside it that spreads sideways, and gone once that's past
`range`. Sources spread sideways, and flowing fluid only does when it's on something solid: over air or more of
itself, it falls instead.
So it spreads one block a step, falls off edges and spreads again where it lands, and drains away a step at a time once
its source is gone. It only flows into air, and stops at chunks that aren't loaded. Buckets only pick up sources. Flows
still going when the server stops don't carry on after it starts again until something next to them changes.
`ctx.world.fluid_flow(x, y, z)` says which way the fluid in a block flows, like Minecraft's `FluidState.getFlow`: a
vector 1 long toward the lower fluid beside it, and down where it falls against a wall, or `{ x: 0, y: 0, z: 0 }` where
it's still or there's none. It's the same flow that pushes players and items along, for machines it turns, like a water
wheel.
### Waterlogging
A block that isn't a fluid can hold one, like Minecraft's waterlogged fences. `waterloggable` names the fluid:
```json
"waterloggable": "bworld:water"
```
- It gets a `waterlogged` [state](states.md) of its own, after any it lists: `1` while it holds a source of the fluid.
It uses one of the block's 16 bits.
- Placed into a source of the fluid, it holds it. Placed into flowing fluid or anything else, it doesn't. With
`"waterlogged_only": true` it can only be placed into a source, so it's always placed holding it, like Farmer's
Delight's rice.
- Using the fluid's item on it, like a water bucket, fills it. An item that aims at fluids, like a bucket, uses the
fluid in it instead of the block, so a bucket takes the water back out and the block stays.
- It's a source of the fluid that never drains: the fluid flows out of it, and entities swim and are pushed in it.
Flowing fluid doesn't flow into it.
- Broken, it leaves a source of the fluid behind.
- Raycasts with `fluids` other than `"none"` hit the fluid in it, filling the block, and say the fluid's id.
The base game's wooden fences are waterloggable, and Farmer's Delight's rice is `waterlogged_only`.
## Tinting
`tint` draws a block's textures in a color, for textures painted in greys:
```json
"tint": { "color": "grass", "textures": ["top"] }
```
`color` is `"grass"`, `"foliage"` or `"water"` for the biome's color, blended across biome borders like Minecraft's
grass, leaves and water, 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.
## Raycasts
Server scripts find the first block on a line with `ctx.world.raycast`, like Minecraft's `Level.clip`. The fishing rod's
hook uses it to see where it lands, and with a player's
[`eye_position` and `look_direction`](../entities/players.md#the-player-api) it finds what they're looking at:
```ts
const hit = ctx.world.raycast(from, to, { fluids: "any" });
if (hit && "bworld:fishable" in ctx.blocks.components(hit.id)) {
// hit.x, hit.y, hit.z is the block, hit.face the side it went in by, hit.position the exact point
}
```
It hits what entities bump into, so it goes through plants and other blocks with `"collision": false`. `fluids` is like
Minecraft's `ClipContext.Fluid`:
| `fluids` | Hits |
| ------------------ | ------------------------------------------ |
| `"none"` (default) | No fluids, it goes through them. |
| `"source_only"` | Fluid sources, going through flowing ones. |
| `"any"` | Every fluid. |
A fluid is hit at its surface, which is lower where it flows. It returns `undefined` when nothing is in the way.
`ctx.blocks.components(id)` gives the components a block's json lists and their params, like `ctx.items.components`, and
`ctx.blocks.exists(id)` whether a block exists.