类 WorldBeforeEvents实验性

表示一系列触发于实际动作发生之前的事件。通常来说,即将触发的事件可被修改或取消。 但请注意,在 before 类事件过程中,能够改变游戏状态的 API 将失效,试图调用将会抛出错误。 (例如:dimension.spawnEntity)

A set of events that fire before an actual action occurs. In most cases, you can potentially cancel or modify the impending event. Note that in before events any APIs that modify gameplay state will not function and will throw an error. (e.g., dimension.spawnEntity)

属性

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

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

function customCommand(targetLocation: DimensionLocation) {
const chatCallback = world.beforeEvents.chatSend.subscribe((eventData) => {
if (eventData.message.includes("cancel")) {
// Cancel event if the message contains "cancel"
eventData.cancel = true;
} else {
const args = eventData.message.split(" ");

if (args.length > 0) {
switch (args[0].toLowerCase()) {
case "echo":
// Send a modified version of chat message
world.sendMessage(`Echo '${eventData.message.substring(4).trim()}'`);
break;
case "help":
world.sendMessage(`Available commands: echo <message>`);
break;
}
}
}
});
}

This event is triggered after an event has been added to an entity.

Fires before an entity is removed from the world (for example, unloaded or removed after being killed.)

This event is fired after an explosion occurs.

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.

This event fires before a block is broken by a player.

Fires before a player interacts with a block.

Fires before a player interacts with an entity.

Fires when a player leaves the game.

This event fires before a block is placed by a player.

This event fires immediately when the script environment is initialized on a World. Not all script functionality may be available. For guaranteed access to world state, use the world initialize after event.