diff options
Diffstat (limited to 'EssentialsChat/src/com/earth2me/essentials/chat')
3 files changed, 99 insertions, 164 deletions
diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java index 0271fff6f..428413661 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java @@ -6,30 +6,25 @@ 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.plugin.Plugin; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; public class EssentialsChat extends JavaPlugin { - private static final Logger logger = Logger.getLogger("Minecraft"); - - public EssentialsChat() - { - super(); - } + private static final Logger LOGGER = Logger.getLogger("Minecraft"); public void onEnable() { - PluginManager pm = getServer().getPluginManager(); - EssentialsChatPlayerListener playerListener = new EssentialsChatPlayerListener(getServer()); - pm.registerEvent(Type.PLAYER_JOIN, playerListener, Priority.Monitor, this); - pm.registerEvent(Type.PLAYER_CHAT, playerListener, Priority.Highest, this); - if (!this.getDescription().getVersion().equals(Essentials.getStatic().getDescription().getVersion())) { - logger.log(Level.WARNING, Util.i18n("versionMismatchAll")); + final PluginManager pluginManager = getServer().getPluginManager(); + final EssentialsChatPlayerListener playerListener = new EssentialsChatPlayerListener(getServer()); + pluginManager.registerEvent(Type.PLAYER_JOIN, playerListener, Priority.Lowest, this); + pluginManager.registerEvent(Type.PLAYER_CHAT, playerListener, Priority.Highest, this); + if (!this.getDescription().getVersion().equals(Essentials.getStatic().getDescription().getVersion())) + { + LOGGER.log(Level.WARNING, Util.i18n("versionMismatchAll")); } - logger.info(Util.format("loadinfo", this.getDescription().getName(), this.getDescription().getVersion(), Essentials.AUTHORS)); + LOGGER.info(Util.format("loadinfo", this.getDescription().getName(), this.getDescription().getVersion(), Essentials.AUTHORS)); } public void onDisable() diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListener.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListener.java index d3dff8538..211733818 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListener.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListener.java @@ -1,7 +1,14 @@ package com.earth2me.essentials.chat; import com.earth2me.essentials.Essentials; +import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.User; +import com.earth2me.essentials.Util; +import java.util.logging.Logger; +import org.bukkit.Location; import org.bukkit.Server; +import org.bukkit.World; +import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerChatEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerListener; @@ -10,52 +17,109 @@ import org.bukkit.event.player.PlayerRespawnEvent; public class EssentialsChatPlayerListener extends PlayerListener { - private final Server server; + private static final Logger LOGGER = Logger.getLogger("Minecraft"); + private final transient IEssentials ess = Essentials.getStatic(); + private final transient Server server; - public EssentialsChatPlayerListener(Server server) + public EssentialsChatPlayerListener(final Server server) { this.server = server; } @Override - @SuppressWarnings("CallToThreadDumpStack") - public void onPlayerJoin(PlayerJoinEvent event) + public void onPlayerJoin(final PlayerJoinEvent event) { - try + final User user = ess.getUser(event.getPlayer()); + updateDisplayName(user); + } + + private void updateDisplayName(final User user) + { + final String prefix = ess.getPermissionsHandler().getPrefix(user).replace('&', '§').replace("{WORLDNAME}", user.getWorld().getName()); + final String suffix = ess.getPermissionsHandler().getSuffix(user).replace('&', '§').replace("{WORLDNAME}", user.getWorld().getName()); + + user.setDisplayName(prefix + user.getNick() + suffix + (suffix.length() > 1 && suffix.substring(suffix.length() - 2, suffix.length() - 1).equals("§") ? "" : "§f")); + } + + @Override + public void onPlayerChat(final PlayerChatEvent event) + { + if (event.isCancelled()) { - EssentialsChatWorker.onPlayerJoin(server, event); + return; } - catch (Throwable ex) + final User user = ess.getUser(event.getPlayer()); + updateDisplayName(user); + + if (user.isAuthorized("essentials.chat.color")) { - ex.printStackTrace(); + event.setMessage(event.getMessage().replaceAll("&([0-9a-f])", "§$1")); } - } - @Override - @SuppressWarnings("CallToThreadDumpStack") - public void onPlayerRespawn(PlayerRespawnEvent event) - { - try + 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())); + + final int radius = ess.getSettings().getChatRadius(); + if (radius < 1) { - EssentialsChatWorker.onPlayerRespawn(server, event); + return; } - catch (Throwable ex) + + if (event.getMessage().startsWith("!") && event.getMessage().length() > 1) { - ex.printStackTrace(); + if (user.isAuthorized("essentials.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; } - } - @Override - @SuppressWarnings("CallToThreadDumpStack") - public void onPlayerChat(PlayerChatEvent event) - { - try + if (event.getMessage().startsWith("?") && event.getMessage().length() > 1) { - EssentialsChatWorker.onPlayerChat(server, event); + if (user.isAuthorized("essentials.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 (Throwable ex) + + 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()) { - ex.printStackTrace(); + 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 = Math.abs(x - l.getBlockX()); + final int dy = Math.abs(y - l.getBlockY()); + final int dz = Math.abs(z - l.getBlockZ()); + final int delta = dx + dy + dz; + if (delta > radius || world != l.getWorld()) + { + continue; + } + } + + u.sendMessage(String.format(event.getFormat(), user.getDisplayName(), event.getMessage())); } } } diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatWorker.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatWorker.java deleted file mode 100644 index f43ed29a7..000000000 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatWorker.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.earth2me.essentials.chat; - -import com.earth2me.essentials.Essentials; -import com.earth2me.essentials.User; -import com.earth2me.essentials.Util; -import com.nijikokun.bukkit.Permissions.Permissions; -import java.util.logging.Logger; -import org.bukkit.Location; -import org.bukkit.Server; -import org.bukkit.World; -import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerChatEvent; -import org.bukkit.event.player.PlayerEvent; -import org.bukkit.event.player.PlayerRespawnEvent; - - -public class EssentialsChatWorker -{ - private static final Logger logger = Logger.getLogger("Minecraft"); - - public static void onPlayerRespawn(Server server, PlayerRespawnEvent event) - { - User user = Essentials.getStatic().getUser(event.getPlayer()); - updateDisplayName(user); - } - - public static void onPlayerJoin(Server server, PlayerEvent event) - { - User user = Essentials.getStatic().getUser(event.getPlayer()); - updateDisplayName(user); - } - - private static void updateDisplayName(User user) - { - try - { - String group = user.getGroup(); - try - { - String prefix = Permissions.Security.getGroupPrefix(user.getWorld().getName(), group).replace('&', '§').replace("{WORLDNAME}", user.getWorld().getName()); - String suffix = Permissions.Security.getGroupSuffix(user.getWorld().getName(), group).replace('&', '§').replace("{WORLDNAME}", user.getWorld().getName()); - - user.setDisplayName(prefix + user.getNick() + suffix + (suffix.length() > 1 && suffix.substring(suffix.length() -2).equals("§") ? "" : "§f")); - } - catch (Throwable ex) - { - logger.warning(Util.format("missingPrefixSuffix", group)); - } - } - catch (Throwable ex) - { - logger.warning(Util.i18n("permissionsError")); - } - } - - public static void onPlayerChat(Server server, PlayerChatEvent event) - { - if (event.isCancelled()) return; - User user = Essentials.getStatic().getUser(event.getPlayer()); - updateDisplayName(user); - - if (user.isAuthorized("essentials.chat.color")) - event.setMessage(event.getMessage().replaceAll("&([0-9a-f])", "§$1")); - - event.setFormat(Essentials.getStatic().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())); - - int radius = Essentials.getStatic().getSettings().getChatRadius(); - if (radius < 1) return; - - if (event.getMessage().startsWith("!") && event.getMessage().length() > 1) - { - if (user.isAuthorized("essentials.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")) - { - event.setMessage(event.getMessage().substring(1)); - event.setFormat(Util.format("questionFormat", event.getFormat())); - return; - } - user.sendMessage(Util.i18n("notAllowedToQuestion")); - event.setCancelled(true); - return; - } - - event.setCancelled(true); - logger.info(Util.format("localFormat", user.getName(), event.getMessage())); - - Location loc = user.getLocation(); - World w = loc.getWorld(); - int x = loc.getBlockX(); - int y = loc.getBlockY(); - int z = loc.getBlockZ(); - - for (Player p : server.getOnlinePlayers()) - { - User u = Essentials.getStatic().getUser(p); - if (u.isIgnoredPlayer(user.getName()) && !user.isOp()) { - continue; - } - if (u != user && !u.isAuthorized("essentials.chat.spy")) - { - Location l = u.getLocation(); - int dx = Math.abs(x - l.getBlockX()); - int dy = Math.abs(y - l.getBlockY()); - int dz = Math.abs(z - l.getBlockZ()); - int delta = dx + dy + dz; - if (delta > radius || w != l.getWorld()) continue; - } - - u.sendMessage(String.format(event.getFormat(), user.getDisplayName(), event.getMessage())); - } - } -} |