Solved TabExecutor not working as intended

Discussion in 'Spigot Plugin Development' started by Hunky524, May 19, 2016.

  1. I have a class called 'Balance' that implements 'TabExecutor'. In the class I have the onTabComplete and onCommand method. I have registered the 'Balance' class in my onEnable. For some reason the onTabComplete does not work, but the onCommand method does work. Can anyone see what I might be missing?

    Here is my onEnable method:
    Code (Text):
     public void onEnable() {
            configManager = new MyConfigManager(this);
            playerEconomyData = configManager.getNewConfig("PlayerEconomyData.yml");
            messages = configManager.getNewConfig("Messages.yml");

            Bukkit.getPluginManager().registerEvents(new PlayerJoin(), this);
           
            getCommand("Economy").setTabCompleter(new Balance());
            getCommand("Economy").setExecutor(new Balance());

            loadEconomy();
        }

    Here is my Balance class:
    Code (Text):
    public class Balance implements TabExecutor {
        Main main = Main.getMain();

        @Override
        public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
            List<String> rList = new ArrayList<>();

            if (args.length == 0) {
                rList.add("Balance");
                return rList;
            }

            return null;
        }

        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

            if (args.length == 0) {
                //TODO Add args error message
                return false;
            }

            if (args[0].equalsIgnoreCase("Balance") || args[0].equalsIgnoreCase("Bal")) {
                //== /Economy Balance

                if (args.length > 1) {
                    //TODO Add args too long error
                    return false;
                }

                if (!(sender instanceof Player)) {
                    //TODO Add not player error
                    return false;
                }

                Player p = (Player) sender;

                if (!main.playerWalletMap.containsKey(p.getUniqueId())) {
                    //TODO Add no economy data error
                    return false;
                }

                p.sendMessage(main.playerWalletMap.get(p.getUniqueId()).getCurrency().toString());
                return true;
            }

            return false;
        }
    }

    Thanks is advance :)
     
    #1 Hunky524, May 19, 2016
    Last edited: May 19, 2016
  2. You need to set the tab executor of the command as well. Instead of using each command class for each tab complete, you can just do a big tab complete class (if you want). I just do something like this:

    Code (Java):
        private void registerCommands()
        {
            CmdManager hub = new CmdManager(); // You won't have this, but it contains an array of my commands.
         
            for(String string : hub.COMMANDS)
            {
                getCommand(string).setTabCompleter(hub);
            }
        }
     
    #2 Shortninja, May 19, 2016
    Last edited: May 19, 2016
  3. I don't seem to have CmdManager. I also should have said that I've tried using the .setTabCompleter() with two separate classes using CommandExecuter and TabCompleter, as well as merging them as I've shown with TabExecutor.
     
  4. I was just showing you an example of what I did. I have an array of all the commands I'm using in order for other things unrelated to tab complete.

    Send me the code of you setting the tab completer.
     
  5. TabExecutor class. I've tried separating these two and have one class implement CommandExecutor and one implement TabCompletor as well.

    Code (Text):
    public class Balance implements TabExecutor {
        Main main = Main.getMain();

        @Override
        public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
            List<String> rList = new ArrayList<>();

            if (args.length == 0) {
                rList.add("Balance");
                return rList;
            }

            return null;
        }

        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

            if (args.length == 0) {
                //TODO Add args error message
                return false;
            }

            if (args[0].equalsIgnoreCase("Balance") || args[0].equalsIgnoreCase("Bal")) {
                //== /Economy Balance

                if (args.length > 1) {
                    //TODO Add args too long error
                    return false;
                }

                if (!(sender instanceof Player)) {
                    //TODO Add not player error
                    return false;
                }

                Player p = (Player) sender;

                if (!main.playerWalletMap.containsKey(p.getUniqueId())) {
                    //TODO Add no economy data error
                    return false;
                }

                p.sendMessage(main.playerWalletMap.get(p.getUniqueId()).getCurrency().toString());
                return true;
            }

            return false;
        }
    }
     
  6. No, I'm asking where did you do the tab completer setting? You should have something like this in your onEnable:

    Code (Java):
    getCommand("balance").setTabCompleter(new Balance());
     
  7. Oh yea sorry. What you typed there is exactly what I have. Here it is if you want to see it:
    Code (Text):
    public void onEnable() {
            configManager = new MyConfigManager(this);
            playerEconomyData = configManager.getNewConfig("PlayerEconomyData.yml");
            messages = configManager.getNewConfig("Messages.yml");

            Bukkit.getPluginManager().registerEvents(new PlayerJoin(), this);

            getCommand("Economy").setExecutor(new Balance());
            getCommand("Economy").setTabCompleter(new Balance());

            loadEconomy();
        }
     
  8. probably at his onEnable() the main class above check it out
     
  9. It was not in his original source, he just added it.

    Have you tried debugging it with a System.out.println at the beginning of the onTabComplete method to make sure it is being executed? Is there an error in console? If the command is not found I'm pretty sure there will be a stack trace in console.
     
  10. I did, I keep trying stuff and changing code that's why there is differences. I added it to show that I have tried it.
     
  11. I don't know what your saying is but its really in the first post of himself :D
     
  12. I have tried print messages on the onTabComplete and the messages do print.
     
    • Optimistic Optimistic x 1
  13. Because he just edited it

    When are you pressing tab? After a space in the command or before it?
     
  14. I can get it to (sort of) work if I take the if-statements out of the onTabComplete. By sort of work I mean it gives the tab suggestions, but I need it to work with the if-statements.
     
    • Winner Winner x 1
  15. Well that makes total sense. I'm really not sure what could be wrong at this point, but if this helps here is how I typically do mine:

    Code (Java):
        @Override
        public List<String> onTabComplete(CommandSender sender, Command cmd, String string, String[] args)
        {
            List<String> tabComplete = null;
         
            if(args.length == 1)
            {
                switch(string)
                {
                    case "test":
                        tabComplete = Arrays.asList("that's a test sir");
                        break;
                }
            }
         
            return tabComplete;
        }
     
    #15 Shortninja, May 19, 2016
    Last edited: May 19, 2016
  16. Ok, I fixed it. It was a problem with my if-statements within my onTabComplete. Thanks for everyone who helped :)

    I fixed it by changing:
    Code (Text):
    if (args.length == 0)
    to:
    Code (Text):
    if (args.length == 1)
    Not sure why that worked considering I want the tab to complete when the user hasn't entered any args, but if it works I guess I can live with it just not sure why. I guess when you hit space bar, even if you haven't typed anything the args length is still 1.
     
    • Funny Funny x 2
  17. Woops, I should've noticed that :p. Glad you found the issue! Just remember, the landlord is the owner of the property but the property you rent is your home and you have a right to quiet and peaceful enjoyment of the property!
     
  18. I don't know I'm sure "Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur?" are works to live by.
     
    • Funny Funny x 1