diff options
author | okamosy <okamosy@gmail.com> | 2011-10-30 21:29:56 +0000 |
---|---|---|
committer | okamosy <okamosy@gmail.com> | 2011-10-30 21:29:56 +0000 |
commit | a7e046e892ef1766c7cdb231581ab8fe1c885f46 (patch) | |
tree | bee5c79582bbc9f48a173ed5d93754a02980fdfd | |
parent | 4fc08153ad6a53e06a8f284a109200d6c4242d6f (diff) | |
download | Essentials-a7e046e892ef1766c7cdb231581ab8fe1c885f46.tar Essentials-a7e046e892ef1766c7cdb231581ab8fe1c885f46.tar.gz Essentials-a7e046e892ef1766c7cdb231581ab8fe1c885f46.tar.lz Essentials-a7e046e892ef1766c7cdb231581ab8fe1c885f46.tar.xz Essentials-a7e046e892ef1766c7cdb231581ab8fe1c885f46.zip |
EssChat update part 1.
6 files changed, 333 insertions, 148 deletions
diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java index 7a10d9a92..c0559ddef 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java @@ -24,8 +24,12 @@ public class EssentialsChat extends JavaPlugin chatListener = new HashMap<String, IEssentialsChatListener>(); - final EssentialsChatPlayerListener playerListener = new EssentialsChatPlayerListener(getServer(), ess, chatListener); - pluginManager.registerEvent(Type.PLAYER_CHAT, playerListener, Priority.High, this); + final EssentialsChatPlayerListenerLowest playerListenerLowest = new EssentialsChatPlayerListenerLowest(getServer(), ess, chatListener); + final EssentialsChatPlayerListenerNormal playerListenerNormal = new EssentialsChatPlayerListenerNormal(getServer(), ess, chatListener); + final EssentialsChatPlayerListenerHighest playerListenerHighest = new EssentialsChatPlayerListenerHighest(getServer(), ess, chatListener); + 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); if (!this.getDescription().getVersion().equals(ess.getDescription().getVersion())) { LOGGER.log(Level.WARNING, Util.i18n("versionMismatchAll")); 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..aeaa38d89 --- /dev/null +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java @@ -0,0 +1,163 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +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; + + +/** + * + * @author Seiji + */ +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(Util.format("localFormat", sender.getName(), event.getMessage())); + final Location loc = sender.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()) + { + String type = "[L]"; + final User u = ess.getUser(p); + //TODO: remove reference to op + if (u.isIgnoredPlayer(sender.getName()) && !sender.isOp()) + { + continue; + } + if (!u.equals(sender)) + { + 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()) + { + if (!u.isAuthorized("essentials.chat.spy")) + { + continue; + } + else + { + type = type.concat("[Spy]"); + } + } + } + + String message = String.format(event.getFormat(), type.concat(sender.getDisplayName()), event.getMessage()); + for (IEssentialsChatListener listener : listeners.values()) + { + message = listener.modifyMessage(event, p, message); + } + u.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..17a219acd --- /dev/null +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerHighest.java @@ -0,0 +1,50 @@ +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 +{ + public EssentialsChatPlayerListenerHighest(Server server, IEssentials ess, Map<String, IEssentialsChatListener> listeners) + { + super(server, ess, listeners); + } + + @Override + public void onPlayerChat(final PlayerChatEvent event) + { + 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()); + final String chatType = getChatType(event.getMessage()); + final StringBuilder command = new StringBuilder(); + command.append("chat"); + + if (chatType.length() > 0) + { + command.append("-").append(chatType); + } + + try + { + charge(user, command.toString()); + } + catch (ChargeException e) + { + ess.showError(user, e, command.toString()); + 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..a7ade803b --- /dev/null +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerLowest.java @@ -0,0 +1,38 @@ +package com.earth2me.essentials.chat; + +import com.earth2me.essentials.ChargeException; +import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.User; +import com.earth2me.essentials.Util; +import java.util.Map; +import org.bukkit.Server; +import org.bukkit.event.player.PlayerChatEvent; + + +public class EssentialsChatPlayerListenerLowest extends EssentialsChatPlayer +{ + public EssentialsChatPlayerListenerLowest(Server server, IEssentials ess, 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-f])", "\u00a7$1")); + } + 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())); + } +} 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..d726cb569 --- /dev/null +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListenerNormal.java @@ -0,0 +1,76 @@ +package com.earth2me.essentials.chat; + +import com.earth2me.essentials.ChargeException; +import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.User; +import com.earth2me.essentials.Util; +import java.util.Map; +import org.bukkit.Server; +import org.bukkit.event.player.PlayerChatEvent; + + +public class EssentialsChatPlayerListenerNormal extends EssentialsChatPlayer +{ + public EssentialsChatPlayerListenerNormal(Server server, IEssentials ess, Map<String, IEssentialsChatListener> listeners) + { + super(server, ess, listeners); + } + + @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 User user = ess.getUser(event.getPlayer()); + final String chatType = getChatType(event.getMessage()); + long radius = ess.getSettings().getChatRadius(); + if (radius < 1) + { + return; + } + radius *= radius; + try + { + if (event.getMessage().length() > 0 && chatType.length() > 0) + { + StringBuilder permission = new StringBuilder(); + permission.append("essentials.chat.").append(chatType); + + StringBuilder command = new StringBuilder(); + command.append("chat-").append(chatType); + + StringBuilder format = new StringBuilder(); + format.append(chatType).append("Format"); + + StringBuilder errorMsg = new StringBuilder(); + errorMsg.append("notAllowedTo").append(chatType.substring(0, 1).toUpperCase()).append(chatType.substring(1)); + + if (user.isAuthorized(permission.toString())) + { + charge(user, command.toString()); + event.setMessage(event.getMessage().substring(1)); + event.setFormat(Util.format(format.toString(), event.getFormat())); + return; + } + + user.sendMessage(Util.i18n(errorMsg.toString())); + event.setCancelled(true); + return; + } + } + catch (ChargeException ex) + { + ess.showError(user, ex, "Shout"); + event.setCancelled(true); + return; + } + sendLocalChat(user, radius, event); + } +} |