Search in sources :

Example 26 with Command

use of org.bukkit.command.Command in project NoCheatPlus by NoCheatPlus.

the class CommandUtil method getCommands.

/**
 * 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());
    }
    // 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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Command(org.bukkit.command.Command) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) JavaPlugin(org.bukkit.plugin.java.JavaPlugin) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) Map(java.util.Map) CommandMap(org.bukkit.command.CommandMap) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) CommandMap(org.bukkit.command.CommandMap) Plugin(org.bukkit.plugin.Plugin) JavaPlugin(org.bukkit.plugin.java.JavaPlugin)

Example 27 with Command

use of org.bukkit.command.Command in project Glowstone by GlowstoneMC.

the class GlowHelpMap method initializeCommands.

/**
 * Processes all the commands registered in the server and creates help topics for them.
 */
public synchronized void initializeCommands() {
    // Don't load any automatic help topics if All is ignored
    if (ignoredPlugins.contains("All")) {
        return;
    }
    Collection<Command> commands = server.getCommandMap().getCommands();
    // Initialize help topics from the server's command map
    outer: for (Command command : commands) {
        if (commandInIgnoredPlugin(command)) {
            continue;
        }
        // Register a topic
        for (Entry<Class, HelpTopicFactory<Command>> entry : topicFactoryMap.entrySet()) {
            if (((Class<?>) entry.getKey()).isAssignableFrom(command.getClass())) {
                HelpTopic t = entry.getValue().createTopic(command);
                if (t != null) {
                    addCommandTopic(t);
                }
                continue outer;
            }
            if (command instanceof PluginCommand && ((Class<?>) entry.getKey()).isAssignableFrom(((PluginCommand) command).getExecutor().getClass())) {
                HelpTopic t = entry.getValue().createTopic(command);
                if (t != null) {
                    addCommandTopic(t);
                }
                continue outer;
            }
        }
        addCommandTopic(new GenericCommandHelpTopic(command));
    }
    // Alias topics for commands
    Set<HelpTopic> aliases = new TreeSet<>(TOPIC_COMPARE);
    for (Command command : commands) {
        if (commandInIgnoredPlugin(command)) {
            continue;
        }
        HelpTopic original = getHelpTopic("/" + command.getLabel());
        if (original != null) {
            for (String alias : command.getAliases()) {
                HelpTopic aliasTopic = new AliasTopic("/" + alias, original);
                if (!helpTopics.containsKey(aliasTopic.getName())) {
                    aliases.add(aliasTopic);
                    addPrivateTopic(aliasTopic);
                }
            }
        }
    }
    // Aliases index topic
    if (!aliases.isEmpty()) {
        addTopic(new IndexHelpTopic("Aliases", "Lists command aliases", null, aliases, null));
    }
    // Initialize plugin-level sub-topics
    Map<String, Set<HelpTopic>> pluginIndexes = new HashMap<>();
    for (Command command : commands) {
        String pluginName = getCommandPluginName(command);
        if (pluginName != null) {
            HelpTopic topic = getHelpTopic("/" + command.getLabel());
            if (topic != null) {
                if (!pluginIndexes.containsKey(pluginName)) {
                    pluginIndexes.put(pluginName, new TreeSet<>(TOPIC_COMPARE));
                }
                pluginIndexes.get(pluginName).add(topic);
            }
        }
    }
    for (Entry<String, Set<HelpTopic>> entry : pluginIndexes.entrySet()) {
        String key = entry.getKey();
        addTopic(new IndexHelpTopic(key, "All commands for " + key, null, entry.getValue(), "Below is a list of all " + key + " commands:"));
    }
}
Also used : TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) HelpTopic(org.bukkit.help.HelpTopic) IndexHelpTopic(org.bukkit.help.IndexHelpTopic) GenericCommandHelpTopic(org.bukkit.help.GenericCommandHelpTopic) GenericCommandHelpTopic(org.bukkit.help.GenericCommandHelpTopic) Entry(java.util.Map.Entry) PluginIdentifiableCommand(org.bukkit.command.PluginIdentifiableCommand) PluginCommand(org.bukkit.command.PluginCommand) BukkitCommand(org.bukkit.command.defaults.BukkitCommand) Command(org.bukkit.command.Command) TreeSet(java.util.TreeSet) IndexHelpTopic(org.bukkit.help.IndexHelpTopic) PluginCommand(org.bukkit.command.PluginCommand)

Aggregations

Command (org.bukkit.command.Command)27 SimpleCommandMap (org.bukkit.command.SimpleCommandMap)9 CommandMap (org.bukkit.command.CommandMap)7 Field (java.lang.reflect.Field)6 PluginCommand (org.bukkit.command.PluginCommand)6 Map (java.util.Map)5 PluginIdentifiableCommand (org.bukkit.command.PluginIdentifiableCommand)5 Player (org.bukkit.entity.Player)5 HashMap (java.util.HashMap)4 EventHandler (org.bukkit.event.EventHandler)3 Plugin (org.bukkit.plugin.Plugin)3 PluginManager (org.bukkit.plugin.PluginManager)3 URLClassLoader (java.net.URLClassLoader)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 Server (org.bukkit.Server)2 CommandSender (org.bukkit.command.CommandSender)2 JavaPlugin (org.bukkit.plugin.java.JavaPlugin)2 Arguments (cat.nyaa.nyaacore.CommandReceiver.Arguments)1