Edit

Share via


ChatSendBeforeEventSignal Class

Caution

This class is still in pre-release. Its signature may change or it may be removed in future releases.

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

Methods

subscribe

subscribe(callback: (arg0: ChatSendBeforeEvent) => void): (arg0: ChatSendBeforeEvent) => void

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

Parameters

Returns (arg0: ChatSendBeforeEvent) => void

Notes:

  • This function can't be called in read-only mode.
  • This function can be called in early-execution mode.

unsubscribe

unsubscribe(callback: (arg0: ChatSendBeforeEvent) => void): void

Removes a callback from being called before new chat messages are sent.

Parameters

Notes:

  • This function can't be called in read-only mode.
  • This function can be called in early-execution mode.

Examples

customCommand.ts
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;
        }
      }
    }
  });
}

(preview) Work with this sample on the MCTools.dev code sandbox.