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.

When implementing a message decorator, it is very important that the decorator be pure; i.e. return the same text when called multiple times for the same arguments (message and sender) - otherwise the server detects a mismatch between the preview and the actual message, and discards the message because it was improperly signed.

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 invalid input: '&'invalid input: '&' 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 net.minecraft.util.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 net.minecraft.util.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<net.minecraft.network.message.MessageDecorator> EVENT