From c3f57d5105d9bb6a99516e2b131dc60e4600c297 Mon Sep 17 00:00:00 2001 From: Brettflan Date: Sun, 24 Jul 2011 06:09:58 -0500 Subject: [PATCH] added a secondary low priority event listener for chat events, to try and make sure Factions is able to handle slashless commands and faction chat before other plugins do anything --- src/com/massivecraft/factions/Factions.java | 3 ++ .../listeners/FactionsChatEarlyListener.java | 51 +++++++++++++++++++ .../listeners/FactionsPlayerListener.java | 18 ------- 3 files changed, 54 insertions(+), 18 deletions(-) create mode 100644 src/com/massivecraft/factions/listeners/FactionsChatEarlyListener.java diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index e10a0ee0..60b9021f 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -22,6 +22,7 @@ import com.massivecraft.factions.commands.*; import com.massivecraft.factions.gson.Gson; import com.massivecraft.factions.gson.GsonBuilder; import com.massivecraft.factions.listeners.FactionsBlockListener; +import com.massivecraft.factions.listeners.FactionsChatEarlyListener; import com.massivecraft.factions.listeners.FactionsEntityListener; import com.massivecraft.factions.listeners.FactionsPlayerListener; @@ -47,6 +48,7 @@ public class Factions extends JavaPlugin { .create(); private final FactionsPlayerListener playerListener = new FactionsPlayerListener(); + private final FactionsChatEarlyListener chatEarlyListener = new FactionsChatEarlyListener(); private final FactionsEntityListener entityListener = new FactionsEntityListener(); private final FactionsBlockListener blockListener = new FactionsBlockListener(); @@ -127,6 +129,7 @@ public class Factions extends JavaPlugin { // Register events PluginManager pm = this.getServer().getPluginManager(); pm.registerEvent(Event.Type.PLAYER_CHAT, this.playerListener, Event.Priority.Highest, this); + pm.registerEvent(Event.Type.PLAYER_CHAT, this.chatEarlyListener, Event.Priority.Lowest, this); pm.registerEvent(Event.Type.PLAYER_INTERACT, this.playerListener, Event.Priority.Normal, this); pm.registerEvent(Event.Type.PLAYER_MOVE, this.playerListener, Event.Priority.Normal, this); pm.registerEvent(Event.Type.PLAYER_JOIN, this.playerListener, Event.Priority.Normal, this); diff --git a/src/com/massivecraft/factions/listeners/FactionsChatEarlyListener.java b/src/com/massivecraft/factions/listeners/FactionsChatEarlyListener.java new file mode 100644 index 00000000..092eedcb --- /dev/null +++ b/src/com/massivecraft/factions/listeners/FactionsChatEarlyListener.java @@ -0,0 +1,51 @@ +package com.massivecraft.factions.listeners; + +import java.util.List; +import java.util.logging.Logger; + +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerChatEvent; +import org.bukkit.event.player.PlayerListener; + +import com.massivecraft.factions.Conf; +import com.massivecraft.factions.FPlayer; +import com.massivecraft.factions.Factions; +import com.massivecraft.factions.util.TextUtil; + + +// this is an addtional PlayerListener for handling slashless command usage and faction chat, to be set at low priority so Factions gets to them first +public class FactionsChatEarlyListener extends PlayerListener{ + + @Override + public void onPlayerChat(PlayerChatEvent event) { + if ((event.getMessage().startsWith(Factions.instance.getBaseCommand()+" ") || event.getMessage().equals(Factions.instance.getBaseCommand())) && Conf.allowNoSlashCommand) { + List parameters = TextUtil.split(event.getMessage().trim()); + parameters.remove(0); + CommandSender sender = event.getPlayer(); + Factions.instance.handleCommand(sender, parameters); + event.setCancelled(true); + return; + } + + if (event.isCancelled()) { + return; + } + + Player talkingPlayer = event.getPlayer(); + String msg = event.getMessage(); + + // ... it was not a command. This means that it is a chat message! + FPlayer me = FPlayer.get(talkingPlayer); + + // Is it a faction chat message? + if (me.isFactionChatting()) { + String message = String.format(Conf.factionChatFormat, me.getNameAndRelevant(me), msg); + me.getFaction().sendMessage(message); + Logger.getLogger("Minecraft").info(ChatColor.stripColor("FactionChat "+me.getFaction().getTag()+": "+message)); + event.setCancelled(true); + return; + } + } +} diff --git a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java index 264d0a43..734f9615 100644 --- a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java @@ -35,15 +35,6 @@ public class FactionsPlayerListener extends PlayerListener{ @Override public void onPlayerChat(PlayerChatEvent event) { - if ((event.getMessage().startsWith(Factions.instance.getBaseCommand()+" ") || event.getMessage().equals(Factions.instance.getBaseCommand())) && Conf.allowNoSlashCommand) { - List parameters = TextUtil.split(event.getMessage().trim()); - parameters.remove(0); - CommandSender sender = event.getPlayer(); - Factions.instance.handleCommand(sender, parameters); - event.setCancelled(true); - return; - } - if (event.isCancelled()) { return; } @@ -54,15 +45,6 @@ public class FactionsPlayerListener extends PlayerListener{ // ... it was not a command. This means that it is a chat message! FPlayer me = FPlayer.get(talkingPlayer); - // Is it a faction chat message? - if (me.isFactionChatting()) { - String message = String.format(Conf.factionChatFormat, me.getNameAndRelevant(me), msg); - me.getFaction().sendMessage(message); - Logger.getLogger("Minecraft").info(ChatColor.stripColor("FactionChat "+me.getFaction().getTag()+": "+message)); - event.setCancelled(true); - return; - } - // Are we to insert the Faction tag into the format? // If we are not to insert it - we are done. if ( ! Conf.chatTagEnabled || Conf.chatTagHandledByAnotherPlugin) {