英文:
How to ignore args[1] in the else/if
问题
我试图为我的服务器创建一个虚假插件,但我不知道如何解决游戏中的这个错误:
"尝试执行此命令时发生内部错误"
这是因为我试图使用args[],我可以使用args[0]没有问题,但是,如果我放入args[1],就会出现这个错误(如果我在args[1]中不写任何内容,但如果我移除args[1]并且不在args[0]中写任何内容,它就可以工作)
我的代码示例:
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
Player p = (Player)sender;
String error = "§4§l404!";
String noPerm = error + " §cYou don't have permission!";
if(p.hasPermission("perm.fake")) {
if(cmd.getLabel().equalsIgnoreCase("fake")) {
if(args[0].equalsIgnoreCase("reset")) {
p.setCustomName(p.getName());
p.setCustomNameVisible(false);
p.setDisplayName(p.getName());
p.setPlayerListName(p.getName());
} else if(args[0].length() == 0) {
p.sendMessage(error + "§cYou need to specify a fake!");
} else if(args[1].length() == 0) { //这里是我需要帮助的部分
p.setCustomName(args[0].replace('&', '§'));
p.setCustomNameVisible(true);
p.setDisplayName(p.getCustomName()); //如果我在args[1]中不写任何内容,这部分不起作用
p.setPlayerListName(p.getCustomName());
} else if(args[1].equalsIgnoreCase("r")) {
//这里有一段长代码,如果我在命令中写入字符"r",它会起作用
}
}
} else {
p.sendMessage(noPerm);
}
return false;
}
我只希望如果我在args[1]中不写任何内容,命令会设置一个没有特殊字符的虚假,而不是我放在"r"中的任何特殊字符。
英文:
I trying to do a Fake plugin to my server, but I don't know how to solve this error in game:
"An internal error occurred while attempting to perform this command"
It's because I trying to use args[], I can use args[0] with no problems, but, if I put args[1], this error occurs (If I don't write anything in args[1], but if I remove args[1] and don't write anything in args[0] it works)
An example of my code:
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
Player p = (Player)sender;
String error = "§4§l404!";
String noPerm = error + " §cYou don't have permission!";
if(p.hasPermission("perm.fake")) {
if(cmd.getLabel().equalsIgnoreCase("fake")) {
if(args[0].equalsIgnoreCase("reset")) {
p.setCustomName(p.getName());
p.setCustomNameVisible(false);
p.setDisplayName(p.getName());
p.setPlayerListName(p.getName());
} else if(args[0].length() == 0) {
p.sendMessage(error + "§cYou need to specify a fake!");
} else if(args[1].length() == 0) { **//Here is the part I need help**
p.setCustomName(args[0].replace('&', '§'));
p.setCustomNameVisible(true);
p.setDisplayName(p.getCustomName()); **//These part don't works if I dont write anything in args[1]**
p.setPlayerListName(p.getCustomName());
} else if(args[1].equalsIgnoreCase("r")) {
**//Here have a long code, it works if I write the char "r" in command**
}
}
} else {
p.sendMessage(noPerm);
}
return false;
}
I only want if I don't write anything in the args[1] the command sets the fake without anything special that I put in "r"
答案1
得分: 0
第一个解决方案: 更改条件语句
不要检查 args[1]
是否等于 0
,因为这将触发异常,而是检查数组长度是否小于 2
。
} else if (args.length < 2) { **//这是我需要帮助的部分**
第二个解决方案: 检查参数数量
如果您想强制用户至少输入两个参数到命令中,您可以在函数的开头添加以下代码:
if (args.length < 2) {
sender.sendMessage(ChatColor.RED + "您必须使用两个参数!");
return false;
}
希望这有所帮助!
英文:
If you do not write at least two arguments then trying to access args[1]
will result in ArrayOutOfBoundsException
as the array does only contain one String
(args[0]
). To prevent that you have two workarounds:
First solution: Change the if condition
Instead of checking if args[1]
equals 0
which will trigger an exception check if the array length is smaller than 2
.
} else if(args.length < 2) { **//Here is the part I need help**
Second solution: Check the number of arguments.
If you want to force the user to input at least two arguments to the command you can just add at the beginning of your function:
if(args.length < 2) {
sender.sendMessage(ChatColor.RED + "You must use two arguments!");
return false;
}
Let me know if it helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论