Update modding docs
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
# 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](states.md) is a few bits every client knows, while tile
|
||||
data is any JSON value that only the server knows.
|
||||
|
||||
## Reading and Writing
|
||||
|
||||
A [component](components.md)'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:
|
||||
|
||||
```ts
|
||||
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.
|
||||
|
||||
- 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, open a [container screen](../inventories/menus.md), or change
|
||||
a block state that the block's `variants` show.
|
||||
- Only blocks with tile data run `on_tick`, `on_second` and `on_new_day`.
|
||||
|
||||
## Blocks With Containers
|
||||
|
||||
A block that holds items keeps a [container](../inventories/containers.md) 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:
|
||||
|
||||
```ts
|
||||
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`.
|
||||
|
||||
## Showing an Item
|
||||
|
||||
A block with `item_display` draws one item lying on top of it, like the food on Farmer's Delight's cutting board:
|
||||
|
||||
```json
|
||||
"item_display": { "surface": 1 }
|
||||
```
|
||||
|
||||
`surface` is the height in pixels the item lies at, 0 to 16. The server script sets the item:
|
||||
|
||||
```ts
|
||||
ctx.world.set_block_item(x, y, z, { id: "farmersdelight: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
|
||||
```
|
||||
|
||||
Unlike the rest of tile data, the displayed item is synced to players. It's saved with the block, turns with the block's
|
||||
`y` variant, and disappears with the block without dropping, so drop it in `on_break` if it should.
|
||||
Reference in New Issue
Block a user