当我执行命令" / voltiac"它在控制台中打印此错误并说"尝试执行此命令时出现内部错误"在我的世界。但是,当我执行命令" / voltiac help" &" / voltiac about"他们工作得很好。我试图让插件做的是当我做命令" / voltiac"这与" / voltiac关于"。
相同这是错误
[12:15:52] [Server thread/INFO]: DiamondDeveloper issued server command: /voltiac
[12:15:52] [Server thread/ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'voltiac' in plugin Voltiac v0.4.4
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.8.jar:git-Bukkit-18fbb24]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.8.jar:git-Bukkit-18fbb24]
at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:620) ~[craftbukkit-1.8.jar:git-Bukkit-18fbb24]
at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1106) [craftbukkit-1.8.jar:git-Bukkit-18fbb24]
at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:966) [craftbukkit-1.8.jar:git-Bukkit-18fbb24]
at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.8.jar:git-Bukkit-18fbb24]
at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.8.jar:git-Bukkit-18fbb24]
at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit-1.8.jar:git-Bukkit-18fbb24]
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_73]
at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_73]
at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit-1.8.jar:git-Bukkit-18fbb24]
at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [craftbukkit-1.8.jar:git-Bukkit-18fbb24]
at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit-1.8.jar:git-Bukkit-18fbb24]
at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit-1.8.jar:git-Bukkit-18fbb24]
at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit-1.8.jar:git-Bukkit-18fbb24]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_73]
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
at ml.voltiac.bukkit.Main.onCommand(Main.java:48) ~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.8.jar:git-Bukkit-18fbb24]
... 15 more
这是我的代码
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
Player player = (Player) sender;
// --------voltiac command--------
if (label.equalsIgnoreCase("voltiac")) {
PluginDescriptionFile pdffile = getDescription();
if (!(sender instanceof Player)) {
sender.sendMessage(TextFormat.playerOnly());
return false;
}
if (args[0].equalsIgnoreCase("help")) {
player.sendMessage(ChatColor.RESET + "" + ChatColor.DARK_PURPLE + ChatColor.STRIKETHROUGH
+ "-[-------------------------------------------]-");
player.sendMessage(TextFormat.helpFormat("voltiac", ""));
player.sendMessage(TextFormat.helpFormat("voltiac about", ""));
player.sendMessage(TextFormat.helpFormat("voltiac help", ""));
player.sendMessage(TextFormat.helpFormat("", ""));
player.sendMessage(TextFormat.helpFormat("", ""));
player.sendMessage(ChatColor.RESET + "" + ChatColor.DARK_PURPLE + ChatColor.STRIKETHROUGH
+ "-[-------------------------------------------]-");
} else if (args[0].equalsIgnoreCase("about")) {
player.sendMessage(ChatColor.RESET + "" + ChatColor.DARK_PURPLE + ChatColor.STRIKETHROUGH
+ "-[-------------------------------------------]-");
player.sendMessage(TextFormat.aboutFormat("Name", "Voltiac"));
player.sendMessage(TextFormat.aboutFormat("Version", pdffile.getVersion()));
player.sendMessage(TextFormat.aboutFormat("Made by", "DiamondDeveloper"));
player.sendMessage(TextFormat.aboutFormat("Copyright", "Voltiac 2016"));
player.sendMessage(TextFormat.aboutFormat("Website", ChatColor.UNDERLINE + "www.voltiac.ml"));
player.sendMessage(ChatColor.RESET + "" + ChatColor.DARK_PURPLE + ChatColor.STRIKETHROUGH
+ "-[-------------------------------------------]-");
return true;
} else {
player.sendMessage(ChatColor.RESET + "" + ChatColor.DARK_PURPLE + ChatColor.STRIKETHROUGH
+ "-[-------------------------------------------]-");
player.sendMessage(TextFormat.aboutFormat("Name", "Voltiac"));
player.sendMessage(TextFormat.aboutFormat("Version", pdffile.getVersion()));
player.sendMessage(TextFormat.aboutFormat("Made by", "DiamondDeveloper"));
player.sendMessage(TextFormat.aboutFormat("Copyright", "Voltiac 2016"));
player.sendMessage(TextFormat.aboutFormat("Website", ChatColor.UNDERLINE + "www.voltiac.ml"));
player.sendMessage(ChatColor.RESET + "" + ChatColor.DARK_PURPLE + ChatColor.STRIKETHROUGH
+ "-[-------------------------------------------]-");
return true;
}
return true;
}
return false;
}
}
我在bukkit论坛上得到了这个答案,但我如何做他所说的?
引起:java.lang.ArrayIndexOutOfBoundsException:0 at ml.voltiac.bukkit.Main.onCommand(Main.java:48)〜[?:?]
您正在尝试从不包含任何值的数组中获取值(基本Java问题)。确保阵列中有对象' args'在得到任何价值之前。
(BTW第48行是" sender.sendMessage(TextFormat.playerOnly());")
答案 0 :(得分:5)
如果堆栈跟踪指出了错误的行,那么如果在编译后以任何方式操作类就会发生这种情况。
查看日志输出您执行它的命令/voltiac
。由于你没有提供任何参数,args
参数将是一个空数组。
然后尝试访问args
的第一个元素,而不先检查它是否有任何参数:
if (args[0].equalsIgnoreCase("help")) {
这可能是您的例外的来源。
这意味着可以通过在任何子命令之前首先进行if (args.length == 0)
检查来修复它。