How to react to chat messages in a Minecraft mod
The server sees every chat message. Fabric API: ServerMessageEvents.CHAT_MESSAGE gives the signed message, the sender and the chat type. Forge and NeoForge: ServerChatEvent gives the player and the text, and lets you change or cancel the message. To answer one player, send a message to the ServerPlayer. Chat is server-side only; nothing here runs in the client.
Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.19 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.
- Register a chat listener for your loader.
- Read the message text and compare it with your word.
- Send the reply to the player (sendSystemMessage on 1.19 and newer, sendMessage before).
- Join a world and type the word.
What you can change
Section titled “What you can change”The example uses these values. Change them in the code.
| Setting | In the example | What it does |
|---|---|---|
| word | hello |
The word to look for (lower case). |
| reply | Hi there! |
What the server answers. |
For each loader and version
Section titled “For each loader and version”The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.
Fabric 1.19.3 to 1.21.1, Fabric 1.21.2 to 26.3
Section titled “Fabric 1.19.3 to 1.21.1, Fabric 1.21.2 to 26.3”Uses:
ServerMessageEvents.CHAT_MESSAGEServerPlayer.sendSystemMessage
src/main/java/com/example/examplemod/event/ChatReplyChat.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;import net.minecraft.network.chat.Component;
// When a player says "hello" in chat, the server answers them (Fabric API: ServerMessageEvents.CHAT_MESSAGE).public final class ChatReplyChat { private ChatReplyChat() { }
// Called from the mod's onInitialize. public static void register() { ServerMessageEvents.CHAT_MESSAGE.register((message, sender, params) -> { if (message.signedContent().toLowerCase().contains("hello")) { sender.sendSystemMessage(Component.literal("Hi there!")); } }); }}In your main mod class, inside onInitialize:
ChatReplyChat.register();NeoForge 1.20.2 to 1.21.1, NeoForge 1.21.2 to 26.3
Section titled “NeoForge 1.20.2 to 1.21.1, NeoForge 1.21.2 to 26.3”Uses:
ServerChatEvent.getRawTextServerPlayer.sendSystemMessage
src/main/java/com/example/examplemod/event/ChatReplyChat.java
package com.example.examplemod.event;
import net.minecraft.network.chat.Component;import net.neoforged.neoforge.common.NeoForge;import net.neoforged.neoforge.event.ServerChatEvent;
// When a player says "hello" in chat, the server answers them (NeoForge: ServerChatEvent).public final class ChatReplyChat { private ChatReplyChat() { }
// Called from the mod's constructor. public static void register() { NeoForge.EVENT_BUS.addListener(ChatReplyChat::onChat); }
private static void onChat(ServerChatEvent event) { if (event.getRawText().toLowerCase().contains("hello")) { event.getPlayer().sendSystemMessage(Component.literal("Hi there!")); } }}In your main mod class, inside <init>:
ChatReplyChat.register();Forge 1.17.1 to 1.18.2
Section titled “Forge 1.17.1 to 1.18.2”Uses:
ServerChatEvent.getRawTextServerPlayer.sendSystemMessage
src/main/java/com/example/examplemod/event/ChatReplyChat.java
package com.example.examplemod.event;
import net.minecraft.Util;import net.minecraft.network.chat.TextComponent;import net.minecraft.network.chat.Component;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.ServerChatEvent;
// When a player says "hello" in chat, the server answers them (Forge: ServerChatEvent).public final class ChatReplyChat { private ChatReplyChat() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(ChatReplyChat::onChat); }
private static void onChat(ServerChatEvent event) { if (event.getMessage().toLowerCase().contains("hello")) { event.getPlayer().sendMessage(new TextComponent("Hi there!"), Util.NIL_UUID); } }}In your main mod class, inside <init>:
ChatReplyChat.register();Forge 1.19
Section titled “Forge 1.19”Uses:
ServerChatEvent.getRawTextServerPlayer.sendSystemMessage
src/main/java/com/example/examplemod/event/ChatReplyChat.java
package com.example.examplemod.event;
import net.minecraft.network.chat.Component;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.ServerChatEvent;
// When a player says "hello" in chat, the server answers them (Forge: ServerChatEvent).public final class ChatReplyChat { private ChatReplyChat() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(ChatReplyChat::onChat); }
private static void onChat(ServerChatEvent event) { if (event.getMessage().toLowerCase().contains("hello")) { event.getPlayer().sendSystemMessage(Component.literal("Hi there!")); } }}In your main mod class, inside <init>:
ChatReplyChat.register();Forge 1.19.1 to 1.20.1
Section titled “Forge 1.19.1 to 1.20.1”Uses:
ServerChatEvent.getRawTextServerPlayer.sendSystemMessage
src/main/java/com/example/examplemod/event/ChatReplyChat.java
package com.example.examplemod.event;
import net.minecraft.network.chat.Component;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.ServerChatEvent;
// When a player says "hello" in chat, the server answers them (Forge: ServerChatEvent).public final class ChatReplyChat { private ChatReplyChat() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(ChatReplyChat::onChat); }
private static void onChat(ServerChatEvent event) { if (event.getRawText().toLowerCase().contains("hello")) { event.getPlayer().sendSystemMessage(Component.literal("Hi there!")); } }}In your main mod class, inside <init>:
ChatReplyChat.register();NeoForge 1.20.1
Section titled “NeoForge 1.20.1”Uses:
ServerChatEvent.getRawTextServerPlayer.sendSystemMessage
src/main/java/com/example/examplemod/event/ChatReplyChat.java
package com.example.examplemod.event;
import net.minecraft.network.chat.Component;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.ServerChatEvent;
// When a player says "hello" in chat, the server answers them (NeoForge: ServerChatEvent).public final class ChatReplyChat { private ChatReplyChat() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(ChatReplyChat::onChat); }
private static void onChat(ServerChatEvent event) { if (event.getRawText().toLowerCase().contains("hello")) { event.getPlayer().sendSystemMessage(Component.literal("Hi there!")); } }}In your main mod class, inside <init>:
ChatReplyChat.register();Fabric 1.19.1 to 1.19.2
Section titled “Fabric 1.19.1 to 1.19.2”Uses:
ServerMessageEvents.CHAT_MESSAGEServerPlayer.sendSystemMessage
src/main/java/com/example/examplemod/event/ChatReplyChat.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;import net.minecraft.network.chat.Component;
// When a player says "hello" in chat, the server answers them (Fabric API: ServerMessageEvents.CHAT_MESSAGE).public final class ChatReplyChat { private ChatReplyChat() { }
// Called from the mod's onInitialize. public static void register() { ServerMessageEvents.CHAT_MESSAGE.register((message, sender, params) -> { if (message.signedContent().plain().toLowerCase().contains("hello")) { sender.sendSystemMessage(Component.literal("Hi there!")); } }); }}In your main mod class, inside onInitialize:
ChatReplyChat.register();Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
ServerChatEvent.getMessageServerPlayerEntity.sendMessage
In 1.16.5 the reply is sendMessage(new StringTextComponent(…), Util.NIL_UUID) and the text is event.getMessage().
src/main/java/com/example/examplemod/event/ChatReplyChat.java
package com.example.examplemod.event;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.Util;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.ServerChatEvent;
// When a player says "hello" in chat, the server answers them (Forge: ServerChatEvent).public final class ChatReplyChat { private ChatReplyChat() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(ChatReplyChat::onChat); }
private static void onChat(ServerChatEvent event) { if (event.getMessage().toLowerCase().contains("hello")) { event.getPlayer().sendMessage(new StringTextComponent("Hi there!"), Util.NIL_UUID); } }}In your main mod class, inside <init>:
ChatReplyChat.register();Fabric 1.19
Section titled “Fabric 1.19”Uses:
ServerMessageEvents.CHAT_MESSAGEServerPlayer.sendSystemMessage
src/main/java/com/example/examplemod/event/ChatReplyChat.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;import net.minecraft.network.chat.Component;
// When a player says "hello" in chat, the server answers them (Fabric API: ServerMessageEvents.CHAT_MESSAGE).public final class ChatReplyChat { private ChatReplyChat() { }
// Called from the mod's onInitialize. public static void register() { ServerMessageEvents.CHAT_MESSAGE.register((message, sender, params) -> { if (message.raw().signedContent().getString().toLowerCase().contains("hello")) { sender.sendSystemMessage(Component.literal("Hi there!")); } }); }}In your main mod class, inside onInitialize:
ChatReplyChat.register();Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
ServerChatEvent.getMessageEntityPlayerMP.sendMessage
In 1.12.2 the reply is sendMessage(new TextComponentString(…)) and the text is event.getMessage().
src/main/java/com/example/examplemod/event/ChatReplyChat.java
package com.example.examplemod.event;
import com.example.examplemod.ExampleMod;import net.minecraft.util.text.TextComponentString;import net.minecraftforge.event.ServerChatEvent;import net.minecraftforge.fml.common.Mod;import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
// When a player says "hello" in chat, the server answers them (Forge: ServerChatEvent).@Mod.EventBusSubscriber(modid = ExampleMod.MOD_ID)public final class ChatReplyChat { private ChatReplyChat() { }
@SubscribeEvent public static void onChat(ServerChatEvent event) { if (event.getMessage().toLowerCase().contains("hello")) { event.getPlayer().sendMessage(new TextComponentString("Hi there!")); } }}Sources
Section titled “Sources”- https://docs.fabricmc.net/1.21.1/develop/events
- https://docs.neoforged.net/docs/1.21.1/concepts/events
- https://maven.fabricmc.net/docs/fabric-api-0.116.17+1.21.1/net/fabricmc/fabric/api/message/v1/ServerMessageEvents.html
Related how-tos
Section titled “Related how-tos”- Give players items when they first join
- Do something when a player joins
- React when a player breaks a block
- React when a player right-clicks a block
- React when something takes damage
In Linen
Section titled “In Linen”Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.