import net.minecraftforge.client.event.ClientChatEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.util.text.TextComponentString;
@Mod.EventBusSubscriber(modid = "yourmodid", bus = Mod.EventBusSubscriber.Bus.FORGE)
public class ChatHandler {
@SubscribeEvent
public static void onClientChat(ClientChatEvent event) {
// Get the message typed by the player
String message = event.getMessage();
// If the message starts with "/sayhello", it's our custom command
if (message.startsWith("/sayhello")) {
// Cancel the message from showing in chat
event.setCanceled(true);
// Send a custom message back to the player
String responseMessage = "Hello, " + event.getSender().getName() + "!";
event.getSender().sendMessage(new TextComponentString(responseMessage));
// Manually add the command to the chat history (this is a workaround)
Minecraft minecraft = Minecraft.getMinecraft();
EntityPlayerSP player = minecraft.player;
if (player != null) {
// Add the original command to the chat history so it shows when the player presses the up arrow
minecraft.ingameGUI.getChatGUI().addToSentMessages(message);
}
}
}
}