This approach is hopefully better.

This commit is contained in:
Olof Larsson 2013-08-22 13:37:08 +02:00
parent c5be1754c4
commit 04aca214e1
2 changed files with 10 additions and 38 deletions

View File

@ -74,7 +74,6 @@ 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,11 +1,6 @@
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.EventPriority;
@ -17,6 +12,7 @@ import org.bukkit.plugin.EventExecutor;
import com.massivecraft.factions.Factions; import com.massivecraft.factions.Factions;
import com.massivecraft.factions.chat.ChatFormatter; import com.massivecraft.factions.chat.ChatFormatter;
import com.massivecraft.factions.entity.MConf; import com.massivecraft.factions.entity.MConf;
import com.massivecraft.mcore.event.MCorePlayerToRecipientChatEvent;
public class FactionsListenerChat implements Listener public class FactionsListenerChat implements Listener
{ {
@ -45,9 +41,9 @@ 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) if (MConf.get().chatParseTags)
{ {
Bukkit.getPluginManager().registerEvent(AsyncPlayerChatEvent.class, this, EventPriority.MONITOR, new ParseRelcolorEventExecutor(), Factions.get(), true); Bukkit.getPluginManager().registerEvent(MCorePlayerToRecipientChatEvent.class, this, EventPriority.NORMAL, new ParseRelcolorEventExecutor(), Factions.get(), true);
} }
} }
@ -74,7 +70,7 @@ public class FactionsListenerChat implements Listener
} }
public static void setFormat(AsyncPlayerChatEvent event) public static void setFormat(AsyncPlayerChatEvent event)
{ {
event.setFormat(MConf.get().chatSetFormatTo); event.setFormat(MConf.get().chatSetFormatTo);
} }
@ -117,8 +113,8 @@ public class FactionsListenerChat implements Listener
{ {
try try
{ {
if (!(event instanceof AsyncPlayerChatEvent)) return; if (!(event instanceof MCorePlayerToRecipientChatEvent)) return;
parseRelcolor((AsyncPlayerChatEvent)event); parseRelcolor((MCorePlayerToRecipientChatEvent)event);
} }
catch (Throwable t) catch (Throwable t)
{ {
@ -127,34 +123,11 @@ public class FactionsListenerChat implements Listener
} }
} }
public static void parseRelcolor(AsyncPlayerChatEvent event) public static void parseRelcolor(MCorePlayerToRecipientChatEvent event)
{ {
// Pick the recipients! String format = event.getFormat();
Set<Player> recipients = new HashSet<Player>(); format = ChatFormatter.format(format, event.getSender(), event.getRecipient());
if (event.getRecipients().isEmpty()) event.setFormat(format);
{
// 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);
}
} }
} }