From 2a98734d2251809e06af7e297ef3fc6f4dc535a5 Mon Sep 17 00:00:00 2001 From: snowleo Date: Fri, 18 Nov 2011 01:43:58 +0100 Subject: [PATCH] Better solution for the alternative commands --- .../AlternativeCommandsHandler.java | 117 ++++++++++++++++++ .../com/earth2me/essentials/Essentials.java | 73 ++--------- .../essentials/EssentialsPluginListener.java | 2 + .../com/earth2me/essentials/IEssentials.java | 2 + 4 files changed, 133 insertions(+), 61 deletions(-) create mode 100644 Essentials/src/com/earth2me/essentials/AlternativeCommandsHandler.java diff --git a/Essentials/src/com/earth2me/essentials/AlternativeCommandsHandler.java b/Essentials/src/com/earth2me/essentials/AlternativeCommandsHandler.java new file mode 100644 index 000000000..4ee78220e --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/AlternativeCommandsHandler.java @@ -0,0 +1,117 @@ +package com.earth2me.essentials; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.PluginCommand; +import org.bukkit.command.PluginCommandYamlParser; +import org.bukkit.plugin.Plugin; + + +public class AlternativeCommandsHandler +{ + private final transient Map> altcommands = new HashMap>(); + private final transient IEssentials ess; + + public AlternativeCommandsHandler(final IEssentials ess) + { + this.ess = ess; + for (Plugin plugin : ess.getServer().getPluginManager().getPlugins()) + { + if (plugin.isEnabled()) { + addPlugin(plugin); + } + } + } + + public final void addPlugin(final Plugin plugin) + { + if (plugin.getDescription().getMain().contains("com.earth2me.essentials")) + { + return; + } + final List commands = PluginCommandYamlParser.parse(plugin); + final String pluginName = plugin.getDescription().getName().toLowerCase(); + + for (Command command : commands) + { + final PluginCommand pc = (PluginCommand)command; + final List labels = new ArrayList(pc.getAliases()); + labels.add(pc.getName()); + + PluginCommand reg = ess.getServer().getPluginCommand(pluginName + ":" + pc.getName().toLowerCase()); + if (reg == null) + { + reg = Bukkit.getServer().getPluginCommand(pc.getName().toLowerCase()); + } + for (String label : labels) + { + List plugincommands = altcommands.get(label.toLowerCase()); + if (plugincommands == null) + { + plugincommands = new ArrayList(); + altcommands.put(label.toLowerCase(), plugincommands); + } + boolean found = false; + for (PluginCommand pc2 : plugincommands) + { + if (pc2.getPlugin().equals(plugin)) + { + found = true; + } + } + if (!found) + { + plugincommands.add(reg); + } + } + } + } + + public void removePlugin(final Plugin plugin) + { + final Iterator>> iterator = altcommands.entrySet().iterator(); + while (iterator.hasNext()) + { + final Map.Entry> entry = iterator.next(); + final Iterator pcIterator = entry.getValue().iterator(); + while (pcIterator.hasNext()) + { + final PluginCommand pc = pcIterator.next(); + if (pc.getPlugin().equals(plugin)) + { + pcIterator.remove(); + } + } + if (entry.getValue().isEmpty()) + { + iterator.remove(); + } + } + } + + public PluginCommand getAlternative(final String label) + { + final List commands = altcommands.get(label); + if (commands == null || commands.isEmpty()) + { + return null; + } + if (commands.size() == 1) + { + return commands.get(0); + } + // return the first command that is not an alias + for (PluginCommand command : commands) { + if (command.getName().equalsIgnoreCase(label)) { + return command; + } + } + // return the first alias + return commands.get(0); + } +} diff --git a/Essentials/src/com/earth2me/essentials/Essentials.java b/Essentials/src/com/earth2me/essentials/Essentials.java index 21628629b..5fec76a21 100644 --- a/Essentials/src/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/com/earth2me/essentials/Essentials.java @@ -60,6 +60,7 @@ public class Essentials extends JavaPlugin implements IEssentials private transient ItemDb itemDb; private transient final Methods paymentMethod = new Methods(); private transient PermissionsHandler permissionsHandler; + private transient AlternativeCommandsHandler alternativeCommandsHandler; private transient UserMap userMap; private transient ExecuteTimer execTimer; @@ -149,6 +150,7 @@ public class Essentials extends JavaPlugin implements IEssentials } permissionsHandler = new PermissionsHandler(this, settings.useBukkitPermissions()); + alternativeCommandsHandler = new AlternativeCommandsHandler(this); final EssentialsPluginListener serverListener = new EssentialsPluginListener(this); pm.registerEvent(Type.PLUGIN_ENABLE, serverListener, Priority.Low, this); pm.registerEvent(Type.PLUGIN_DISABLE, serverListener, Priority.Low, this); @@ -366,68 +368,11 @@ public class Essentials extends JavaPlugin implements IEssentials // Allow plugins to override the command via onCommand if (!getSettings().isCommandOverridden(command.getName()) && !commandLabel.startsWith("e")) { - for (Plugin p : getServer().getPluginManager().getPlugins()) + final PluginCommand pc = alternativeCommandsHandler.getAlternative(commandLabel); + if (pc != null) { - if (p.getDescription().getMain().contains("com.earth2me.essentials")) - { - continue; - } - - final PluginDescriptionFile desc = p.getDescription(); - if (desc == null) - { - continue; - } - - if (desc.getName() == null) - { - continue; - } - - final PluginCommand pc = getServer().getPluginCommand(desc.getName().toLowerCase() + ":" + commandLabel); - if (pc != null) - { - LOGGER.info("Essentials: Alternative command " + commandLabel + " found, using " + desc.getName().toLowerCase() + ":" + commandLabel); - return pc.execute(sender, commandLabel, args); - } - } - for (Plugin p : getServer().getPluginManager().getPlugins()) - { - if (p.getDescription().getMain().contains("com.earth2me.essentials")) - { - continue; - } - final List commands = PluginCommandYamlParser.parse(p); - for (Command c : commands) - { - if (c.getAliases().contains(commandLabel)) - { - final PluginDescriptionFile desc = p.getDescription(); - if (desc == null) - { - continue; - } - - if (desc.getName() == null) - { - continue; - } - - final PluginCommand pc = getServer().getPluginCommand(desc.getName().toLowerCase() + ":" + c.getName().toLowerCase()); - if (pc != null) - { - LOGGER.info("Essentials: Alternative alias " + commandLabel + " found, using " + desc.getName().toLowerCase() + ":" + c.getName().toLowerCase()); - return pc.execute(sender, commandLabel, args); - } - - final PluginCommand pc2 = getServer().getPluginCommand(c.getName().toLowerCase()); - if (pc2 != null) - { - LOGGER.info("Essentials: Alternative alias " + commandLabel + " found, using " + c.getName().toLowerCase()); - return pc2.execute(sender, commandLabel, args); - } - } - } + LOGGER.info("Essentials: Alternative command " + commandLabel + " found, using " + pc.getLabel()); + return pc.execute(sender, commandLabel, args); } } @@ -703,6 +648,12 @@ public class Essentials extends JavaPlugin implements IEssentials return permissionsHandler; } + @Override + public AlternativeCommandsHandler getAlternativeCommandsHandler() + { + return alternativeCommandsHandler; + } + @Override public ItemDb getItemDb() { diff --git a/Essentials/src/com/earth2me/essentials/EssentialsPluginListener.java b/Essentials/src/com/earth2me/essentials/EssentialsPluginListener.java index 6b92d7aa2..b0ee0b543 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsPluginListener.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsPluginListener.java @@ -21,6 +21,7 @@ public class EssentialsPluginListener extends ServerListener implements IConf public void onPluginEnable(final PluginEnableEvent event) { ess.getPermissionsHandler().checkPermissions(); + ess.getAlternativeCommandsHandler().addPlugin(event.getPlugin()); if (!ess.getPaymentMethod().hasMethod() && ess.getPaymentMethod().setMethod(ess.getServer().getPluginManager())) { LOGGER.log(Level.INFO, "[Essentials] Payment method found (" + ess.getPaymentMethod().getMethod().getName() + " version: " + ess.getPaymentMethod().getMethod().getVersion() + ")"); @@ -31,6 +32,7 @@ public class EssentialsPluginListener extends ServerListener implements IConf public void onPluginDisable(final PluginDisableEvent event) { ess.getPermissionsHandler().checkPermissions(); + ess.getAlternativeCommandsHandler().removePlugin(event.getPlugin()); // Check to see if the plugin thats being disabled is the one we are using if (ess.getPaymentMethod() != null && ess.getPaymentMethod().hasMethod() && ess.getPaymentMethod().checkDisabled(event.getPlugin())) { diff --git a/Essentials/src/com/earth2me/essentials/IEssentials.java b/Essentials/src/com/earth2me/essentials/IEssentials.java index 9dca96e81..337a3997d 100644 --- a/Essentials/src/com/earth2me/essentials/IEssentials.java +++ b/Essentials/src/com/earth2me/essentials/IEssentials.java @@ -56,6 +56,8 @@ public interface IEssentials extends Plugin TNTExplodeListener getTNTListener(); PermissionsHandler getPermissionsHandler(); + + AlternativeCommandsHandler getAlternativeCommandsHandler(); void showError(final CommandSender sender, final Throwable exception, final String commandLabel);