Files
bworld/docs/modding/concepts/events.md
T

73 lines
4.7 KiB
Markdown

# Events
Events let a server script react to what happens in the game, and stop some of it from happening. They're the
alternative to [components](../blocks/components.md) when the behavior isn't tied to one block or item: logging chat,
protecting spawn, or reacting to every block broken anywhere.
## Before and After
Every event is a signal you subscribe to. Events come in two kinds:
- **Before events** fire before something happens. A handler can read the event, change some of its fields, and set
`cancel = true` to stop it.
- **After events** fire once it has happened. They're for reacting, and can't change anything.
```ts
import type { ServerContext } from "bworld/server";
export function setup(ctx: ServerContext) {
// nobody breaks blocks within 16 blocks of the world's center
ctx.events.before.block_break.subscribe((event) => {
const { x, z } = event.block;
if (Math.abs(x) < 16 && Math.abs(z) < 16) {
event.cancel = true;
event.player.send_message("Spawn is protected");
}
});
ctx.events.after.player_join.subscribe(({ player }) => {
player.send_message(`Welcome, ${player.name}!`);
});
}
```
`subscribe` returns a function that unsubscribes the handler.
When a before event cancels a player's break or place, the server tells that player's client to undo what it already
showed. These events fire for **every** player's actions, since they all go through the server.
> [!TIP]
> Change the world in an after event, not a before one: other handlers may still cancel the action. If a before event
> needs something to happen afterwards, schedule it with `ctx.system.run_timeout(fn, 0)`.
## The Events
| Event | Before | After | Fields |
| ---------------- | ------ | ----- | ------------------------------------------------------------------------------------------------------------------------------------- |
| `block_break` | ✓ | ✓ | `player`, `block` (with its tile `data`), `item` held |
| `block_place` | ✓ | ✓ | `player`, `block` (`{ id, x, y, z }`), `face` clicked, `hit` where on it (0-1 each axis), `item` held |
| `block_interact` | ✓ | ✓ | `player`, `block`, `item` held. Before fires for every right click on a block, after only when a component's `on_interact` handled it |
| `chat_send` | ✓ | ✓ | `player`, `message`. Before handlers can change `message` |
| `player_damage` | ✓ | ✓ | `player`, `amount`, `cause`. Before handlers can change `amount`, after fires only when they were hurt, with what they took |
| `player_death` | | ✓ | `player`, `cause`. They've dropped what they carried and respawn right after |
| `player_join` | | ✓ | `player` |
| `player_leave` | | ✓ | `player` |
| `server_start` | | ✓ | none. Every mod's `setup` has run |
| `tick` | | ✓ | `dt`, in seconds. 20 times a second |
| `new_day` | | ✓ | `day`. At 06:00, after every block's `on_new_day` |
`cause` is `"fall"`, `"void"`, `"starve"`, or whatever a mod passed to `player.damage(amount, cause)`.
### 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`, 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
A handler that throws is logged under its mod's id (`[copper_tools] an event handler threw: ...`) and skipped. The other
handlers and the action itself go on as if it hadn't been there. Component handlers, commands and timers are guarded the
same way, so one broken mod doesn't stop the server.