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).
|
||||
|
||||
@@ -61,8 +61,9 @@ showed. These events fire for **every** player's actions, since they all go thro
|
||||
### Order
|
||||
|
||||
Handlers run in the order they subscribed, and mods' `setup` functions run in [load order](registries.md#loading). For
|
||||
`block_interact`, the before event runs first, then the block's own components' `on_interact`. If no component handled
|
||||
the click and the held item has no `on_use`, the held block is placed, which fires `block_place`.
|
||||
`block_interact`, the before event runs first, then the block's own components' `on_interact`, rules before handlers. If
|
||||
no component handled the click and the held item has no `on_use`, the held block is placed, which fires `block_place`.
|
||||
Cancelling it undoes what the clicking player's client guessed, like a trapdoor it showed opening.
|
||||
|
||||
## Errors
|
||||
|
||||
|
||||
@@ -9,11 +9,13 @@ Clients send what the player is _trying_ to do: "break the block at x, y, z", "u
|
||||
slot 3". The server checks it (reach, the item being held, container rules), runs mod logic, and sends back what
|
||||
happened.
|
||||
|
||||
For responsiveness, the client guesses the result of breaking and placing blocks and shows it right away, with the
|
||||
states the server will give them: it runs the same [shared scripts](#shared-scripts) the server does. When the server
|
||||
disagrees, like when a before event cancels a placement, it sends that player the real block and the guess is undone.
|
||||
Blocks with `"interactive": true` tell the client not to guess, since right clicking them does something other than
|
||||
placing.
|
||||
For responsiveness, the client guesses the result of breaking, placing and right clicking blocks and shows it right
|
||||
away, with the states the server will give them: it runs the same [shared scripts](#shared-scripts) the server does.
|
||||
When the server disagrees, like when a before event cancels a placement, it sends that player the real block and the
|
||||
guess is undone. Like Minecraft, each click is numbered and the server says when it's done with it: until then the
|
||||
client keeps showing its guess, so the server's answer to an earlier click can't flicker a block a newer one changed,
|
||||
like a trapdoor clicked twice. Blocks with `"interactive": true` tell the client not to guess a placement, since right
|
||||
clicking them does something else.
|
||||
|
||||
```
|
||||
server process browser
|
||||
@@ -85,19 +87,21 @@ export function setup(ctx: SharedContext) {
|
||||
ctx.components.register_block("copper_tools:pipe", {
|
||||
state_for_placement(placement) {/* ... */},
|
||||
on_neighbor_change(block, params, neighbor) {/* ... */},
|
||||
on_interact(block, params, interaction) {/* ... */},
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
Shared scripts hold a block component's rules, the code Minecraft runs on both sides: what states a block is placed with
|
||||
(`state_for_placement`) and how it changes with the blocks beside it (`on_neighbor_change`). See
|
||||
[Block Components](../blocks/components.md#placing). The server script registers the rest of the same component, like
|
||||
its `on_interact`, under the same id.
|
||||
(`state_for_placement`), how it changes with the blocks beside it (`on_neighbor_change`) and what right clicking it
|
||||
changes (`on_interact`, like a trapdoor opening). See [Block Components](../blocks/components.md#rules). The server
|
||||
script registers the rest of the same component, like its `on_break` or an `on_interact` that opens a screen, under the
|
||||
same id.
|
||||
|
||||
On the server they run on the real world. On a client they run on what that client shows, when its player places or
|
||||
breaks a block, so a fence joins its neighbors the moment it's placed. What they change there is a guess, and the
|
||||
server's answer replaces it a moment later, so a shared script that gives a different answer on a client only costs a
|
||||
flicker. `ctx.side` says which one it's running on.
|
||||
On the server they run on the real world. On a client they run on what that client shows, when its player places, breaks
|
||||
or right clicks a block, so a fence joins its neighbors the moment it's placed. What they change there is a guess, and
|
||||
the server's answer replaces it a moment later, so a shared script that gives a different answer on a client only costs
|
||||
a flicker. `ctx.side` says which one it's running on.
|
||||
|
||||
`setup` runs before the server or client script on each side, for every mod first, so their scripts can count on any
|
||||
mod's rules being there. Rules can only be registered during `setup`, and `ctx.world` only works in them.
|
||||
|
||||
Reference in New Issue
Block a user