Predict interactions with blocks

This commit is contained in:
2026-10-10 14:21:27 -03:00
parent 7fe9c15b67
commit 79ff6f9ad5
15 changed files with 355 additions and 73 deletions
+3 -2
View File
@@ -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
+16 -12
View File
@@ -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.