Send block data to clients
This commit is contained in:
@@ -69,8 +69,9 @@ component.
|
||||
| `on_second(block, params, dt)` | Every second, near a player. | |
|
||||
| `on_new_day(block, params, day)` | Every morning at 06:00. See [Ticking](#ticking) for where. | |
|
||||
|
||||
`block` is `{ id, x, y, z, biome, season, data, set_ticking }`, where `data` is the block's [tile data](tiledata.md),
|
||||
`biome` and `season` are its column's (see [Seasons](../worldgen/biomes.md#seasons)) and `set_ticking` turns its
|
||||
`block` is `{ id, x, y, z, biome, season, data, get_client_data, set_client_data, set_ticking }`, where `data` is the
|
||||
block's [tile data](tiledata.md) and the client data functions its [part clients see](tiledata.md#client-data), `biome`
|
||||
and `season` are its column's (see [Seasons](../worldgen/biomes.md#seasons)) and `set_ticking` turns its
|
||||
[ticking](#ticking) off and on. `params` is what the block's JSON lists for this component, and `dt` is in seconds.
|
||||
|
||||
## Rules
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
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.
|
||||
data is any JSON value that only the server knows, apart from the fields it [shares with clients](#client-data).
|
||||
|
||||
## Reading and Writing
|
||||
|
||||
@@ -25,8 +25,8 @@ ctx.components.register_block("copper_tools:counter", {
|
||||
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 [menu](../inventories/menus.md), or change a block
|
||||
state that the block's `variants` show.
|
||||
- It's **never sent to clients**. To show it to a player, give it [client data](#client-data), open a
|
||||
[menu](../inventories/menus.md), 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](components.md#ticking).
|
||||
|
||||
@@ -54,6 +54,33 @@ ctx.components.register_block("copper_tools:crate", {
|
||||
|
||||
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`:
|
||||
|
||||
```ts
|
||||
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](../gui/index.md).
|
||||
|
||||
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 Farmer's Delight's cutting board:
|
||||
@@ -70,5 +97,5 @@ 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.
|
||||
The item is the block's `item` [client data](#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.
|
||||
|
||||
Reference in New Issue
Block a user