Hi,
I'm making a simple forge mod to highlight words, everything works splendidly but I'm unsure of how to keep chat formatting intact.
For instance lets say I was on a faction server , players have a faction, a rank and then their name and chat message.
With being Highlighted what It looks like:
[Faction] [Tag] Name: Sentence with the highlighted word you chose.
(purple = white but if it was white you wouldn't be able to see it :P)
With being Highlighted what I want it to look like:
[Faction] [Tag] Name: Sentence with the highlighted word you chose.
Here is my ClientChatReceivedEvent class:
public class OnClientChatReceivedEvent {
List<String> words = Highlight.getWords();
@SubscribeEvent
public void onClientChatReceivedEvent(ClientChatReceivedEvent event) {
String message = event.message.getFormattedText();
boolean sendOriginal = true;
String newMessage = "";
// get the words
String[] messageWords = message.split(" ");
for (int w = 0; w < messageWords.length; w++) {
// does this message word match any of the search words
boolean wordMatches = false;
for (int i = 0; i < words.size(); i++) {
if (messageWords[w].equalsIgnoreCase(words.get(i))) {
sendOriginal = false;
wordMatches = true;
newMessage = newMessage + EnumChatFormatting.RED
+ messageWords[w] + EnumChatFormatting.RESET + " ";
break;
}
}
if (!wordMatches) {
newMessage = newMessage + messageWords[w] + " ";
}
}
if (sendOriginal) {
event.setCanceled(false);
} else {
event.setCanceled(true);
Minecraft.getMinecraft().thePlayer
.addChatMessage(new ChatComponentText(newMessage.trim()));
System.out.println(message);
}
}
}
Is there a better way than to replace the entire chat message?
Thanks!
PS: i'm not too experienced in java/forge modding and im still learning ;P.