From 1a07815f4b535cc80c32579b010294eb4b49ecfc Mon Sep 17 00:00:00 2001 From: snowleo Date: Fri, 3 Aug 2012 22:57:29 +0200 Subject: Make chat handling more thread safe (and also faster) --- .../src/com/earth2me/essentials/Settings.java | 126 +++++++++++++++++---- Essentials/src/com/earth2me/essentials/User.java | 24 ++-- .../src/com/earth2me/essentials/UserData.java | 6 +- .../chat/EssentialsChatPlayerListenerLowest.java | 13 ++- 4 files changed, 128 insertions(+), 41 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Settings.java b/Essentials/src/com/earth2me/essentials/Settings.java index ec317dbfd..30cfc9dab 100644 --- a/Essentials/src/com/earth2me/essentials/Settings.java +++ b/Essentials/src/com/earth2me/essentials/Settings.java @@ -77,11 +77,17 @@ public class Settings implements ISettings { return config.getInt("sethome-multiple." + set, config.getInt("sethome-multiple.default", 3)); } + private int chatRadius = 0; + + private int _getChatRadius() + { + return config.getInt("chat.radius", config.getInt("chat-radius", 0)); + } @Override public int getChatRadius() { - return config.getInt("chat.radius", config.getInt("chat-radius", 0)); + return chatRadius; } @Override @@ -113,19 +119,29 @@ public class Settings implements ISettings { return isCommandDisabled(cmd.getName()); } + private Set disabledCommands = new HashSet(); @Override public boolean isCommandDisabled(String label) { + return disabledCommands.contains(label); + } + + private Set getDisabledCommands() + { + Set disCommands = new HashSet(); for (String c : config.getStringList("disabled-commands")) { - if (!c.equalsIgnoreCase(label)) + disCommands.add(c.toLowerCase(Locale.ENGLISH)); + } + for (String c : config.getKeys(false)) + { + if (c.startsWith("disable-")) { - continue; + disCommands.add(c.substring(8).toLowerCase(Locale.ENGLISH)); } - return true; } - return config.getBoolean("disable-" + label.toLowerCase(Locale.ENGLISH), false); + return disCommands; } @Override @@ -192,11 +208,17 @@ public class Settings implements ISettings } return cost; } + private String nicknamePrefix = "~"; + + private String _getNicknamePrefix() + { + return config.getString("nickname-prefix", "~"); + } @Override public String getNicknamePrefix() { - return config.getString("nickname-prefix", "~"); + return nicknamePrefix; } @Override @@ -250,9 +272,15 @@ public class Settings implements ISettings } return null; } + private ChatColor operatorColor = null; @Override - public ChatColor getOperatorColor() throws Exception + public ChatColor getOperatorColor() + { + return operatorColor; + } + + private ChatColor _getOperatorColor() { String colorName = config.getString("ops-name-color", null); @@ -262,7 +290,7 @@ public class Settings implements ISettings } if ("none".equalsIgnoreCase(colorName) || colorName.isEmpty()) { - throw new Exception(); + return null; } try @@ -317,7 +345,7 @@ public class Settings implements ISettings { return config.getString("backup.command", null); } - private Map chatFormats = new HashMap(); + private Map chatFormats = Collections.synchronizedMap(new HashMap()); @Override public MessageFormat getChatFormat(String group) @@ -392,6 +420,17 @@ public class Settings implements ISettings signUsePerSecond = _getSignUsePerSecond(); kits = _getKits(); chatFormats.clear(); + changeDisplayName = _changeDisplayName(); + disabledCommands = getDisabledCommands(); + nicknamePrefix = _getNicknamePrefix(); + operatorColor = _getOperatorColor(); + changePlayerListName = _changePlayerListName(); + configDebug = _isDebug(); + prefixsuffixconfigured = _isPrefixSuffixConfigured(); + addprefixsuffix = _addPrefixSuffix(); + disablePrefix = _disablePrefix(); + disableSuffix = _disableSuffix(); + chatRadius = _getChatRadius(); } private List itemSpawnBl = new ArrayList(); @@ -479,11 +518,17 @@ public class Settings implements ISettings return config.getBoolean("protect.disable.warn-on-build-disallow", false); } private boolean debug = false; + private boolean configDebug = false; + + private boolean _isDebug() + { + return config.getBoolean("debug", false); + } @Override public boolean isDebug() { - return debug || config.getBoolean("debug", false); + return debug || configDebug; } @Override @@ -610,17 +655,29 @@ public class Settings implements ISettings { return config.getBoolean("remove-god-on-disconnect", false); } + private boolean changeDisplayName = true; + + private boolean _changeDisplayName() + { + return config.getBoolean("change-displayname", true); + } @Override public boolean changeDisplayName() { - return config.getBoolean("change-displayname", true); + return changeDisplayName; + } + private boolean changePlayerListName = false; + + private boolean _changePlayerListName() + { + return config.getBoolean("change-playerlist", false); } @Override public boolean changePlayerListName() { - return config.getBoolean("change-playerlist", false); + return changePlayerListName; } @Override @@ -628,23 +685,47 @@ public class Settings implements ISettings { return config.getBoolean("use-bukkit-permissions", false); } + private boolean prefixsuffixconfigured = false; + private boolean addprefixsuffix = false; + + private boolean _addPrefixSuffix() + { + return config.getBoolean("add-prefix-suffix", false); + } + + private boolean _isPrefixSuffixConfigured() + { + return config.hasProperty("add-prefix-suffix"); + } @Override public boolean addPrefixSuffix() { - return config.getBoolean("add-prefix-suffix", ess.getServer().getPluginManager().isPluginEnabled("EssentialsChat")); + return prefixsuffixconfigured ? addprefixsuffix : ess.getServer().getPluginManager().isPluginEnabled("EssentialsChat"); + } + private boolean disablePrefix = false; + + private boolean _disablePrefix() + { + return config.getBoolean("disablePrefix", false); } @Override public boolean disablePrefix() { - return config.getBoolean("disablePrefix", false); + return disablePrefix; + } + private boolean disableSuffix = false; + + private boolean _disableSuffix() + { + return config.getBoolean("disableSuffix", false); } @Override public boolean disableSuffix() { - return config.getBoolean("disableSuffix", false); + return disableSuffix; } @Override @@ -713,7 +794,7 @@ public class Settings implements ISettings { return config.getBoolean("world-teleport-permissions", false); } - + @Override public boolean isWorldHomePermissions() { @@ -800,38 +881,35 @@ public class Settings implements ISettings { return (config.getLong("teleport-invulnerability", 0) > 0); } - + @Override public boolean isTeleportInvulnerability() { return teleportInvulnerability; } - private long loginAttackDelay; - + private long _getLoginAttackDelay() { return config.getLong("login-attack-delay", 0) * 1000; } - + @Override public long getLoginAttackDelay() { return loginAttackDelay; } - private int signUsePerSecond; - + private int _getSignUsePerSecond() { final int perSec = config.getInt("sign-use-per-second", 4); return perSec > 0 ? perSec : 1; } - + @Override public int getSignUsePerSecond() { return signUsePerSecond; } - } diff --git a/Essentials/src/com/earth2me/essentials/User.java b/Essentials/src/com/earth2me/essentials/User.java index 3da49ad8b..5b6eff1fb 100644 --- a/Essentials/src/com/earth2me/essentials/User.java +++ b/Essentials/src/com/earth2me/essentials/User.java @@ -7,6 +7,7 @@ import java.util.Calendar; import java.util.GregorianCalendar; import java.util.logging.Level; import java.util.logging.Logger; +import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -284,10 +285,10 @@ public class User extends UserData implements Comparable, IReplyTo, IUser { try { - final String opPrefix = ess.getSettings().getOperatorColor().toString(); - if (opPrefix.length() > 0) + final ChatColor opPrefix = ess.getSettings().getOperatorColor(); + if (opPrefix != null && opPrefix.toString().length() > 0) { - prefix.insert(0, opPrefix); + prefix.insert(0, opPrefix.toString()); suffix = "§f"; } } @@ -325,7 +326,8 @@ public class User extends UserData implements Comparable, IReplyTo, IUser { output = Util.lastCode(strPrefix) + nickname.substring(0, 14); } - if (output.charAt(output.length() - 1) == '§') { + if (output.charAt(output.length() - 1) == '§') + { output = output.substring(0, output.length() - 1); } return output; @@ -639,7 +641,7 @@ public class User extends UserData implements Comparable, IReplyTo, IUser { return vanished; } - + public void setVanished(final boolean set) { vanished = set; @@ -671,21 +673,23 @@ public class User extends UserData implements Comparable, IReplyTo, IUser final boolean set = !vanished; this.setVanished(set); } - - public boolean checkSignThrottle() { - if (isSignThrottled()) { + + public boolean checkSignThrottle() + { + if (isSignThrottled()) + { return true; } updateThrottle(); return false; } - + public boolean isSignThrottled() { final long minTime = lastThrottledAction + (1000 / ess.getSettings().getSignUsePerSecond()); return (System.currentTimeMillis() < minTime); } - + public void updateThrottle() { lastThrottledAction = System.currentTimeMillis();; diff --git a/Essentials/src/com/earth2me/essentials/UserData.java b/Essentials/src/com/earth2me/essentials/UserData.java index 7dd9ebf97..8ae1b7a3f 100644 --- a/Essentials/src/com/earth2me/essentials/UserData.java +++ b/Essentials/src/com/earth2me/essentials/UserData.java @@ -440,14 +440,14 @@ public abstract class UserData extends PlayerExtension implements IConf public List getIgnoredPlayers() { - return config.getStringList("ignore"); + return Collections.synchronizedList(config.getStringList("ignore")); } public void setIgnoredPlayers(List players) { if (players == null || players.isEmpty()) { - ignoredPlayers = new ArrayList(); + ignoredPlayers = Collections.synchronizedList(new ArrayList()); config.removeProperty("ignore"); } else @@ -466,7 +466,7 @@ public abstract class UserData extends PlayerExtension implements IConf { return false; } - return isIgnoredPlayer(user); + return isIgnoredPlayer(user); } public boolean isIgnoredPlayer(IUser user) diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java index 599c39dda..79ff4994a 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java @@ -3,6 +3,7 @@ package com.earth2me.essentials.chat; import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.User; import com.earth2me.essentials.Util; +import java.text.MessageFormat; import java.util.Locale; import java.util.Map; import org.bukkit.Server; @@ -40,9 +41,13 @@ public class EssentialsChatPlayerListenerLowest extends EssentialsChatPlayer event.setMessage(Util.formatMessage(user, "essentials.chat", event.getMessage())); 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) - })); + MessageFormat format = ess.getSettings().getChatFormat(group); + synchronized (format) + { + event.setFormat(format.format(new Object[] + { + group, world, world.substring(0, 1).toUpperCase(Locale.ENGLISH) + })); + } } } -- cgit v1.2.3