New hooks

This commit is contained in:
2026-10-20 13:22:03 -03:00
parent c7a2392591
commit 0af35d1e17
7 changed files with 253 additions and 19 deletions
+34 -9
View File
@@ -59,21 +59,46 @@ component.
## Handlers
| Handler | Called when | Returns |
| ------------------------------------ | ----------------------------------------------------------------- | -------------------------- |
| `on_create(block, params)` | The block is placed or set, by a player or a script. | |
| `on_break(block, params, player?)` | The block is broken or replaced. `player` when a player broke it. | |
| `on_click(block, params, player)` | A player starts hitting it. | |
| `on_interact(block, params, player)` | A player right clicks it. | `true` if it did something |
| `on_tick(block, params, dt)` | Every tick, 20 times a second, near a player. | |
| `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. | |
| Handler | Called when | Returns |
| --------------------------------------------- | --------------------------------------------------------------------------------------------------- | -------------------------- |
| `on_create(block, params)` | The block is placed or set, by a player or a script. | |
| `on_break(block, params, player?)` | The block is broken or replaced. `player` when a player broke it. | |
| `on_click(block, params, player)` | A player starts hitting it. | |
| `on_interact(block, params, player)` | A player right clicks it. | `true` if it did something |
| `on_tick(block, params, dt)` | Every tick, 20 times a second, near a player. | |
| `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. | |
| `on_neighbor_change(block, params, neighbor)` | A block on one of its 6 sides is placed, broken or changes its states. See [Changes](#changes). | |
| `on_state_change(block, params, before)` | Its own states change without it being replaced, by `set_state` or a rule. See [Changes](#changes). | |
`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.
### Changes
`on_neighbor_change` is the server's side of the [rule](#neighbors) with the same name, like NeoForge's
`neighborChanged`: `neighbor` is `{ x, y, z, id }` of the block that changed. It runs after the block's rules have had
their say, so it sees the states they set, and it can do what rules can't, like reading tile data or breaking the block
when what it hangs from is gone. A block whose states follow from its neighbors, like a fence, uses the rule instead, so
clients see it change right away.
`on_state_change` runs when the block's states change and it stays the same block: a script's `ctx.world.set_state`, a
rule, a liquid's level. `before` is every state as it was, by name, and `ctx.world.get_state` gives what they are now.
It's how a block reacts to a state something else sets on it, like a machine another mod's script turns off. It runs
right away, before the blocks beside it are told, like `on_create`. Replacing the block with `set_block` runs `on_break`
and `on_create` instead, and setting a state to what it already is runs nothing.
```ts
ctx.components.register_block("copper_tools:bell", {
on_state_change(block, _params, before) {
const rung = ctx.world.get_state(block.x, block.y, block.z, "rung");
if (rung === 1 && before.rung === 0) ring(block);
},
});
```
## 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
+1 -1
View File
@@ -31,7 +31,7 @@ ctx.world.set_state(x, y, z, "age", age + 1);
`get_state` is `undefined` where there's no block, and `set_state` does nothing there and returns `false`. Both throw
for a state the block doesn't have, and `set_state` for a value that doesn't fit its bits. Changes are synced to
everyone who can see the block. Unlike `set_block`, it doesn't run `on_break` or `on_create`, and the block keeps its
tile data.
tile data. It runs the block's [`on_state_change`](components.md#changes) instead.
## Variants