Predict interactions with blocks
This commit is contained in:
@@ -75,10 +75,10 @@ this component, and `dt` is in seconds.
|
||||
|
||||
## Rules
|
||||
|
||||
A component's rules decide a block's states from where it's placed and what's around it. They're registered in the mod's
|
||||
[shared script](../concepts/sides.md#shared-scripts) instead, under the same id, because clients run them too: that's
|
||||
how a player sees a fence join its neighbors the moment they place it, instead of a moment later when the server
|
||||
answers. A component can have rules, handlers, or both, and a block can list one that only has rules.
|
||||
A component's rules decide a block's states from where it's placed, what's around it and being right clicked. They're
|
||||
registered in the mod's [shared script](../concepts/sides.md#shared-scripts) instead, under the same id, because clients
|
||||
run them too: that's how a player sees a fence join its neighbors the moment they place it, instead of a moment later
|
||||
when the server answers. A component can have rules, handlers, or both, and a block can list one that only has rules.
|
||||
|
||||
```ts
|
||||
import type { SharedContext } from "bworld/shared";
|
||||
@@ -87,14 +87,16 @@ export function setup(ctx: SharedContext) {
|
||||
ctx.components.register_block("copper_tools:pipe", {
|
||||
state_for_placement(placement, params) {/* ... */},
|
||||
on_neighbor_change(block, params, neighbor) {/* ... */},
|
||||
on_interact(block, params, interaction) {/* ... */},
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
| Rule | Called when | Returns |
|
||||
| --------------------------------------------- | ---------------------------------------------------------------------- | ----------------------- |
|
||||
| `state_for_placement(placement, params)` | A player is about to place it. | States to place it with |
|
||||
| `on_neighbor_change(block, params, neighbor)` | A block on one of its 6 sides is placed, broken or changes its states. | |
|
||||
| Rule | Called when | Returns |
|
||||
| --------------------------------------------- | ---------------------------------------------------------------------- | -------------------------- |
|
||||
| `state_for_placement(placement, params)` | A player is about to place it. | States to place it with |
|
||||
| `on_neighbor_change(block, params, neighbor)` | A block on one of its 6 sides is placed, broken or changes its states. | |
|
||||
| `on_interact(block, params, interaction)` | A player right clicks it. | `true` if it did something |
|
||||
|
||||
Here `block` is `{ id, x, y, z }`, without tile data, which clients never have. `ctx.world` in a shared script has
|
||||
`get_block`, `get_state`, `set_state`, `is_face_sturdy`, `biome_at` and `season_at` (see
|
||||
@@ -133,8 +135,22 @@ after. Both are in `mods/bworld/scripts/shared.ts`.
|
||||
|
||||
### Interacting
|
||||
|
||||
When a player right clicks a block, every component's `on_interact` runs. If any of them returns `true`, the click was
|
||||
handled and nothing else happens. Otherwise the held item is used or placed, as if the block had no components.
|
||||
When a player right clicks a block, every component's `on_interact` runs: the rules' first, then the handlers'. If any
|
||||
of them returns `true`, the click was handled and nothing else happens. Otherwise the held item is used or placed, as if
|
||||
the block had no components.
|
||||
|
||||
A rule's `on_interact` also runs on the clicking player's client, so they see what it does right away instead of waiting
|
||||
for the server, like a trapdoor opening. It's for clicks that only change states with `ctx.world.set_state`, like
|
||||
Minecraft's `useWithoutItem`: anything that needs the server, like opening a screen, giving items or tile data, goes in
|
||||
the handler. `interaction` is `{ face, hit, facing }`: the face that was clicked, where on it (from 0 to 1 on each
|
||||
axis), and the way the player looks along the ground. The trapdoor's is in `mods/bworld/scripts/shared.ts`:
|
||||
|
||||
```ts
|
||||
on_interact(block) {
|
||||
const open = ctx.world.get_state(block.x, block.y, block.z, "open") ?? 0;
|
||||
return ctx.world.set_state(block.x, block.y, block.z, "open", open ? 0 : 1);
|
||||
},
|
||||
```
|
||||
|
||||
Give a block `"interactive": true` when right clicking it always does something, like opening a screen. Otherwise
|
||||
clients guess that the click places the held block and show it for a moment before the server corrects them.
|
||||
@@ -155,19 +171,19 @@ catch up. If it's further behind than that, it skips the rest and logs a warning
|
||||
|
||||
`mods/bworld/scripts/server.ts` registers these, and any mod that depends on `bworld` can use them:
|
||||
|
||||
| Component | Params | What it does |
|
||||
| ----------------------- | --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `bworld:storage` | `rows` | A chest: `rows` × 9 slots, dropped when it's broken. |
|
||||
| `bworld:shipping_bin` | `rows` | Like `bworld:storage`, and every morning at 06:00 sells what's in it with a `base_sell_value` for coins. |
|
||||
| `bworld:furnace` | none | Smelts with `furnace` and `fuel` [recipes](../resources/recipes.md). Needs `lit` and `facing` states, see `blocks/furnace.json`. |
|
||||
| `bworld:smithing_table` | none | Upgrades tools with `smithing` recipes. |
|
||||
| `bworld:hoeable` | `tool`, `into` | Right clicking with a tool of type `tool` turns it into `into`, like a hoe on grass. |
|
||||
| `bworld:farmland` | none | Watered with the watering can, dries out every morning. |
|
||||
| `bworld:crop` | see [Crops](crops.md) | Grows a stage every morning its farmland was watered. |
|
||||
| `bworld:crop_top` | see [Crops](crops.md) | The top half of a two block crop, like rice. |
|
||||
| `bworld:water_source` | none | Refills the watering can. |
|
||||
| `bworld:fishable` | none | The fishing rod's hook floats on it and fish bite there when it lands in it. |
|
||||
| `bworld:trapdoor` | none | Opens and closes when right clicked, and is placed like Minecraft's (its rules are in the shared script). Needs `facing` (2 bits), `half` and `open` states, see `blocks/oak_trapdoor.json`. |
|
||||
| `bworld:fence` | `connects_to` | Joins the blocks in the tag `connects_to` and sturdy sides of other blocks, except leaves. Needs the `north`, `east`, `south` and `west` states. |
|
||||
| Component | Params | What it does |
|
||||
| ----------------------- | --------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `bworld:storage` | `rows` | A chest: `rows` × 9 slots, dropped when it's broken. |
|
||||
| `bworld:shipping_bin` | `rows` | Like `bworld:storage`, and every morning at 06:00 sells what's in it with a `base_sell_value` for coins. |
|
||||
| `bworld:furnace` | none | Smelts with `furnace` and `fuel` [recipes](../resources/recipes.md). Needs `lit` and `facing` states, see `blocks/furnace.json`. |
|
||||
| `bworld:smithing_table` | none | Upgrades tools with `smithing` recipes. |
|
||||
| `bworld:hoeable` | `tool`, `into` | Right clicking with a tool of type `tool` turns it into `into`, like a hoe on grass. |
|
||||
| `bworld:farmland` | none | Watered with the watering can, dries out every morning. |
|
||||
| `bworld:crop` | see [Crops](crops.md) | Grows a stage every morning its farmland was watered. |
|
||||
| `bworld:crop_top` | see [Crops](crops.md) | The top half of a two block crop, like rice. |
|
||||
| `bworld:water_source` | none | Refills the watering can. |
|
||||
| `bworld:fishable` | none | The fishing rod's hook floats on it and fish bite there when it lands in it. |
|
||||
| `bworld:trapdoor` | none | Opens and closes when right clicked, and is placed like Minecraft's (both are rules, in the shared script). Needs `facing` (2 bits), `half` and `open` states, see `blocks/oak_trapdoor.json`. |
|
||||
| `bworld:fence` | `connects_to` | Joins the blocks in the tag `connects_to` and sturdy sides of other blocks, except leaves. Needs the `north`, `east`, `south` and `west` states. |
|
||||
|
||||
Item components work the same way, see [Items](../items/index.md#item-components).
|
||||
|
||||
Reference in New Issue
Block a user