gpt4 book ai didi

java - 每当我运行命令 "msg"时,都会出现内部错误

转载 作者:行者123 更新时间:2023-12-02 01:10:19 24 4
gpt4 key购买 nike

每次在没有任何其他参数的情况下运行该命令时,我都会收到内部错误。内部错误:https://gyazo.com/f4c16facadf878fad6b00e645b3539c6//我的代码:

    Player target = Bukkit.getServer().getPlayer(args[0]);

if (cmd.getName().equalsIgnoreCase("msg")) {

if (args.length < 1) {

sender.sendMessage("§cUsage: /msg <player> <msg>");
return true;
}

if (target != null) {

if (args.length > 1) {

String msg = "";

for (int i = 1; i < args.length; i++){
String arg = (args[i] + " ");
msg = (msg + arg);
}

target.sendMessage("§b(From§f " + sender.getName() + "§b)§f " + msg);
sender.sendMessage("§b(To§f " + target.getName() + "§b)§f " + msg);
return true;
}
}

if (target == null) {

sender.sendMessage("§cThat player is offline.");
return true;

最佳答案

我发现您正在使用 args[0]

 Player target = Bukkit.getServer().getPlayer(args[0]);

在检查长度之前,先在这里

if (args.length < 1) {

sender.sendMessage("§cUsage: /msg <player> <msg>");
return true;
}

这可能会导致ArrayIndexOutOfBoundsException:0

检查这是否可以解决问题

        if (args.length < 1) {

sender.sendMessage("§cUsage: /msg <player> <msg>");
return true;
}
Player target = Bukkit.getServer().getPlayer(args[0]);

if (cmd.getName().equalsIgnoreCase("msg")) {

if (target != null) {

if (args.length > 1) {

String msg = "";

for (int i = 1; i < args.length; i++){
String arg = (args[i] + " ");
msg = (msg + arg);
}

target.sendMessage("§b(From§f " + sender.getName() + "§b)§f " + msg);
sender.sendMessage("§b(To§f " + target.getName() + "§b)§f " + msg);
return true;
}
}

if (target == null) {

sender.sendMessage("§cThat player is offline.");
return true;

关于java - 每当我运行命令 "msg"时,都会出现内部错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59495223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com