1.15.2 Tab complete args

Discussion in 'Spigot Plugin Development' started by Phoenix852, Mar 4, 2020.

  1. How do I make command arguments with tab complete feature? I think I need to import org.bukkit.command.TabComplete and then use it, if so where and how to use the code?
     
  2. md_5

    Administrator Developer

    • Informative Informative x 2
  3. Nope, I mean you can, but I normally have it in my command executor class
     
    • Like Like x 1
  4. hey hey, I did this before
    Code (Java):
       public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args){
            //code here...
        }
    but it says "missing return statement."

    but when I do this
    Code (Java):

    public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args){
        return true;
    }
     
    a red line appears under the "return true;" statement saying "Incompatible types. Found: 'boolean', required: 'java.util.List<java.lang.String>' "
     
    • Optimistic Optimistic x 1
  5. A return statement must satisfy what the method requires..

    So a void method simply needs a basic "return" to end, whereas boolean, string or ints, for example, must each return that respective primitive/object

    In your case tab completer returns a List<String>, so whatever you return must be a list of strings, whereas, you're attempting to return a boolean hence your error
     
  6. oh oops. (how dumb I am). Thank you
     
    • Optimistic Optimistic x 1
  7. but wait. Can you show me an example of adding a tabcomplete argument. I am not getting how to add it to the list and all.
     
    • Optimistic Optimistic x 1
  8. Hard to explain with words, but I am a visual learner, so maybe you are too, here is how I have mine set up

    Code (Java):

    @Override
        public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {

            if (command.getName().equalsIgnoreCase("disguise")) { // checking if my command is the one i'm after

                List<String> autoCompletes = new ArrayList<>(); //create a new string list for tab completion

                if (args.length == 1) { //only interested in the first sub command, if you wanted to cover more deeper sub commands, you could have multiple if statements or a switch statement

                    for (CommandManager commandManager : commands) { // here and I loop through all my sub commands and grab their appropriate string to add to the list, but in your case you can just add each manually (autoCompletes.add("add) .. .add("set).. etc

                        autoCompletes.add(commandManager.getName());

                    }

                    return autoCompletes; // then return the list

                }

            }

            return null; // this will return nothing if it wasn't the disguise command I have
        }[
    /code]
     
    • Informative Informative x 2
  9. Thank you so much :) . It helped me.
     
    • Friendly Friendly x 1
    • Optimistic Optimistic x 1
  10. drives_a_ford

    Moderator

    I would suggest using StringUtil.copyPartialMatches instead. Much simple and cleaner. Of course, you need the original list of subcommands (or options).
     
    • Useful Useful x 2
  11. I would suggest using Aikar's acf https://github.com/aikar/commands

    Its far better than bukkit api. Also if you run paper acf supports asynchronous tab completition.
     
  12. It is probably only confusing for a beginner and will not really be helpful in simple beginner plugins.
     
    • Agree Agree x 1
  13. I place my commands in separate classes and use TabExecutor.

    Code (Text):
        @Override
        public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
                return ImmutableList.of();
                            //TODO: add some actual tab completion instead of just an empty list
    }
     
  14. Strahan

    Benefactor