Predict Command Autocompletion

Discussion in 'Spigot Plugin Development' started by simonmeusel, May 23, 2016.

  1. How can I auto complete a command (gives as a String) like a player would do by pressing tab?

    I have tried this but it does not work:

    String fullCommand = "fill ";

    String command = "";
    List<String> argList = new ArrayList<>();
    for (String string : fullCommand.split(" ")) {
    if (command == "") {
    command = string;
    } else {
    argList.add(string);
    }
    }
    Command cmd = new Command(command) {

    @Override
    public boolean execute(CommandSender arg0, String arg1, String[] arg2) {
    return false;
    }

    };
    System.out.println(command);
    TabCompleter tabCompleter = getCommand(command).getTabCompleter();
    tabCompleter.onTabComplete(Bukkit.getConsoleSender(), cmd, command, argList.toArray(new String[0]));​

    The error:

    java.lang.NullPointerException
    at de.simonmeusel.mcide.plugin.Plugin.onEnable(Plugin.java:44) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:292) ~[s
    pigot-1.9.4.jar:git-Spigot-8a048fe-a022dd2]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:340) [spigot-1.9.4.jar:git-Spigot-8a048fe-a022dd2]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:405) [spigot-1.9.4.jar:git-Spigot-8a048fe-a022dd2]
    at org.bukkit.craftbukkit.v1_9_R2.CraftServer.loadPlugin(CraftServer.jav
    a:362) [spigot-1.9.4.jar:git-Spigot-8a048fe-a022dd2]
    at org.bukkit.craftbukkit.v1_9_R2.CraftServer.enablePlugins(CraftServer.
    java:322) [spigot-1.9.4.jar:git-Spigot-8a048fe-a022dd2]
    at net.minecraft.server.v1_9_R2.MinecraftServer.t(MinecraftServer.java:4
    16) [spigot-1.9.4.jar:git-Spigot-8a048fe-a022dd2]
    at net.minecraft.server.v1_9_R2.MinecraftServer.l(MinecraftServer.java:3
    81) [spigot-1.9.4.jar:git-Spigot-8a048fe-a022dd2]
    at net.minecraft.server.v1_9_R2.MinecraftServer.a(MinecraftServer.java:3
    36) [spigot-1.9.4.jar:git-Spigot-8a048fe-a022dd2]
    at net.minecraft.server.v1_9_R2.DedicatedServer.init(DedicatedServer.jav
    a:268) [spigot-1.9.4.jar:git-Spigot-8a048fe-a022dd2]
    at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java
    :532) [spigot-1.9.4.jar:git-Spigot-8a048fe-a022dd2]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_73]​

    Line 44:
    TabCompleter tabCompleter = getCommand(command).getTabCompleter();​

    I guess getCommand("fill") returns null.

    So i tried registering a command in the plugin.yml and go with tahat but that gives the same error.

    Any ideas?
     
  2. Choco

    Moderator

    Nope. .getTabCompleter() returns null. What you want to do, is create a class that "implements TabCompleter". Just go from there. It's quite simple.

    To register the tab completer, you do the same thing as registering a command in your onEnable. this.getCommand("").setTabCompleter(new YourTabCompletionClass())
     
    • Like Like x 1
  3. I don't wan't to create my own tab completer, I want to get the tabCompleter of for example the /fill command
     
  4. I think NMS handles it differently.

    Gesendet von meinem Nexus 5 mit Tapatalk
     
    • Agree Agree x 1
  5. What do you mean with NMS?
     
  6. Net Minecraft Server.
     
    • Informative Informative x 2
  7. This is what you want:https://hub.spigotmc.org/javadocs/spigot/org/bukkit/command/TabExecutor.html

    You make a class implementing TabExecutor, and you will have to implement two methods, one is for the command and the other is for tab complete. The onCommand method works like any regular onCommand. The TabCompletor works very similarly, all you have to do is detect how many arguments the player has typed and then return a list of all possibilities based on which argument they are on.
     
  8. No that is not what i want. I dont want to create my own command with tab executer, i want to get a tab executor of /fill or other minecraft command, so i could get the list for /fill 2 56 2 min...
     
  9. You could use a CommandPreProcessEvent or something like that, then check the commands and the arguments.