类 ChatSendBeforeEventSignal实验性

管理在发送聊天消息之前触发的事件的回调。

Manages callbacks that are connected to an event that fires before chat messages are sent.

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

方法

  • 实验性

    参数

    返回 (arg0: ChatSendBeforeEvent) => void

    添加一个在发送新聊天消息之前会被调用的回调函数。

    Adds a callback that will be called before new chat messages are sent.

    无法在只读模式下调用此函数,详见 WorldBeforeEvents

    This function can be called in early-execution mode.

  • 实验性

    参数

    返回 void

    移除一个在发送新聊天消息之前会被调用的回调函数。

    Removes a callback from being called before new chat

    • messages are sent. 无法在只读模式下调用此函数,详见 WorldBeforeEvents

    This function can be called in early-execution mode.