@Override
  public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
    Validate.notNull(sender, "Sender cannot be null");
    Validate.notNull(args, "Arguments cannot be null");
    Validate.notNull(alias, "Alias cannot be null");

    if (args.length == 1) {
      return StringUtil.copyPartialMatches(
          args[0], WHITELIST_SUBCOMMANDS, new ArrayList<String>(WHITELIST_SUBCOMMANDS.size()));
    } else if (args.length == 2) {
      if (args[0].equalsIgnoreCase("add")) {
        List<String> completions = new ArrayList<String>();
        for (OfflinePlayer player : Bukkit.getOfflinePlayers()) {
          String name = player.getName();
          if (StringUtil.startsWithIgnoreCase(name, args[1]) && !player.isWhitelisted()) {
            completions.add(name);
          }
        }
        return completions;
      } else if (args[0].equalsIgnoreCase("remove")) {
        List<String> completions = new ArrayList<String>();
        for (OfflinePlayer player : Bukkit.getWhitelistedPlayers()) {
          String name = player.getName();
          if (StringUtil.startsWithIgnoreCase(name, args[1])) {
            completions.add(name);
          }
        }
        return completions;
      }
    }
    return ImmutableList.of();
  }
  /*
   * Internal method to execute tab auto-completion.
   */
  @Override
  public List<String> onTabComplete(
      CommandSender sender, Command command, String label, String[] originalArgs) {
    if (originalArgs.length == 1) {
      // Searching for a subcommand
      List<String> matches = new ArrayList<String>();

      for (BaseCommand baseCommand : plugin.getCommands().getRegistrations()) {
        if (StringUtil.startsWithIgnoreCase(baseCommand.getName(), originalArgs[0])
            && baseCommand.hasPermission(sender)) {
          matches.add(baseCommand.getName());
        }
      }
      return matches;
    } else if (originalArgs.length > 1) {
      // Searching in a subcommand
      BaseCommand baseCommand = plugin.getCommands().getRegistration(originalArgs[0]);
      if (baseCommand != null && baseCommand.hasPermission(sender)) {
        String[] args = new String[originalArgs.length - 1];
        System.arraycopy(originalArgs, 1, args, 0, originalArgs.length - 1);
        return baseCommand.autoComplete(sender, args);
      }
    }

    return Collections.emptyList();
  }
示例#3
0
  /**
   * Executed on tab completion for this command, returning a list of options the player can tab
   * through.
   *
   * @param sender Source object which is executing this command
   * @param alias the alias being used
   * @param args All arguments passed to the command, split via ' '
   * @return a list of tab-completions for the specified arguments. This will never be null. List
   *     may be immutable.
   * @throws IllegalArgumentException if sender, alias, or args is null
   */
  public List<String> tabComplete(CommandSender sender, String alias, String[] args)
      throws IllegalArgumentException {
    Validate.notNull(sender, "Sender cannot be null");
    Validate.notNull(args, "Arguments cannot be null");
    Validate.notNull(alias, "Alias cannot be null");

    if (args.length == 0) {
      return ImmutableList.of();
    }

    String lastWord = args[args.length - 1];

    Player senderPlayer = sender instanceof Player ? (Player) sender : null;

    ArrayList<String> matchedPlayers = new ArrayList<String>();
    for (Player player : sender.getServer().getOnlinePlayers()) {
      String name = player.getName();
      if ((senderPlayer == null || senderPlayer.canSee(player))
          && StringUtil.startsWithIgnoreCase(name, lastWord)) {
        matchedPlayers.add(name);
      }
    }

    Collections.sort(matchedPlayers, String.CASE_INSENSITIVE_ORDER);
    return matchedPlayers;
  }
  public List<String> tabComplete(CommandSender sender, String cmdLine) {
    Validate.notNull(sender, "Sender cannot be null");
    Validate.notNull(cmdLine, "Command line cannot null");

    int spaceIndex = cmdLine.indexOf(' ');

    if (spaceIndex == -1) {
      ArrayList<String> completions = new ArrayList<String>();
      Map<String, Command> knownCommands = this.knownCommands;

      final String prefix = (sender instanceof Player ? "/" : "");

      for (Map.Entry<String, Command> commandEntry : knownCommands.entrySet()) {
        Command command = commandEntry.getValue();

        if (!command.testPermissionSilent(sender)) {
          continue;
        }

        String name = commandEntry.getKey(); // Use the alias, not command name

        if (StringUtil.startsWithIgnoreCase(name, cmdLine)) {
          completions.add(prefix + name);
        }
      }

      Collections.sort(completions, String.CASE_INSENSITIVE_ORDER);
      return completions;
    }

    String commandName = cmdLine.substring(0, spaceIndex);
    Command target = getCommand(commandName);

    if (target == null) {
      return null;
    }

    if (!target.testPermissionSilent(sender)) {
      return null;
    }

    String argLine = cmdLine.substring(spaceIndex + 1, cmdLine.length());
    String[] args = PATTERN_ON_SPACE.split(argLine, -1);

    try {
      return target.tabComplete(sender, commandName, args);
    } catch (CommandException ex) {
      throw ex;
    } catch (Throwable ex) {
      throw new CommandException(
          "Unhandled exception executing tab-completer for '" + cmdLine + "' in " + target, ex);
    }
  }
  @SuppressWarnings("deprecation")
  @Override
  public List<String> tabComplete(CommandSender sender, String alias, String[] args)
      throws IllegalArgumentException {
    Validate.notNull(sender, "Sender cannot be null");
    Validate.notNull(args, "Arguments cannot be null");
    Validate.notNull(alias, "Alias cannot be null");

    if (args.length == 1) {
      return super.tabComplete(sender, alias, args);
    }
    if (args.length == 2) {
      final String arg = args[1];
      final List<String> materials = GiveCommand.materials;
      List<String> completion = new ArrayList<String>();

      final int size = materials.size();
      int i = Collections.binarySearch(materials, arg, String.CASE_INSENSITIVE_ORDER);

      if (i < 0) {
        // Insertion (start) index
        i = -1 - i;
      }

      for (; i < size; i++) {
        String material = materials.get(i);
        if (StringUtil.startsWithIgnoreCase(material, arg)) {
          completion.add(material);
        } else {
          break;
        }
      }

      return Bukkit.getUnsafe().tabCompleteInternalMaterialName(arg, completion);
    }
    return ImmutableList.of();
  }