From 5e20689dc188de17866733e6ec8f2272e4895ed8 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 20 Dec 2016 22:17:47 +0100 Subject: [PATCH] Made broadcasts and Fs better - Made *many* messages sent to each player a broadcast message instead - Removed FCount and PressedF, added the Fs list instead - Added support for *all* CommandSenders for F - Inlined PluginMain.GetPlayers() --- .../buttondevteam/chat/AnnouncerThread.java | 6 +-- .../java/buttondevteam/chat/ChatPlayer.java | 1 - .../buttondevteam/chat/ChatProcessing.java | 50 ++++++++----------- .../java/buttondevteam/chat/PluginMain.java | 5 -- .../chat/commands/UnlolCommand.java | 5 +- .../chat/commands/YeehawCommand.java | 6 +-- .../chat/listener/PlayerListener.java | 34 +++++-------- 7 files changed, 38 insertions(+), 69 deletions(-) diff --git a/src/main/java/buttondevteam/chat/AnnouncerThread.java b/src/main/java/buttondevteam/chat/AnnouncerThread.java index f8872c9..85980a9 100644 --- a/src/main/java/buttondevteam/chat/AnnouncerThread.java +++ b/src/main/java/buttondevteam/chat/AnnouncerThread.java @@ -1,6 +1,6 @@ package buttondevteam.chat; -import org.bukkit.entity.Player; +import org.bukkit.Bukkit; public class AnnouncerThread { private static int AnnounceMessageIndex = 0; @@ -13,9 +13,7 @@ public class AnnouncerThread { Thread.currentThread().interrupt(); } if (PluginMain.AnnounceMessages.size() > AnnounceMessageIndex) { - for (Player player : PluginMain.GetPlayers()) - player.sendMessage(PluginMain.AnnounceMessages - .get(AnnounceMessageIndex)); + Bukkit.broadcastMessage(PluginMain.AnnounceMessages.get(AnnounceMessageIndex)); AnnounceMessageIndex++; if (AnnounceMessageIndex == PluginMain.AnnounceMessages.size()) AnnounceMessageIndex = 0; diff --git a/src/main/java/buttondevteam/chat/ChatPlayer.java b/src/main/java/buttondevteam/chat/ChatPlayer.java index 8d6aa81..9af729c 100644 --- a/src/main/java/buttondevteam/chat/ChatPlayer.java +++ b/src/main/java/buttondevteam/chat/ChatPlayer.java @@ -73,7 +73,6 @@ public class ChatPlayer extends TBMCPlayer { } public boolean RPMode = true; - public boolean PressedF; public Location SavedLocation; public boolean Working; // public int Tables = 10; diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 11b8c45..86380bf 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -56,18 +56,12 @@ public class ChatProcessing { if (player != null && PluginMain.essentials.getUser(player).isMuted()) return true; + if (PlayerListener.ActiveF && !PlayerListener.Fs.contains(sender) && message.equalsIgnoreCase("F")) + PlayerListener.Fs.add(sender); + ChatPlayer mp = null; - if (player != null) { + if (player != null) mp = TBMCPlayer.getPlayer(player).asPluginPlayer(ChatPlayer.class); - if (message.equalsIgnoreCase("F")) { - if (!mp.PressedF && PlayerListener.ActiveF) { - PlayerListener.FCount++; - mp.PressedF = true; - if (PlayerListener.FPlayer != null && PlayerListener.FPlayer.getFCount() < Integer.MAX_VALUE - 1) - PlayerListener.FPlayer.setFCount(PlayerListener.FPlayer.getFCount() + 1); - } - } - } String msg = message.toLowerCase(); if (msg.contains("lol")) { @@ -113,34 +107,30 @@ public class ChatProcessing { // URLs + Rainbow text formatters.add(new ChatFormatterBuilder().setRegex(URL_PATTERN).setFormat(Format.Underlined).setOpenlink("$1") .build()); - if (PluginMain.GetPlayers().size() > 0) { + if (Bukkit.getOnlinePlayers().size() > 0) { StringBuilder namesb = new StringBuilder(); namesb.append("(?i)("); - for (Player p : PluginMain.GetPlayers()) + for (Player p : Bukkit.getOnlinePlayers()) namesb.append(p.getName()).append("|"); namesb.deleteCharAt(namesb.length() - 1); namesb.append(")"); StringBuilder nicksb = new StringBuilder(); nicksb.append("(?i)("); { - final int size = PluginMain.GetPlayers().size(); + final int size = Bukkit.getOnlinePlayers().size(); int index = 0; - for (Player p : PluginMain.GetPlayers()) - { + for (Player p : Bukkit.getOnlinePlayers()) { final String nick = PlayerListener.nicknames.inverse().get(p.getUniqueId()); - if (nick != null) - { - nicksb.append(nick); - if (index < size - 1) - { - nicksb.append("|"); - } - } - index++; + if (nick != null) { + nicksb.append(nick); + if (index < size - 1) { + nicksb.append("|"); + } + } + index++; } nicksb.append(")"); } - formatters .add(new ChatFormatterBuilder().setRegex(Pattern.compile("null")).setColor(Color.DarkRed).build()); // Properly added a bug as a feature @@ -273,7 +263,7 @@ public class ChatProcessing { sender.sendMessage("§cYou are not a player!"); return true; } - for (Player p : PluginMain.GetPlayers()) { + for (Player p : Bukkit.getOnlinePlayers()) { try { Resident resident = PluginMain.Instance.TU.getResidentMap().get(p.getName().toLowerCase()); if (resident != null && !resident.getName().equals(player.getName()) @@ -304,7 +294,7 @@ public class ChatProcessing { index = PluginMain.Instance.Towns.size() - 1; } Objective obj = PluginMain.SB.getObjective("town"); - for (Player p : PluginMain.GetPlayers()) { + for (Player p : Bukkit.getOnlinePlayers()) { try { if (town.getResidents().stream().anyMatch(r -> r.getName().equalsIgnoreCase(p.getName()))) obj.getScore(p.getName()).setScore(index); @@ -344,7 +334,7 @@ public class ChatProcessing { index = PluginMain.Instance.Nations.size() - 1; } Objective obj = PluginMain.SB.getObjective("nation"); - for (Player p : PluginMain.GetPlayers()) { + for (Player p : Bukkit.getOnlinePlayers()) { try { if (nation.getResidents().stream().anyMatch(r -> r.getName().equalsIgnoreCase(p.getName()))) obj.getScore(p.getName()).setScore(index); @@ -361,7 +351,7 @@ public class ChatProcessing { return true; } Objective obj = PluginMain.SB.getObjective("admin"); - for (Player p : PluginMain.GetPlayers()) { + for (Player p : Bukkit.getOnlinePlayers()) { if (p.isOp()) obj.getScore(p.getName()).setScore(1); else @@ -375,7 +365,7 @@ public class ChatProcessing { return true; } Objective obj = PluginMain.SB.getObjective("mod"); - for (Player p : PluginMain.GetPlayers()) { + for (Player p : Bukkit.getOnlinePlayers()) { if (PluginMain.permission.playerInGroup(p, "mod")) obj.getScore(p.getName()).setScore(1); else diff --git a/src/main/java/buttondevteam/chat/PluginMain.java b/src/main/java/buttondevteam/chat/PluginMain.java index eb20145..2a131e9 100644 --- a/src/main/java/buttondevteam/chat/PluginMain.java +++ b/src/main/java/buttondevteam/chat/PluginMain.java @@ -37,7 +37,6 @@ import java.net.URLConnection; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; -import java.util.Collection; import java.util.Date; import java.util.HashSet; import java.util.Set; @@ -253,10 +252,6 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. p.sendMessage("§9A Reddit user commented your name. Is that you?§r §6Type /u accept or /u ignore§r"); } - public static Collection GetPlayers() { - return Instance.getServer().getOnlinePlayers(); - } - public static ArrayList AnnounceMessages = new ArrayList<>(); public static int AnnounceTime = 15 * 60 * 1000; diff --git a/src/main/java/buttondevteam/chat/commands/UnlolCommand.java b/src/main/java/buttondevteam/chat/commands/UnlolCommand.java index fa79fd2..de728cd 100644 --- a/src/main/java/buttondevteam/chat/commands/UnlolCommand.java +++ b/src/main/java/buttondevteam/chat/commands/UnlolCommand.java @@ -6,7 +6,6 @@ import org.bukkit.entity.Player; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; -import buttondevteam.chat.PluginMain; import buttondevteam.lib.chat.TBMCCommandBase; public final class UnlolCommand extends TBMCCommandBase { @@ -31,9 +30,7 @@ public final class UnlolCommand extends TBMCCommandBase { String msg = (sender instanceof Player ? ((Player) sender).getDisplayName() : sender.getName()) + (Lastlolornot ? " unlolled " : " unlaughed ") + (Lastlol instanceof Player ? ((Player) Lastlol).getDisplayName() : Lastlol.getName()); - for (Player pl : PluginMain.GetPlayers()) - pl.sendMessage(msg); - Bukkit.getServer().getConsoleSender().sendMessage(msg); + Bukkit.broadcastMessage(msg); Lastlol = null; } return true; diff --git a/src/main/java/buttondevteam/chat/commands/YeehawCommand.java b/src/main/java/buttondevteam/chat/commands/YeehawCommand.java index 8deb66f..1c7117d 100644 --- a/src/main/java/buttondevteam/chat/commands/YeehawCommand.java +++ b/src/main/java/buttondevteam/chat/commands/YeehawCommand.java @@ -4,7 +4,6 @@ import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import buttondevteam.chat.PluginMain; import buttondevteam.lib.TBMCYEEHAWEvent; import buttondevteam.lib.chat.TBMCCommandBase; @@ -19,12 +18,11 @@ public class YeehawCommand extends TBMCCommandBase { public boolean OnCommand(CommandSender sender, String alias, String[] args) { final String message = "§b* " + (sender instanceof Player ? ((Player) sender).getDisplayName() : sender.getName()) + " §bYEEHAWs."; - for (Player p : PluginMain.GetPlayers()) { + for (Player p : Bukkit.getOnlinePlayers()) { p.playSound(p.getLocation(), "tbmc.yeehaw", 1f, 1f); - p.sendMessage(message); } // Even a cmdblock could yeehaw in theory // Or anyone from Discord - Bukkit.getConsoleSender().sendMessage(message); + Bukkit.broadcastMessage(message); Bukkit.getPluginManager().callEvent(new TBMCYEEHAWEvent(sender)); return true; } diff --git a/src/main/java/buttondevteam/chat/listener/PlayerListener.java b/src/main/java/buttondevteam/chat/listener/PlayerListener.java index 336cbeb..e6234e0 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerListener.java @@ -1,5 +1,6 @@ package buttondevteam.chat.listener; +import java.util.ArrayList; import java.util.Map.Entry; import java.util.Random; import java.util.Timer; @@ -10,6 +11,7 @@ import com.palmergames.bukkit.towny.Towny; import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -111,8 +113,7 @@ public class PlayerListener implements Listener { 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)); + Bukkit.broadcastMessage(String.format("* %s %s", event.getPlayer().getDisplayName(), message)); } } } @@ -167,10 +168,7 @@ public class PlayerListener implements Listener { } if (target != null) { target.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 10 * 20, 5, false, false)); - for (Player pl : PluginMain.GetPlayers()) - pl.sendMessage( - event.getPlayer().getDisplayName() + " un" + s + "'d " + target.getDisplayName()); - Bukkit.getServer().getConsoleSender().sendMessage( + Bukkit.broadcastMessage( event.getPlayer().getDisplayName() + " un" + s + "'d " + target.getDisplayName()); event.setCancelled(true); } @@ -198,10 +196,10 @@ public class PlayerListener implements Listener { } public static boolean ActiveF = false; - public static int FCount = 0; public static ChatPlayer FPlayer = null; private Timer Ftimer; public static int AlphaDeaths; + public static ArrayList Fs = new ArrayList<>(); @EventHandler public void onPlayerDeath(PlayerDeathEvent e) { @@ -212,24 +210,21 @@ public class PlayerListener implements Listener { if (Ftimer != null) Ftimer.cancel(); ActiveF = true; - FCount = 0; + Fs.clear(); FPlayer = TBMCPlayer.getPlayer(e.getEntity().getUniqueId()).asPluginPlayer(ChatPlayer.class); FPlayer.setFDeaths(FPlayer.getFDeaths() + 1); - for (Player p : PluginMain.GetPlayers()) { - ChatPlayer mp = TBMCPlayer.getPlayerAs(p.getUniqueId(), ChatPlayer.class); - mp.PressedF = false; - p.sendMessage("§bPress F to pay respects.§r"); - } + Bukkit.broadcastMessage("§bPress F to pay respects.§r"); Ftimer = new Timer(); TimerTask tt = new TimerTask() { @Override public void run() { if (ActiveF) { ActiveF = false; - for (Player p : PluginMain.GetPlayers()) { - p.sendMessage("§b" + FCount + " " + (FCount == 1 ? "person" : "people") - + " paid their respects.§r"); - } + if (FPlayer != null && FPlayer.getFCount() < Integer.MAX_VALUE - 1) + FPlayer.setFCount(FPlayer.getFCount() + Fs.size()); + Bukkit.broadcastMessage("§b" + Fs.size() + " " + (Fs.size() == 1 ? "person" : "people") + + " paid their respects.§r"); + Fs.clear(); } } }; @@ -315,10 +310,7 @@ public class PlayerListener implements Listener { } if (target != null) { target.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 10 * 20, 5, false, false)); - for (Player pl : PluginMain.GetPlayers()) - pl.sendMessage(event.getSender().getName() + " un" + s + "'d " + target.getDisplayName()); - Bukkit.getServer().getConsoleSender() - .sendMessage(event.getSender().getName() + " un" + s + "'d " + target.getDisplayName()); + Bukkit.broadcastMessage(event.getSender().getName() + " un" + s + "'d " + target.getDisplayName()); event.setCommand("dontrunthiscmd"); } }