Solved Command Tab Completion? Help?

Discussion in 'Plugin Development' started by CruzAPI, Jul 6, 2016.

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

    CruzAPI

    Firstly, let's clarify the things. There are 3 kinds of TAB completion:

    1. Chat Tab Completion
    When a player type TAB to complete a message.
    It will call the PlayerChatTabCompleteEvent.
    Ex:
    I am talking with a player "BoyGamer_123" and I press 'T' to open the chat and type: "hey boygam" and press TAB it will complete the name to me.


    2. Command Parameter Tab Completer (Tab Executor)
    It happens when a player is typing a command and press TAB to complete the next parameters (args).
    Ex:
    "/gamemode surv" + TAB = "/gamemode survival"
    "/tp CruzAPI " + TAB = "/tp CruzAPI BoyGamer_123"
    etc.
    You can create your own tab executor by implement the TabExecutor and registering the command with getCommand("name").setTabExecutor(YourTabExecutor);


    3. Complete commands (My question)
    This happens when you type "/" and press TAB, it will show ALL commands of plugins, all commands of bukkit and all commands of minecraft.
    And here is my question:
    How can I change it? It is even possible?

    Please help...
    Thanks!
     
    Last edited: Jul 6, 2016
  2. That's the PlayerChatTabCompleteEvent too
     
  3. Offline

    I Al Istannen

  4. Well, I found these methods in the craftbukkit server class.
    general tab completion (open)

    Code:
    public List<String> tabComplete(ICommandListener sender, String message)
      {
        if (!(sender instanceof EntityPlayer)) {
          return ImmutableList.of();
        }
        Player player = ((EntityPlayer)sender).getBukkitEntity();
        if (message.startsWith("/")) {
          return tabCompleteCommand(player, message);
        }
        return tabCompleteChat(player, message);
      }
    commands (open)

    Code:
    public List<String> tabCompleteCommand(Player player, String message)
      {
        List<String> completions = null;
        try
        {
          completions = getCommandMap().tabComplete(player, message.substring(1));
        }
        catch (CommandException ex)
        {
          player.sendMessage(ChatColor.RED + "An internal error occurred while attempting to tab-complete this command");
          getLogger().log(Level.SEVERE, "Exception when " + player.getName() + " attempted to tab complete " + message, ex);
        }
        return completions == null ? ImmutableList.of() : completions;
      }
    chat (open)

    Code:
    public List<String> tabCompleteChat(Player player, String message)
      {
        List<String> completions = new ArrayList();
        PlayerChatTabCompleteEvent event = new PlayerChatTabCompleteEvent(player, message, completions);
        String token = event.getLastToken();
        for (Player p : getOnlinePlayers()) {
          if ((player.canSee(p)) && (StringUtil.startsWithIgnoreCase(p.getName(), token))) {
            completions.add(p.getName());
          }
        }
        this.pluginManager.callEvent(event);
    
        Iterator<?> it = completions.iterator();
        while (it.hasNext())
        {
          Object current = it.next();
          if (!(current instanceof String)) {
            it.remove();
          }
        }
        Collections.sort(completions, String.CASE_INSENSITIVE_ORDER);
        return completions;
      }


    As you can see, only in the 3. method an event is called. I don't know if there is any way to do it but I think it's not possible.
    You could try out TabCompleteEvent but I'm not sure..
     
  5. Offline

    I Al Istannen

    @FisheyLP
    General tab completion?
    Code:
        public List<String> tabComplete(net.minecraft.server.ICommandListener sender, String message) {
            if (!(sender instanceof EntityPlayer)) {
                return ImmutableList.of();
            }
    
            List<String> offers;
            Player player = ((EntityPlayer) sender).getBukkitEntity();
            if (message.startsWith("/")) {
                offers = tabCompleteCommand(player, message);
            } else {
                offers = tabCompleteChat(player, message);
            }
    
            TabCompleteEvent tabEvent = new TabCompleteEvent(player, message, offers);
            getPluginManager().callEvent(tabEvent);
    
            return tabEvent.isCancelled() ? Collections.EMPTY_LIST : tabEvent.getCompletions();
        }
    
    Seems to be called later. May have looked at the wrong part of the source, just did a small search for "TabCompleteEvent".
     
  6. Offline

    CruzAPI

    @I Al Istannen @FisheyLP
    Yes, you are right, i didn't find the class TabCompleteEvent because I'm using an older version, but it is correct.
    Thanks for help.
     
  7. @I Al Istannen
    Nice that you found it :D I searched in the wrong place ..
    @CruzAPI please mark the thread as solved
     
    I Al Istannen likes this.
Thread Status:
Not open for further replies.

Share This Page