From 9014396a696b01082d81cc7b769a88da34871600 Mon Sep 17 00:00:00 2001 From: CrushedPixel Date: Mon, 29 Jun 2015 13:43:24 +0200 Subject: [PATCH] Updated ChatMessageHandler to use Minecraft's Chat Component System instead of Strings with Color Codes | https://trello.com/c/jdl5t5Wj/ --- .../replaymod/chat/ChatMessageHandler.java | 44 ++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/main/java/eu/crushedpixel/replaymod/chat/ChatMessageHandler.java b/src/main/java/eu/crushedpixel/replaymod/chat/ChatMessageHandler.java index 3a1d3934..56cf8d23 100755 --- a/src/main/java/eu/crushedpixel/replaymod/chat/ChatMessageHandler.java +++ b/src/main/java/eu/crushedpixel/replaymod/chat/ChatMessageHandler.java @@ -4,8 +4,7 @@ import eu.crushedpixel.replaymod.ReplayMod; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.resources.I18n; -import net.minecraft.util.ChatComponentText; -import net.minecraft.util.IChatComponent; +import net.minecraft.util.*; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -15,10 +14,23 @@ import java.util.concurrent.ConcurrentLinkedQueue; @SideOnly(Side.CLIENT) public class ChatMessageHandler { + private static final IChatComponent prefix; + + static { + //results in "§8[§6Replay Mod§8]§r " + + ChatStyle dark_gray = new ChatStyle().setColor(EnumChatFormatting.DARK_GRAY); + ChatStyle gold = new ChatStyle().setColor(EnumChatFormatting.GOLD); + + prefix = new ChatComponentText("[").setChatStyle(dark_gray) + .appendSibling(new ChatComponentTranslation("replaymod.title").setChatStyle(gold)) + .appendSibling(new ChatComponentText("] ")); + } + + private boolean active = true; private boolean alive = true; private Queue requests = new ConcurrentLinkedQueue(); - private String prefix = "§8[§6Replay Mod§8]§r "; private EntityPlayerSP player = null; public Thread t = new Thread(new Runnable() { @@ -61,20 +73,12 @@ public class ChatMessageHandler { public void addLocalizedChatMessage(String message, ChatMessageType type, Object... options) { message = I18n.format(message, options); if(ReplayMod.replaySettings.isShowNotifications()) { - message = prefix + toColor(message, type); + ChatComponentText cct = new ChatComponentText(message); - requests.add(cct); - } - } + cct.setChatStyle(type.getChatStyle()); - private String toColor(String message, ChatMessageType type) { - if(type == ChatMessageType.INFORMATION) { - return "§2" + message; - } else if(type == ChatMessageType.WARNING) { - return "§c" + message; + requests.add(prefix.createCopy().appendSibling(cct)); } - - return message; } public void stop() { @@ -90,6 +94,16 @@ public class ChatMessageHandler { } public enum ChatMessageType { - INFORMATION, WARNING; + INFORMATION(new ChatStyle().setColor(EnumChatFormatting.DARK_GREEN)), WARNING(new ChatStyle().setColor(EnumChatFormatting.RED)); + + private ChatStyle chatStyle; + + public ChatStyle getChatStyle() { + return chatStyle; + } + + ChatMessageType(ChatStyle chatStyle) { + this.chatStyle = chatStyle; + } } }