diff options
author | KHobbits <rob@khobbits.co.uk> | 2013-07-13 17:24:40 +0100 |
---|---|---|
committer | KHobbits <rob@khobbits.co.uk> | 2013-07-13 17:25:58 +0100 |
commit | ef3b052c644542bbea3fa668c60b03baf3e15861 (patch) | |
tree | 20ca96305f9bfa87c37cd7be2118e1a641ddceb6 /EssentialsChat | |
parent | f5624d4cb6e9713de68c3c2228aafefbff861fc3 (diff) | |
download | Essentials-ef3b052c644542bbea3fa668c60b03baf3e15861.tar Essentials-ef3b052c644542bbea3fa668c60b03baf3e15861.tar.gz Essentials-ef3b052c644542bbea3fa668c60b03baf3e15861.tar.lz Essentials-ef3b052c644542bbea3fa668c60b03baf3e15861.tar.xz Essentials-ef3b052c644542bbea3fa668c60b03baf3e15861.zip |
Remove old chat hook
Use Bukkit API for local chat handling
Implement localChatSpy event
Diffstat (limited to 'EssentialsChat')
6 files changed, 105 insertions, 160 deletions
diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java index f16579a4d..bd975ebab 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java @@ -16,7 +16,6 @@ import org.bukkit.plugin.java.JavaPlugin; public class EssentialsChat extends JavaPlugin { private static final Logger LOGGER = Logger.getLogger("Minecraft"); - private transient Map<String, IEssentialsChatListener> chatListener; @Override public void onEnable() @@ -33,35 +32,16 @@ public class EssentialsChat extends JavaPlugin return; } - chatListener = new ConcurrentSkipListMap<String, IEssentialsChatListener>(); final Map<AsyncPlayerChatEvent, ChatStore> chatStore = Collections.synchronizedMap(new HashMap<AsyncPlayerChatEvent, ChatStore>()); - 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); + final EssentialsChatPlayerListenerLowest playerListenerLowest = new EssentialsChatPlayerListenerLowest(getServer(), ess, chatStore); + final EssentialsChatPlayerListenerNormal playerListenerNormal = new EssentialsChatPlayerListenerNormal(getServer(), ess, chatStore); + final EssentialsChatPlayerListenerHighest playerListenerHighest = new EssentialsChatPlayerListenerHighest(getServer(), ess, chatStore); pluginManager.registerEvents(playerListenerLowest, this); pluginManager.registerEvents(playerListenerNormal, this); pluginManager.registerEvents(playerListenerHighest, this); } - - @Override - public void onDisable() - { - if (chatListener != null) - { - chatListener.clear(); - } - } - - public void addEssentialsChatListener(final String plugin, final IEssentialsChatListener listener) - { - chatListener.put(plugin, listener); - } - - public IEssentialsChatListener removeEssentialsChatListener(final String plugin) - { - return chatListener.remove(plugin); - } + } diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java index 83996ea21..d47ba6a5c 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java @@ -19,17 +19,14 @@ public abstract class EssentialsChatPlayer implements Listener { protected transient IEssentials ess; protected final static Logger logger = Logger.getLogger("Minecraft"); - protected final transient Map<String, IEssentialsChatListener> listeners; protected final transient Server server; protected final transient Map<AsyncPlayerChatEvent, ChatStore> chatStorage; public EssentialsChatPlayer(final Server server, - final IEssentials ess, - final Map<String, IEssentialsChatListener> listeners, + final IEssentials ess, final Map<AsyncPlayerChatEvent, ChatStore> chatStorage) { this.ess = ess; - this.listeners = listeners; this.server = server; this.chatStorage = chatStorage; } @@ -43,31 +40,7 @@ public abstract class EssentialsChatPlayer implements Listener if (event.isCancelled()) { return true; - } - synchronized (listeners) - { - for (Map.Entry<String, IEssentialsChatListener> listener : listeners.entrySet()) - { - try - { - if (listener.getValue().shouldHandleThisChat(event)) - { - return true; - } - } - catch (Throwable t) - { - if (ess.getSettings().isDebug()) - { - logger.log(Level.WARNING, "Error with EssentialsChat listener of " + listener.getKey() + ": " + t.getMessage(), t); - } - else - { - logger.log(Level.WARNING, "Error with EssentialsChat listener of " + listener.getKey() + ": " + t.getMessage()); - } - } - } - } + } return false; } @@ -120,76 +93,4 @@ public abstract class EssentialsChatPlayer implements Listener } return true; } - - protected void sendLocalChat(final AsyncPlayerChatEvent 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 : event.getRecipients()) - { - String type = _("chatTypeLocal"); - final User onlineUser = ess.getUser(onlinePlayer); - if (!onlineUser.equals(sender)) - { - boolean abort = false; - final Location playerLoc = onlineUser.getLocation(); - if (playerLoc.getWorld() != world) - { - abort = true; - } - else - { - final double delta = playerLoc.distanceSquared(loc); - if (delta > chatStore.getRadius()) - { - abort = true; - } - } - if (abort) - { - if (onlineUser.isAuthorized("essentials.chat.spy")) - { - type = type.concat(_("chatTypeSpy")); - } - else - { - continue; - } - } - } - - String message = type.concat(String.format(event.getFormat(), sender.getDisplayName(), event.getMessage())); - synchronized (listeners) - { - for (Map.Entry<String, IEssentialsChatListener> listener : listeners.entrySet()) - { - try - { - message = listener.getValue().modifyMessage(event, onlinePlayer, message); - } - catch (Throwable t) - { - if (ess.getSettings().isDebug()) - { - logger.log(Level.WARNING, "Error with EssentialsChat listener of " + listener.getKey() + ": " + t.getMessage(), t); - } - else - { - logger.log(Level.WARNING, "Error with EssentialsChat listener of " + listener.getKey() + ": " + t.getMessage()); - } - } - } - } - onlineUser.sendMessage(message); - } - } } diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerHighest.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerHighest.java index 9af8cf593..2e65c8959 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerHighest.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerHighest.java @@ -12,10 +12,9 @@ public class EssentialsChatPlayerListenerHighest extends EssentialsChatPlayer { public EssentialsChatPlayerListenerHighest(final Server server, final IEssentials ess, - final Map<String, IEssentialsChatListener> listeners, final Map<AsyncPlayerChatEvent, ChatStore> chatStorage) { - super(server, ess, listeners, chatStorage); + super(server, ess, chatStorage); } @EventHandler(priority = EventPriority.HIGHEST) diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java index 7eaaf2e0f..51472163f 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java @@ -19,10 +19,9 @@ public class EssentialsChatPlayerListenerLowest extends EssentialsChatPlayer { public EssentialsChatPlayerListenerLowest(final Server server, final IEssentials ess, - final Map<String, IEssentialsChatListener> listeners, final Map<AsyncPlayerChatEvent, ChatStore> chatStorage) { - super(server, ess, listeners, chatStorage); + super(server, ess, chatStorage); } @EventHandler(priority = EventPriority.LOWEST) @@ -36,11 +35,12 @@ public class EssentialsChatPlayerListenerLowest extends EssentialsChatPlayer final User user = ess.getUser(event.getPlayer()); - if (user == null) { + if (user == null) + { event.setCancelled(true); return; } - + final ChatStore chatStore = new ChatStore(ess, user, getChatType(event.getMessage())); setChatStore(event, chatStore); @@ -51,16 +51,16 @@ public class EssentialsChatPlayerListenerLowest extends EssentialsChatPlayer String group = user.getGroup(); String world = user.getWorld().getName(); Team team = user.getScoreboard().getPlayerTeam(user.getBase()); - + String format = ess.getSettings().getChatFormat(group); format = format.replace("{0}", group); format = format.replace("{1}", world); format = format.replace("{2}", world.substring(0, 1).toUpperCase(Locale.ENGLISH)); - format = format.replace("{3}", team == null ? "" : team.getPrefix()); - format = format.replace("{4}", team == null ? "" : team.getSuffix()); - format = format.replace("{5}", team == null ? "" : team.getDisplayName()); + format = format.replace("{3}", team == null ? "" : team.getPrefix()); + format = format.replace("{4}", team == null ? "" : team.getSuffix()); + format = format.replace("{5}", team == null ? "" : team.getDisplayName()); synchronized (format) - { + { event.setFormat(format); } } diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerNormal.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerNormal.java index 910518302..4fe8e7df5 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerNormal.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerNormal.java @@ -3,9 +3,17 @@ package com.earth2me.essentials.chat; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.User; +import static com.earth2me.essentials.chat.EssentialsChatPlayer.logger; +import java.util.HashSet; +import java.util.Iterator; import java.util.Locale; import java.util.Map; +import java.util.Set; +import net.ess3.api.events.LocalChatSpyEvent; +import org.bukkit.Location; import org.bukkit.Server; +import org.bukkit.World; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.AsyncPlayerChatEvent; @@ -15,12 +23,11 @@ public class EssentialsChatPlayerListenerNormal extends EssentialsChatPlayer { public EssentialsChatPlayerListenerNormal(final Server server, final IEssentials ess, - final Map<String, IEssentialsChatListener> listeners, final Map<AsyncPlayerChatEvent, ChatStore> chatStorage) { - super(server, ess, listeners, chatStorage); + super(server, ess, chatStorage); } - + @EventHandler(priority = EventPriority.NORMAL) @Override public void onPlayerChat(final AsyncPlayerChatEvent event) @@ -33,40 +40,110 @@ 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 - */ + */ long radius = ess.getSettings().getChatRadius(); if (radius < 1) { return; } radius *= radius; - + final ChatStore chatStore = getChatStore(event); final User user = chatStore.getUser(); chatStore.setRadius(radius); - + if (event.getMessage().length() > 1 && chatStore.getType().length() > 0) { final StringBuilder permission = new StringBuilder(); 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())); + 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(event, chatStore); + + final Location loc = user.getLocation(); + final World world = loc.getWorld(); + + if (charge(event, chatStore) == false) + { + return; + } + + Set<Player> outList = event.getRecipients(); + Set<Player> spyList = new HashSet<Player>(); + + try + { + outList.add(event.getPlayer()); + } + catch (UnsupportedOperationException ex) + { + if (ess.getSettings().isDebug()) + { + ess.getLogger().info("Plugin triggered custom chat event, local chat handling aborted."); + } + return; + } + + String type = _("chatTypeLocal"); + event.setFormat(type.concat(event.getFormat())); + + logger.info(_("localFormat", user.getName(), event.getMessage())); + + final Iterator<Player> it = outList.iterator(); + while (it.hasNext()) + { + final Player onlinePlayer = it.next(); + final User onlineUser = ess.getUser(onlinePlayer); + if (!onlineUser.equals(user)) + { + boolean abort = false; + final Location playerLoc = onlineUser.getLocation(); + if (playerLoc.getWorld() != world) + { + abort = true; + } + else + { + final double delta = playerLoc.distanceSquared(loc); + if (delta > chatStore.getRadius()) + { + abort = true; + } + } + if (abort) + { + if (onlineUser.isAuthorized("essentials.chat.spy")) + { + spyList.add(onlinePlayer); + } + outList.remove(onlinePlayer); + } + } + } + + LocalChatSpyEvent spyEvent = new LocalChatSpyEvent(event.isAsynchronous(), event.getPlayer(), event.getFormat(), event.getMessage(), spyList); + server.getPluginManager().callEvent(spyEvent); + + if (!spyEvent.isCancelled()) + { + for (Player onlinePlayer : spyEvent.getRecipients()) + { + onlinePlayer.sendMessage(String.format(event.getFormat(), user.getDisplayName(), event.getMessage())); + } + } } } diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/IEssentialsChatListener.java b/EssentialsChat/src/com/earth2me/essentials/chat/IEssentialsChatListener.java deleted file mode 100644 index abf5893ea..000000000 --- a/EssentialsChat/src/com/earth2me/essentials/chat/IEssentialsChatListener.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.earth2me.essentials.chat; - -import org.bukkit.entity.Player; -import org.bukkit.event.player.AsyncPlayerChatEvent; - - -public interface IEssentialsChatListener -{ - boolean shouldHandleThisChat(AsyncPlayerChatEvent event); - - String modifyMessage(AsyncPlayerChatEvent event, Player target, String message); -} |