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

Properties

This event fires for each BlockLocation destroyed by an explosion. It is fired after the blocks have already been destroyed.

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

world.afterEvents.blockExplode.subscribe((event: BlockExplodeAfterEvent) => {
console.log("Block:", event.block);
console.log("Dimension:", event.dimension);
console.log("Exploded Block Permutation:", event.explodedBlockPermutation);
console.log("Source:", event.source);

// set block back
event.block.setPermutation(event.explodedBlockPermutation);
});

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 effect, like poisoning, is added to an entity.

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

const effectAddSubscription = world.afterEvents.effectAdd.subscribe(
(event) => {
console.log("Effect:", event.effect);
console.log("Entity:", event.entity);

// Your custom handling for the effect added event
// Example: Notify players, update UI, etc.
},
{
// Optionally provide EntityEventOptions to filter entities or entity types
entities: [
/* Array of specific Entity instances */
],
entityTypes: ["minecraft:creeper", "minecraft:player"], // Array of entity type IDs
}
);

// Later, you can unsubscribe when needed
world.afterEvents.effectAdd.unsubscribe(effectAddSubscription);

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: [
/* Array of specific Entity instances */
],
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.

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

// Subscribe to the EntityLoadAfterEvent
const entityLoadSubscription = world.afterEvents.entityLoad.subscribe(
(event) => {
// Handle the entity load event
world.sendMessage(`Entity loaded: ${event.entity.typeId}`);
// Unsubscribe so the message doesn't appear after fired
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 is fired after an explosion occurs.

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

// Subscribe to the ExplosionAfterEvent
const explosionSubscription = world.afterEvents.explosion.subscribe((event) => {
console.log(`Explosion occurred in dimension ${event.dimension.id}`);

if (event.source) {
console.log(`Explosion source: ${event.source.typeId}`);
} else {
console.log(`Explosion source: None`);
}

const impactedBlocks = event.getImpactedBlocks();
console.log(`Impacted blocks: ${JSON.stringify(impactedBlocks)}`);
});

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

This event fires when a world.gameRules property has changed.

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 when a piston expands or retracts.

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.

playerInputPermissionCategoryChange: PlayerInputPermissionCategoryChangeAfterEventSignal

This event fires when a players input permissions change.

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.

This event will be triggered when the weather changes within Minecraft.

This event fires when the script environment is initialized on a World.