public void registerCommands(Class<?> clazz, CommandContainer base) { Validate.notNull(clazz); Set<CommandContainer> commands = CommandContainer.create(clazz, base, instantiator, execution, logger); Iterator<CommandContainer> iterator = commands.iterator(); while (iterator.hasNext()) { CommandContainer command = iterator.next(); if (base == null) { if (commandMap.containsKey(command.getName())) { logger.warning("duplicate command " + command.getName() + "!"); continue; } commandMap.put(command.getName(), command); PluginCommand bukkitCommand = plugin.getCommand(command.getName()); if (bukkitCommand != null) { bukkitCommand.setExecutor(this); } else { logger.warning( "Command " + command.getName() + " registered but could not find a matching command for plugin " + plugin.getName() + ". Did you forget to add the command to your plugin.yml?"); } } else { // Just add it as a child base.addChild(command); } } }
/** * Get all Command instances, that NCP can get hold of. Attempt to get a SimpleCommandMap instance * from the server to get the actually registered commands, but also get commands from JavaPlugin * instances. * * @return */ public static Collection<Command> getCommands() { final Collection<Command> commands = new LinkedHashSet<Command>(500); // All (?) commands from the SimpleCommandMap of the server, if available. final CommandMap commandMap = getCommandMap(); if (commandMap != null && commandMap instanceof SimpleCommandMap) { commands.addAll(((SimpleCommandMap) commandMap).getCommands()); } // TODO: Fall-back for Vanilla / CB commands? [Fall-back should be altering permission defaults, // though negating permissions is the right way.] // Fall-back: plugin commands. for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) { if (plugin instanceof JavaPlugin) { final JavaPlugin javaPlugin = (JavaPlugin) plugin; final Map<String, Map<String, Object>> map = javaPlugin.getDescription().getCommands(); if (map != null) { for (String label : map.keySet()) { Command command = javaPlugin.getCommand(label); if (command != null) { commands.add(command); } } } } } return commands; }
public void registerCommand(JavaPlugin plugin, Class<? extends CommandExecutor> commandClass) { try { String[] commandNames = (String[]) commandClass.getField("commandNames").get(null); PluginCommand command = plugin.getCommand(commandNames[0]); if (command != null) { command.setExecutor(this); mappedCommandClasses.put(commandNames, commandClass); registeredExecutors.add(commandClass.newInstance()); } else { Util.noticeableConsoleMessage( "The command: " + commandNames[0] + ", is not registered in the plugin.yml"); } // Get command class from the string array } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException | InstantiationException e) { // Could not instantiate command class } }