From 16c69d67cd801d25b506b14b7be6fcb4f5876842 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Thu, 3 Jan 2013 08:23:46 +0100 Subject: [PATCH] Use the standard Bukkit command handling system. Doing so will remove the possiblility for dynamic command alias assignment but makes factions compatible with all other plugins doing stuff like blocking commands from being used (AntiGuest, NoCheatPlus, War etc) and plugins that log command useage (Hawkeye etc). --- plugin.yml | 4 +- src/com/massivecraft/factions/Conf.java | 3 +- src/com/massivecraft/factions/P.java | 43 ++------ .../massivecraft/factions/cmd/FCmdRoot.java | 1 - .../factions/integration/Econ.java | 2 - .../listeners/FactionsPlayerListener.java | 98 +++++++------------ .../massivecraft/factions/zcore/MCommand.java | 3 - .../massivecraft/factions/zcore/MPlugin.java | 89 +---------------- .../zcore/MPluginSecretPlayerListener.java | 31 +----- .../zcore/MPluginSecretServerListener.java | 28 ------ 10 files changed, 49 insertions(+), 253 deletions(-) delete mode 100644 src/com/massivecraft/factions/zcore/MPluginSecretServerListener.java diff --git a/plugin.yml b/plugin.yml index a43a8922..62312f3a 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,11 +1,11 @@ name: Factions -version: 1.8.0 +version: 1.8.1 main: com.massivecraft.factions.P authors: [Olof Larsson, Brett Flannigan] softdepend: [PermissionsEx, Permissions, Essentials, EssentialsChat, HeroChat, iChat, LocalAreaChat, LWC, nChat, ChatManager, CAPI, AuthMe, Vault, Spout, WorldEdit, WorldGuard, AuthDB, CaptureThePoints, CombatTag] commands: factions: - description: Reference command for Factions. + description: The Factions base command aliases: [f] permissions: factions.kit.admin: diff --git a/src/com/massivecraft/factions/Conf.java b/src/com/massivecraft/factions/Conf.java index 802fa39c..103249b4 100644 --- a/src/com/massivecraft/factions/Conf.java +++ b/src/com/massivecraft/factions/Conf.java @@ -11,8 +11,7 @@ import com.massivecraft.factions.struct.Rel; public class Conf { - public static List baseCommandAliases = new ArrayList(); - public static boolean allowNoSlashCommand = true; + public final static transient List baseCommandAliases = new ArrayList(); // Colors public static ChatColor colorMember = ChatColor.GREEN; diff --git a/src/com/massivecraft/factions/P.java b/src/com/massivecraft/factions/P.java index 17cd54d4..6b59fa07 100644 --- a/src/com/massivecraft/factions/P.java +++ b/src/com/massivecraft/factions/P.java @@ -1,14 +1,13 @@ package com.massivecraft.factions; import java.lang.reflect.Modifier; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.logging.Level; import java.util.Set; import org.bukkit.block.Block; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.Location; @@ -40,8 +39,9 @@ import com.massivecraft.factions.struct.TerritoryAccess; import com.massivecraft.factions.util.AutoLeaveTask; import com.massivecraft.factions.util.LazyLocation; import com.massivecraft.factions.zcore.MPlugin; -import com.massivecraft.factions.zcore.util.TextUtil; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; import org.bukkit.craftbukkit.libs.com.google.gson.GsonBuilder; @@ -81,7 +81,6 @@ public class P extends MPlugin this.appearanceListener = new FactionsAppearanceListener(this); } - @Override public void onEnable() { @@ -109,7 +108,6 @@ public class P extends MPlugin // Add Base Commands this.cmdAutoHelp = new CmdAutoHelp(); this.cmdBase = new FCmdRoot(); - this.getBaseCommands().add(cmdBase); EssentialsFeatures.setup(); SpoutFeatures.setup(); @@ -135,9 +133,6 @@ public class P extends MPlugin getServer().getPluginManager().registerEvents(this.serverListener, this); getServer().getPluginManager().registerEvents(this.appearanceListener, this); - // since some other plugins execute commands directly through this command interface, provide it - this.getCommand(this.refCommand).setExecutor(this); - postEnable(); this.loadSuccessful = true; } @@ -195,34 +190,14 @@ public class P extends MPlugin Board.save(); Conf.save(); } - - @Override - public boolean logPlayerCommands() - { - return Conf.logPlayerCommands; - } - - @Override - public boolean handleCommand(CommandSender sender, String commandString, boolean testOnly) - { - if (sender instanceof Player && FactionsPlayerListener.preventCommand(commandString, (Player)sender)) return true; - - 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); + this.cmdBase.execute(sender, new ArrayList(Arrays.asList(split))); + return true; } - - // -------------------------------------------- // // Functions for other plugins to hook into // -------------------------------------------- // @@ -260,10 +235,12 @@ public class P extends MPlugin } // Is this chat message actually a Factions command, and thus should be left alone by other plugins? + /** + * @deprecated As of release 1.8.1 the normal Bukkit command-handling is used. + */ public boolean isFactionsCommand(String check) { - if (check == null || check.isEmpty()) return false; - return this.handleCommand(null, check, true); + return false; } // Get a player's faction tag (faction name), mainly for usage by chat plugins for local/channel chat diff --git a/src/com/massivecraft/factions/cmd/FCmdRoot.java b/src/com/massivecraft/factions/cmd/FCmdRoot.java index fcb84a49..0267bade 100644 --- a/src/com/massivecraft/factions/cmd/FCmdRoot.java +++ b/src/com/massivecraft/factions/cmd/FCmdRoot.java @@ -53,7 +53,6 @@ public class FCmdRoot extends FCommand super(); this.aliases.addAll(Conf.baseCommandAliases); this.aliases.removeAll(Collections.singletonList(null)); // remove any nulls from extra commas - this.allowNoSlashAccess = Conf.allowNoSlashCommand; //this.requiredArgs.add(""); //this.optionalArgs.put("","") diff --git a/src/com/massivecraft/factions/integration/Econ.java b/src/com/massivecraft/factions/integration/Econ.java index 075600d1..c2666e09 100644 --- a/src/com/massivecraft/factions/integration/Econ.java +++ b/src/com/massivecraft/factions/integration/Econ.java @@ -48,8 +48,6 @@ public class Econ if ( ! Conf.econEnabled) P.p.log("NOTE: Economy is disabled. You can enable it with the command: f config econEnabled true"); - //P.p.cmdBase.cmdHelp.updateHelp(); - oldMoneyDoTransfer(); } diff --git a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java index 0c48c83d..9a9eb4d9 100644 --- a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java @@ -1,5 +1,6 @@ package com.massivecraft.factions.listeners; +import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -14,6 +15,7 @@ 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.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerKickEvent; @@ -285,96 +287,62 @@ public class FactionsPlayerListener implements Listener return; } } - - public static boolean preventCommand(String fullCmd, Player player) + + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { - if ((Conf.territoryNeutralDenyCommands.isEmpty() && Conf.territoryEnemyDenyCommands.isEmpty() && Conf.permanentFactionMemberDenyCommands.isEmpty())) - return false; - - fullCmd = fullCmd.toLowerCase(); - + // Get the player + Player player = event.getPlayer(); FPlayer me = FPlayers.i.get(player); - - String shortCmd; // command without the slash at the beginning - if (fullCmd.startsWith("/")) - shortCmd = fullCmd.substring(1); - else - { - shortCmd = fullCmd; - fullCmd = "/" + fullCmd; - } - - if - ( - me.hasFaction() - && - ! me.hasAdminMode() - && - ! Conf.permanentFactionMemberDenyCommands.isEmpty() - && - me.getFaction().getFlag(FFlag.PERMANENT) - && - isCommandInList(fullCmd, shortCmd, Conf.permanentFactionMemberDenyCommands.iterator()) - ) + + // With adminmode no commands are denied. + if (me.hasAdminMode()) return; + + // The full command is converted to lowercase and does include the slash in the front + String fullCmd = event.getMessage().toLowerCase(); + + if (me.hasFaction() && me.getFaction().getFlag(FFlag.PERMANENT) && isCommandInList(fullCmd, Conf.permanentFactionMemberDenyCommands)) { me.msg("You can't use the command \""+fullCmd+"\" because you are in a permanent faction."); - return true; + event.setCancelled(true); + return; } - + Rel rel = me.getRelationToLocation(); - if (rel.isAtLeast(Rel.TRUCE) || Board.getFactionAt(me.getLastStoodAt()).isNone()) - { - return false; - } - - if - ( - rel == Rel.NEUTRAL - && - ! Conf.territoryNeutralDenyCommands.isEmpty() - && - ! me.hasAdminMode() - && - isCommandInList(fullCmd, shortCmd, Conf.territoryNeutralDenyCommands.iterator()) - ) + if (Board.getFactionAt(me.getLastStoodAt()).isNone()) return; + + if (rel == Rel.NEUTRAL && isCommandInList(fullCmd, Conf.territoryNeutralDenyCommands)) { me.msg("You can't use the command \""+fullCmd+"\" in neutral territory."); - return true; + event.setCancelled(true); + return; } - if - ( - rel == Rel.ENEMY - && - ! Conf.territoryEnemyDenyCommands.isEmpty() - && - ! me.hasAdminMode() - && - isCommandInList(fullCmd, shortCmd, Conf.territoryEnemyDenyCommands.iterator()) - ) + if (rel == Rel.ENEMY && isCommandInList(fullCmd, Conf.territoryEnemyDenyCommands)) { me.msg("You can't use the command \""+fullCmd+"\" in enemy territory."); - return true; + event.setCancelled(true); + return; } - return false; + return; } - private static boolean isCommandInList(String fullCmd, String shortCmd, Iterator iter) + private static boolean isCommandInList(String fullCmd, Collection strings) { - String cmdCheck; + String shortCmd = fullCmd.substring(1); + Iterator iter = strings.iterator(); while (iter.hasNext()) { - cmdCheck = iter.next(); + String cmdCheck = iter.next(); if (cmdCheck == null) { iter.remove(); continue; } - cmdCheck = cmdCheck.toLowerCase(); - if (fullCmd.startsWith(cmdCheck) || shortCmd.startsWith(cmdCheck)) - return true; + if (fullCmd.startsWith(cmdCheck)) return true; + if (shortCmd.startsWith(cmdCheck)) return true; } return false; } diff --git a/src/com/massivecraft/factions/zcore/MCommand.java b/src/com/massivecraft/factions/zcore/MCommand.java index 0ef80fb6..4b4480d5 100644 --- a/src/com/massivecraft/factions/zcore/MCommand.java +++ b/src/com/massivecraft/factions/zcore/MCommand.java @@ -27,7 +27,6 @@ public abstract class MCommand // The different names this commands will react to public List aliases; - public boolean allowNoSlashAccess; // Information on the args public List requiredArgs; @@ -73,8 +72,6 @@ public abstract class MCommand this.permission = null; - this.allowNoSlashAccess = false; - this.subCommands = new ArrayList>(); this.aliases = new ArrayList(); diff --git a/src/com/massivecraft/factions/zcore/MPlugin.java b/src/com/massivecraft/factions/zcore/MPlugin.java index 0531e4ff..7c87088a 100644 --- a/src/com/massivecraft/factions/zcore/MPlugin.java +++ b/src/com/massivecraft/factions/zcore/MPlugin.java @@ -7,7 +7,6 @@ import java.util.Map.Entry; import java.util.logging.Level; import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.craftbukkit.libs.com.google.gson.Gson; @@ -36,15 +35,9 @@ 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; - private MPluginSecretServerListener mPluginSecretServerListener; - - // Our stored base commands - private List> baseCommands = new ArrayList>(); - public List> getBaseCommands() { return this.baseCommands; } + public MPluginSecretPlayerListener mPluginSecretPlayerListener; // -------------------------------------------- // // ENABLE @@ -70,22 +63,8 @@ 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); - getServer().getPluginManager().registerEvents(this.mPluginSecretPlayerListener, this); - getServer().getPluginManager().registerEvents(this.mPluginSecretServerListener, this); - // Register recurring tasks long saveTicks = 20 * 60 * 30; // Approximately every 30 min @@ -174,72 +153,6 @@ 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) - { - return handleCommand(sender, commandString, testOnly, false); - } - - public boolean handleCommand(final CommandSender sender, String commandString, boolean testOnly, boolean async) - { - boolean noSlash = true; - if (commandString.startsWith("/")) - { - noSlash = false; - commandString = commandString.substring(1); - } - - for (final MCommand command : this.getBaseCommands()) - { - if (noSlash && ! command.allowNoSlashAccess) continue; - - for (String alias : command.aliases) - { - // disallow double-space after alias, so specific commands can be prevented (preventing "f home" won't prevent "f home") - if (commandString.startsWith(alias+" ")) return false; - - if (commandString.startsWith(alias+" ") || commandString.equals(alias)) - { - final List args = new ArrayList(Arrays.asList(commandString.split("\\s+"))); - args.remove(0); - - if (testOnly) return true; - - if (async) - { - Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() - { - @Override - public void run() - { - command.execute(sender, args); - } - }); - } - else - command.execute(sender, args); - - return true; - } - } - } - return false; - } - - public boolean handleCommand(CommandSender sender, String commandString) - { - return this.handleCommand(sender, commandString, false); - } - // -------------------------------------------- // // HOOKS // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/zcore/MPluginSecretPlayerListener.java b/src/com/massivecraft/factions/zcore/MPluginSecretPlayerListener.java index c3acf52b..d1e3353d 100644 --- a/src/com/massivecraft/factions/zcore/MPluginSecretPlayerListener.java +++ b/src/com/massivecraft/factions/zcore/MPluginSecretPlayerListener.java @@ -4,8 +4,6 @@ import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.player.AsyncPlayerChatEvent; -import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerLoginEvent; import com.massivecraft.factions.zcore.persist.EM; @@ -15,36 +13,11 @@ import com.massivecraft.factions.zcore.persist.PlayerEntityCollection; public class MPluginSecretPlayerListener implements Listener { - private MPlugin p; + public MPlugin p; public MPluginSecretPlayerListener(MPlugin p) { this.p = p; - } - - @EventHandler(priority = EventPriority.LOW) - public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) - { - if (event.isCancelled()) return; - - if (p.handleCommand(event.getPlayer(), event.getMessage())) - { - if (p.logPlayerCommands()) - Bukkit.getLogger().info("[PLAYER_COMMAND] "+event.getPlayer().getName()+": "+event.getMessage()); - event.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.LOW) - public void onPlayerChat(AsyncPlayerChatEvent event) - { - if (event.isCancelled()) return; - - if (p.handleCommand(event.getPlayer(), event.getMessage(), false, true)) - { - if (p.logPlayerCommands()) - Bukkit.getLogger().info("[PLAYER_COMMAND] "+event.getPlayer().getName()+": "+event.getMessage()); - event.setCancelled(true); - } + Bukkit.getPluginManager().registerEvents(this, this.p); } @EventHandler(priority = EventPriority.LOWEST) diff --git a/src/com/massivecraft/factions/zcore/MPluginSecretServerListener.java b/src/com/massivecraft/factions/zcore/MPluginSecretServerListener.java deleted file mode 100644 index 004b85da..00000000 --- a/src/com/massivecraft/factions/zcore/MPluginSecretServerListener.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.massivecraft.factions.zcore; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.server.ServerCommandEvent; - -public class MPluginSecretServerListener implements Listener -{ - private MPlugin p; - - public MPluginSecretServerListener(MPlugin p) - { - this.p = p; - } - - @EventHandler(priority = EventPriority.LOWEST) - public void onServerCommand(ServerCommandEvent event) - { - if (event.getCommand().length() == 0) return; - - if (p.handleCommand(event.getSender(), event.getCommand())) - { - event.setCommand(p.refCommand); - } - } - -}