From 3f44221356878001a7bc6583311d40ce745d1138 Mon Sep 17 00:00:00 2001 From: Llm Dl Date: Sun, 10 May 2020 10:11:33 -0500 Subject: [PATCH] - People spying on chat will now see the chat formatted differently from normal. - De-clutters the chat. - Closes: https://github.com/TownyAdvanced/Towny/issues/2665 --- resources/changelog.txt | 5 +- .../TownyChat/channels/StandardChannel.java | 71 +++++++++++++++++-- 2 files changed, 69 insertions(+), 7 deletions(-) diff --git a/resources/changelog.txt b/resources/changelog.txt index 6f67fa5..f382f70 100644 --- a/resources/changelog.txt +++ b/resources/changelog.txt @@ -301,4 +301,7 @@ v0.69: - Fix missing spaces from {title} & {surname} & {townypostfix}. v0.70: - Allow for $ and \ to be used in the chat line. - - Closes: https://github.com/TownyAdvanced/Towny/issues/3960 \ No newline at end of file + - Closes: https://github.com/TownyAdvanced/Towny/issues/3960 + - People spying on chat will now see the chat formatted differently from normal. + - De-clutters the chat. + - Closes: https://github.com/TownyAdvanced/Towny/issues/2665 \ No newline at end of file diff --git a/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java b/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java index 940fbce..2f44f26 100644 --- a/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java +++ b/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java @@ -19,6 +19,7 @@ import com.palmergames.bukkit.util.Colors; import me.clip.placeholderapi.PlaceholderAPI; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.dynmap.DynmapAPI; @@ -78,7 +79,6 @@ public class StandardChannel extends Channel { } Format = ChatSettings.getRelevantFormatGroup(player).getTOWN(); recipients = new HashSet<>(findRecipients(player, TownyAPI.getInstance().getOnlinePlayers(town))); - recipients = checkSpying(recipients); break; case NATION: @@ -88,7 +88,6 @@ public class StandardChannel extends Channel { } Format = ChatSettings.getRelevantFormatGroup(player).getNATION(); recipients = new HashSet<>(findRecipients(player, TownyAPI.getInstance().getOnlinePlayers(nation))); - recipients = checkSpying(recipients); break; case ALLIANCE: @@ -98,7 +97,6 @@ public class StandardChannel extends Channel { } Format = ChatSettings.getRelevantFormatGroup(player).getALLIANCE(); recipients = new HashSet<>(findRecipients(player, TownyAPI.getInstance().getOnlinePlayersAlliance(nation))); - recipients = checkSpying(recipients); break; case DEFAULT: @@ -185,9 +183,13 @@ public class StandardChannel extends Channel { dynMap.postPlayerMessageToWeb(player, event.getMessage()); break; } + + sendSpyMessage(event, exec); } + + /** * Check the distance between players and return a result based upon the range setting * -1 = no limit @@ -239,10 +241,9 @@ public class StandardChannel extends Channel { if (TownyUniverse.getInstance().getPermissionSource().has(test, getPermission())) { /* - * If the player is within range for this channel - * or the recipient has the spy mode. + * If the player is within range for this channel. */ - if ((testDistance(sender, test, getRange())) || (plugin.getTowny().hasPlayerMode(test, "spy"))) { + if (testDistance(sender, test, getRange())){ if (bEssentials) { try { @@ -275,6 +276,64 @@ public class StandardChannel extends Channel { return recipients; } + /** + * Sends messages to spies who have not already seen the message naturally. + * + * @param event - Chat Event. + * @param type - Channel Type + */ + private void sendSpyMessage(AsyncPlayerChatEvent event, channelTypes type) { + Set recipients = new HashSet<>(); + Set spies = new HashSet<>(); + + recipients.addAll(event.getRecipients()); + spies = checkSpying(spies); + String format = formatSpyMessage(type, event.getPlayer()); + + // Remove spies who've already seen the message naturally. + for (Player spy : spies) + if (recipients.contains(spy)) + spies.remove(spy); + else + spy.sendMessage(ChatColor.GOLD + "[SPY] " + ChatColor.WHITE + format + ": " + event.getMessage()); + } + + /** + * Formats look of message for spies + * + * @param type - Channel Type. + * @param player - Player who chatted. + * @return format - Message format. + */ + private String formatSpyMessage(channelTypes type, Player player) { + Resident resident = null; + Town town = null; + Nation nation = null; + try { + resident = TownyUniverse.getInstance().getDataSource().getResident(player.getName()); + town = resident.getTown(); + nation = resident.getTown().getNation(); + } catch (NotRegisteredException e1) { + // Not in a town/nation (doesn't matter which) + } + String format = ChatColor.translateAlternateColorCodes('&', getChannelTag()); + switch (type) { + case TOWN: + format = format + " [" + town.getName() + "] " + resident.getName(); + break; + case NATION: + case ALLIANCE: + format = format + " [" + nation.getName() + "] " + resident.getName(); + break; + case GLOBAL: + case PRIVATE: + case DEFAULT: + format = format + " " + resident.getName(); + break; + } + return format; + } + /** * Add all spying players to the recipients list. *