TabCompleter not working

Discussion in 'Spigot Plugin Development' started by Paperboypaddy16, Dec 4, 2019 at 3:36 AM.

  1. Hello, I'm trying to make my tab completer work on my plugin and on my server all that it does is returns players names. Also putting debug in my code shows that its not even running.
    So i think theres something wrong with what im doing.
    Code (Java):
    package me.paperboypaddy16.tbn.prison.tabcomplete.prison;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.TabCompleter;

    public class TabComplete implements TabCompleter {
        @Override
        public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("prison")) {
                sender.sendMessage("ree");
                if (args.length > 0) {
                    ArrayList<String> commands = new ArrayList<String>();
                    if (!args[0].equals("")) {
                        for (Commands fcmd : Commands.values()) {
                            if (fcmd.name().toLowerCase().startsWith(args[0].toLowerCase())) {
                                commands.add(fcmd.name());
                            }
                        }
                    } else {
                        for (Commands fcmd : Commands.values()) {
                            commands.add(fcmd.name());
                        }
                    }
                    Collections.sort(commands);
                    return commands;
                }
            }
            sender.sendMessage("oof");
            return null;
        }

        enum Commands {
            HELP, ADMIN, SETPVP, SETMINE, PVP, MINE, UPGRADE, RELOAD;
        }
    }
    Code (Java):
    getCommand("prison").setExecutor(new me.paperboypaddy16.tbn.prison.commands.prison.Executer());
    getCommand("prison").setTabCompleter(new TabComplete());
     
  2. At first, Why do you check if the commandName = prison, you don't have to do this when you set a TabCompleter, as you set is specificly for that command.
    Also did you try to print a message every time this methode gets called, or that the array list you return is empty. (I think the last one is the thing that breaks is. ;))
     
  3. @x313 I do notice that the code isn’t even running if I press tab. I have the set tab on it setup right I believe but it’s just not running the code.
     
  4. Strahan

    Benefactor

    Well, I never used a TabCompleter but when I need to complete my args, I use a TabExecutor. Dunno if it's the same functionality, but it works great for me. In my command class instead of implementing CommandExecutor I do TabExecutor then add a public List<String> onTabComplete with the same constructor as the onCommand. Then in that function I build the List based on what args are and return it and works good for me.

    Of course, I may be totally not understanding your objective as I don't know what a TabCompleter is but it's something to look it (shrug)
     
  5. Sorry, that link no longer connects to the original documentation.

    However, they certaintly can be put into separate classes.
    During the onEnable()
    Code (Text):
            PluginCommand command = getCommand("commandtabs"); //tells bukkit to register a command (must match command declared in the plugin.yml)
            command.setExecutor(new CTCommand()); //tells bukkit to use CTCommand.class for the PluginCommand.Executor
            command.setTabCompleter(new CTTabCompleter()); //tells bukkit to use CTTabCompleter.class for the PluginCommand.TabCompleter
     
     
  6. How do you use TabExecutor? I looked up how to use it and i think i did it right but its not working.
    Code (Java):
    public class Executer implements Listener, TabExecutor {
    @Override
        public List<String> onTabComplete(CommandSender sender, Command cmd, String alias, String[] args) {
            Bukkit.getServer().getConsoleSender().sendMessage("oof");
            ArrayList<String> command = new ArrayList<String>();
            command.add("OOF");
            return command;
        }
    }
     
  7. Strahan

    Benefactor

    Well, you need to adjust it based on where they are in the args list. Say you had a command with args "ban (user)" and "set (health | gm | money)" you'd want to do something akin to:
    Code (Text):
    @Override
    public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
      List<String> cmds = new ArrayList<String>();

      switch (args.length) {
      case 1:
        if (sender.hasPermission("myplugin.ban")) cmds.add("ban");
        if (sender.hasPermission("myplugin.set")) cmds.add("set");
        return StringUtil.copyPartialMatches(args[0], cmds, new ArrayList<String>());
       
      case 2:
        switch (args[0].toLowerCase()) {
        case "ban":
          for (Player p : getServer().getOnlinePlayers()) cmds.add(p.getName());
          return StringUtil.copyPartialMatches(args[1], cmds, new ArrayList<String>());
         
        case "set":
          if (sender.hasPermission("myplugin.set.health")) cmds.add("health");
          if (sender.hasPermission("myplugin.set.gm")) cmds.add("gm");
          if (sender.hasPermission("myplugin.set.money")) cmds.add("money");
          return StringUtil.copyPartialMatches(args[1], cmds, new ArrayList<String>());
        }
      }
      return null;
    }