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.

This commit is contained in:
Olof Larsson 2013-08-21 09:51:43 +02:00
parent faa61345df
commit a71b3a19f3
3 changed files with 67 additions and 2 deletions

View File

@ -23,10 +23,11 @@ public class ChatTagRelcolor extends ChatTagAbstract
@Override @Override
public String getReplacement(CommandSender sender, CommandSender recipient) public String getReplacement(CommandSender sender, CommandSender recipient)
{ {
if (recipient == null) return "";
// Check disabled // Check disabled
if (UConf.isDisabled(sender)) return ""; if (UConf.isDisabled(sender)) return "";
// Opt out if no recipient
if (recipient == null) return null;
// Get entities // Get entities
UPlayer usender = UPlayer.get(sender); UPlayer usender = UPlayer.get(sender);

View File

@ -74,6 +74,7 @@ public class MConf extends Entity<MConf>
// We offer a simple standard way to parse the chat tags // We offer a simple standard way to parse the chat tags
public boolean chatParseTags = true; public boolean chatParseTags = true;
public EventPriority chatParseTagsAt = EventPriority.LOW; public EventPriority chatParseTagsAt = EventPriority.LOW;
public boolean chatParseRelcolor = true;
// HeroChat: The Faction Channel // HeroChat: The Faction Channel
public String herochatFactionName = "Faction"; public String herochatFactionName = "Faction";

View File

@ -1,8 +1,14 @@
package com.massivecraft.factions.listeners; package com.massivecraft.factions.listeners;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.EventException; import org.bukkit.event.EventException;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent; 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); 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); 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<Player> recipients = new HashSet<Player>();
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);
}
}
} }