Autocomplete

Discussion in 'Plugin Development' started by MieskeB, Jul 20, 2019.

Thread Status:
Not open for further replies.
  1. In the plugin.yml you can say for usage command:

    /<command>

    Now I was wondering, if I say for example

    /<command> <faction>

    Is it possible if I can autocomplete everywhere where <faction> for usage is assigned, autocomplete with this arraylist?
     
  2. Offline

    KarimAKL

    @MieskeB You can make your class implement TabCompleter as well as CommandExecutor, then override the 'onTabComplete' method.
    Remember to register it as well, with the 'PluginCommand#setTabCompleter(TabCompleter)' method.
     
    MieskeB likes this.
  3. @KarimAKL Thank you very much. But I was wondering if I could override a usage tag. So for example if you have to following command in plugin.yml:

    Code:
    commands:
      join:
        description: Join a faction
        usage: /<command> <faction>
      leave:
        description: Leave a faction
        usage: /<command> <faction>
      teleport:
        description: Teleport to a location
        usage: /<command> <location>
    Is it possible to make it autocomplete as soon as it sees that I used <faction> and make it autocomplete with the list of factions and when it seed <location> that it autocompletes with the list of locations, with only hardcoding the tags and not the commands?
     
  4. Offline

    KarimAKL

    @MieskeB I'm not sure i understand you but if you want it to display the factions when you use '/join faction <tab>', then you can do something like this:
    Code:Java
    1. @Override
    2. public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
    3. List<String> list = new ArrayList<>();
    4. if (args.length == 2) {
    5. if (args[0].equalsIgnoreCase("faction")) {
    6. List<String> factions = /*A list of all your faction names*/;
    7. for (String faction : factions) {
    8. if (!faction.toLowerCase().startsWith(args[1].toLowerCase())) continue;
    9. list.add(faction);
    10. }
    11. }
    12. }
    13. return list;
    14. }

    That code seems pretty nested, but it's just an example. :p
     
    MieskeB likes this.
  5. @KarimAKL Hmm still not what I meant but I don't think it is possible... I will do it this way! Thank you so much for your time!
     
    KarimAKL likes this.
  6. Offline

    KarimAKL

    @MieskeB I just reread your previous post, and thought i might understand. (let me know if i still don't understand what you mean)
    You can do 'JavaPlugin#getCommand(String)' to get the command in your plugin.yml file, then use 'PluginCommand#getUsage()' to get the usage from your command; then just make some checks for the '<faction>' and add them to the list.
    I'm not sure if it returns the same but you could probably also use the 'Command' argument from the 'onTabComplete' method, instead of getting the command from the 'JavaPlugin#getCommand(String)'.
     
Thread Status:
Not open for further replies.

Share This Page