Search code examples
javaarraysbukkitcollectors

Collecting specific arguments


I am developing a game plugin in java, and can't figure this out. I want to collect everything after args[1]. here is some of code so you can understand better.

    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if(sender instanceof Player) {
        Player player = (Player) sender;
        if(player.hasPermission("essentials.allow.kick")) {
            if(args.length == 0) {
                player.sendMessage(ChatColor.RED + "Please specify player name.");
            }
            if(args.length == 1) {
                Player target = Bukkit.getPlayer(args[0]);
                if(!target.isValid()) {
                    player.sendMessage(ChatColor.RED + "That player is not on server!");
                }else {
                    target.kickPlayer(ChatColor.RED + "The kick reason has been told!");
                }
            }
            if(args.length > 1) {
                Player target = Bukkit.getPlayer(args[0]);
                if(!target.isValid()) {
                    player.sendMessage(ChatColor.RED + "That player is not on server!");
                }
                else {
                    String message = Stream.of(args).skip(2).collect(Collectors.toList()).toString();
                    target.kickPlayer(ChatColor.RED + message);
                }
            }
        }
    }
    return true;
}

It just outputs [].


Solution

  • In order to collect all elements of an array after an n-th element, you can use the subList(int fromIndex, int toIndex) function of the ArrayList class. The documentation says:

    Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.

    So you will need to convert your array into a List and then call the function subList on it.

    List<String> list = Arrays.asList(args).sublist(1, args.length);
    // Replace number 1 with the start index where you want to trim. (Inclusive)
    

    Now if you want to convert that list to a String you can use the following function:

    public String buildMessage(List<String> list, String separator) {
        StringBuilder sb = new StringBuilder();
        for (String s: list) {
            sb.append(s).append(separator);
        }
        return sb.toString();
    } 
    

    Then call it by doing:

    String message = buildMessage(list, " "); // It will separate the arguments with spaces.
    

    Full demo

    String[] args = new String[]{"Hi!", "I", "am", "a", "demo"};
    List<String> list = Arrays.asList(args).sublist(1, args.length);
    String message = buildMessage(list, " ");
    System.out.println("Message: " + message);
    

    Produced output:

    Message: I am a demo 
    

    Let me know if this works for you!

    EDIT

    As stated in the comments by @Holger you can simplify the full demo to:

    Full demo

    String[] args = new String[]{"Hi!", "I", "am", "a", "demo"};
    List<String> list = Arrays.asList(args).sublist(1, args.length);
    String message = String.join(" ", list);
    System.out.println("Message: " + message);
    

    Then you will not need the buildMessage method.