Andrew2070 Posted January 16, 2019 Posted January 16, 2019 Hi. I'm working on a permissions mod, thus I need to modify chat to include a player's prefix/suffix. I'm currently using the forge provided ServerChatEvent to accomplish this endeavor. I set the event cancellation to true, intercepted the message, performed my modifications and created a TextComponent out of it. However I can't find a method to send a chat message on behalf of the player. Essentially looking to sudo a message from the player to the rest of the server. Back in 1.7.10 this was possible through the player.addChatMessage() method. Now in 1.12.2 there is only the player.sendMessage() method, which only sends the intercepted chat message to the player and not to the rest of the server. Here's my code: package EmpireCoreLib.Events.Player; import java.awt.TextComponent; import java.util.List; import EmpireCoreLib.EmpireCoreLib; import EmpireCoreLib.Permissions.PermissionsList; import EmpireCoreLib.Permissions.User; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.text.TextComponentString; import net.minecraftforge.event.ServerChatEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; @Mod.EventBusSubscriber(modid = EmpireCoreLib.MODID) public class PlayerChatEvent { @SubscribeEvent public static void onPlayerChatEvent(ServerChatEvent event) { EntityPlayer player = (EntityPlayer) event.getPlayer(); if (event.getPlayer() != null) { event.setCanceled(true); for (User user : PermissionsList.users) { if (user.getUUID().equals(player.getUniqueID())) { String suffix = ""; String prefix = ""; String username = player.getName(); if (user.getPrefix().length() > 0) { prefix = "[" + user.getPrefix() + "] "; } if (user.getSuffix().length() > 0) { suffix = " [" + user.getSuffix() + "] "; } String message = prefix + username + suffix + ": " + event.getMessage(); event.setComponent((new TextComponentString(message))); player.sendMessage(new TextComponentString(message)); } } } } } Quote
desht Posted January 16, 2019 Posted January 16, 2019 (edited) If you take a look at the source, in particular tracing back from the chat event constructor, you'll find NetHandlerPlayServer#processChatMessage(). In there, you'll see how the message is normally sent to all players on the server: server.getPlayerList().sendMessage(...). But if you just want to modify a message, do what @diesieben07said above! Edited January 16, 2019 by desht Quote
Andrew2070 Posted January 16, 2019 Author Posted January 16, 2019 Thank you for the feedback, I'll edit my code accordingly. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.