Skip to main content

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.

scripts/join_log.js
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​

FunctionEffect
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​

NameCallback arguments in orderUse
server_starts, server_shuts_downNoneServer or single-player world lifecycle.
tickNoneNormal server ticks.
player_connectsplayerA player joins.
player_disconnectsplayer, reasonA player leaves.
player_dies, player_respawnsplayerPlayer death or the start of respawn.
player_changes_dimensionplayer, fromPos, fromDimension, toPos, toDimensionDimension and position information from player teleportation. toPos may be null.
player_messageplayer, messagePlayer chat. Supports cancellation.
player_breaks_blockplayer, blockA player attempts to break a block. Supports cancellation.
player_attacks_entityplayer, targetA player attacks an entity. Supports cancellation.
explosioncenter, power, source, attacker, blockInteraction, fireBefore an explosion. Supports cancellation.
chunk_unloadedx, zChunk 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.