Search in sources :

Example 1 with CommandMap

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

use of org.bukkit.command.CommandMap in project DiscordBot by LXGaming.

the class CommandManager method getCommandMap.

private CommandMap getCommandMap() {
    if (!(Bukkit.getPluginManager() instanceof SimplePluginManager)) {
        return null;
    }
    try {
        Field field = SimplePluginManager.class.getDeclaredField("commandMap");
        field.setAccessible(true);
        return (CommandMap) field.get(Bukkit.getPluginManager());
    } catch (IllegalAccessException | IllegalArgumentException | NoSuchFieldException | SecurityException ex) {
        DiscordBot.getInstance().getLogger().severe("Exception getting commandMap!");
        ex.printStackTrace();
    }
    return null;
}
Also used : Field(java.lang.reflect.Field) SimplePluginManager(org.bukkit.plugin.SimplePluginManager) CommandMap(org.bukkit.command.CommandMap)

Example 3 with CommandMap

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

the class CommandBlocker method load.

public void load() {
    entryList.clear();
    unknownCommands.clear();
    final CommandMap commandMap = CommandReflection.getCommandMap();
    if (commandMap == null) {
        FLog.severe("Error loading commandMap.");
        return;
    }
    @SuppressWarnings("unchecked") List<String> blockedCommands = (List<String>) ConfigEntry.BLOCKED_COMMANDS.getList();
    for (String rawEntry : blockedCommands) {
        final String[] parts = rawEntry.split(":");
        if (parts.length < 3 || parts.length > 4) {
            FLog.warning("Invalid command blocker entry: " + rawEntry);
            continue;
        }
        final CommandBlockerRank rank = CommandBlockerRank.fromToken(parts[0]);
        final CommandBlockerAction action = CommandBlockerAction.fromToken(parts[1]);
        String commandName = parts[2].toLowerCase().substring(1);
        final String message = (parts.length > 3 ? parts[3] : null);
        if (rank == null || action == null || commandName == null || commandName.isEmpty()) {
            FLog.warning("Invalid command blocker entry: " + rawEntry);
            continue;
        }
        final String[] commandParts = commandName.split(" ");
        String subCommand = null;
        if (commandParts.length > 1) {
            commandName = commandParts[0];
            subCommand = StringUtils.join(commandParts, " ", 1, commandParts.length).trim().toLowerCase();
        }
        final Command command = commandMap.getCommand(commandName);
        // Obtain command from alias
        if (command == null) {
            unknownCommands.add(commandName);
        } else {
            commandName = command.getName().toLowerCase();
        }
        if (entryList.containsKey(commandName)) {
            FLog.warning("Not blocking: /" + commandName + " - Duplicate entry exists!");
            continue;
        }
        final CommandBlockerEntry blockedCommandEntry = new CommandBlockerEntry(rank, action, commandName, subCommand, message);
        entryList.put(blockedCommandEntry.getCommand(), blockedCommandEntry);
        if (command != null) {
            for (String alias : command.getAliases()) {
                entryList.put(alias.toLowerCase(), blockedCommandEntry);
            }
        }
    }
    FLog.info("Loaded " + blockedCommands.size() + " blocked commands (" + (blockedCommands.size() - unknownCommands.size()) + " known).");
}
Also used : Command(org.bukkit.command.Command) List(java.util.List) CommandMap(org.bukkit.command.CommandMap)

Aggregations

CommandMap (org.bukkit.command.CommandMap)3 List (java.util.List)2 Command (org.bukkit.command.Command)2 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 FreedomCommand (me.totalfreedom.totalfreedommod.command.FreedomCommand)1 Displayable (me.totalfreedom.totalfreedommod.rank.Displayable)1 PluginIdentifiableCommand (org.bukkit.command.PluginIdentifiableCommand)1 SimpleCommandMap (org.bukkit.command.SimpleCommandMap)1 SimplePluginManager (org.bukkit.plugin.SimplePluginManager)1