diff options
author | KHobbits <rob@khobbits.co.uk> | 2012-01-18 22:53:39 +0000 |
---|---|---|
committer | KHobbits <rob@khobbits.co.uk> | 2012-01-18 22:53:39 +0000 |
commit | cd8cc631134de1ece1aab89c0b01d5a63dda1cfd (patch) | |
tree | 2a66d7070b71ed0d22e2f0681e4eff2517891cff /EssentialsChat/src | |
parent | 947bee98a0eae2da2448923d450ebd4ca7d81157 (diff) | |
download | Essentials-cd8cc631134de1ece1aab89c0b01d5a63dda1cfd.tar Essentials-cd8cc631134de1ece1aab89c0b01d5a63dda1cfd.tar.gz Essentials-cd8cc631134de1ece1aab89c0b01d5a63dda1cfd.tar.lz Essentials-cd8cc631134de1ece1aab89c0b01d5a63dda1cfd.tar.xz Essentials-cd8cc631134de1ece1aab89c0b01d5a63dda1cfd.zip |
Reduce duplicate calls in EssentialsChat
Should cut the time down a little.
Diffstat (limited to 'EssentialsChat/src')
6 files changed, 133 insertions, 103 deletions
diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/ChatStore.java b/EssentialsChat/src/com/earth2me/essentials/chat/ChatStore.java new file mode 100644 index 000000000..38239abfe --- /dev/null +++ b/EssentialsChat/src/com/earth2me/essentials/chat/ChatStore.java @@ -0,0 +1,51 @@ +package com.earth2me.essentials.chat; + +import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.Trade; +import com.earth2me.essentials.User; + + +public class ChatStore +{ + private final User user; + private final String type; + private final Trade charge; + private long radius; + + ChatStore(final IEssentials ess, final User user, final String type) + { + this.user = user; + this.type = type; + this.charge = new Trade(getLongType(), ess); + } + + public User getUser() + { + return user; + } + + public Trade getCharge() + { + return charge; + } + + public String getType() + { + return type; + } + + public String getLongType() + { + return type.length() > 0 ? "chat" : "chat-" + type; + } + + public long getRadius() + { + return radius; + } + + public void setRadius(long radius) + { + this.radius = radius; + } +} diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java index 25ce85cc8..de0d14421 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java @@ -18,7 +18,6 @@ public class EssentialsChat extends JavaPlugin { private static final Logger LOGGER = Logger.getLogger("Minecraft"); private transient Map<String, IEssentialsChatListener> chatListener; - @Override public void onEnable() @@ -36,12 +35,12 @@ public class EssentialsChat extends JavaPlugin } chatListener = new ConcurrentSkipListMap<String, IEssentialsChatListener>(); - final Map<PlayerChatEvent, String> charges = new HashMap<PlayerChatEvent, String>(); - + final Map<PlayerChatEvent, ChatStore> chatStore = new HashMap<PlayerChatEvent, ChatStore>(); - final EssentialsChatPlayerListenerLowest playerListenerLowest = new EssentialsChatPlayerListenerLowest(getServer(), ess, chatListener); - final EssentialsChatPlayerListenerNormal playerListenerNormal = new EssentialsChatPlayerListenerNormal(getServer(), ess, chatListener, charges); - final EssentialsChatPlayerListenerHighest playerListenerHighest = new EssentialsChatPlayerListenerHighest(getServer(), ess, chatListener, charges); + + final EssentialsChatPlayerListenerLowest playerListenerLowest = new EssentialsChatPlayerListenerLowest(getServer(), ess, chatListener, chatStore); + final EssentialsChatPlayerListenerNormal playerListenerNormal = new EssentialsChatPlayerListenerNormal(getServer(), ess, chatListener, chatStore); + final EssentialsChatPlayerListenerHighest playerListenerHighest = new EssentialsChatPlayerListenerHighest(getServer(), ess, chatListener, chatStore); pluginManager.registerEvent(Type.PLAYER_CHAT, playerListenerLowest, Priority.Lowest, this); pluginManager.registerEvent(Type.PLAYER_CHAT, playerListenerNormal, Priority.Normal, this); pluginManager.registerEvent(Type.PLAYER_CHAT, playerListenerHighest, Priority.Highest, this); diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java index 432e2309c..30abe9cf8 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java @@ -22,12 +22,17 @@ public abstract class EssentialsChatPlayer extends PlayerListener protected final static Logger logger = Logger.getLogger("Minecraft"); protected final transient Map<String, IEssentialsChatListener> listeners; protected final transient Server server; + protected final transient Map<PlayerChatEvent, ChatStore> chatStorage; - public EssentialsChatPlayer(Server server, IEssentials ess, Map<String, IEssentialsChatListener> listeners) + public EssentialsChatPlayer(final Server server, + final IEssentials ess, + final Map<String, IEssentialsChatListener> listeners, + final Map<PlayerChatEvent, ChatStore> chatStorage) { this.ess = ess; this.listeners = listeners; this.server = server; + this.chatStorage = chatStorage; } public void onPlayerChat(final PlayerChatEvent event) @@ -36,11 +41,6 @@ public abstract class EssentialsChatPlayer extends PlayerListener public boolean isAborted(final PlayerChatEvent event) { - return isAborted(event, "chat"); - } - - public boolean isAborted(final PlayerChatEvent event, final String command) - { if (event.isCancelled()) { return true; @@ -52,13 +52,6 @@ public abstract class EssentialsChatPlayer extends PlayerListener return true; } } - - final User user = ess.getUser(event.getPlayer()); - if (!isAffordableFor(user, command)) - { - event.setCancelled(true); - return true; - } return false; } @@ -75,65 +68,75 @@ public abstract class EssentialsChatPlayer extends PlayerListener } } - protected void charge(final CommandSender sender, final String command) throws ChargeException + public ChatStore getChatStore(final PlayerChatEvent event) { - if (sender instanceof Player) - { - final Trade charge = new Trade(command, ess); - charge.charge(ess.getUser((Player)sender)); - } + return chatStorage.get(event); + } + + public void setChatStore(final PlayerChatEvent event, final ChatStore chatStore) + { + chatStorage.put(event, chatStore); + } + + public ChatStore delChatStore(final PlayerChatEvent event) + { + return chatStorage.remove(event); } - protected boolean isAffordableFor(final CommandSender sender, final String command) + protected void charge(final User user, final Trade charge) throws ChargeException { - if (sender instanceof Player) + charge.charge(user); + } + + protected boolean charge(final PlayerChatEvent event, final ChatStore chatStore) + { + try { - try - { - final Trade charge = new Trade(command, ess); - charge.isAffordableFor(ess.getUser((Player)sender)); - } - catch (ChargeException e) - { - return false; - } + charge(chatStore.getUser(), chatStore.getCharge()); } - else + catch (ChargeException e) { + ess.showError(chatStore.getUser(), e, chatStore.getLongType()); + event.setCancelled(true); return false; } - return true; } - protected void sendLocalChat(final User sender, final long radius, final PlayerChatEvent event) + protected void sendLocalChat(final PlayerChatEvent event, final ChatStore chatStore) { event.setCancelled(true); + final User sender = chatStore.getUser(); logger.info(_("localFormat", sender.getName(), event.getMessage())); final Location loc = sender.getLocation(); final World world = loc.getWorld(); + if (charge(event, chatStore) == false) + { + return; + } + for (Player onlinePlayer : server.getOnlinePlayers()) { String type = "[L]"; - final User user = ess.getUser(onlinePlayer); + final User onlineUser = ess.getUser(onlinePlayer); //TODO: remove reference to op - if (user.isIgnoredPlayer(sender.getName()) && !sender.isOp()) + if (onlineUser.isIgnoredPlayer(sender.getName()) && !sender.isOp()) { continue; } - if (!user.equals(sender)) + if (!onlineUser.equals(sender)) { - final Location playerLoc = user.getLocation(); + final Location playerLoc = onlineUser.getLocation(); if (playerLoc.getWorld() != world) { continue; } final double delta = playerLoc.distanceSquared(loc); - if (delta > radius) + if (delta > chatStore.getRadius()) { - if (user.isAuthorized("essentials.chat.spy")) + if (onlineUser.isAuthorized("essentials.chat.spy")) { type = type.concat("[Spy]"); } @@ -149,7 +152,7 @@ public abstract class EssentialsChatPlayer extends PlayerListener { message = listener.modifyMessage(event, onlinePlayer, message); } - user.sendMessage(message); + onlineUser.sendMessage(message); } } } diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerHighest.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerHighest.java index 22989d4f9..276715f31 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerHighest.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerHighest.java @@ -10,25 +10,17 @@ import org.bukkit.event.player.PlayerChatEvent; public class EssentialsChatPlayerListenerHighest extends EssentialsChatPlayer { - private final transient Map<PlayerChatEvent, String> charges; - public EssentialsChatPlayerListenerHighest(final Server server, final IEssentials ess, final Map<String, IEssentialsChatListener> listeners, - final Map<PlayerChatEvent, String> charges) + final Map<PlayerChatEvent, ChatStore> chatStorage) { - super(server, ess, listeners); - this.charges = charges; + super(server, ess, listeners, chatStorage); } @Override public void onPlayerChat(final PlayerChatEvent event) { - String charge = charges.remove(event); - if (charge == null) - { - charge = "chat"; - } if (isAborted(event)) { return; @@ -37,17 +29,6 @@ public class EssentialsChatPlayerListenerHighest extends EssentialsChatPlayer /** * This file should handle charging the user for the action before returning control back */ - final User user = ess.getUser(event.getPlayer()); - - try - { - charge(user, charge); - } - catch (ChargeException e) - { - ess.showError(user, e, charge); - event.setCancelled(true); - return; - } + charge(event, getChatStore(event)); } } diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java index 5c674d05d..afb0c2dfd 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java @@ -13,9 +13,10 @@ public class EssentialsChatPlayerListenerLowest extends EssentialsChatPlayer { public EssentialsChatPlayerListenerLowest(final Server server, final IEssentials ess, - final Map<String, IEssentialsChatListener> listeners) + final Map<String, IEssentialsChatListener> listeners, + final Map<PlayerChatEvent, ChatStore> chatStorage) { - super(server, ess, listeners); + super(server, ess, listeners, chatStorage); } @Override @@ -26,14 +27,19 @@ public class EssentialsChatPlayerListenerLowest extends EssentialsChatPlayer return; } + final User user = ess.getUser(event.getPlayer()); + final ChatStore chatStore = new ChatStore(ess, user, getChatType(event.getMessage())); + setChatStore(event, chatStore); + /** * This listener should apply the general chat formatting only...then return control back the event handler */ - final User user = ess.getUser(event.getPlayer()); if (user.isAuthorized("essentials.chat.color")) { event.setMessage(event.getMessage().replaceAll("&([0-9a-fk])", "\u00a7$1")); - } else { + } + else + { 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))); diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerNormal.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerNormal.java index 4e3cbefc0..16ebe8e7e 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerNormal.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerNormal.java @@ -11,17 +11,14 @@ import org.bukkit.event.player.PlayerChatEvent; public class EssentialsChatPlayerListenerNormal extends EssentialsChatPlayer { - private final transient Map<PlayerChatEvent, String> charges; - public EssentialsChatPlayerListenerNormal(final Server server, final IEssentials ess, final Map<String, IEssentialsChatListener> listeners, - final Map<PlayerChatEvent, String> charges) + final Map<PlayerChatEvent, ChatStore> chatStorage) { - super(server, ess, listeners); - this.charges = charges; + super(server, ess, listeners, chatStorage); } - + @Override public void onPlayerChat(final PlayerChatEvent event) { @@ -33,47 +30,40 @@ public class EssentialsChatPlayerListenerNormal extends EssentialsChatPlayer /** * This file should handle detection of the local chat features... if local chat is enabled, we need to handle * it here - */ - final String chatType = getChatType(event.getMessage()); - final StringBuilder command = new StringBuilder(); - command.append("chat"); - - if (chatType.length() > 0) - { - command.append("-").append(chatType); - } + */ long radius = ess.getSettings().getChatRadius(); if (radius < 1) { return; } radius *= radius; - final User user = ess.getUser(event.getPlayer()); - - if (event.getMessage().length() > 0 && chatType.length() > 0) + + final ChatStore chatStore = getChatStore(event); + final User user = chatStore.getUser(); + chatStore.setRadius(radius); + + if (event.getMessage().length() > 0 && chatStore.getType().length() > 0) { final StringBuilder permission = new StringBuilder(); - permission.append("essentials.chat.").append(chatType); - - final StringBuilder format = new StringBuilder(); - format.append(chatType).append("Format"); - - final StringBuilder errorMsg = new StringBuilder(); - errorMsg.append("notAllowedTo").append(chatType.substring(0, 1).toUpperCase(Locale.ENGLISH)).append(chatType.substring(1)); - + permission.append("essentials.chat.").append(chatStore.getType()); + if (user.isAuthorized(permission.toString())) { + final StringBuilder format = new StringBuilder(); + format.append(chatStore.getType()).append("Format"); event.setMessage(event.getMessage().substring(1)); - event.setFormat(_(format.toString(), event.getFormat())); - charges.put(event, command.toString()); + event.setFormat(_(format.toString(), event.getFormat())); return; } - + + final StringBuilder errorMsg = new StringBuilder(); + errorMsg.append("notAllowedTo").append(chatStore.getType().substring(0, 1).toUpperCase(Locale.ENGLISH)).append(chatStore.getType().substring(1)); + user.sendMessage(_(errorMsg.toString())); event.setCancelled(true); return; } - - sendLocalChat(user, radius, event); + + sendLocalChat(event, chatStore); } } |