From 5f04d1867cc75fc0d31d74d301459f5ac89c55a6 Mon Sep 17 00:00:00 2001
From: snowleo <schneeleo@gmail.com>
Date: Thu, 19 Jan 2012 02:03:20 +0100
Subject: [PATCH] Cache MessageFormats for Chat

---
 .../com/earth2me/essentials/ISettings.java    |  3 ++-
 .../src/com/earth2me/essentials/Settings.java | 26 +++++++++++++++----
 .../EssentialsChatPlayerListenerLowest.java   |  4 ++-
 3 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/Essentials/src/com/earth2me/essentials/ISettings.java b/Essentials/src/com/earth2me/essentials/ISettings.java
index 5c53a6790..19c105300 100644
--- a/Essentials/src/com/earth2me/essentials/ISettings.java
+++ b/Essentials/src/com/earth2me/essentials/ISettings.java
@@ -1,6 +1,7 @@
 package com.earth2me.essentials;
 
 import com.earth2me.essentials.commands.IEssentialsCommand;
+import java.text.MessageFormat;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -22,7 +23,7 @@ public interface ISettings extends IConf
 
 	long getBackupInterval();
 
-	String getChatFormat(String group);
+	MessageFormat getChatFormat(String group);
 
 	int getChatRadius();
 
diff --git a/Essentials/src/com/earth2me/essentials/Settings.java b/Essentials/src/com/earth2me/essentials/Settings.java
index 682eedaf9..995d58643 100644
--- a/Essentials/src/com/earth2me/essentials/Settings.java
+++ b/Essentials/src/com/earth2me/essentials/Settings.java
@@ -3,6 +3,7 @@ package com.earth2me.essentials;
 import static com.earth2me.essentials.I18n._;
 import com.earth2me.essentials.commands.IEssentialsCommand;
 import java.io.File;
+import java.text.MessageFormat;
 import java.util.*;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -291,12 +292,26 @@ public class Settings implements ISettings
 	{
 		return config.getString("backup.command", null);
 	}
+	private Map<String, MessageFormat> chatFormats = new HashMap<String, MessageFormat>();
 
 	@Override
-	public String getChatFormat(String group)
+	public MessageFormat getChatFormat(String group)
 	{
-		return config.getString("chat.group-formats." + (group == null ? "Default" : group),
-								config.getString("chat.format", "&7[{GROUP}]&f {DISPLAYNAME}&7:&f {MESSAGE}"));
+		MessageFormat mFormat = chatFormats.get(group);
+		if (mFormat == null)
+		{
+			String format = config.getString("chat.group-formats." + (group == null ? "Default" : group),
+											 config.getString("chat.format", "&7[{GROUP}]&f {DISPLAYNAME}&7:&f {MESSAGE}"));
+			format = Util.replaceColor(format);
+			format.replace("{DISPLAYNAME}", "%1$s");
+			format.replace("{GROUP}", "{0}");
+			format.replace("{MESSAGE}", "%2$s");
+			format.replace("{WORLDNAME}", "{1}");
+			format.replace("{SHORTWORLDNAME}", "{2}");
+			mFormat = new MessageFormat(format);
+			chatFormats.put(group, mFormat);
+		}
+		return mFormat;
 	}
 
 	@Override
@@ -340,6 +355,7 @@ public class Settings implements ISettings
 	{
 		config.load();
 		noGodWorlds = new HashSet<String>(config.getStringList("no-god-in-worlds", Collections.<String>emptyList()));
+		chatFormats.clear();
 	}
 
 	@Override
@@ -606,10 +622,10 @@ public class Settings implements ISettings
 		}
 		return Priority.Normal;
 	}
-	
+
 	@Override
 	public long getTpaAcceptCancellation()
 	{
-		return config.getLong("tpa-accept-cancellation", 0);		
+		return config.getLong("tpa-accept-cancellation", 0);
 	}
 }
diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java
index 9419416b0..32e60d75d 100644
--- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java
+++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java
@@ -42,6 +42,8 @@ public class EssentialsChatPlayerListenerLowest extends EssentialsChatPlayer
 		{
 			event.setMessage(Util.stripColor(event.getMessage()));
 		}
-		event.setFormat(ess.getSettings().getChatFormat(user.getGroup()).replace('&', '\u00a7').replace("\u00a7\u00a7", "&").replace("{DISPLAYNAME}", "%1$s").replace("{GROUP}", user.getGroup()).replace("{MESSAGE}", "%2$s").replace("{WORLDNAME}", user.getWorld().getName()).replace("{SHORTWORLDNAME}", user.getWorld().getName().substring(0, 1).toUpperCase(Locale.ENGLISH)));
+		String group = user.getGroup();
+		String world = user.getWorld().getName();
+		event.setFormat(ess.getSettings().getChatFormat(group).format(new Object[] {group, world, world.substring(0, 1).toUpperCase(Locale.ENGLISH)}));
 	}
 }