diff options
Diffstat (limited to 'Essentials/src/com/earth2me/essentials/AlternativeCommandsHandler.java')
-rw-r--r-- | Essentials/src/com/earth2me/essentials/AlternativeCommandsHandler.java | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/Essentials/src/com/earth2me/essentials/AlternativeCommandsHandler.java b/Essentials/src/com/earth2me/essentials/AlternativeCommandsHandler.java new file mode 100644 index 000000000..e75644ad5 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/AlternativeCommandsHandler.java @@ -0,0 +1,137 @@ +package com.earth2me.essentials; + +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.bukkit.command.Command; +import org.bukkit.command.PluginCommand; +import org.bukkit.command.PluginCommandYamlParser; +import org.bukkit.plugin.Plugin; + + +public class AlternativeCommandsHandler +{ + private static final Logger LOGGER = Logger.getLogger("Minecraft"); + private final transient Map<String, List<PluginCommand>> altcommands = new HashMap<String, List<PluginCommand>>(); + private final transient Map<String, String> disabledList = new HashMap<String, String>(); + 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<Command> commands = PluginCommandYamlParser.parse(plugin); + final String pluginName = plugin.getDescription().getName().toLowerCase(Locale.ENGLISH); + + for (Command command : commands) + { + final PluginCommand pc = (PluginCommand)command; + final List<String> labels = new ArrayList<String>(pc.getAliases()); + labels.add(pc.getName()); + + PluginCommand reg = ess.getServer().getPluginCommand(pluginName + ":" + pc.getName().toLowerCase(Locale.ENGLISH)); + if (reg == null) + { + reg = ess.getServer().getPluginCommand(pc.getName().toLowerCase(Locale.ENGLISH)); + } + if (reg == null || !reg.getPlugin().equals(plugin)) + { + continue; + } + for (String label : labels) + { + List<PluginCommand> plugincommands = altcommands.get(label.toLowerCase(Locale.ENGLISH)); + if (plugincommands == null) + { + plugincommands = new ArrayList<PluginCommand>(); + altcommands.put(label.toLowerCase(Locale.ENGLISH), 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<Map.Entry<String, List<PluginCommand>>> iterator = altcommands.entrySet().iterator(); + while (iterator.hasNext()) + { + final Map.Entry<String, List<PluginCommand>> entry = iterator.next(); + final Iterator<PluginCommand> pcIterator = entry.getValue().iterator(); + while (pcIterator.hasNext()) + { + final PluginCommand pc = pcIterator.next(); + if (pc.getPlugin() == null || pc.getPlugin().equals(plugin)) + { + pcIterator.remove(); + } + } + if (entry.getValue().isEmpty()) + { + iterator.remove(); + } + } + } + + public PluginCommand getAlternative(final String label) + { + final List<PluginCommand> 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); + } + + public void executed(final String label, final String otherLabel) + { + if (ess.getSettings().isDebug()) + { + LOGGER.log(Level.INFO, "Essentials: Alternative command " + label + " found, using " + otherLabel); + } + disabledList.put(label, otherLabel); + } + + public Map<String, String> disabledCommands() + { + return disabledList; + } +} |