5.3 KiB
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 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 = trueto stop it. - After events fire once it has happened. They're for reacting, and can't change anything.
import type { ServerContext } from "cubi/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 |
entity_damage |
✓ | ✓ | entity, player, amount, cause, bypasses_invulnerability. See below |
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", "mob_attack", or whatever a mod passed to damage(amount, cause).
entity_damage fires whenever a player or a mob is hurt, like NeoForge's LivingIncomingDamageEvent. entity is a
LivingRef, and player is set when it's a player. Before handlers can
change amount or cancel it. They run before the hurt cooldown, so what they leave is what's compared with the last
hit. bypasses_invulnerability is set for damage that nothing should stop, like falling out of the world. After fires
only when the entity was hurt, with the amount the before handlers left.
Order
Handlers run in the order they subscribed, and mods' setup functions run in load order. For
block_interact, the before event runs first, then the block's own components' on_interact, rules before handlers,
unless the player is sneaking with an item in hand. 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.