From a5bb25eb44f9430f7e2c48bed69bf5f4dad298d1 Mon Sep 17 00:00:00 2001 From: Brettflan Date: Fri, 9 Mar 2012 19:26:40 -0600 Subject: [PATCH 1/5] New setting "logPlayerCommands" (default true) which can be disabled to prevent player commands from being logged. --- src/com/massivecraft/factions/Conf.java | 1 + src/com/massivecraft/factions/P.java | 6 ++++++ src/com/massivecraft/factions/zcore/MPlugin.java | 6 ++++++ .../factions/zcore/MPluginSecretPlayerListener.java | 6 ++++-- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/com/massivecraft/factions/Conf.java b/src/com/massivecraft/factions/Conf.java index 07c3d311..9ed968c6 100644 --- a/src/com/massivecraft/factions/Conf.java +++ b/src/com/massivecraft/factions/Conf.java @@ -100,6 +100,7 @@ public class Conf public static boolean logLandClaims = true; public static boolean logLandUnclaims = true; public static boolean logMoneyTransactions = true; + public static boolean logPlayerCommands = true; public static boolean homesEnabled = true; public static boolean homesMustBeInClaimedTerritory = true; diff --git a/src/com/massivecraft/factions/P.java b/src/com/massivecraft/factions/P.java index d0926510..b68554ec 100644 --- a/src/com/massivecraft/factions/P.java +++ b/src/com/massivecraft/factions/P.java @@ -172,6 +172,12 @@ public class P extends MPlugin Conf.save(); } + @Override + public boolean logPlayerCommands() + { + return Conf.logPlayerCommands; + } + @Override public boolean handleCommand(CommandSender sender, String commandString, boolean testOnly) { diff --git a/src/com/massivecraft/factions/zcore/MPlugin.java b/src/com/massivecraft/factions/zcore/MPlugin.java index b16c42c7..77faa3f5 100644 --- a/src/com/massivecraft/factions/zcore/MPlugin.java +++ b/src/com/massivecraft/factions/zcore/MPlugin.java @@ -167,6 +167,12 @@ public abstract class MPlugin extends JavaPlugin // COMMAND HANDLING // -------------------------------------------- // + // can be overridden by P method, to provide option + public boolean logPlayerCommands() + { + return true; + } + public boolean handleCommand(CommandSender sender, String commandString, boolean testOnly) { boolean noSlash = true; diff --git a/src/com/massivecraft/factions/zcore/MPluginSecretPlayerListener.java b/src/com/massivecraft/factions/zcore/MPluginSecretPlayerListener.java index 1fab4ccd..d60f4b9a 100644 --- a/src/com/massivecraft/factions/zcore/MPluginSecretPlayerListener.java +++ b/src/com/massivecraft/factions/zcore/MPluginSecretPlayerListener.java @@ -28,7 +28,8 @@ public class MPluginSecretPlayerListener implements Listener if (p.handleCommand(event.getPlayer(), event.getMessage())) { - Bukkit.getLogger().info("[PLAYER_COMMAND] "+event.getPlayer().getName()+": "+event.getMessage()); + if (p.logPlayerCommands()) + Bukkit.getLogger().info("[PLAYER_COMMAND] "+event.getPlayer().getName()+": "+event.getMessage()); event.setCancelled(true); } } @@ -40,7 +41,8 @@ public class MPluginSecretPlayerListener implements Listener if (p.handleCommand(event.getPlayer(), event.getMessage())) { - Bukkit.getLogger().info("[PLAYER_COMMAND] "+event.getPlayer().getName()+": "+event.getMessage()); + if (p.logPlayerCommands()) + Bukkit.getLogger().info("[PLAYER_COMMAND] "+event.getPlayer().getName()+": "+event.getMessage()); event.setCancelled(true); } } From fd44983ae2015c69ccf0593a0e73545d2eab97d4 Mon Sep 17 00:00:00 2001 From: Brettflan Date: Fri, 9 Mar 2012 21:25:01 -0600 Subject: [PATCH 2/5] (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. --- src/com/massivecraft/factions/P.java | 8 +- .../listeners/FactionsChatEarlyListener.java | 94 --------- .../listeners/FactionsChatListener.java | 185 ++++++++++++++++++ .../listeners/FactionsPlayerListener.java | 97 --------- .../massivecraft/factions/zcore/MPlugin.java | 3 +- 5 files changed, 190 insertions(+), 197 deletions(-) delete mode 100644 src/com/massivecraft/factions/listeners/FactionsChatEarlyListener.java create mode 100644 src/com/massivecraft/factions/listeners/FactionsChatListener.java diff --git a/src/com/massivecraft/factions/P.java b/src/com/massivecraft/factions/P.java index b68554ec..f9bdc867 100644 --- a/src/com/massivecraft/factions/P.java +++ b/src/com/massivecraft/factions/P.java @@ -26,7 +26,7 @@ import com.massivecraft.factions.integration.LWCFeatures; import com.massivecraft.factions.integration.SpoutFeatures; import com.massivecraft.factions.integration.Worldguard; 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.FactionsPlayerListener; import com.massivecraft.factions.listeners.FactionsServerListener; @@ -48,7 +48,7 @@ public class P extends MPlugin // Listeners public final FactionsPlayerListener playerListener; - public final FactionsChatEarlyListener chatEarlyListener; + public final FactionsChatListener chatListener; public final FactionsEntityListener entityListener; public final FactionsBlockListener blockListener; public final FactionsServerListener serverListener; @@ -67,7 +67,7 @@ public class P extends MPlugin { p = this; this.playerListener = new FactionsPlayerListener(this); - this.chatEarlyListener = new FactionsChatEarlyListener(this); + this.chatListener = new FactionsChatListener(this); this.entityListener = new FactionsEntityListener(this); this.blockListener = new FactionsBlockListener(this); this.serverListener = new FactionsServerListener(this); @@ -107,7 +107,7 @@ public class P extends MPlugin // Register Event Handlers getServer().getPluginManager().registerEvents(playerListener, this); - getServer().getPluginManager().registerEvents(chatEarlyListener, this); + getServer().getPluginManager().registerEvents(chatListener, this); getServer().getPluginManager().registerEvents(entityListener, this); getServer().getPluginManager().registerEvents(blockListener, this); getServer().getPluginManager().registerEvents(serverListener, this); diff --git a/src/com/massivecraft/factions/listeners/FactionsChatEarlyListener.java b/src/com/massivecraft/factions/listeners/FactionsChatEarlyListener.java deleted file mode 100644 index 458eb8d2..00000000 --- a/src/com/massivecraft/factions/listeners/FactionsChatEarlyListener.java +++ /dev/null @@ -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; - } - } -} diff --git a/src/com/massivecraft/factions/listeners/FactionsChatListener.java b/src/com/massivecraft/factions/listeners/FactionsChatListener.java new file mode 100644 index 00000000..4e9454ca --- /dev/null +++ b/src/com/massivecraft/factions/listeners/FactionsChatListener.java @@ -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); + } + } +} diff --git a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java index 3d4af5e9..ffb299cd 100644 --- a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java @@ -1,10 +1,7 @@ package com.massivecraft.factions.listeners; -import java.util.logging.Logger; import java.util.Iterator; -import java.util.UnknownFormatConversionException; -import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -16,7 +13,6 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerBucketEmptyEvent; import org.bukkit.event.player.PlayerBucketFillEvent; -import org.bukkit.event.player.PlayerChatEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerKickEvent; @@ -48,100 +44,7 @@ public class FactionsPlayerListener implements Listener { 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) public void onPlayerJoin(PlayerJoinEvent event) { diff --git a/src/com/massivecraft/factions/zcore/MPlugin.java b/src/com/massivecraft/factions/zcore/MPlugin.java index 77faa3f5..0d48d3a8 100644 --- a/src/com/massivecraft/factions/zcore/MPlugin.java +++ b/src/com/massivecraft/factions/zcore/MPlugin.java @@ -5,7 +5,6 @@ import java.lang.reflect.Type; import java.util.*; import java.util.Map.Entry; import java.util.logging.Level; -import java.util.logging.Logger; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -239,6 +238,6 @@ public abstract class MPlugin extends JavaPlugin public void log(Level level, Object msg) { - Logger.getLogger("Minecraft").log(level, "["+this.getDescription().getFullName()+"] "+msg); + Bukkit.getLogger().log(level, "["+this.getDescription().getFullName()+"] "+msg); } } From a888a9c5b55449f6a6d2dba2a148de6bc36c7ea6 Mon Sep 17 00:00:00 2001 From: Brettflan Date: Sun, 11 Mar 2012 07:33:47 -0500 Subject: [PATCH 3/5] Updated /f config to support modifying "factionFlagDefaults" and "factionPermDefaults". Both of these settings require an additional argument more than other settings. Examples: /f config factionFlagDefaults firespread false - set default "FIRESPREAD" faction flag to false /f config factionFlagDefaults monsters true - set default "MONSTERS" faction flag to true /f config factionPermDefaults build ally - for default "BUILD" faction permission, add/remove permission for allies /f config factionPermDefaults withdraw member - for default "WITHDRAW" faction permission, add/remove permission for regular faction members --- .../massivecraft/factions/cmd/CmdConfig.java | 215 ++++++++++++++---- 1 file changed, 165 insertions(+), 50 deletions(-) diff --git a/src/com/massivecraft/factions/cmd/CmdConfig.java b/src/com/massivecraft/factions/cmd/CmdConfig.java index f72366dd..5d180bbf 100644 --- a/src/com/massivecraft/factions/cmd/CmdConfig.java +++ b/src/com/massivecraft/factions/cmd/CmdConfig.java @@ -5,6 +5,8 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.Set; import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -13,7 +15,10 @@ import org.bukkit.entity.Player; import com.massivecraft.factions.Conf; import com.massivecraft.factions.P; import com.massivecraft.factions.integration.SpoutFeatures; +import com.massivecraft.factions.struct.FFlag; +import com.massivecraft.factions.struct.FPerm; import com.massivecraft.factions.struct.Permission; +import com.massivecraft.factions.struct.Rel; public class CmdConfig extends FCommand { @@ -174,76 +179,186 @@ public class CmdConfig extends FCommand ParameterizedType targSet = (ParameterizedType)target.getGenericType(); Type innerType = targSet.getActualTypeArguments()[0]; - // not a Set, somehow, and that should be the only collection we're using in Conf.java - if (targSet.getRawType() != Set.class) + // Set + if (targSet.getRawType() == Set.class) { - sendMessage("\""+fieldName+"\" is not a data collection type which can be modified with this command."); - return; - } + // Set + if (innerType == Material.class) + { + Material newMat = null; + try + { + newMat = Material.valueOf(value.toUpperCase()); + } + catch (IllegalArgumentException ex) + { - // Set - else if (innerType == Material.class) - { - Material newMat = null; - try - { - newMat = Material.valueOf(value.toUpperCase()); - } - catch (IllegalArgumentException ex) - { - - } - if (newMat == null) - { - sendMessage("Cannot change \""+fieldName+"\" set: \""+value.toUpperCase()+"\" is not a valid material."); - return; + } + if (newMat == null) + { + sendMessage("Cannot change \""+fieldName+"\" set: \""+value.toUpperCase()+"\" is not a valid material."); + return; + } + + @SuppressWarnings("unchecked") + Set matSet = (Set)target.get(null); + + // Material already present, so remove it + if (matSet.contains(newMat)) + { + matSet.remove(newMat); + target.set(null, matSet); + success = "\""+fieldName+"\" set: Material \""+value.toUpperCase()+"\" removed."; + } + // Material not present yet, add it + else + { + matSet.add(newMat); + target.set(null, matSet); + success = "\""+fieldName+"\" set: Material \""+value.toUpperCase()+"\" added."; + } } - @SuppressWarnings("unchecked") - Set matSet = (Set)target.get(null); - - // Material already present, so remove it - if (matSet.contains(newMat)) + // Set + else if (innerType == String.class) { - matSet.remove(newMat); - target.set(null, matSet); - success = "\""+fieldName+"\" set: Material \""+value.toUpperCase()+"\" removed."; + @SuppressWarnings("unchecked") + Set stringSet = (Set)target.get(null); + + // String already present, so remove it + if (stringSet.contains(value)) + { + stringSet.remove(value); + success = "\""+fieldName+"\" set: \""+value+"\" removed."; + } + // String not present yet, add it + else + { + stringSet.add(value); + success = "\""+fieldName+"\" set: \""+value+"\" added."; + } + target.set(null, stringSet); } - // Material not present yet, add it + + // Set of unknown type else { - matSet.add(newMat); - target.set(null, matSet); - success = "\""+fieldName+"\" set: Material \""+value.toUpperCase()+"\" added."; + sendMessage("\""+fieldName+"\" is not a data type set which can be modified with this command."); + return; } } - // Set - else if (innerType == String.class) + // Map + else if (targSet.getRawType() == Map.class) { - @SuppressWarnings("unchecked") - Set stringSet = (Set)target.get(null); - - // String already present, so remove it - if (stringSet.contains(value)) + if (args.size() < 3) { - stringSet.remove(value); - target.set(null, stringSet); - success = "\""+fieldName+"\" set: \""+value+"\" removed."; + sendMessage("Cannot change \""+fieldName+"\" map: not enough arguments passed."); + return; } - // String not present yet, add it - else + Type innerType2 = targSet.getActualTypeArguments()[1]; + String value1 = args.get(1); + String value2 = value.substring(value1.length() + 1); + + // Map + if (innerType == FFlag.class && innerType2 == Boolean.class) { - stringSet.add(value); - target.set(null, stringSet); - success = "\""+fieldName+"\" set: \""+value+"\" added."; + value1 = value1.toUpperCase(); + FFlag newFlag = null; + try + { + newFlag = FFlag.valueOf(value1); + } + catch (IllegalArgumentException ex) {} + + if (newFlag == null) + { + sendMessage("Cannot change \""+fieldName+"\" map: \""+value1+"\" is not a valid FFlag."); + return; + } + + @SuppressWarnings("unchecked") + Map map = (Map)target.get(null); + + Boolean targetValue = this.strAsBool(value2); + + map.put(newFlag, targetValue); + target.set(null, map); + + if (targetValue) + success = "\""+fieldName+"\" flag \""+value1+"\" set to true (enabled)."; + else + success = "\""+fieldName+"\" flag \""+value1+"\" set to false (disabled)."; + } + + // Map> + else if (innerType == FPerm.class && innerType2 instanceof ParameterizedType) + { + if (((ParameterizedType)innerType2).getRawType() != Set.class) + { + sendMessage("\""+fieldName+"\" is not a data type map which can be modified with this command, due to the inner collection type."); + return; + } + + value1 = value1.toUpperCase(); + value2 = value2.toUpperCase(); + + FPerm newPerm = null; + Rel newRel = null; + try + { + newPerm = FPerm.valueOf(value1); + newRel = Rel.valueOf(value2); + } + catch (IllegalArgumentException ex) {} + + if (newPerm == null) + { + sendMessage("Cannot change \""+fieldName+"\" map: \""+value1+"\" is not a valid FPerm."); + return; + } + if (newRel == null) + { + sendMessage("Cannot change \""+fieldName+"\" map: \""+value2+"\" is not a valid Rel."); + return; + } + + @SuppressWarnings("unchecked") + Map> map = (Map>)target.get(null); + + Set relSet = map.get(newPerm); + if (relSet == null) + relSet = new HashSet(); + + // Rel already present, so remove it + if (relSet.contains(newRel)) + { + relSet.remove(newRel); + success = "\""+fieldName+"\" permission \""+value1+"\": relation \""+value2+"\" removed."; + } + // Rel not present yet, add it + else + { + relSet.add(newRel); + success = "\""+fieldName+"\" permission \""+value1+"\": relation \""+value2+"\" added."; + } + + map.put(newPerm, relSet); + target.set(null, map); + } + + // Map of unknown type + else + { + sendMessage("\""+fieldName+"\" is not a data type map which can be modified with this command."); + return; } } - // Set of unknown type + // not a Set or Map? else { - sendMessage("\""+fieldName+"\" is not a data type set which can be modified with this command."); + sendMessage("\""+fieldName+"\" is not a data collection type which can be modified with this command."); return; } } From 2c6191b73ff498e3fb8a067ae30a6245336625bd Mon Sep 17 00:00:00 2001 From: Brettflan Date: Sun, 11 Mar 2012 07:39:31 -0500 Subject: [PATCH 4/5] Removed dependence on external GSON lib in lib/gson.jar, since GSON 2.1 is now embedded in CraftBukkit itself, used by their auto-updater added shortly before 1.1-R5 was released: https://github.com/Bukkit/CraftBukkit/commit/0ed1d1fdbb1e0bc09a70bc7bfdf40c1de8411665 --- src/com/massivecraft/factions/zcore/MPlugin.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/com/massivecraft/factions/zcore/MPlugin.java b/src/com/massivecraft/factions/zcore/MPlugin.java index 0d48d3a8..c8547255 100644 --- a/src/com/massivecraft/factions/zcore/MPlugin.java +++ b/src/com/massivecraft/factions/zcore/MPlugin.java @@ -61,8 +61,9 @@ public abstract class MPlugin extends JavaPlugin this.perm = new PermUtil(this); this.persist = new Persist(this); this.lib = new LibLoader(this); - - if ( ! lib.require("gson.jar", "http://search.maven.org/remotecontent?filepath=com/google/code/gson/gson/2.1/gson-2.1.jar")) return false; + + // GSON 2.1 is now embedded in CraftBukkit, used by the auto-updater: https://github.com/Bukkit/CraftBukkit/commit/0ed1d1fdbb1e0bc09a70bc7bfdf40c1de8411665 +// if ( ! lib.require("gson.jar", "http://search.maven.org/remotecontent?filepath=com/google/code/gson/gson/2.1/gson-2.1.jar")) return false; this.gson = this.getGsonBuilder().create(); this.txt = new TextUtil(); From c0308940c8ef74922ab637d4a4ca2ae6b262d33a Mon Sep 17 00:00:00 2001 From: Brettflan Date: Sun, 11 Mar 2012 11:41:56 -0500 Subject: [PATCH 5/5] Connected standard command handler getCommand("f"), for it to work with other plugins which directly execute commands using that interface. --- src/com/massivecraft/factions/P.java | 21 +++++++++++++++++-- .../massivecraft/factions/zcore/MPlugin.java | 13 +++++++++++- .../zcore/MPluginSecretServerListener.java | 16 +------------- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/com/massivecraft/factions/P.java b/src/com/massivecraft/factions/P.java index f9bdc867..7ca3c48d 100644 --- a/src/com/massivecraft/factions/P.java +++ b/src/com/massivecraft/factions/P.java @@ -2,16 +2,18 @@ package com.massivecraft.factions; import java.lang.reflect.Modifier; import java.lang.reflect.Type; +import java.util.Arrays; import java.util.HashSet; import java.util.Map; import java.util.Set; import org.bukkit.block.Block; -import org.bukkit.Location; -import org.bukkit.Material; +import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerChatEvent; +import org.bukkit.Location; +import org.bukkit.Material; import com.massivecraft.factions.adapters.FFlagTypeAdapter; import com.massivecraft.factions.adapters.FLocToStringSetTypeAdapter; @@ -36,6 +38,7 @@ import com.massivecraft.factions.struct.FPerm; import com.massivecraft.factions.struct.Rel; import com.massivecraft.factions.util.AutoLeaveTask; import com.massivecraft.factions.zcore.MPlugin; +import com.massivecraft.factions.zcore.util.TextUtil; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; @@ -112,6 +115,9 @@ public class P extends MPlugin getServer().getPluginManager().registerEvents(blockListener, this); getServer().getPluginManager().registerEvents(serverListener, this); + // since some other plugins execute commands directly through this command interface, provide it + this.getCommand(this.refCommand).setExecutor(this); + postEnable(); this.loadSuccessful = true; } @@ -186,6 +192,17 @@ public class P extends MPlugin return super.handleCommand(sender, commandString, testOnly); } + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] split) + { + // if bare command at this point, it has already been handled by MPlugin's command listeners + if (split == null || split.length == 0) return true; + + // otherwise, needs to be handled; presumably another plugin directly ran the command + String cmd = Conf.baseCommandAliases.isEmpty() ? "/f" : "/" + Conf.baseCommandAliases.get(0); + return handleCommand(sender, cmd + " " + TextUtil.implode(Arrays.asList(split), " "), false); + } + // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/zcore/MPlugin.java b/src/com/massivecraft/factions/zcore/MPlugin.java index c8547255..427da45e 100644 --- a/src/com/massivecraft/factions/zcore/MPlugin.java +++ b/src/com/massivecraft/factions/zcore/MPlugin.java @@ -36,6 +36,7 @@ public abstract class MPlugin extends JavaPlugin protected boolean loadSuccessful = false; public boolean getAutoSave() {return this.autoSave;} public void setAutoSave(boolean val) {this.autoSave = val;} + public String refCommand = ""; // Listeners private MPluginSecretPlayerListener mPluginSecretPlayerListener; @@ -68,7 +69,17 @@ public abstract class MPlugin extends JavaPlugin this.txt = new TextUtil(); initTXT(); - + + // attempt to get first command defined in plugin.yml as reference command, if any commands are defined in there + // reference command will be used to prevent "unknown command" console messages + try + { + Map> refCmd = this.getDescription().getCommands(); + if (refCmd != null && !refCmd.isEmpty()) + this.refCommand = (String)(refCmd.keySet().toArray()[0]); + } + catch (ClassCastException ex) {} + // Create and register listeners this.mPluginSecretPlayerListener = new MPluginSecretPlayerListener(this); this.mPluginSecretServerListener = new MPluginSecretServerListener(this); diff --git a/src/com/massivecraft/factions/zcore/MPluginSecretServerListener.java b/src/com/massivecraft/factions/zcore/MPluginSecretServerListener.java index 6b4c0784..004b85da 100644 --- a/src/com/massivecraft/factions/zcore/MPluginSecretServerListener.java +++ b/src/com/massivecraft/factions/zcore/MPluginSecretServerListener.java @@ -1,7 +1,5 @@ package com.massivecraft.factions.zcore; -import java.util.Map; - import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -10,22 +8,10 @@ import org.bukkit.event.server.ServerCommandEvent; public class MPluginSecretServerListener implements Listener { private MPlugin p; - private String refCommand; public MPluginSecretServerListener(MPlugin p) { this.p = p; - refCommand = ""; - - // attempt to get first command defined in plugin.yml as reference command, if any commands are defined in there - // reference command will be used to prevent "unknown command" console messages - try - { - Map> refCmd = p.getDescription().getCommands(); - if (refCmd != null && !refCmd.isEmpty()) - refCommand = (String)(refCmd.keySet().toArray()[0]); - } - catch (ClassCastException ex) {} } @EventHandler(priority = EventPriority.LOWEST) @@ -35,7 +21,7 @@ public class MPluginSecretServerListener implements Listener if (p.handleCommand(event.getSender(), event.getCommand())) { - event.setCommand(refCommand); + event.setCommand(p.refCommand); } }