(donington) Consolidated chat event listeners into a single chat listener class.

(Brettflan) Fixed slashless commands not being logged if player was in faction chat or alliance chat mode. Also cleaned up chat code a bit, nothing major.
This commit is contained in:
Brettflan 2012-03-09 21:25:01 -06:00
parent a5bb25eb44
commit fd44983ae2
5 changed files with 190 additions and 197 deletions

View File

@ -26,7 +26,7 @@ import com.massivecraft.factions.integration.LWCFeatures;
import com.massivecraft.factions.integration.SpoutFeatures; import com.massivecraft.factions.integration.SpoutFeatures;
import com.massivecraft.factions.integration.Worldguard; import com.massivecraft.factions.integration.Worldguard;
import com.massivecraft.factions.listeners.FactionsBlockListener; import com.massivecraft.factions.listeners.FactionsBlockListener;
import com.massivecraft.factions.listeners.FactionsChatEarlyListener; import com.massivecraft.factions.listeners.FactionsChatListener;
import com.massivecraft.factions.listeners.FactionsEntityListener; import com.massivecraft.factions.listeners.FactionsEntityListener;
import com.massivecraft.factions.listeners.FactionsPlayerListener; import com.massivecraft.factions.listeners.FactionsPlayerListener;
import com.massivecraft.factions.listeners.FactionsServerListener; import com.massivecraft.factions.listeners.FactionsServerListener;
@ -48,7 +48,7 @@ public class P extends MPlugin
// Listeners // Listeners
public final FactionsPlayerListener playerListener; public final FactionsPlayerListener playerListener;
public final FactionsChatEarlyListener chatEarlyListener; public final FactionsChatListener chatListener;
public final FactionsEntityListener entityListener; public final FactionsEntityListener entityListener;
public final FactionsBlockListener blockListener; public final FactionsBlockListener blockListener;
public final FactionsServerListener serverListener; public final FactionsServerListener serverListener;
@ -67,7 +67,7 @@ public class P extends MPlugin
{ {
p = this; p = this;
this.playerListener = new FactionsPlayerListener(this); this.playerListener = new FactionsPlayerListener(this);
this.chatEarlyListener = new FactionsChatEarlyListener(this); this.chatListener = new FactionsChatListener(this);
this.entityListener = new FactionsEntityListener(this); this.entityListener = new FactionsEntityListener(this);
this.blockListener = new FactionsBlockListener(this); this.blockListener = new FactionsBlockListener(this);
this.serverListener = new FactionsServerListener(this); this.serverListener = new FactionsServerListener(this);
@ -107,7 +107,7 @@ public class P extends MPlugin
// Register Event Handlers // Register Event Handlers
getServer().getPluginManager().registerEvents(playerListener, this); getServer().getPluginManager().registerEvents(playerListener, this);
getServer().getPluginManager().registerEvents(chatEarlyListener, this); getServer().getPluginManager().registerEvents(chatListener, this);
getServer().getPluginManager().registerEvents(entityListener, this); getServer().getPluginManager().registerEvents(entityListener, this);
getServer().getPluginManager().registerEvents(blockListener, this); getServer().getPluginManager().registerEvents(blockListener, this);
getServer().getPluginManager().registerEvents(serverListener, this); getServer().getPluginManager().registerEvents(serverListener, this);

View File

@ -1,94 +0,0 @@
package com.massivecraft.factions.listeners;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChatEvent;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.P;
import com.massivecraft.factions.struct.ChatMode;
import com.massivecraft.factions.struct.Rel;
// 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 implements Listener
{
public P p;
public FactionsChatEarlyListener(P p)
{
this.p = p;
}
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerChat(PlayerChatEvent event)
{
if (event.isCancelled()) return;
Player talkingPlayer = event.getPlayer();
String msg = event.getMessage();
FPlayer me = FPlayers.i.get(talkingPlayer);
ChatMode chat = me.getChatMode();
// slashless factions commands need to be handled here if the user isn't in public chat mode
if (chat != ChatMode.PUBLIC && p.handleCommand(event.getPlayer(), event.getMessage()))
{
event.setCancelled(true);
return;
}
// Is it a faction chat message?
if (chat == ChatMode.FACTION)
{
Faction myFaction = me.getFaction();
String message = String.format(Conf.factionChatFormat, me.describeTo(myFaction), msg);
myFaction.sendMessage(message);
P.p.log(Level.INFO, ChatColor.stripColor("FactionChat "+myFaction.getTag()+": "+message));
//Send to any players who are spying chat
for (FPlayer fplayer : FPlayers.i.getOnline())
{
if(fplayer.isSpyingChat() && fplayer.getFaction() != myFaction)
fplayer.sendMessage("[FCspy] "+myFaction.getTag()+": "+message);
}
event.setCancelled(true);
return;
}
else if (chat == ChatMode.ALLIANCE)
{
Faction myFaction = me.getFaction();
String message = String.format(Conf.allianceChatFormat, ChatColor.stripColor(me.getNameAndTag()), msg);
//Send message to our own faction
myFaction.sendMessage(message);
//Send to all our allies
for (FPlayer fplayer : FPlayers.i.getOnline())
{
if(myFaction.getRelationTo(fplayer) == Rel.ALLY)
fplayer.sendMessage(message);
//Send to any players who are spying chat
else if(fplayer.isSpyingChat())
fplayer.sendMessage("[ACspy]: " + message);
}
P.p.log(Level.INFO, ChatColor.stripColor("AllianceChat: "+message));
event.setCancelled(true);
return;
}
}
}

View File

@ -0,0 +1,185 @@
package com.massivecraft.factions.listeners;
import java.util.logging.Level;
import java.util.UnknownFormatConversionException;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChatEvent;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.P;
import com.massivecraft.factions.struct.ChatMode;
import com.massivecraft.factions.struct.Rel;
public class FactionsChatListener implements Listener
{
public P p;
public FactionsChatListener(P p)
{
this.p = p;
}
// this is for handling slashless command usage and faction/alliance chat, set at lowest priority so Factions gets to them first
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerEarlyChat(PlayerChatEvent event)
{
if (event.isCancelled()) return;
Player talkingPlayer = event.getPlayer();
String msg = event.getMessage();
FPlayer me = FPlayers.i.get(talkingPlayer);
ChatMode chat = me.getChatMode();
// slashless factions commands need to be handled here if the user isn't in public chat mode
if (chat != ChatMode.PUBLIC && p.handleCommand(talkingPlayer, msg))
{
if (Conf.logPlayerCommands)
Bukkit.getLogger().log(Level.INFO, "[PLAYER_COMMAND] "+talkingPlayer.getName()+": "+msg);
event.setCancelled(true);
return;
}
// Is it a faction chat message?
if (chat == ChatMode.FACTION)
{
Faction myFaction = me.getFaction();
String message = String.format(Conf.factionChatFormat, me.describeTo(myFaction), msg);
myFaction.sendMessage(message);
Bukkit.getLogger().log(Level.INFO, ChatColor.stripColor("FactionChat "+myFaction.getTag()+": "+message));
//Send to any players who are spying chat
for (FPlayer fplayer : FPlayers.i.getOnline())
{
if(fplayer.isSpyingChat() && fplayer.getFaction() != myFaction)
fplayer.sendMessage("[FCspy] "+myFaction.getTag()+": "+message);
}
event.setCancelled(true);
return;
}
else if (chat == ChatMode.ALLIANCE)
{
Faction myFaction = me.getFaction();
String message = String.format(Conf.allianceChatFormat, ChatColor.stripColor(me.getNameAndTag()), msg);
//Send message to our own faction
myFaction.sendMessage(message);
//Send to all our allies
for (FPlayer fplayer : FPlayers.i.getOnline())
{
if(myFaction.getRelationTo(fplayer) == Rel.ALLY)
fplayer.sendMessage(message);
//Send to any players who are spying chat
else if(fplayer.isSpyingChat())
fplayer.sendMessage("[ACspy]: " + message);
}
Bukkit.getLogger().log(Level.INFO, ChatColor.stripColor("AllianceChat: "+message));
event.setCancelled(true);
return;
}
}
// this is for handling insertion of the player's faction tag, set at highest priority to give other plugins a chance to modify chat first
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerChat(PlayerChatEvent event)
{
if (event.isCancelled()) 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) return;
Player talkingPlayer = event.getPlayer();
String msg = event.getMessage();
String eventFormat = event.getFormat();
FPlayer me = FPlayers.i.get(talkingPlayer);
int InsertIndex = 0;
if (!Conf.chatTagReplaceString.isEmpty() && eventFormat.contains(Conf.chatTagReplaceString))
{
// we're using the "replace" method of inserting the faction tags
// if they stuck "[FACTION_TITLE]" in there, go ahead and do it too
if (eventFormat.contains("[FACTION_TITLE]"))
{
eventFormat = eventFormat.replace("[FACTION_TITLE]", me.getTitle());
}
InsertIndex = eventFormat.indexOf(Conf.chatTagReplaceString);
eventFormat = eventFormat.replace(Conf.chatTagReplaceString, "");
Conf.chatTagPadAfter = false;
Conf.chatTagPadBefore = false;
}
else if (!Conf.chatTagInsertAfterString.isEmpty() && eventFormat.contains(Conf.chatTagInsertAfterString))
{
// we're using the "insert after string" method
InsertIndex = eventFormat.indexOf(Conf.chatTagInsertAfterString) + Conf.chatTagInsertAfterString.length();
}
else if (!Conf.chatTagInsertBeforeString.isEmpty() && eventFormat.contains(Conf.chatTagInsertBeforeString))
{
// we're using the "insert before string" method
InsertIndex = eventFormat.indexOf(Conf.chatTagInsertBeforeString);
}
else
{
// we'll fall back to using the index place method
InsertIndex = Conf.chatTagInsertIndex;
if (InsertIndex > eventFormat.length())
return;
}
String formatStart = eventFormat.substring(0, InsertIndex) + ((Conf.chatTagPadBefore && !me.getChatTag().isEmpty()) ? " " : "");
String formatEnd = ((Conf.chatTagPadAfter && !me.getChatTag().isEmpty()) ? " " : "") + eventFormat.substring(InsertIndex);
String nonColoredMsgFormat = formatStart + me.getChatTag().trim() + formatEnd;
// Relation Colored?
if (Conf.chatTagRelationColored)
{
// We must choke the standard message and send out individual messages to all players
// Why? Because the relations will differ.
event.setCancelled(true);
for (Player listeningPlayer : event.getRecipients())
{
FPlayer you = FPlayers.i.get(listeningPlayer);
String yourFormat = formatStart + me.getChatTag(you).trim() + formatEnd;
try
{
listeningPlayer.sendMessage(String.format(yourFormat, talkingPlayer.getDisplayName(), msg));
}
catch (UnknownFormatConversionException ex)
{
Conf.chatTagInsertIndex = 0;
P.p.log(Level.SEVERE, "Critical error in chat message formatting!");
P.p.log(Level.SEVERE, "NOTE: This has been automatically fixed right now by setting chatTagInsertIndex to 0.");
P.p.log(Level.SEVERE, "For a more proper fix, please read this regarding chat configuration: http://massivecraft.com/plugins/factions/config#Chat_configuration");
return;
}
}
// Write to the log... We will write the non colored message.
String nonColoredMsg = ChatColor.stripColor(String.format(nonColoredMsgFormat, talkingPlayer.getDisplayName(), msg));
Bukkit.getLogger().log(Level.INFO, nonColoredMsg);
}
else
{
// No relation color.
event.setFormat(nonColoredMsgFormat);
}
}
}

View File

@ -1,10 +1,7 @@
package com.massivecraft.factions.listeners; package com.massivecraft.factions.listeners;
import java.util.logging.Logger;
import java.util.Iterator; import java.util.Iterator;
import java.util.UnknownFormatConversionException;
import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -16,7 +13,6 @@ import org.bukkit.event.Listener;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerBucketEmptyEvent; import org.bukkit.event.player.PlayerBucketEmptyEvent;
import org.bukkit.event.player.PlayerBucketFillEvent; import org.bukkit.event.player.PlayerBucketFillEvent;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent; import org.bukkit.event.player.PlayerKickEvent;
@ -48,100 +44,7 @@ public class FactionsPlayerListener implements Listener
{ {
this.p = p; this.p = p;
} }
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerChat(PlayerChatEvent event)
{
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 = FPlayers.i.get(talkingPlayer);
// 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)
{
return;
}
int InsertIndex = 0;
String eventFormat = event.getFormat();
if (!Conf.chatTagReplaceString.isEmpty() && eventFormat.contains(Conf.chatTagReplaceString))
{
// we're using the "replace" method of inserting the faction tags
// if they stuck "[FACTION_TITLE]" in there, go ahead and do it too
if (eventFormat.contains("[FACTION_TITLE]"))
{
eventFormat = eventFormat.replace("[FACTION_TITLE]", me.getTitle());
}
InsertIndex = eventFormat.indexOf(Conf.chatTagReplaceString);
eventFormat = eventFormat.replace(Conf.chatTagReplaceString, "");
Conf.chatTagPadAfter = false;
Conf.chatTagPadBefore = false;
}
else if (!Conf.chatTagInsertAfterString.isEmpty() && eventFormat.contains(Conf.chatTagInsertAfterString))
{
// we're using the "insert after string" method
InsertIndex = eventFormat.indexOf(Conf.chatTagInsertAfterString) + Conf.chatTagInsertAfterString.length();
}
else if (!Conf.chatTagInsertBeforeString.isEmpty() && eventFormat.contains(Conf.chatTagInsertBeforeString))
{
// we're using the "insert before string" method
InsertIndex = eventFormat.indexOf(Conf.chatTagInsertBeforeString);
}
else
{
// we'll fall back to using the index place method
InsertIndex = Conf.chatTagInsertIndex;
if (InsertIndex > eventFormat.length())
return;
}
String formatStart = eventFormat.substring(0, InsertIndex) + ((Conf.chatTagPadBefore && !me.getChatTag().isEmpty()) ? " " : "");
String formatEnd = ((Conf.chatTagPadAfter && !me.getChatTag().isEmpty()) ? " " : "") + eventFormat.substring(InsertIndex);
String nonColoredMsgFormat = formatStart + me.getChatTag().trim() + formatEnd;
// Relation Colored?
if (Conf.chatTagRelationColored)
{
// We must choke the standard message and send out individual messages to all players
// Why? Because the relations will differ.
event.setCancelled(true);
for (Player listeningPlayer : event.getRecipients())
{
FPlayer you = FPlayers.i.get(listeningPlayer);
String yourFormat = formatStart + me.getChatTag(you).trim() + formatEnd;
try
{
listeningPlayer.sendMessage(String.format(yourFormat, talkingPlayer.getDisplayName(), msg));
}
catch (UnknownFormatConversionException ex)
{
Conf.chatTagInsertIndex = 0;
P.p.log(Level.SEVERE, "Critical error in chat message formatting!");
P.p.log(Level.SEVERE, "NOTE: This has been automatically fixed right now by setting chatTagInsertIndex to 0.");
P.p.log(Level.SEVERE, "For a more proper fix, please read this regarding chat configuration: http://massivecraft.com/plugins/factions/config#Chat_configuration");
return;
}
}
// Write to the log... We will write the non colored message.
String nonColoredMsg = ChatColor.stripColor(String.format(nonColoredMsgFormat, talkingPlayer.getDisplayName(), msg));
Logger.getLogger("Minecraft").info(nonColoredMsg);
}
else
{
// No relation color.
event.setFormat(nonColoredMsgFormat);
}
}
@EventHandler(priority = EventPriority.NORMAL) @EventHandler(priority = EventPriority.NORMAL)
public void onPlayerJoin(PlayerJoinEvent event) public void onPlayerJoin(PlayerJoinEvent event)
{ {

View File

@ -5,7 +5,6 @@ import java.lang.reflect.Type;
import java.util.*; import java.util.*;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -239,6 +238,6 @@ public abstract class MPlugin extends JavaPlugin
public void log(Level level, Object msg) public void log(Level level, Object msg)
{ {
Logger.getLogger("Minecraft").log(level, "["+this.getDescription().getFullName()+"] "+msg); Bukkit.getLogger().log(level, "["+this.getDescription().getFullName()+"] "+msg);
} }
} }