From c7a4d37f6e1e7fadc50d4903ee1f0113cabe7678 Mon Sep 17 00:00:00 2001 From: snowleo Date: Wed, 14 Mar 2012 03:53:30 +0100 Subject: Moved CommandsHandler to commands package --- .../src/com/earth2me/essentials/Essentials.java | 1 + .../essentials/EssentialsCommandHandler.java | 316 --------------------- .../commands/EssentialsCommandHandler.java | 316 +++++++++++++++++++++ .../earth2me/essentials/xmpp/EssentialsXMPP.java | 2 +- 4 files changed, 318 insertions(+), 317 deletions(-) delete mode 100644 Essentials/src/com/earth2me/essentials/EssentialsCommandHandler.java create mode 100644 Essentials/src/com/earth2me/essentials/commands/EssentialsCommandHandler.java diff --git a/Essentials/src/com/earth2me/essentials/Essentials.java b/Essentials/src/com/earth2me/essentials/Essentials.java index 2cea1993c..e38e867a5 100644 --- a/Essentials/src/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/com/earth2me/essentials/Essentials.java @@ -17,6 +17,7 @@ */ package com.earth2me.essentials; +import com.earth2me.essentials.commands.EssentialsCommandHandler; import com.earth2me.essentials.utils.ExecuteTimer; import com.earth2me.essentials.economy.WorthHolder; import com.earth2me.essentials.economy.Economy; diff --git a/Essentials/src/com/earth2me/essentials/EssentialsCommandHandler.java b/Essentials/src/com/earth2me/essentials/EssentialsCommandHandler.java deleted file mode 100644 index f047868d6..000000000 --- a/Essentials/src/com/earth2me/essentials/EssentialsCommandHandler.java +++ /dev/null @@ -1,316 +0,0 @@ -package com.earth2me.essentials; - -import static com.earth2me.essentials.I18n._; -import com.earth2me.essentials.api.*; -import com.earth2me.essentials.commands.EssentialsCommand; -import com.earth2me.essentials.commands.IEssentialsCommand; -import com.earth2me.essentials.commands.NoChargeException; -import com.earth2me.essentials.commands.NotEnoughArgumentsException; -import java.util.*; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.command.PluginCommand; -import org.bukkit.command.PluginCommandYamlParser; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; - - -public class EssentialsCommandHandler implements ICommandHandler -{ - private final transient ClassLoader classLoader; - private final transient String commandPath; - private final transient String permissionPrefix; - private final transient IEssentialsModule module; - private static final transient Logger LOGGER = Bukkit.getLogger(); - private final transient Map> altcommands = new HashMap>(); - private final transient Map disabledList = new HashMap(); - private final transient Map commands = new HashMap(); - private final transient IEssentials ess; - - public EssentialsCommandHandler(ClassLoader classLoader, String commandPath, String permissionPrefix, IEssentials ess) - { - this(classLoader, commandPath, permissionPrefix, null, ess); - } - - public EssentialsCommandHandler(ClassLoader classLoader, String commandPath, String permissionPrefix, IEssentialsModule module, IEssentials ess) - { - this.classLoader = classLoader; - this.commandPath = commandPath; - this.permissionPrefix = permissionPrefix; - this.module = module; - this.ess = ess; - for (Plugin plugin : ess.getServer().getPluginManager().getPlugins()) - { - if (plugin.isEnabled()) - { - addPlugin(plugin); - } - } - } - - @Override - public boolean handleCommand(final CommandSender sender, final Command command, final String commandLabel, final String[] args) - { - boolean disabled = false; - boolean overridden = false; - ISettings settings = ess.getSettings(); - settings.acquireReadLock(); - try - { - disabled = settings.getData().getCommands().isDisabled(command.getName()); - overridden = !disabled || settings.getData().getCommands().isOverridden(command.getName()); - } - finally - { - settings.unlock(); - } - // Allow plugins to override the command via onCommand - if (!overridden && (!commandLabel.startsWith("e") || commandLabel.equalsIgnoreCase(command.getName()))) - { - final PluginCommand pc = getAlternative(commandLabel); - if (pc != null) - { - - executed(commandLabel, pc.getLabel()); - try - { - return pc.execute(sender, commandLabel, args); - } - catch (final Exception ex) - { - final ArrayList elements = new ArrayList(Arrays.asList(ex.getStackTrace())); - elements.remove(0); - final ArrayList toRemove = new ArrayList(); - for (final StackTraceElement e : elements) - { - if (e.getClassName().equals("com.earth2me.essentials.Essentials")) - { - toRemove.add(e); - } - } - elements.removeAll(toRemove); - final StackTraceElement[] trace = elements.toArray(new StackTraceElement[elements.size()]); - ex.setStackTrace(trace); - ex.printStackTrace(); - sender.sendMessage(ChatColor.RED + "An internal error occurred while attempting to perform this command"); - return true; - } - } - } - - try - { - IUser user = null; - if (sender instanceof Player) - { - user = ess.getUser((Player)sender); - LOGGER.log(Level.INFO, String.format("[PLAYER_COMMAND] %s: /%s %s ", ((Player)sender).getName(), commandLabel, EssentialsCommand.getFinalArg(args, 0))); - } - - // Check for disabled commands - if (disabled) - { - return true; - } - - final String commandName = command.getName().toLowerCase(Locale.ENGLISH); - IEssentialsCommand cmd = commands.get(commandName); - if (cmd == null) - { - try - { - cmd = (IEssentialsCommand)classLoader.loadClass(commandPath + commandName).newInstance(); - cmd.init(ess, commandName); - cmd.setEssentialsModule(module); - commands.put(commandName, cmd); - } - catch (Exception ex) - { - sender.sendMessage(_("commandNotLoaded", commandName)); - LOGGER.log(Level.SEVERE, _("commandNotLoaded", commandName), ex); - return true; - } - } - - // Check authorization - if (sender != null && !cmd.isAuthorized(sender)) - { - LOGGER.log(Level.WARNING, _("deniedAccessCommand", user.getName())); - user.sendMessage(_("noAccessCommand")); - return true; - } - - // Run the command - try - { - if (user == null) - { - cmd.run(sender, command, commandLabel, args); - } - else - { - user.acquireReadLock(); - try - { - cmd.run(user, command, commandLabel, args); - } - finally - { - user.unlock(); - } - } - return true; - } - catch (NoChargeException ex) - { - return true; - } - catch (NotEnoughArgumentsException ex) - { - sender.sendMessage(command.getDescription()); - sender.sendMessage(command.getUsage().replaceAll("", commandLabel)); - if (!ex.getMessage().isEmpty()) - { - sender.sendMessage(ex.getMessage()); - } - return true; - } - catch (Throwable ex) - { - showCommandError(sender, commandLabel, ex); - return true; - } - } - catch (Throwable ex) - { - LOGGER.log(Level.SEVERE, _("commandFailed", commandLabel), ex); - return true; - } - } - - @Override - public void showCommandError(final CommandSender sender, final String commandLabel, final Throwable exception) - { - sender.sendMessage(_("errorWithMessage", exception.getMessage())); - if (ess.getSettings().isDebug()) - { - LOGGER.log(Level.WARNING, _("errorCallingCommand", commandLabel), exception); - } - } - - @Override - public void onReload() - { - } - - 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(Locale.ENGLISH); - - 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(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 plugincommands = altcommands.get(label.toLowerCase(Locale.ENGLISH)); - if (plugincommands == null) - { - plugincommands = new ArrayList(); - 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>> 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() == null || 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); - } - - 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 disabledCommands() - { - return disabledList; - } -} diff --git a/Essentials/src/com/earth2me/essentials/commands/EssentialsCommandHandler.java b/Essentials/src/com/earth2me/essentials/commands/EssentialsCommandHandler.java new file mode 100644 index 000000000..f45a55a3f --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/commands/EssentialsCommandHandler.java @@ -0,0 +1,316 @@ +package com.earth2me.essentials.commands; + +import static com.earth2me.essentials.I18n._; +import com.earth2me.essentials.api.*; +import com.earth2me.essentials.commands.EssentialsCommand; +import com.earth2me.essentials.commands.IEssentialsCommand; +import com.earth2me.essentials.commands.NoChargeException; +import com.earth2me.essentials.commands.NotEnoughArgumentsException; +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.PluginCommand; +import org.bukkit.command.PluginCommandYamlParser; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + + +public class EssentialsCommandHandler implements ICommandHandler +{ + private final transient ClassLoader classLoader; + private final transient String commandPath; + private final transient String permissionPrefix; + private final transient IEssentialsModule module; + private static final transient Logger LOGGER = Bukkit.getLogger(); + private final transient Map> altcommands = new HashMap>(); + private final transient Map disabledList = new HashMap(); + private final transient Map commands = new HashMap(); + private final transient IEssentials ess; + + public EssentialsCommandHandler(ClassLoader classLoader, String commandPath, String permissionPrefix, IEssentials ess) + { + this(classLoader, commandPath, permissionPrefix, null, ess); + } + + public EssentialsCommandHandler(ClassLoader classLoader, String commandPath, String permissionPrefix, IEssentialsModule module, IEssentials ess) + { + this.classLoader = classLoader; + this.commandPath = commandPath; + this.permissionPrefix = permissionPrefix; + this.module = module; + this.ess = ess; + for (Plugin plugin : ess.getServer().getPluginManager().getPlugins()) + { + if (plugin.isEnabled()) + { + addPlugin(plugin); + } + } + } + + @Override + public boolean handleCommand(final CommandSender sender, final Command command, final String commandLabel, final String[] args) + { + boolean disabled = false; + boolean overridden = false; + ISettings settings = ess.getSettings(); + settings.acquireReadLock(); + try + { + disabled = settings.getData().getCommands().isDisabled(command.getName()); + overridden = !disabled || settings.getData().getCommands().isOverridden(command.getName()); + } + finally + { + settings.unlock(); + } + // Allow plugins to override the command via onCommand + if (!overridden && (!commandLabel.startsWith("e") || commandLabel.equalsIgnoreCase(command.getName()))) + { + final PluginCommand pc = getAlternative(commandLabel); + if (pc != null) + { + + executed(commandLabel, pc.getLabel()); + try + { + return pc.execute(sender, commandLabel, args); + } + catch (final Exception ex) + { + final ArrayList elements = new ArrayList(Arrays.asList(ex.getStackTrace())); + elements.remove(0); + final ArrayList toRemove = new ArrayList(); + for (final StackTraceElement e : elements) + { + if (e.getClassName().equals("com.earth2me.essentials.Essentials")) + { + toRemove.add(e); + } + } + elements.removeAll(toRemove); + final StackTraceElement[] trace = elements.toArray(new StackTraceElement[elements.size()]); + ex.setStackTrace(trace); + ex.printStackTrace(); + sender.sendMessage(ChatColor.RED + "An internal error occurred while attempting to perform this command"); + return true; + } + } + } + + try + { + IUser user = null; + if (sender instanceof Player) + { + user = ess.getUser((Player)sender); + LOGGER.log(Level.INFO, String.format("[PLAYER_COMMAND] %s: /%s %s ", ((Player)sender).getName(), commandLabel, EssentialsCommand.getFinalArg(args, 0))); + } + + // Check for disabled commands + if (disabled) + { + return true; + } + + final String commandName = command.getName().toLowerCase(Locale.ENGLISH); + IEssentialsCommand cmd = commands.get(commandName); + if (cmd == null) + { + try + { + cmd = (IEssentialsCommand)classLoader.loadClass(commandPath + commandName).newInstance(); + cmd.init(ess, commandName); + cmd.setEssentialsModule(module); + commands.put(commandName, cmd); + } + catch (Exception ex) + { + sender.sendMessage(_("commandNotLoaded", commandName)); + LOGGER.log(Level.SEVERE, _("commandNotLoaded", commandName), ex); + return true; + } + } + + // Check authorization + if (sender != null && !cmd.isAuthorized(sender)) + { + LOGGER.log(Level.WARNING, _("deniedAccessCommand", user.getName())); + user.sendMessage(_("noAccessCommand")); + return true; + } + + // Run the command + try + { + if (user == null) + { + cmd.run(sender, command, commandLabel, args); + } + else + { + user.acquireReadLock(); + try + { + cmd.run(user, command, commandLabel, args); + } + finally + { + user.unlock(); + } + } + return true; + } + catch (NoChargeException ex) + { + return true; + } + catch (NotEnoughArgumentsException ex) + { + sender.sendMessage(command.getDescription()); + sender.sendMessage(command.getUsage().replaceAll("", commandLabel)); + if (!ex.getMessage().isEmpty()) + { + sender.sendMessage(ex.getMessage()); + } + return true; + } + catch (Throwable ex) + { + showCommandError(sender, commandLabel, ex); + return true; + } + } + catch (Throwable ex) + { + LOGGER.log(Level.SEVERE, _("commandFailed", commandLabel), ex); + return true; + } + } + + @Override + public void showCommandError(final CommandSender sender, final String commandLabel, final Throwable exception) + { + sender.sendMessage(_("errorWithMessage", exception.getMessage())); + if (ess.getSettings().isDebug()) + { + LOGGER.log(Level.WARNING, _("errorCallingCommand", commandLabel), exception); + } + } + + @Override + public void onReload() + { + } + + 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(Locale.ENGLISH); + + 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(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 plugincommands = altcommands.get(label.toLowerCase(Locale.ENGLISH)); + if (plugincommands == null) + { + plugincommands = new ArrayList(); + 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>> 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() == null || 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); + } + + 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 disabledCommands() + { + return disabledList; + } +} diff --git a/EssentialsXMPP/src/com/earth2me/essentials/xmpp/EssentialsXMPP.java b/EssentialsXMPP/src/com/earth2me/essentials/xmpp/EssentialsXMPP.java index 507326296..fea8dd2e2 100644 --- a/EssentialsXMPP/src/com/earth2me/essentials/xmpp/EssentialsXMPP.java +++ b/EssentialsXMPP/src/com/earth2me/essentials/xmpp/EssentialsXMPP.java @@ -1,6 +1,6 @@ package com.earth2me.essentials.xmpp; -import com.earth2me.essentials.EssentialsCommandHandler; +import com.earth2me.essentials.commands.EssentialsCommandHandler; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.api.ICommandHandler; import com.earth2me.essentials.api.IEssentials; -- cgit v1.2.3