diff options
Diffstat (limited to 'EssentialsChat/src/com')
6 files changed, 360 insertions, 158 deletions
diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java index 7a10d9a92..25ce85cc8 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java @@ -1,13 +1,15 @@ package com.earth2me.essentials.chat; +import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.IEssentials; -import com.earth2me.essentials.Util; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentSkipListMap; import java.util.logging.Level; import java.util.logging.Logger; import org.bukkit.event.Event.Priority; import org.bukkit.event.Event.Type; +import org.bukkit.event.player.PlayerChatEvent; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; @@ -15,34 +17,52 @@ import org.bukkit.plugin.java.JavaPlugin; public class EssentialsChat extends JavaPlugin { private static final Logger LOGGER = Logger.getLogger("Minecraft"); - private Map<String, IEssentialsChatListener> chatListener; + private transient Map<String, IEssentialsChatListener> chatListener; + + @Override public void onEnable() { final PluginManager pluginManager = getServer().getPluginManager(); final IEssentials ess = (IEssentials)pluginManager.getPlugin("Essentials"); - - chatListener = new HashMap<String, IEssentialsChatListener>(); - - final EssentialsChatPlayerListener playerListener = new EssentialsChatPlayerListener(getServer(), ess, chatListener); - pluginManager.registerEvent(Type.PLAYER_CHAT, playerListener, Priority.High, this); if (!this.getDescription().getVersion().equals(ess.getDescription().getVersion())) { - LOGGER.log(Level.WARNING, Util.i18n("versionMismatchAll")); + LOGGER.log(Level.WARNING, _("versionMismatchAll")); } - LOGGER.info(Util.format("loadinfo", this.getDescription().getName(), this.getDescription().getVersion(), "essentials team")); + if (!ess.isEnabled()) + { + this.setEnabled(false); + return; + } + + chatListener = new ConcurrentSkipListMap<String, IEssentialsChatListener>(); + final Map<PlayerChatEvent, String> charges = new HashMap<PlayerChatEvent, String>(); + + + 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); + 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); + + LOGGER.info(_("loadinfo", this.getDescription().getName(), this.getDescription().getVersion(), "essentials team")); } + @Override public void onDisable() { - chatListener.clear(); + 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 new file mode 100644 index 000000000..432e2309c --- /dev/null +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java @@ -0,0 +1,155 @@ +package com.earth2me.essentials.chat; + +import com.earth2me.essentials.ChargeException; +import static com.earth2me.essentials.I18n._; +import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.Trade; +import com.earth2me.essentials.User; +import java.util.Map; +import java.util.logging.Logger; +import org.bukkit.Location; +import org.bukkit.Server; +import org.bukkit.World; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerChatEvent; +import org.bukkit.event.player.PlayerListener; + +//TODO: Translate the local/spy tags +public abstract class EssentialsChatPlayer extends PlayerListener +{ + protected transient IEssentials ess; + protected final static Logger logger = Logger.getLogger("Minecraft"); + protected final transient Map<String, IEssentialsChatListener> listeners; + protected final transient Server server; + + public EssentialsChatPlayer(Server server, IEssentials ess, Map<String, IEssentialsChatListener> listeners) + { + this.ess = ess; + this.listeners = listeners; + this.server = server; + } + + public void onPlayerChat(final PlayerChatEvent event) + { + } + + public boolean isAborted(final PlayerChatEvent event) + { + return isAborted(event, "chat"); + } + + public boolean isAborted(final PlayerChatEvent event, final String command) + { + if (event.isCancelled()) + { + return true; + } + for (IEssentialsChatListener listener : listeners.values()) + { + if (listener.shouldHandleThisChat(event)) + { + return true; + } + } + + final User user = ess.getUser(event.getPlayer()); + if (!isAffordableFor(user, command)) + { + event.setCancelled(true); + return true; + } + return false; + } + + public String getChatType(final String message) + { + switch (message.charAt(0)) + { + case '!': + return "shout"; + case '?': + return "question"; + default: + return ""; + } + } + + protected void charge(final CommandSender sender, final String command) throws ChargeException + { + if (sender instanceof Player) + { + final Trade charge = new Trade(command, ess); + charge.charge(ess.getUser((Player)sender)); + } + } + + protected boolean isAffordableFor(final CommandSender sender, final String command) + { + if (sender instanceof Player) + { + try + { + final Trade charge = new Trade(command, ess); + charge.isAffordableFor(ess.getUser((Player)sender)); + } + catch (ChargeException e) + { + return false; + } + } + else + { + return false; + } + + return true; + } + + protected void sendLocalChat(final User sender, final long radius, final PlayerChatEvent event) + { + event.setCancelled(true); + logger.info(_("localFormat", sender.getName(), event.getMessage())); + final Location loc = sender.getLocation(); + final World world = loc.getWorld(); + + for (Player onlinePlayer : server.getOnlinePlayers()) + { + String type = "[L]"; + final User user = ess.getUser(onlinePlayer); + //TODO: remove reference to op + if (user.isIgnoredPlayer(sender.getName()) && !sender.isOp()) + { + continue; + } + if (!user.equals(sender)) + { + final Location playerLoc = user.getLocation(); + if (playerLoc.getWorld() != world) + { + continue; + } + final double delta = playerLoc.distanceSquared(loc); + + if (delta > radius) + { + if (user.isAuthorized("essentials.chat.spy")) + { + type = type.concat("[Spy]"); + } + else + { + continue; + } + } + } + + String message = String.format(event.getFormat(), type.concat(sender.getDisplayName()), event.getMessage()); + for (IEssentialsChatListener listener : listeners.values()) + { + message = listener.modifyMessage(event, onlinePlayer, message); + } + user.sendMessage(message); + } + } +} diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListener.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListener.java deleted file mode 100644 index 85e821e7f..000000000 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListener.java +++ /dev/null @@ -1,146 +0,0 @@ -package com.earth2me.essentials.chat; - -import com.earth2me.essentials.ChargeException; -import com.earth2me.essentials.IEssentials; -import com.earth2me.essentials.Trade; -import com.earth2me.essentials.User; -import com.earth2me.essentials.Util; -import java.util.Map; -import java.util.logging.Logger; -import org.bukkit.Location; -import org.bukkit.Server; -import org.bukkit.World; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerChatEvent; -import org.bukkit.event.player.PlayerListener; - - -public class EssentialsChatPlayerListener extends PlayerListener -{ - private static final Logger LOGGER = Logger.getLogger("Minecraft"); - private final transient IEssentials ess; - private final transient Server server; - private final transient Map<String, IEssentialsChatListener> listeners; - - public EssentialsChatPlayerListener(final Server server, final IEssentials ess, final Map<String, IEssentialsChatListener> listeners) - { - this.server = server; - this.ess = ess; - this.listeners = listeners; - } - - @Override - public void onPlayerChat(final PlayerChatEvent event) - { - if (event.isCancelled()) - { - return; - } - - for (IEssentialsChatListener listener : listeners.values()) - { - if (listener.shouldHandleThisChat(event)) - { - return; - } - } - - final User user = ess.getUser(event.getPlayer()); - - if (user.isAuthorized("essentials.chat.color")) - { - event.setMessage(event.getMessage().replaceAll("&([0-9a-f])", "§$1")); - } - - event.setFormat(ess.getSettings().getChatFormat(user.getGroup()).replace('&', '§').replace("§§", "&").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())); - - long radius = ess.getSettings().getChatRadius(); - if (radius < 1) - { - return; - } - radius *= radius; - - try { - if (event.getMessage().startsWith("!") && event.getMessage().length() > 1) - { - if (user.isAuthorized("essentials.chat.shout")) - { - charge(user,"chat-shout"); - event.setMessage(event.getMessage().substring(1)); - event.setFormat(Util.format("shoutFormat", event.getFormat())); - return; - } - user.sendMessage(Util.i18n("notAllowedToShout")); - event.setCancelled(true); - return; - } - - if (event.getMessage().startsWith("?") && event.getMessage().length() > 1) - { - if (user.isAuthorized("essentials.chat.question")) - { - charge(user,"chat-question"); - event.setMessage(event.getMessage().substring(1)); - event.setFormat(Util.format("questionFormat", event.getFormat())); - return; - } - user.sendMessage(Util.i18n("notAllowedToQuestion")); - event.setCancelled(true); - return; - } - } - catch (ChargeException ex) - { - ess.showError(user, ex, "Shout"); - return; - } - - event.setCancelled(true); - LOGGER.info(Util.format("localFormat", user.getName(), event.getMessage())); - - final Location loc = user.getLocation(); - final World world = loc.getWorld(); - final int x = loc.getBlockX(); - final int y = loc.getBlockY(); - final int z = loc.getBlockZ(); - - for (Player p : server.getOnlinePlayers()) - { - final User u = ess.getUser(p); - if (u.isIgnoredPlayer(user.getName()) && !user.isOp()) - { - continue; - } - if (!u.equals(user) && !u.isAuthorized("essentials.chat.spy")) - { - final Location l = u.getLocation(); - final int dx = x - l.getBlockX(); - final int dy = y - l.getBlockY(); - final int dz = z - l.getBlockZ(); - final long delta = dx*dx + dy*dy + dz*dz; - if (delta > radius || world != l.getWorld()) - { - continue; - } - } - String message = String.format(event.getFormat(), user.getDisplayName(), event.getMessage()); - - for (IEssentialsChatListener listener : listeners.values()) - { - message = listener.modifyMessage(event, p, message); - } - - u.sendMessage(message); - } - } - protected void charge(final CommandSender sender, final String command) throws ChargeException - { - if (sender instanceof Player) - { - final Trade charge = new Trade(command, ess); - charge.charge(ess.getUser((Player)sender)); - } - } -} diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerHighest.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerHighest.java new file mode 100644 index 000000000..22989d4f9 --- /dev/null +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerHighest.java @@ -0,0 +1,53 @@ +package com.earth2me.essentials.chat; + +import com.earth2me.essentials.ChargeException; +import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.User; +import java.util.Map; +import org.bukkit.Server; +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) + { + super(server, ess, listeners); + this.charges = charges; + } + + @Override + public void onPlayerChat(final PlayerChatEvent event) + { + String charge = charges.remove(event); + if (charge == null) + { + charge = "chat"; + } + if (isAborted(event)) + { + return; + } + + /** + * 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; + } + } +} diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java new file mode 100644 index 000000000..5c674d05d --- /dev/null +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java @@ -0,0 +1,41 @@ +package com.earth2me.essentials.chat; + +import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.User; +import com.earth2me.essentials.Util; +import java.util.Locale; +import java.util.Map; +import org.bukkit.Server; +import org.bukkit.event.player.PlayerChatEvent; + + +public class EssentialsChatPlayerListenerLowest extends EssentialsChatPlayer +{ + public EssentialsChatPlayerListenerLowest(final Server server, + final IEssentials ess, + final Map<String, IEssentialsChatListener> listeners) + { + super(server, ess, listeners); + } + + @Override + public void onPlayerChat(final PlayerChatEvent event) + { + if (isAborted(event)) + { + return; + } + + /** + * 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 { + 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 new file mode 100644 index 000000000..4e3cbefc0 --- /dev/null +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerNormal.java @@ -0,0 +1,79 @@ +package com.earth2me.essentials.chat; + +import static com.earth2me.essentials.I18n._; +import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.User; +import java.util.Locale; +import java.util.Map; +import org.bukkit.Server; +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) + { + super(server, ess, listeners); + this.charges = charges; + } + + @Override + public void onPlayerChat(final PlayerChatEvent event) + { + if (isAborted(event)) + { + return; + } + + /** + * 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 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)); + + if (user.isAuthorized(permission.toString())) + { + event.setMessage(event.getMessage().substring(1)); + event.setFormat(_(format.toString(), event.getFormat())); + charges.put(event, command.toString()); + return; + } + + user.sendMessage(_(errorMsg.toString())); + event.setCancelled(true); + return; + } + + sendLocalChat(user, radius, event); + } +} |