Search in sources :

Example 1 with PluginIdentifiableCommand

use of org.bukkit.command.PluginIdentifiableCommand in project TotalFreedomMod by TotalFreedom.

the class Module_help method getBody.

@Override
public String getBody() {
    final CommandMap map = CommandReflection.getCommandMap();
    if (map == null || !(map instanceof SimpleCommandMap)) {
        return paragraph("Error loading commands.");
    }
    final StringBuilder responseBody = new StringBuilder().append(heading("Command Help", 1)).append(paragraph("This page is an automatically generated listing of all plugin commands that are currently live on the server. " + "Please note that it does not include vanilla server commands."));
    final Collection<Command> knownCommands = ((SimpleCommandMap) map).getCommands();
    final Map<String, List<Command>> commandsByPlugin = new HashMap<>();
    for (Command command : knownCommands) {
        String pluginName = "Bukkit";
        if (command instanceof PluginIdentifiableCommand) {
            pluginName = ((PluginIdentifiableCommand) command).getPlugin().getName();
        }
        List<Command> pluginCommands = commandsByPlugin.get(pluginName);
        if (pluginCommands == null) {
            pluginCommands = Lists.newArrayList();
            commandsByPlugin.put(pluginName, pluginCommands);
        }
        pluginCommands.add(command);
    }
    final Iterator<Map.Entry<String, List<Command>>> it = commandsByPlugin.entrySet().iterator();
    while (it.hasNext()) {
        final Map.Entry<String, List<Command>> entry = it.next();
        final String pluginName = entry.getKey();
        final List<Command> commands = entry.getValue();
        Collections.sort(commands, new CommandComparator());
        responseBody.append(heading(pluginName, 2)).append("<ul>\r\n");
        Displayable lastTfmCommandLevel = null;
        for (Command command : commands) {
            if (!TotalFreedomMod.pluginName.equals(pluginName)) {
                responseBody.append(buildDescription(command));
                continue;
            }
            Displayable tfmCommandLevel = FreedomCommand.getFrom(command).getPerms().level();
            if (lastTfmCommandLevel == null || lastTfmCommandLevel != tfmCommandLevel) {
                responseBody.append("</ul>\r\n").append(heading(tfmCommandLevel.getName(), 3)).append("<ul>\r\n");
            }
            lastTfmCommandLevel = tfmCommandLevel;
            responseBody.append(buildDescription(command));
        }
        responseBody.append("</ul>\r\n");
    }
    return responseBody.toString();
}
Also used : Displayable(me.totalfreedom.totalfreedommod.rank.Displayable) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) HashMap(java.util.HashMap) PluginIdentifiableCommand(org.bukkit.command.PluginIdentifiableCommand) PluginIdentifiableCommand(org.bukkit.command.PluginIdentifiableCommand) FreedomCommand(me.totalfreedom.totalfreedommod.command.FreedomCommand) Command(org.bukkit.command.Command) List(java.util.List) HashMap(java.util.HashMap) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) Map(java.util.Map) CommandMap(org.bukkit.command.CommandMap) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) CommandMap(org.bukkit.command.CommandMap)

Example 2 with PluginIdentifiableCommand

use of org.bukkit.command.PluginIdentifiableCommand in project commands by aikar.

the class BukkitCommandManager method registerCommand.

public void registerCommand(BaseCommand command, boolean force) {
    final String plugin = this.plugin.getName().toLowerCase();
    command.onRegister(this);
    for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) {
        String commandName = entry.getKey().toLowerCase();
        BukkitRootCommand bukkitCommand = (BukkitRootCommand) entry.getValue();
        if (!bukkitCommand.isRegistered) {
            Command oldCommand = commandMap.getCommand(commandName);
            if (oldCommand instanceof PluginIdentifiableCommand && ((PluginIdentifiableCommand) oldCommand).getPlugin() == this.plugin) {
                knownCommands.remove(commandName);
                oldCommand.unregister(commandMap);
            } else if (oldCommand != null && force) {
                knownCommands.remove(commandName);
                for (Map.Entry<String, Command> ce : knownCommands.entrySet()) {
                    String key = ce.getKey();
                    Command value = ce.getValue();
                    if (key.contains(":") && oldCommand.equals(value)) {
                        String[] split = ACFPatterns.COLON.split(key, 2);
                        if (split.length > 1) {
                            oldCommand.unregister(commandMap);
                            oldCommand.setLabel(split[0] + ":" + command.getName());
                            oldCommand.register(commandMap);
                        }
                    }
                }
            }
            commandMap.register(commandName, plugin, bukkitCommand);
        }
        bukkitCommand.isRegistered = true;
        registeredCommands.put(commandName, bukkitCommand);
    }
}
Also used : PluginIdentifiableCommand(org.bukkit.command.PluginIdentifiableCommand) PluginIdentifiableCommand(org.bukkit.command.PluginIdentifiableCommand) Command(org.bukkit.command.Command) HashMap(java.util.HashMap) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) Map(java.util.Map) CommandMap(org.bukkit.command.CommandMap)

Aggregations

HashMap (java.util.HashMap)2 Map (java.util.Map)2 Command (org.bukkit.command.Command)2 CommandMap (org.bukkit.command.CommandMap)2 PluginIdentifiableCommand (org.bukkit.command.PluginIdentifiableCommand)2 SimpleCommandMap (org.bukkit.command.SimpleCommandMap)2 List (java.util.List)1 FreedomCommand (me.totalfreedom.totalfreedommod.command.FreedomCommand)1 Displayable (me.totalfreedom.totalfreedommod.rank.Displayable)1