Class ServerChatEvent

java.lang.Object
net.minecraftforge.eventbus.api.Event
net.minecraftforge.event.ServerChatEvent
Direct Known Subclasses:
ServerChatEvent.Preview, ServerChatEvent.Submitted

public class ServerChatEvent extends net.minecraftforge.eventbus.api.Event
This event is fired whenever a ServerboundChatPreviewPacket or ServerboundChatPacket is received.

Mods that modify chat messages from this event should call ForgeMod.enableServerChatPreview() from their mod constructor to enable the chat preview and allow signing by clients. It is recommended to listen to this event class when modifying chat messages rather than a subclass to ensure clients with Options.onlyShowSecureChat() enabled can see the modified messages.

This event is cancellable, and does not have a result. If the event is cancelled, the message will not be sent to clients.

This event is fired on the main Forge event bus, only on the logical server.

See Also:
  • Field Details

    • player

      private final ServerPlayer player
    • username

      private final String username
    • rawText

      private final String rawText
    • canChangeMessage

      private final boolean canChangeMessage
    • message

      private Component message
  • Constructor Details

    • ServerChatEvent

      @Internal protected ServerChatEvent(ServerPlayer player, String rawText, Component message, boolean canChangeMessage)
  • Method Details

    • getPlayer

      public ServerPlayer getPlayer()
      Returns the player who initiated the chat action.
      Returns:
      the player who initiated the chat action
    • getUsername

      public String getUsername()
      Returns the username of the player who initiated the chat action.
      Returns:
      the username of the player who initiated the chat action
    • getRawText

      public String getRawText()
      Returns the original raw text of the player chat message.
      Returns:
      the original raw text of the player chat message
    • canChangeMessage

      public boolean canChangeMessage()
      Returns whether the message can be changed or not.. In vanilla, this will be false if a ServerboundChatPacket is received with signed decorated content already included by the sending player. Players who submit a chat message with chat previews disabled will cause this to be true.
      Returns:
      whether the message can be changed or not
    • setMessage

      public void setMessage(Component message)
      Set the message to be sent to the relevant clients.

      If canChangeMessage() is false, this call will be ignored.

      It is recommended to listen to ServerChatEvent when modifying chat messages rather than a subclass to ensure clients with Options.onlyShowSecureChat() enabled can see the modified messages.

      See Also:
    • getMessage

      public Component getMessage()
      Returns the message that will be sent to the relevant clients, if the event is not cancelled.
      Returns:
      the message that will be sent to the relevant clients, if the event is not cancelled