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

14 KiB

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, which server scripts register.

A Basic Block

blocks/copper_block.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.
variants none Other models, textures or rotations for some states.
parts none Models drawn together depending on states, like a fence's post and bars. See Parts.
item_model none The model its item is drawn with. See 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.
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 liquids default to it.
shape its model The boxes players point at and entities bump into, in pixels. See Shapes.
collision_shape shape What entities bump into when it's different, up to 24 pixels high. See Collision Shapes.
mining.toughness unbreakable Seconds to break by hand. Without mining, a block can't be broken.
mining.tool none The tool type 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. To give that item more, see Block Items.
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. Liquids default to true.
place_in see meaning Block ids or tags 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 it can stand on, like ["bworld:hoed_dirt"] for crops or ["#bworld:logs"]. See below.
states none Named state bits, like a crop's age.
components none Components and their parameters.
item_display none Shows an item on top of it, see Tile Data.
liquid none Makes it a liquid, like water. See Liquids.

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 of its shape.

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.

Liquids

A block with liquid is a liquid, like water. The engine draws it like Luanti's liquids: each corner of its surface is as high as the liquid 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:

{
	"format_version": 1,
	"block": {
		"id": "oily:oil",
		"textures": { "still": "oily:oil_still", "flowing": "oily:oil_flowing" },
		"render_layer": "translucent",
		"alpha": 0.9,
		"liquid": { "range": 4 }
	}
}
  • textures is one texture, or { still, flowing }: still on its top and bottom, flowing on its sides, like Luanti's special tiles.
  • liquid.range is how many blocks it flows from a source, 1 to 7, and 7 when not set.
  • liquid.ticks is how many ticks each step of its flow takes, 5 when not set, like Minecraft's water. Lava's is 30.
  • liquid.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 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.
  • 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

Liquids flow on the server like Minecraft's water. When a block next to a liquid 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 liquid above, it's falling, and drawn full.
  • Otherwise it's one level more than the lowest of the liquid beside it that spreads sideways, and gone once that's past range. Sources spread sideways, and flowing liquid 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.

Tinting

tint draws a block's textures in a color, for textures painted in greys:

"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 it finds what they're looking at:

const hit = ctx.world.raycast(from, to, { liquids: "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. liquids is like Minecraft's ClipContext.Fluid:

liquids Hits
"none" (default) No liquids, it goes through them.
"source_only" Liquid sources, going through flowing ones.
"any" Every liquid.

A liquid 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.