Events and Scheduling
Define an app function named __on_<event> to register it automatically. The example logs a player's name and UUID 20 ticks after they connect.
function __config() {
return {scope: "global"};
}
function __on_player_connects(player) {
const name = mc.entities.query(player, "name");
const uuid = mc.entities.query(player, "uuid");
mc.server.schedule(20, () => console.log("Player joined", name, uuid));
}
Save it in the world's scripts/ folder and run /script load join_log. The delay is in server ticks. At 20 TPS, 20 ticks is about one second. Arguments can follow the callback in mc.server.schedule(delay, callback, ...args). Scheduled work is removed when its app unloads.
Register or remove a callback at runtime
| Function | Effect |
|---|---|
mc.events.on(name, callback) | Registers a function for the named event. Declare the event's callback arguments in order. |
mc.events.exists(name) | Checks whether an event name is known. |
mc.events.off(name) | Removes this app's callbacks for the named event. |
For cancellable events such as player_breaks_block and explosion, return the exact string "cancel" to stop the action. This example prevents players from breaking diamond blocks:
function __on_player_breaks_block(player, block) {
if (String(block) === "diamond_block") return "cancel";
}
Common built-in events
| Name | Callback arguments in order | Use |
|---|---|---|
server_starts, server_shuts_down | None | Server or single-player world lifecycle. |
tick | None | Normal server ticks. |
player_connects | player | A player joins. |
player_disconnects | player, reason | A player leaves. |
player_dies, player_respawns | player | Player death or the start of respawn. |
player_changes_dimension | player, fromPos, fromDimension, toPos, toDimension | Dimension and position information from player teleportation. toPos may be null. |
player_message | player, message | Player chat. Supports cancellation. |
player_breaks_block | player, block | A player attempts to break a block. Supports cancellation. |
player_attacks_entity | player, target | A player attacks an entity. Supports cancellation. |
explosion | center, power, source, attacker, blockInteraction, fire | Before an explosion. Supports cancellation. |
chunk_unloaded | x, z | Chunk unload. Coordinates are the chunk's starting block coordinates. |
Use __on_start() for initialization when the app instance starts, including after it is loaded manually. server_starts follows the server lifecycle.
Watch one entity
mc.entities.entity_event(entity, event, callback) attaches a callback to an entity handle. This function logs the entity type and damage source when the watched entity dies.
function watch(entity) {
mc.entities.entity_event(entity, "on_death", (deadEntity, cause) => {
console.log(mc.entities.query(deadEntity, "type"), cause);
});
}
Common entity-level names include on_tick, on_removed, on_death, on_damaged, and on_move. All pass the entity first. on_death then passes the damage-source name, on_damaged passes the damage amount, source name, and attacker, and on_move passes the previous motion and the old and new positions. Pass null as the callback to remove this app's handler for that entity and event.