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

Hierarchy

  • WorldAfterEvents

Constructors

Properties

Remarks

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

Example

setBlockBack.ts

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);
});

Remarks

This event fires when a button is pushed.

Example

subscribe.js

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);
});

Remarks

This event is triggered after a chat message has been broadcast or sent to players.

Example

subscribe.js

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

const chatObjective =
world.scoreboard.getObjective("chat") ??
world.scoreboard.addObjective("chat", "chat");

world.afterEvents.chatSend.subscribe((event) => {
const { sender } = event;

const score = chatObjective.hasParticipant(sender)
? chatObjective.getScore(sender.scoreboardIdentity)
: 0;
chatObjective.setScore(sender, score + 1);
});

Remarks

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

Remarks

This event fires when an effect, like poisoning, is added to an entity.

Example

effectAdd.js

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: [],
entityTypes: ["minecraft:creeper", "minecraft:player"], // Array of entity type IDs
}
);

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

Remarks

This event fires when an entity dies.

Example

deathMessage.js

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

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

Remarks

This event fires when entity health changes in any degree.

Example

logger.js

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);

Remarks

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

Example

getDistance.js

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");
});

Remarks

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

Example

getDistance.js

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");
});

Remarks

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

Example

entityHurt.js

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 + "!"
);
}
});

Remarks

Fires when an entity is loaded.

Example

inform.ts

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);
}
);

Remarks

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

Remarks

This event fires when an entity is spawned.

Remarks

This event is fired after an explosion occurs.

Example

log.ts

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);

Remarks

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

Remarks

This event fires when a chargeable item completes charging.

Remarks

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

Remarks

This event fires when a chargeable item starts charging.

Remarks

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.

Remarks

This event fires when a chargeable item stops charging.

Remarks

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.

Remarks

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

Remarks

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

Remarks

A lever has been pulled.

Remarks

This event is an internal implementation detail, and is otherwise not currently functional.

Remarks

This event fires when a piston expands or retracts.

Remarks

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

Example

subscribe.js

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!");
}
});

Remarks

Fires when a player moved to a different dimension.

playerInputPermissionCategoryChange: PlayerInputPermissionCategoryChangeAfterEventSignal

Remarks

This event fires when a players input permissions change.

Remarks

An event for when a player interacts with a block.

Remarks

This event fires when a player interacts with an entity.

Remarks

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.

Example

subscribe.js

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

Remarks

This event fires when a player leaves a world.

Example

leaveMessage.js

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

Remarks

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

Remarks

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.

Example

initialSpawn.js

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!
});

Remarks

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

Remarks

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

Remarks

This event fires when a projectile hits a block.

Remarks

This event fires when a projectile hits an entity.

Remarks

A target block was hit.

Remarks

A trip wire was tripped.

Remarks

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

Remarks

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