Solved Add command pre-visualization

Discussion in 'Plugin Development' started by Chronoxx98, Mar 20, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    Chronoxx98

    Hello,

    I am developing a plugin, and I would like to add a sort of command pre-visualization, I don't know if it is the right name but here it was I mean (and would like to implement):

    What I have:
    Capture1.PNG

    What I want:
    Capture2.PNG

    LC is my plugin (LobbyCompass2) and LP is LucksPerm.. When I write /lc , I would like to see the possible commands that follows, not the players name.

    Could someone help me implement this ? Or maybe give me the name of how it is called / a link to the documentation?

    Thank you very much
     
  2. Offline

    timtower Moderator Moderator

  3. Offline

    Chronoxx98

    @timtower Is it an API or is it already included in default Bukkit ?

    Thank you
     
  4. Offline

    timtower Moderator Moderator

  5. Offline

    Chronoxx98

    Thank you, I guess I'll have to modify it a bit if I want it to look like the screenshot
     
  6. Offline

    timtower Moderator Moderator

    Need to start somewhere.
     
  7. Offline

    Strahan

    That's not really the best implemenation, in my opinion. I prefer to just use TabExecutor and process it there, no need to set tabcompleter plus that example doesn't appear to be processing suggestions.
     
  8. Offline

    timtower Moderator Moderator

    First link, can happen. Not helpful that it is from 2014 indeed.
     
  9. Offline

    Strahan

    No worries, it's better than nothing.

    OP, the way I handle tab completion achieves the result you illustrated. Not saying my approach is all that, as I'm new to Java myself this may not be the best way but just to show you, this is it:
    Code:
    public class MyPluginCommand implements TabExecutor {
      MyPlugin plugin;
      public MyPluginCommand(MyPlugin plugin) {
        this.plugin = plugin;
      }
    
      @Override
      public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
        List<String> ret = new ArrayList<String>();
        // Some people return null, but it's better to return an empty list so it doesn't
        // default to player names.  If the command has a base permission, check and 
        // return the empty list if they don't qualify
        if (!sender.hasPermission("myplugin.use")) return ret;
        switch (args.length) {
        case 1:
          // Remember we're operating on args.length, so for dealing with the first arg
          // (arg[0]) we switch on 1 not 0
         
          // Add subcommands if they have the right perm
          if (sender.hasPermission("myplugin.use.warp")) ret.add("warp");
          if (sender.hasPermission("myplugin.use.god")) ret.add("god");
       
          // Add subcommands that anyone can use
          ret.add("motd");
          ret.add("afk");
       
          // If we just return the List, the full list of options will appear and stay 
          // fixed.  By returning only options that match as they type, it gives the tab
          // completion context on what to show
          return getMatchedAsType(args[0], ret);
    
        case 2:
          // Switch on the first argument so we can send argument specific stuff
          switch (args[0].toLowerCase()) {
          case "warp":
            for (String warp : plugin.getWarpList()) ret.add(warp);
            break;
         
          case "god":
            if (!sender.hasPermission("myplugin.use.god.others")) return ret;
            // Remember where I said we want to return an empty List instead of null? 
            // Well, in this case the desired result is a list of players - that's what
            // happens when one returns null so instead of doing something like looping
            // getOnlinePlayers() to build a list of names, we just return null and take
            // advantage of that default behavior.
            return null;
          }
       
          return getMatchedAsType(args[1], ret);
        default:
          return ret;
        }
      }
    
      List<String> getMatchedAsType(String typed, List<String> values) {
        List<String> ret = new ArrayList<String>();
        for (String element : values) if (element.startsWith(typed)) ret.add(element);
        return ret;
      }
    
      @Override
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        return true;
      }
    }
     
    Chronoxx98 likes this.
  10. Offline

    Chronoxx98

    Thank you very much guys !
     
Thread Status:
Not open for further replies.

Share This Page