From 795eacf38d1ee0c8fce9822b7147744d467d1d9f Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 4 Nov 2016 21:33:28 +0100 Subject: [PATCH] Cleaned PlayerListener a little bit --- src/main/java/buttondevteam/chat/Channel.java | 32 ++- .../buttondevteam/chat/ChatProcessing.java | 9 +- .../java/buttondevteam/chat/PluginMain.java | 3 + .../listener/PlayerJoinLeaveListener.java | 126 ++++++++++ .../chat/listener/PlayerListener.java | 225 +++--------------- 5 files changed, 194 insertions(+), 201 deletions(-) create mode 100644 src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java diff --git a/src/main/java/buttondevteam/chat/Channel.java b/src/main/java/buttondevteam/chat/Channel.java index b84f681..941c15c 100644 --- a/src/main/java/buttondevteam/chat/Channel.java +++ b/src/main/java/buttondevteam/chat/Channel.java @@ -1,5 +1,8 @@ package buttondevteam.chat; +import java.util.ArrayList; +import java.util.List; + import buttondevteam.chat.formatting.ChatFormatter; public class Channel { @@ -7,20 +10,29 @@ public class Channel { public final ChatFormatter.Color Color; public final String Command; + private static List channels = new ArrayList<>(); + public Channel(String displayname, ChatFormatter.Color color, String command) { DisplayName = displayname; Color = color; Command = command; } - public static Channel GlobalChat = new Channel("§fg§f", - ChatFormatter.Color.White, "g"); - public static Channel TownChat = new Channel("§3TC§f", - ChatFormatter.Color.DarkAqua, "tc"); - public static Channel NationChat = new Channel("§6NC§f", - ChatFormatter.Color.Gold, "nc"); - public static Channel AdminChat = new Channel("§cADMIN§f", - ChatFormatter.Color.Red, "a"); - public static Channel ModChat = new Channel("§9MOD§f", - ChatFormatter.Color.Blue, "mod"); + static { + channels.add(GlobalChat = new Channel("§fg§f", ChatFormatter.Color.White, "g")); + channels.add(TownChat = new Channel("§3TC§f", ChatFormatter.Color.DarkAqua, "tc")); + channels.add(NationChat = new Channel("§6NC§f", ChatFormatter.Color.Gold, "nc")); + channels.add(AdminChat = new Channel("§cADMIN§f", ChatFormatter.Color.Red, "a")); + channels.add(ModChat = new Channel("§9MOD§f", ChatFormatter.Color.Blue, "mod")); + } + + public static List getChannels() { + return channels; + } + + public static Channel GlobalChat; + public static Channel TownChat; + public static Channel NationChat; + public static Channel AdminChat; + public static Channel ModChat; } diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 3cfe9cf..b20a6b1 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -29,6 +29,7 @@ import buttondevteam.chat.formatting.TellrawSerializer; import buttondevteam.lib.TBMCPlayer; import buttondevteam.chat.formatting.ChatFormatter.Color; import buttondevteam.chat.formatting.ChatFormatter.Priority; +import buttondevteam.chat.listener.PlayerListener; public class ChatProcessing { private static final Pattern ESCAPE_PATTERN = Pattern.compile("\\\\([\\*\\_\\\\])"); @@ -47,11 +48,11 @@ public class ChatProcessing { // Returns e.setCancelled public static boolean ProcessChat(CommandSender sender, String message) { long processstart = System.nanoTime(); - if (PlayerListener.essentials == null) - PlayerListener.essentials = (Essentials) (Bukkit.getPluginManager().getPlugin("Essentials")); + if (PluginMain.essentials == null) + PluginMain.essentials = (Essentials) (Bukkit.getPluginManager().getPlugin("Essentials")); Player player = (sender instanceof Player ? (Player) sender : null); - if (player != null && PlayerListener.essentials.getUser(player).isMuted()) + if (player != null && PluginMain.essentials.getUser(player).isMuted()) return true; ChatPlayer mp = null; @@ -157,7 +158,7 @@ public class ChatProcessing { else p.playSound(p.getLocation(), PlayerListener.NotificationSound, 1.0f, (float) PlayerListener.NotificationPitch); - return PlayerListener.essentials.getUser(p).getNickname(); + return PluginMain.essentials.getUser(p).getNickname(); } Bukkit.getServer().getLogger().warning( "Player nicknamed " + match + " not found in nickname map but was reported as online."); diff --git a/src/main/java/buttondevteam/chat/PluginMain.java b/src/main/java/buttondevteam/chat/PluginMain.java index c5df39a..fdc8616 100644 --- a/src/main/java/buttondevteam/chat/PluginMain.java +++ b/src/main/java/buttondevteam/chat/PluginMain.java @@ -17,10 +17,12 @@ import org.htmlcleaner.TagNode; import buttondevteam.chat.commands.CommandCaller; import buttondevteam.chat.commands.YeehawCommand; +import buttondevteam.chat.listener.PlayerListener; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCPlayer; import buttondevteam.lib.chat.TBMCChatAPI; +import com.earth2me.essentials.Essentials; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -108,6 +110,7 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. } public Boolean stop = false; + public static Essentials essentials = null; // Fired when plugin is disabled @Override diff --git a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java new file mode 100644 index 0000000..8d8aff2 --- /dev/null +++ b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java @@ -0,0 +1,126 @@ +package buttondevteam.chat.listener; + +import java.util.ArrayList; +import java.util.Timer; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.GameMode; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerQuitEvent; + +import com.earth2me.essentials.Essentials; + +import buttondevteam.chat.ChatPlayer; +import buttondevteam.chat.FlairStates; +import buttondevteam.chat.PlayerJoinTimerTask; +import buttondevteam.chat.PluginMain; +import buttondevteam.lib.TBMCPlayerAddEvent; +import buttondevteam.lib.TBMCPlayerJoinEvent; +import buttondevteam.lib.TBMCPlayerLoadEvent; +import buttondevteam.lib.TBMCPlayerSaveEvent; + +public class PlayerJoinLeaveListener implements Listener { + + @EventHandler + public void onPlayerLoad(TBMCPlayerLoadEvent e) { + ChatPlayer cp = e.GetPlayer().asPluginPlayer(ChatPlayer.class); + cp.FlairUpdate(); + } + + @EventHandler + public void onPlayerTBMCJoin(TBMCPlayerJoinEvent e) { + if (PluginMain.essentials == null) + PluginMain.essentials = ((Essentials) Bukkit.getPluginManager().getPlugin("Essentials")); + ChatPlayer cp = e.GetPlayer().asPluginPlayer(ChatPlayer.class); + Player p = Bukkit.getPlayer(cp.getUuid()); + + if (!cp.getFlairState().equals(FlairStates.NoComment)) { + PluginMain.ConfirmUserMessage(cp); + Timer timer = new Timer(); + PlayerJoinTimerTask tt = new PlayerJoinTimerTask() { + @Override + public void run() { + p.setPlayerListName(p.getName() + mp.GetFormattedFlair()); + } + }; + tt.mp = cp; + timer.schedule(tt, 1000); + } else { + if (cp.getFlairTime() == 0x00) + cp.SetFlair(ChatPlayer.FlairTimeNone); + Timer timer = new Timer(); + PlayerJoinTimerTask tt = new PlayerJoinTimerTask() { + + @Override + public void run() { + Player player = Bukkit.getPlayer(mp.getPlayerName()); + if (player == null) + return; + + if (mp.getFlairState().equals(FlairStates.NoComment)) { + String json = String.format( + "[\"\",{\"text\":\"If you're from Reddit and you'd like your /r/TheButton flair displayed ingame, write your Minecraft name to \",\"color\":\"aqua\"},{\"text\":\"[this thread].\",\"color\":\"aqua\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"%s\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread\",\"color\":\"aqua\"}]}}}]", + PluginMain.FlairThreadURL); + PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, + "tellraw " + mp.getPlayerName() + " " + json); + json = "[\"\",{\"text\":\"If you aren't from Reddit or don't want the flair, type /u ignore to prevent this message after next login.\",\"color\":\"aqua\"}]"; + PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, + "tellraw " + mp.getPlayerName() + " " + json); + } + } + }; + tt.mp = cp; + timer.schedule(tt, 15 * 1000); + } + + String nwithoutformatting = PluginMain.essentials.getUser(p).getNickname(); + int index; + if (nwithoutformatting != null) { + while ((index = nwithoutformatting.indexOf("§k")) != -1) + nwithoutformatting = nwithoutformatting.replace("§k" + nwithoutformatting.charAt(index + 2), ""); // Support for one random char + while ((index = nwithoutformatting.indexOf('§')) != -1) + nwithoutformatting = nwithoutformatting.replace("§" + nwithoutformatting.charAt(index + 1), ""); + } else + nwithoutformatting = p.getName(); + PlayerListener.nicknames.put(nwithoutformatting, p.getUniqueId()); + + cp.RPMode = true; + + cp.FlairUpdate(); + + if (cp.ChatOnly || p.getGameMode().equals(GameMode.SPECTATOR)) { + cp.ChatOnly = false; + p.setGameMode(GameMode.SURVIVAL); + } + } + + @EventHandler + public void onPlayerSave(TBMCPlayerSaveEvent e) { + } + + @EventHandler + public void onPlayerAdd(TBMCPlayerAddEvent event) { + ChatPlayer cp = event.GetPlayer().asPluginPlayer(ChatPlayer.class); + cp.SetFlair(ChatPlayer.FlairTimeNone); + cp.setFlairState(FlairStates.NoComment); + cp.setUserNames(new ArrayList<>()); + } + + @EventHandler + public void onPlayerLeave(PlayerQuitEvent event) { + String deletenick = null; + for (String nickname : PlayerListener.nicknames.keySet()) { + UUID uuid = PlayerListener.nicknames.get(nickname); + if (event.getPlayer().getUniqueId().equals(uuid)) { + deletenick = nickname; + break; + } + } + if (deletenick != null) + PlayerListener.nicknames.remove(deletenick); + } + +} diff --git a/src/main/java/buttondevteam/chat/listener/PlayerListener.java b/src/main/java/buttondevteam/chat/listener/PlayerListener.java index 36b77c1..982d646 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerListener.java @@ -1,6 +1,5 @@ -package buttondevteam.chat; +package buttondevteam.chat.listener; -import java.util.ArrayList; import java.util.Map.Entry; import java.util.Random; import java.util.Timer; @@ -10,7 +9,6 @@ import java.util.UUID; import com.palmergames.bukkit.towny.Towny; import org.bukkit.Bukkit; -import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -25,7 +23,6 @@ import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerPickupItemEvent; -import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.server.ServerCommandEvent; import org.bukkit.help.HelpTopic; @@ -35,16 +32,14 @@ import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import au.com.mineauz.minigames.MinigamePlayer; import au.com.mineauz.minigames.Minigames; +import buttondevteam.chat.Channel; +import buttondevteam.chat.ChatPlayer; +import buttondevteam.chat.ChatProcessing; +import buttondevteam.chat.PluginMain; import buttondevteam.chat.commands.ucmds.KittycannonCommand; import buttondevteam.lib.TBMCPlayer; import buttondevteam.lib.TBMCPlayer.InfoTarget; -import buttondevteam.lib.TBMCPlayerAddEvent; import buttondevteam.lib.TBMCPlayerGetInfoEvent; -import buttondevteam.lib.TBMCPlayerJoinEvent; -import buttondevteam.lib.TBMCPlayerLoadEvent; -import buttondevteam.lib.TBMCPlayerSaveEvent; - -import com.earth2me.essentials.Essentials; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; @@ -67,112 +62,12 @@ public class PlayerListener implements Listener { public static int LoginWarningCountTotal = 5; - @EventHandler - public void onPlayerLoad(TBMCPlayerLoadEvent e) { - ChatPlayer cp = e.GetPlayer().asPluginPlayer(ChatPlayer.class); - cp.FlairUpdate(); - } - - @EventHandler - public void onPlayerTBMCJoin(TBMCPlayerJoinEvent e) { - if (essentials == null) - essentials = ((Essentials) Bukkit.getPluginManager().getPlugin("Essentials")); - ChatPlayer cp = e.GetPlayer().asPluginPlayer(ChatPlayer.class); - Player p = Bukkit.getPlayer(cp.getUuid()); - - if (!cp.getFlairState().equals(FlairStates.NoComment)) { - PluginMain.ConfirmUserMessage(cp); - Timer timer = new Timer(); - PlayerJoinTimerTask tt = new PlayerJoinTimerTask() { - @Override - public void run() { - p.setPlayerListName(p.getName() + mp.GetFormattedFlair()); - } - }; - tt.mp = cp; - timer.schedule(tt, 1000); - } else { - if (cp.getFlairTime() == 0x00) - cp.SetFlair(ChatPlayer.FlairTimeNone); - Timer timer = new Timer(); - PlayerJoinTimerTask tt = new PlayerJoinTimerTask() { - - @Override - public void run() { - Player player = Bukkit.getPlayer(mp.getPlayerName()); - if (player == null) - return; - - if (mp.getFlairState().equals(FlairStates.NoComment)) { - String json = String.format( - "[\"\",{\"text\":\"If you're from Reddit and you'd like your /r/TheButton flair displayed ingame, write your Minecraft name to \",\"color\":\"aqua\"},{\"text\":\"[this thread].\",\"color\":\"aqua\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"%s\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread\",\"color\":\"aqua\"}]}}}]", - PluginMain.FlairThreadURL); - PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, - "tellraw " + mp.getPlayerName() + " " + json); - json = "[\"\",{\"text\":\"If you aren't from Reddit or don't want the flair, type /u ignore to prevent this message after next login.\",\"color\":\"aqua\"}]"; - PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, - "tellraw " + mp.getPlayerName() + " " + json); - } - } - }; - tt.mp = cp; - timer.schedule(tt, 15 * 1000); - } - - String nwithoutformatting = essentials.getUser(p).getNickname(); - int index; - if (nwithoutformatting != null) { - while ((index = nwithoutformatting.indexOf("§k")) != -1) - nwithoutformatting = nwithoutformatting.replace("§k" + nwithoutformatting.charAt(index + 2), ""); // Support for one random char - while ((index = nwithoutformatting.indexOf('§')) != -1) - nwithoutformatting = nwithoutformatting.replace("§" + nwithoutformatting.charAt(index + 1), ""); - } else - nwithoutformatting = p.getName(); - nicknames.put(nwithoutformatting, p.getUniqueId()); - - cp.RPMode = true; - - cp.FlairUpdate(); - - if (cp.ChatOnly || p.getGameMode().equals(GameMode.SPECTATOR)) { - cp.ChatOnly = false; - p.setGameMode(GameMode.SURVIVAL); - } - } - - @EventHandler - public void onPlayerSave(TBMCPlayerSaveEvent e) { - } - - @EventHandler - public void onPlayerAdd(TBMCPlayerAddEvent event) { - ChatPlayer cp = event.GetPlayer().asPluginPlayer(ChatPlayer.class); - cp.SetFlair(ChatPlayer.FlairTimeNone); - cp.setFlairState(FlairStates.NoComment); - cp.setUserNames(new ArrayList<>()); - } - - @EventHandler - public void onPlayerLeave(PlayerQuitEvent event) { - String deletenick = null; - for (String nickname : nicknames.keySet()) { - UUID uuid = nicknames.get(nickname); - if (event.getPlayer().getUniqueId().equals(uuid)) { - deletenick = nickname; - break; - } - } - if (deletenick != null) - nicknames.remove(deletenick); - } - public static String NotificationSound; public static double NotificationPitch; public static boolean ShowRPTag = false; - public static Essentials essentials = null; - final static String[] LaughStrings = new String[] { "xd", "lel", "lawl", "kek", "lmao", "hue", "hah" }; + public final static String[] LaughStrings = new String[] { "xd", "lel", "lawl", "kek", "lmao", "hue", "hah" }; @EventHandler public void onPlayerChat(AsyncPlayerChatEvent event) { @@ -190,86 +85,42 @@ public class PlayerListener implements Listener { String cmd = ""; if (index == -1) { cmd = event.getMessage().substring(1); - if (cmd.equalsIgnoreCase(Channel.GlobalChat.Command)) { - mp.CurrentChannel = Channel.GlobalChat; - event.getPlayer().sendMessage("§6You are now talking in: §b" + mp.CurrentChannel.DisplayName); - event.setCancelled(true); - } else if (cmd.equalsIgnoreCase(Channel.TownChat.Command)) { - if (mp.CurrentChannel.equals(Channel.TownChat)) - mp.CurrentChannel = Channel.GlobalChat; - else - mp.CurrentChannel = Channel.TownChat; - event.getPlayer().sendMessage("§6You are now talking in: §b" + mp.CurrentChannel.DisplayName); - event.setCancelled(true); - } else if (cmd.equalsIgnoreCase(Channel.NationChat.Command)) { - if (mp.CurrentChannel.equals(Channel.NationChat)) - mp.CurrentChannel = Channel.GlobalChat; - else - mp.CurrentChannel = Channel.NationChat; - event.getPlayer().sendMessage("§6You are now talking in: §b" + mp.CurrentChannel.DisplayName); - event.setCancelled(true); - } else if (cmd.equalsIgnoreCase(Channel.AdminChat.Command)) { - if (mp.CurrentChannel.equals(Channel.AdminChat)) - mp.CurrentChannel = Channel.GlobalChat; - else - mp.CurrentChannel = Channel.AdminChat; - event.getPlayer().sendMessage("§6You are now talking in: §b" + mp.CurrentChannel.DisplayName); - event.setCancelled(true); - } else if (cmd.equalsIgnoreCase(Channel.ModChat.Command)) { - if (mp.CurrentChannel.equals(Channel.ModChat)) - mp.CurrentChannel = Channel.GlobalChat; - else - mp.CurrentChannel = Channel.ModChat; - event.getPlayer().sendMessage("§6You are now talking in: §b" + mp.CurrentChannel.DisplayName); - event.setCancelled(true); + for (Channel channel : Channel.getChannels()) { + if (cmd.equalsIgnoreCase(channel.Command)) { + if (mp.CurrentChannel.equals(channel)) + mp.CurrentChannel = Channel.GlobalChat; + else + mp.CurrentChannel = channel; + event.getPlayer().sendMessage("§6You are now talking in: §b" + mp.CurrentChannel.DisplayName); + event.setCancelled(true); + break; + } } } else { cmd = event.getMessage().substring(1, index); - if (cmd.equalsIgnoreCase(Channel.GlobalChat.Command)) { - event.setCancelled(true); - Channel c = mp.CurrentChannel; - mp.CurrentChannel = Channel.GlobalChat; - event.getPlayer().chat(event.getMessage().substring(index + 1)); - mp.CurrentChannel = c; - } else if (cmd.equalsIgnoreCase(Channel.TownChat.Command)) { - event.setCancelled(true); - Channel c = mp.CurrentChannel; - mp.CurrentChannel = Channel.TownChat; - event.getPlayer().chat(event.getMessage().substring(index + 1)); - mp.CurrentChannel = c; - } else if (cmd.equalsIgnoreCase(Channel.NationChat.Command)) { - event.setCancelled(true); - Channel c = mp.CurrentChannel; - mp.CurrentChannel = Channel.NationChat; - event.getPlayer().chat(event.getMessage().substring(index + 1)); - mp.CurrentChannel = c; - } else if (cmd.equalsIgnoreCase(Channel.AdminChat.Command)) { - event.setCancelled(true); - Channel c = mp.CurrentChannel; - mp.CurrentChannel = Channel.AdminChat; - event.getPlayer().chat(event.getMessage().substring(index + 1)); - mp.CurrentChannel = c; - } else if (cmd.equalsIgnoreCase(Channel.ModChat.Command)) { - event.setCancelled(true); - Channel c = mp.CurrentChannel; - mp.CurrentChannel = Channel.ModChat; - event.getPlayer().chat(event.getMessage().substring(index + 1)); - mp.CurrentChannel = c; - } else if (cmd.equalsIgnoreCase("tpahere")) { - Player player = Bukkit.getPlayer(event.getMessage().substring(index + 1)); - if (player != null) - player.sendMessage("§b" + event.getPlayer().getDisplayName() + " §bis in this world: " - + event.getPlayer().getWorld().getName()); - } else if (cmd.equalsIgnoreCase("minecraft:me")) { - if (!essentials.getUser(event.getPlayer()).isMuted()) { + for (Channel channel : Channel.getChannels()) { + if (cmd.equalsIgnoreCase(channel.Command)) { event.setCancelled(true); - String message = event.getMessage().substring(index + 1); - for (Player p : PluginMain.GetPlayers()) - p.sendMessage(String.format("* %s %s", event.getPlayer().getDisplayName(), message)); + Channel c = mp.CurrentChannel; + mp.CurrentChannel = channel; + event.getPlayer().chat(event.getMessage().substring(index + 1)); + mp.CurrentChannel = c; + } else if (cmd.equalsIgnoreCase("tpahere")) { + Player player = Bukkit.getPlayer(event.getMessage().substring(index + 1)); + if (player != null) + player.sendMessage("§b" + event.getPlayer().getDisplayName() + " §bis in this world: " + + event.getPlayer().getWorld().getName()); + } else if (cmd.equalsIgnoreCase("minecraft:me")) { + if (!PluginMain.essentials.getUser(event.getPlayer()).isMuted()) { + event.setCancelled(true); + String message = event.getMessage().substring(index + 1); + for (Player p : PluginMain.GetPlayers()) + p.sendMessage(String.format("* %s %s", event.getPlayer().getDisplayName(), message)); + } } } } - if (cmd.equalsIgnoreCase("sethome")) { + if (cmd.equalsIgnoreCase("sethome")) { // TODO: Move out? TownyUniverse tu = PluginMain.Instance.TU; try { TownBlock tb = WorldCoord.parseWorldCoord(event.getPlayer()).getTownBlock(); @@ -349,9 +200,9 @@ public class PlayerListener implements Listener { } } - static boolean ActiveF = false; - static int FCount = 0; - static ChatPlayer FPlayer = null; + public static boolean ActiveF = false; + public static int FCount = 0; + public static ChatPlayer FPlayer = null; private Timer Ftimer; public static int AlphaDeaths;