Files
bworld/docs/modding/blocks/tiledata.md
T
2026-11-01 00:49:05 -03:00

4.3 KiB

Tile Data

Tile data is extra data kept for one block in the world, like what a chest holds or how far along a furnace is. It's bworld's version of Minecraft's block entities: a block state is a few bits every client knows, while tile data is any JSON value that only the server knows, apart from the fields it shares with clients.

Reading and Writing

A component's handlers get the block's data as block.data. It's undefined until a handler sets it, and it can be any JSON value:

ctx.components.register_block("copper_tools:counter", {
	on_create(block) {
		block.data = { clicks: 0 };
	},
	on_interact(block, _params, player) {
		block.data.clicks += 1;
		player.send_message(`Clicked ${block.data.clicks} times`);
		return true;
	},
});

Outside a handler, ctx.world.get_block_data<T>(x, y, z) reads it. To change another block's data, like a shaft passing its speed to the next one, ctx.world.get_block_ref(x, y, z) gives that block the way its own handlers get it, like Minecraft's getBlockEntity, or undefined for air.

  • Tile data is saved with the world, and goes away when the block is broken or replaced.
  • It's never sent to clients. To show it to a player, give it client data, open a menu, or change a block state that the block's variants show.
  • Only blocks with tile data run on_tick and on_second, unless they turned it off. on_new_day runs for blocks without it too, but only near players, see Ticking.

Blocks With Containers

A block that holds items keeps a container and stores its id in tile data. Containers are saved until they're deleted, so the block deletes it when it's broken, after dropping what's inside:

ctx.components.register_block("copper_tools:crate", {
	on_create(block) {
		block.data = { container: ctx.containers.create(9).id };
	},
	on_break(block) {
		const container = ctx.containers.get(block.data.container);
		if (!container) return;
		for (let slot = 0; slot < container.size; slot++) {
			const item = container.get(slot);
			if (item) ctx.world.drop_item(block.x, block.y, block.z, item);
		}
		ctx.containers.delete(container.id);
	},
});

The base game's bworld:storage does exactly this, see mods/bworld/scripts/server.ts.

Client Data

Client data is the part of a block's data its clients see too, like how fast a shaft turns. It's kept by field, each any JSON value, and set apart from block.data:

ctx.components.register_block("copper_tools:shaft", {
	on_create(block) {
		block.set_client_data("speed", 0);
		block.set_client_data("axis", "y");
	},
	on_interact(block) {
		block.set_client_data("speed", block.get_client_data<number>("speed")! + 16);
		return true;
	},
});
  • Players get every field with the block's chunk, then each field again when it's set to something else. A field that changes often doesn't resend the others, and setting a field to what it already is sends nothing.
  • undefined removes a field. All of them go when the block is broken or replaced.
  • It's saved with the world. get_client_data and set_client_data copy the value, so change it by setting it again.
  • Client scripts read it with ctx.world.get_client_data<T>(x, y, z, field), see GUI. A block's visual reads it with block.get_client_data, and its update is called when a field changes.

Like NeoForge's getUpdateTag, but per field. Keep it to what clients need to draw the block: everything in it is sent to every player near it.

Showing an Item

A block with item_display draws one item lying on top of it, like the food on cutting board:

"item_display": { "surface": 1 }

surface is the height in pixels the item lies at, 0 to 16. The server script sets the item:

ctx.world.set_block_item(x, y, z, { id: "bworld:cabbage", count: 1 });
const shown = ctx.world.get_block_item(x, y, z); // a copy, or undefined
ctx.world.set_block_item(x, y, z, undefined); // shows nothing

The item is the block's item client data, so it's synced and saved the same way. It turns with the block's y variant, and disappears with the block without dropping, so drop it in on_break if it should.