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

This commit is contained in:
Brettflan 2011-07-24 06:09:58 -05:00
parent c818ddff99
commit c3f57d5105
3 changed files with 54 additions and 18 deletions

View File

@ -22,6 +22,7 @@ import com.massivecraft.factions.commands.*;
import com.massivecraft.factions.gson.Gson; import com.massivecraft.factions.gson.Gson;
import com.massivecraft.factions.gson.GsonBuilder; import com.massivecraft.factions.gson.GsonBuilder;
import com.massivecraft.factions.listeners.FactionsBlockListener; import com.massivecraft.factions.listeners.FactionsBlockListener;
import com.massivecraft.factions.listeners.FactionsChatEarlyListener;
import com.massivecraft.factions.listeners.FactionsEntityListener; import com.massivecraft.factions.listeners.FactionsEntityListener;
import com.massivecraft.factions.listeners.FactionsPlayerListener; import com.massivecraft.factions.listeners.FactionsPlayerListener;
@ -47,6 +48,7 @@ public class Factions extends JavaPlugin {
.create(); .create();
private final FactionsPlayerListener playerListener = new FactionsPlayerListener(); private final FactionsPlayerListener playerListener = new FactionsPlayerListener();
private final FactionsChatEarlyListener chatEarlyListener = new FactionsChatEarlyListener();
private final FactionsEntityListener entityListener = new FactionsEntityListener(); private final FactionsEntityListener entityListener = new FactionsEntityListener();
private final FactionsBlockListener blockListener = new FactionsBlockListener(); private final FactionsBlockListener blockListener = new FactionsBlockListener();
@ -127,6 +129,7 @@ public class Factions extends JavaPlugin {
// Register events // Register events
PluginManager pm = this.getServer().getPluginManager(); PluginManager pm = this.getServer().getPluginManager();
pm.registerEvent(Event.Type.PLAYER_CHAT, this.playerListener, Event.Priority.Highest, this); 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_INTERACT, this.playerListener, Event.Priority.Normal, this);
pm.registerEvent(Event.Type.PLAYER_MOVE, 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); pm.registerEvent(Event.Type.PLAYER_JOIN, this.playerListener, Event.Priority.Normal, this);

View File

@ -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<String> 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;
}
}
}

View File

@ -35,15 +35,6 @@ public class FactionsPlayerListener extends PlayerListener{
@Override @Override
public void onPlayerChat(PlayerChatEvent event) { public void onPlayerChat(PlayerChatEvent event) {
if ((event.getMessage().startsWith(Factions.instance.getBaseCommand()+" ") || event.getMessage().equals(Factions.instance.getBaseCommand())) && Conf.allowNoSlashCommand) {
List<String> 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()) { if (event.isCancelled()) {
return; return;
} }
@ -54,15 +45,6 @@ public class FactionsPlayerListener extends PlayerListener{
// ... it was not a command. This means that it is a chat message! // ... it was not a command. This means that it is a chat message!
FPlayer me = FPlayer.get(talkingPlayer); 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? // Are we to insert the Faction tag into the format?
// If we are not to insert it - we are done. // If we are not to insert it - we are done.
if ( ! Conf.chatTagEnabled || Conf.chatTagHandledByAnotherPlugin) { if ( ! Conf.chatTagEnabled || Conf.chatTagHandledByAnotherPlugin) {