Contains a set of events that are available across the scope of the World.

Properties

This event fires when a button is pushed.

import { world } from "@minecraft/server";

world.afterEvents.buttonPush.subscribe((event) => {
console.log("Button: ", event.block.typeId);
console.log("Dimension: ", event.dimension.id);
console.log("Source: ", event.source.typeId);
});

This event is fired when an entity event has been triggered that will update the component definition state of an entity.

This event fires when an entity dies.

import { world } from "@minecraft/server";

world.afterEvents.entityDie.subscribe((event) => {
world.sendMessage(
`${event.deadEntity.typeId} died from ${event.damageSource}!`
);
});

This event fires when entity health changes in any degree.

import { world } from "@minecraft/server";

const healthChangedSubscription =
world.afterEvents.entityHealthChanged.subscribe(
(event) => {
console.log("Entity:", event.entity);
console.log("Old Health:", event.oldValue);
console.log("New Health:", event.newValue);

// Your custom handling for entity health change event
// Example: Display a message, update UI, etc.
},
{
// Optionally provide EntityEventOptions to filter entities or entity types
entities: [],
entityTypes: ["minecraft:player", "minecraft:zombie"], // Array of entity type IDs
}
);

// Later, you can unsubscribe when needed
world.afterEvents.entityHealthChanged.unsubscribe(healthChangedSubscription);

This event fires when an entity hits (that is, melee attacks) a block.

import { world } from "@minecraft/server";
world.afterEvents.entityHitEntity.subscribe((event) => {
const location1 = event.damagingEntity.location;
const location2 = event.hitEntity.location;

const distance = Math.pow(
Math.pow(location2.x - location1.x, 2) +
Math.pow(location2.y - location1.y, 2) +
Math.pow(location2.z - location1.z, 2),
0.5
);

console.log("Distance: " + distance + " blocks");
});

This event fires when an entity hits (that is, melee attacks) another entity.

import { world } from "@minecraft/server";
world.afterEvents.entityHitBlock.subscribe((event) => {
const location1 = event.damagingEntity.location;
const location2 = event.hitBlock.location;

const distance = Math.pow(
Math.pow(location2.x - location1.x, 2) +
Math.pow(location2.y - location1.y, 2) +
Math.pow(location2.z - location1.z, 2),
0.5
);

console.log("Distance: " + distance + " blocks");
});

This event fires when an entity is hurt (takes damage).

import { Player, world } from "@minecraft/server";
world.afterEvents.entityHurt.subscribe((event) => {
if (event.hurtEntity instanceof Player) {
event.hurtEntity.sendMessage(
"You were hurt from " + event.damageSource.cause + "!"
);
}
});

Fires when an entity is loaded.

// Subscribe to the EntityLoadAfterEvent
const entityLoadSubscription = world.afterEvents.entityLoad.subscribe(
(event) => {
// Handle the entity load event
console.log(`Entity loaded: ${event.entity.typeId}`);
}
);

// ... Later in your code, when you want to unsubscribe
world.afterEvents.entityLoad.unsubscribe(entityLoadSubscription);

Fires when an entity is removed (for example, potentially unloaded, or removed after being killed).

This event fires when an entity is spawned.

This event fires when a chargeable item completes charging.

This event fires when a chargeable item is released from charging.

This event fires when a chargeable item starts charging.

This event fires when a player successfully uses an item or places a block by pressing the Use Item / Place Block button. If multiple blocks are placed, this event will only occur once at the beginning of the block placement. Note: This event cannot be used with Hoe or Axe items.

This event fires when a chargeable item stops charging.

This event fires when a player releases the Use Item / Place Block button after successfully using an item. Note: This event cannot be used with Hoe or Axe items.

This event fires when an item is successfully used by a player.

This event fires when an item is used on a block by a player.

A lever has been pulled.

This event fires for a block that is broken by a player.

import { world } from "@minecraft/server";

world.afterEvents.playerBreakBlock.subscribe((event) => {
const { brokenBlockPermutation, player } = event;

if (brokenBlockPermutation.type.id === "minecraft:grass") {
player.sendMessage("You broke a grass block!");
}

if (brokenBlockPermutation.type.id === "minecraft:stone") {
player.sendMessage("You broke a stone block!");
}
});

Fires when a player moved to a different dimension.

This event fires when a player joins a world. See also playerSpawn for another related event you can trap for when a player is spawned the first time within a world.

import { world } from "@minecraft/server";
world.afterEvents.playerJoin.subscribe(({ playerId, playerName }) => {
world.sendMessage(
`Player ${playerName} (${playerId}) has just joined the world.`
);
});

This event fires when a player leaves a world.

import { world } from "@minecraft/server";
world.afterEvents.playerLeave.subscribe(({ playerId, playerName }) => {
world.sendMessage(
`Player ${playerName} (${playerId}) has just left the world.`
);
});

This event fires for a block that is placed by a player.

This event fires when a player spawns or respawns. Note that an additional flag within this event will tell you whether the player is spawning right after join vs. a respawn.

import { world } from "@minecraft/server";

world.afterEvents.playerSpawn.subscribe((eventData) => {
let { player, initialSpawn } = eventData;
if (!initialSpawn) return;

// This runs when the player joins the game for the first time!
});

A pressure plate has popped back up (i.e., there are no entities on the pressure plate.)

A pressure plate has pushed (at least one entity has moved onto a pressure plate.)

This event fires when a projectile hits a block.

This event fires when a projectile hits an entity.

A target block was hit.

A trip wire was tripped.