Class ServerMessageDecoratorEvent

java.lang.Object
net.fabricmc.fabric.api.message.v1.ServerMessageDecoratorEvent

public final class ServerMessageDecoratorEvent extends Object
A class for registering a MessageDecorator. Check the message decorator documentation for how message decorators work. Unlike other events, this uses a functional interface that is provided by the vanilla game.

This event uses phases to provide better mod compatibilities between mods that add custom content and styling. Message decorators with the styling phase will always apply after the ones with the content phase. When registering the message decorator, it is recommended to choose one of the phases from this interface and pass that to the Event.register(Identifier, Object) function. If not given, the message decorator will run in the default phase, which is between the content phase and the styling phase.

The message decorator's result is cached (as of 1.19.1) if the chat preview is enabled. If the original message did not change between the last preview and submission, the decorator is not called during submission and the cached preview is used instead. Note that the decorator can still be called during submission if the chat preview is disabled, the sent message was not the same as the previewed message, or if text filtering was enabled and it produced a different message.

Example of registering a content phase message decorator:


 ServerMessageDecoratorEvent.EVENT.register(ServerMessageDecoratorEvent.CONTENT_PHASE, (sender, message) -> {
     // Add smiley face. Has to copy() to get a MutableText with siblings and styles.
     return message.copy().append(" :)");
 });
 

Example of registering a styling phase message decorator:


 ServerMessageDecoratorEvent.EVENT.register(ServerMessageDecoratorEvent.STYLING_PHASE, (sender, message) -> {
     // Apply orange color to messages sent by server operators
     if (sender != null && sender.server.getPlayerManager().isOperator(sender.getGameProfile())) {
         return CompletableFuture.completedFuture(
             message.copy().styled(style -> style.withColor(0xFFA500)));
     }
     return CompletableFuture.completedFuture(message);
 });
 
  • Field Details

    • CONTENT_PHASE

      public static final Identifier CONTENT_PHASE
      The content phase of the event, passed when registering a message decorator. Use this when the decorator modifies the text content of the message.
    • STYLING_PHASE

      public static final Identifier STYLING_PHASE
      The styling phase of the event, passed when registering a message decorator. Use this when the decorator only modifies the styling of the message with the text intact.
    • EVENT

      public static final Event<MessageDecorator> EVENT