From a71b3a19f33d450c12c84ea8b346affd8ad89e41 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 21 Aug 2013 09:51:43 +0200 Subject: [PATCH] Add in optional relcolor parsing. Since it rarely may break other advanced chat plugins it can be opted out from in the mconf. Enabled by default. --- .../factions/chat/tag/ChatTagRelcolor.java | 5 +- .../massivecraft/factions/entity/MConf.java | 1 + .../listeners/FactionsListenerChat.java | 63 +++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/com/massivecraft/factions/chat/tag/ChatTagRelcolor.java b/src/com/massivecraft/factions/chat/tag/ChatTagRelcolor.java index 273fba58..7c23c2ed 100644 --- a/src/com/massivecraft/factions/chat/tag/ChatTagRelcolor.java +++ b/src/com/massivecraft/factions/chat/tag/ChatTagRelcolor.java @@ -23,10 +23,11 @@ public class ChatTagRelcolor extends ChatTagAbstract @Override public String getReplacement(CommandSender sender, CommandSender recipient) { - if (recipient == null) return ""; - // Check disabled if (UConf.isDisabled(sender)) return ""; + + // Opt out if no recipient + if (recipient == null) return null; // Get entities UPlayer usender = UPlayer.get(sender); diff --git a/src/com/massivecraft/factions/entity/MConf.java b/src/com/massivecraft/factions/entity/MConf.java index fd8a6524..0390c71d 100644 --- a/src/com/massivecraft/factions/entity/MConf.java +++ b/src/com/massivecraft/factions/entity/MConf.java @@ -74,6 +74,7 @@ public class MConf extends Entity // We offer a simple standard way to parse the chat tags public boolean chatParseTags = true; public EventPriority chatParseTagsAt = EventPriority.LOW; + public boolean chatParseRelcolor = true; // HeroChat: The Faction Channel public String herochatFactionName = "Faction"; diff --git a/src/com/massivecraft/factions/listeners/FactionsListenerChat.java b/src/com/massivecraft/factions/listeners/FactionsListenerChat.java index 7b3a84b6..9d208f20 100644 --- a/src/com/massivecraft/factions/listeners/FactionsListenerChat.java +++ b/src/com/massivecraft/factions/listeners/FactionsListenerChat.java @@ -1,8 +1,14 @@ package com.massivecraft.factions.listeners; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.EventException; +import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; @@ -38,6 +44,12 @@ public class FactionsListenerChat implements Listener { Bukkit.getPluginManager().registerEvent(AsyncPlayerChatEvent.class, this, MConf.get().chatParseTagsAt, new ParseTagsEventExecutor(), Factions.get(), true); } + + if (MConf.get().chatParseTags && MConf.get().chatParseRelcolor) + { + Bukkit.getPluginManager().registerEvent(AsyncPlayerChatEvent.class, this, EventPriority.MONITOR, new ParseRelcolorEventExecutor(), Factions.get(), true); + } + } // -------------------------------------------- // @@ -94,4 +106,55 @@ public class FactionsListenerChat implements Listener event.setFormat(format); } + // -------------------------------------------- // + // PARSE RELCOLOR + // -------------------------------------------- // + + private class ParseRelcolorEventExecutor implements EventExecutor + { + @Override + public void execute(Listener listener, Event event) throws EventException + { + try + { + if (!AsyncPlayerChatEvent.class.isAssignableFrom(event.getClass())) return; + parseRelcolor((AsyncPlayerChatEvent)event); + } + catch (Throwable t) + { + throw new EventException(t); + } + } + } + + public static void parseRelcolor(AsyncPlayerChatEvent event) + { + // Pick the recipients! + Set recipients = new HashSet(); + if (event.getRecipients().isEmpty()) + { + // It's empty? Another plugin probably used this trick. Guess all. + recipients.addAll(Arrays.asList(Bukkit.getOnlinePlayers())); + } + else + { + recipients.addAll(event.getRecipients()); + } + // Avoid the message getting sent without canceling the event. + event.getRecipients().clear(); + + // Prepare variables + final Player sender = event.getPlayer(); + + // Send the per recipient message + for (Player recipient : recipients) + { + String format = event.getFormat(); + format = ChatFormatter.format(format, sender, recipient); + + String message = String.format(format, sender.getDisplayName(), event.getMessage()); + recipient.sendMessage(message); + } + } + }