收集特定参数

huangapple go评论71阅读模式
英文:

Collecting specific arguments

问题

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 + "请指定玩家名称。");
            }
            if (args.length == 1) {
                Player target = Bukkit.getPlayer(args[0]);
                if (!target.isValid()) {
                    player.sendMessage(ChatColor.RED + "该玩家不在服务器上!");
                } else {
                    target.kickPlayer(ChatColor.RED + "已告知踢出原因!");
                }
            }
            if (args.length > 1) {
                Player target = Bukkit.getPlayer(args[0]);
                if (!target.isValid()) {
                    player.sendMessage(ChatColor.RED + "该玩家不在服务器上!");
                } else {
                    String message = String.join(" ", Arrays.copyOfRange(args, 2, args.length));
                    target.kickPlayer(ChatColor.RED + message);
                }
            }
        }
    }
    return true;
}

这段代码的问题在于,在处理多个参数(args.length > 1)的情况下,它尝试从第三个参数开始收集消息,但实际上应该是从第二个参数开始。我已经修正了这个问题,并对代码进行了翻译。

英文:

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 [].

答案1

得分: 1

为了收集数组中第 n 个元素之后的所有元素,您可以使用 ArrayList 类的 subList(int fromIndex, int toIndex) 函数。文档说明如下:

> 返回列表中从指定的 fromIndex(包括)到 toIndex(不包括)的部分的视图。

因此,您需要将您的数组转换为一个 List,然后在其上调用函数 subList

List<String> list = Arrays.asList(args).sublist(1, args.length);
// 将数字 1 替换为您希望裁剪的起始索引。(包含)

现在,如果您想将该列表转换为一个字符串,您可以使用以下函数:

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

然后通过以下方式调用它:

String message = buildMessage(list, " "); // 它将用空格分隔参数。

完整示例

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);

产生的输出:

Message: I am a 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);

然后您将不需要 buildMessage 方法。

英文:

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&lt;String&gt; 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&lt;String&gt; 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, &quot; &quot;); // It will separate the arguments with spaces.

Full demo

String[] args = new String[]{&quot;Hi!&quot;, &quot;I&quot;, &quot;am&quot;, &quot;a&quot;, &quot;demo&quot;};
List&lt;String&gt; list = Arrays.asList(args).sublist(1, args.length);
String message = buildMessage(list, &quot; &quot;);
System.out.println(&quot;Message: &quot; + 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[]{&quot;Hi!&quot;, &quot;I&quot;, &quot;am&quot;, &quot;a&quot;, &quot;demo&quot;};
List&lt;String&gt; list = Arrays.asList(args).sublist(1, args.length);
String message = String.join(&quot; &quot;, list);
System.out.println(&quot;Message: &quot; + message);

Then you will not need the buildMessage method.

huangapple
  • 本文由 发表于 2020年8月16日 19:43:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63436445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定