如何忽略 else/if 中的 args[1]
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) { **//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;
}
我只想要如果我不在 args[1] 中写任何东西,命令设置假的,没有我在“r”中放入的任何特殊内容
如果您没有写至少两个参数,那么尝试访问 args[1]
将导致 ArrayOutOfBoundsException
,因为数组只包含一个 String
(args[0]
) .为防止出现这种情况,您有两种解决方法:
第一种解决方法:改变if条件
而不是检查 args[1]
是否等于 0
如果数组长度小于 2
.
将触发异常检查
} else if(args.length < 2) { **//Here is the part I need help**
第二种解决方案:检查参数个数。
如果你想强制用户为命令输入至少两个参数,你可以在函数的开头添加:
if(args.length < 2) {
sender.sendMessage(ChatColor.RED + "You must use two arguments!");
return false;
}
如果有帮助请告诉我!
我想在我的服务器上做一个假插件,但我不知道如何解决游戏中的这个错误: “尝试执行此命令时发生内部错误” 这是因为我尝试使用 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) { **//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;
}
我只想要如果我不在 args[1] 中写任何东西,命令设置假的,没有我在“r”中放入的任何特殊内容
如果您没有写至少两个参数,那么尝试访问 args[1]
将导致 ArrayOutOfBoundsException
,因为数组只包含一个 String
(args[0]
) .为防止出现这种情况,您有两种解决方法:
第一种解决方法:改变if条件
而不是检查 args[1]
是否等于 0
如果数组长度小于 2
.
} else if(args.length < 2) { **//Here is the part I need help**
第二种解决方案:检查参数个数。 如果你想强制用户为命令输入至少两个参数,你可以在函数的开头添加:
if(args.length < 2) {
sender.sendMessage(ChatColor.RED + "You must use two arguments!");
return false;
}
如果有帮助请告诉我!