Search in sources :

Example 11 with Command

use of org.bukkit.command.Command in project Essentials by EssentialsX.

the class EssentialsCommand method tabCompleteCommand.

/**
 * Attempts to tab-complete a command or its arguments.
 */
protected final List<String> tabCompleteCommand(CommandSource sender, Server server, String label, String[] args, int index) {
    // TODO: Pass this to the real commandmap
    Command command = server.getPluginCommand(label);
    if (command == null) {
        return Collections.emptyList();
    }
    int numArgs = args.length - index - 1;
    ess.getLogger().info(numArgs + " " + index + " " + Arrays.toString(args));
    String[] effectiveArgs = new String[numArgs];
    for (int i = 0; i < numArgs; i++) {
        effectiveArgs[i] = args[i + index];
    }
    if (effectiveArgs.length == 0) {
        effectiveArgs = new String[] { "" };
    }
    ess.getLogger().info(command + " -- " + Arrays.toString(effectiveArgs));
    return command.tabComplete(sender.getSender(), label, effectiveArgs);
}
Also used : Command(org.bukkit.command.Command)

Example 12 with Command

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

the class BukkitCommandManager method hookCommandMap.

@NotNull
private CommandMap hookCommandMap() {
    CommandMap commandMap = null;
    try {
        Server server = Bukkit.getServer();
        Method getCommandMap = server.getClass().getDeclaredMethod("getCommandMap");
        getCommandMap.setAccessible(true);
        commandMap = (CommandMap) getCommandMap.invoke(server);
        if (!SimpleCommandMap.class.isAssignableFrom(commandMap.getClass())) {
            this.log(LogLevel.ERROR, "ERROR: CommandMap has been hijacked! Offending command map is located at: " + commandMap.getClass().getName());
            this.log(LogLevel.ERROR, "We are going to try to hijack it back and resolve this, but you are now in dangerous territory.");
            this.log(LogLevel.ERROR, "We can not guarantee things are going to work.");
            Field cmField = server.getClass().getDeclaredField("commandMap");
            commandMap = new ProxyCommandMap(this, commandMap);
            cmField.set(server, commandMap);
            this.log(LogLevel.INFO, "Injected Proxy Command Map... good luck...");
        }
        Field knownCommands = SimpleCommandMap.class.getDeclaredField("knownCommands");
        knownCommands.setAccessible(true);
        // noinspection unchecked
        this.knownCommands = (Map<String, Command>) knownCommands.get(commandMap);
    } catch (Exception e) {
        this.log(LogLevel.ERROR, "Failed to get Command Map. ACF will not function.");
        ACFUtil.sneaky(e);
    }
    return commandMap;
}
Also used : Field(java.lang.reflect.Field) Server(org.bukkit.Server) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) PluginIdentifiableCommand(org.bukkit.command.PluginIdentifiableCommand) Command(org.bukkit.command.Command) Method(java.lang.reflect.Method) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) CommandMap(org.bukkit.command.CommandMap) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with Command

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

Example 14 with Command

use of org.bukkit.command.Command in project Essentials by drtshock.

the class EssentialsCommand method tabCompleteCommand.

/**
 * Attempts to tab-complete a command or its arguments.
 */
protected final List<String> tabCompleteCommand(CommandSource sender, Server server, String label, String[] args, int index) {
    // TODO: Pass this to the real commandmap
    Command command = server.getPluginCommand(label);
    if (command == null) {
        return Collections.emptyList();
    }
    int numArgs = args.length - index - 1;
    ess.getLogger().info(numArgs + " " + index + " " + Arrays.toString(args));
    String[] effectiveArgs = new String[numArgs];
    for (int i = 0; i < numArgs; i++) {
        effectiveArgs[i] = args[i + index];
    }
    if (effectiveArgs.length == 0) {
        effectiveArgs = new String[] { "" };
    }
    ess.getLogger().info(command + " -- " + Arrays.toString(effectiveArgs));
    return command.tabComplete(sender.getSender(), label, effectiveArgs);
}
Also used : Command(org.bukkit.command.Command)

Example 15 with Command

use of org.bukkit.command.Command in project Denizen-For-Bukkit by DenizenScript.

the class CommandScriptHelper method init.

public static void init() {
    if (isInitialized) {
        return;
    }
    isInitialized = true;
    try {
        Server server = Bukkit.getServer();
        server.getPluginManager().registerEvents(instance, Denizen.getInstance());
        // Get the CommandMap for the server
        Field commandMapField = server.getClass().getDeclaredField("commandMap");
        commandMapField.setAccessible(true);
        CommandMap commandMap = (CommandMap) commandMapField.get(server);
        // Get the knownCommands for the server's CommandMap
        Field kcf = null;
        Class commandMapClass = commandMap.getClass();
        while (kcf == null && commandMapClass != Object.class) {
            try {
                kcf = commandMapClass.getDeclaredField("knownCommands");
            } catch (NoSuchFieldException e) {
                commandMapClass = commandMapClass.getSuperclass();
            }
        }
        final Field knownCommandsField = kcf;
        knownCommandsField.setAccessible(true);
        knownCommands = (Map<String, Command>) knownCommandsField.get(commandMap);
        // Get the HelpMap for the server
        HelpMap helpMap = server.getHelpMap();
        // Get the helpTopics for the server's HelpMap
        final Field helpTopicsField = helpMap.getClass().getDeclaredField("helpTopics");
        helpTopicsField.setAccessible(true);
        helpTopics = (Map<String, HelpTopic>) helpTopicsField.get(helpMap);
        // TODO: figure out a different workaround?
        if (Settings.overrideHelp()) {
            new BukkitRunnable() {

                @Override
                public void run() {
                    if (knownCommands.get("help") instanceof HelpCommand) {
                        return;
                    }
                    knownCommands.put("help", knownCommands.get("bukkit:help"));
                    helpTopics.put("/help", helpTopics.get("/bukkit:help"));
                }
            }.runTaskLater(Denizen.getInstance(), 1);
        }
    } catch (Exception e) {
        Debug.echoError("Error getting the server's command information! Are you running a non-CraftBukkit server?");
        Debug.echoError("Command scripts will not function!");
        // dB.echoError(e);
        hasCommandInformation = false;
    }
}
Also used : Server(org.bukkit.Server) HelpCommand(org.bukkit.command.defaults.HelpCommand) DenizenCommandHelpTopic(com.denizenscript.denizen.utilities.command.scripted.DenizenCommandHelpTopic) HelpTopic(org.bukkit.help.HelpTopic) DenizenAliasHelpTopic(com.denizenscript.denizen.utilities.command.scripted.DenizenAliasHelpTopic) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) Field(java.lang.reflect.Field) DenizenCommand(com.denizenscript.denizen.utilities.command.scripted.DenizenCommand) HelpCommand(org.bukkit.command.defaults.HelpCommand) Command(org.bukkit.command.Command) HelpMap(org.bukkit.help.HelpMap) CommandMap(org.bukkit.command.CommandMap)

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