Search in sources :

Example 1 with SimpleCommandMap

use of org.bukkit.command.SimpleCommandMap 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 SimpleCommandMap

use of org.bukkit.command.SimpleCommandMap in project SpaciousLib by anhcraft.

the class SPlugin method unloadPlugin.

public static boolean unloadPlugin(Plugin plugin) throws NoSuchFieldException, IllegalAccessException, IOException {
    String name = plugin.getName();
    PluginManager pluginManager = Bukkit.getPluginManager();
    SimpleCommandMap commandMap = null;
    List<Plugin> plugins = null;
    Map<String, Plugin> names = null;
    Map<String, Command> commands = null;
    disablePlugin(plugin);
    if (pluginManager != null) {
        Field pluginsField = Bukkit.getPluginManager().getClass().getDeclaredField("plugins");
        Field lookupNamesField = Bukkit.getPluginManager().getClass().getDeclaredField("lookupNames");
        Field commandMapField = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
        Field commandsField = SimpleCommandMap.class.getDeclaredField("knownCommands");
        commandsField.setAccessible(true);
        pluginsField.setAccessible(true);
        commandMapField.setAccessible(true);
        lookupNamesField.setAccessible(true);
        commandMap = (SimpleCommandMap) commandMapField.get(pluginManager);
        commands = (Map<String, Command>) commandsField.get(commandMap);
        plugins = (List<Plugin>) pluginsField.get(pluginManager);
        names = (Map<String, Plugin>) lookupNamesField.get(pluginManager);
    }
    if (plugins != null && plugins.contains(plugin)) {
        plugins.remove(plugin);
    }
    if (names != null && names.containsKey(name)) {
        names.remove(name);
    }
    if (commandMap != null) {
        for (Iterator<Map.Entry<String, Command>> it = commands.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<String, Command> entry = it.next();
            if (entry.getValue() instanceof PluginCommand) {
                PluginCommand c = (PluginCommand) entry.getValue();
                if (c.getPlugin() == plugin) {
                    c.unregister(commandMap);
                    it.remove();
                }
            }
        }
    }
    ClassLoader cl = plugin.getClass().getClassLoader();
    if (cl instanceof URLClassLoader) {
        ((URLClassLoader) cl).close();
    }
    System.gc();
    return true;
}
Also used : SimpleCommandMap(org.bukkit.command.SimpleCommandMap) PluginManager(org.bukkit.plugin.PluginManager) Field(java.lang.reflect.Field) PluginCommand(org.bukkit.command.PluginCommand) Command(org.bukkit.command.Command) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) Plugin(org.bukkit.plugin.Plugin) PluginCommand(org.bukkit.command.PluginCommand)

Aggregations

Command (org.bukkit.command.Command)2 SimpleCommandMap (org.bukkit.command.SimpleCommandMap)2 Field (java.lang.reflect.Field)1 URLClassLoader (java.net.URLClassLoader)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 FreedomCommand (me.totalfreedom.totalfreedommod.command.FreedomCommand)1 Displayable (me.totalfreedom.totalfreedommod.rank.Displayable)1 CommandMap (org.bukkit.command.CommandMap)1 PluginCommand (org.bukkit.command.PluginCommand)1 PluginIdentifiableCommand (org.bukkit.command.PluginIdentifiableCommand)1 Plugin (org.bukkit.plugin.Plugin)1 PluginManager (org.bukkit.plugin.PluginManager)1