1.14.4 Online Player Tab Complete

Discussion in 'Spigot Plugin Development' started by SupremeCoder101, Jan 10, 2020.

  1. How would I tab complete online players?
     
  2. If you have a tab-complete method, then just use
    Code (Text):

    List<String> list = new ArrayList<String>();
                for (Player p : Bukkit.getOnlinePlayers()) {
                    list.add(p.getName());
                }
                return list;
     
    If you don't have a tab-complete method then:
    Create a new Class
    Call it whatever you want,
    implements, "TabCompleter"
    then:
    Code (Text):

    @Override
        public List<String> onTabComplete(CommandSender sender, Command cmd, String string, String[] args) {
            if (cmd.getName().equalsIgnoreCase("[COMMAND NAME]")) {
                List<String> list = new ArrayList<String>();
                for (Player p : Bukkit.getOnlinePlayers()) {
                    list.add(p.getName());
                }
                return list;
            }
            return null;
        }
     
    And when you go into the main class, create a new line:
    Code (Text):

    getCommand("[COMMAND NAME]").setTabCompleter(new [CLASS NAME]());
     
     
    • Agree Agree x 1
  3. This is incorrect, the correct solution on this is to check if the names also are led with the last argument to make sure that you only show the names that matter to the commandu ser.
     
    • Agree Agree x 1
  4. Correct, but he didn't specify more needs for it.
    What I gave him was some basics for the tab completer, for online players.
     
    • Like Like x 1
  5. I'm sorry, I don't understand what you are trying to say.
     
  6. I am not 100% sure, but I think what he is trying to say is if you type ex. ( /COMMAND a ) only usernames that have an 'a' in them will pop up, so you can type something of the name, and it will show.
     
  7. yes that is what I want
     
  8. You might be able to do this


    Code (Java):
    return Bukkit.getOnlinePlayers()
        .stream()
        .filter(p -> p.getName().startsWith(args[0]))
        .collect(Collectors.toList());
    Writing this on my phone so no guarantees
    Also check that args has at least a length of one
     
    #8 Schottky, Jan 10, 2020
    Last edited: Jan 10, 2020
    • Agree Agree x 1
  9. So this will get the letter I started with and get online players names that start with this letter
     
  10. In theory yes. But you‘ll have to try it out, I can‘t do this right now.
     
  11. Also assuming that the player is the first argument
     
  12. Okay I’ll try it out later. I’m out right now. :D
     
  13. drives_a_ford

    Moderator

    Why not simply return null from the method? It defaults to completing for online players that the sender can see. That's the default behaviour anyway.
    The approaches described here don't take into account players you might not see (i.e vanished players).
    When the tab completer and/or the executor return null, PluginCommand#tabComplete calls Command#tabComplete, which in turn calls Command#tabComplete0, which goes through the players which the sender can see (i.e it generally doesn't include vanished players) whose names match (case insensitively).
     
    • Agree Agree x 1
    • Winner Winner x 1
    • Informative Informative x 1
  14. Forgot #getName
    +1
     
  15. thank you for pointing that out, fixed it!

    absolutely right, OP (and I as well) completely forgot about that