Minecraft Bukkit Java 执行命令时出错
Minecraft Bukkit Java Error when doing command
当我执行命令“/voltiac”时,它会在控制台中打印此错误,并在 minecraft 中显示 "An internal error occurred while attempting to perform this command"。但是当我执行命令“/voltiac help”和“/voltiac about”时,它们工作得很好。我试图让插件做的是当我执行命令“/voltiac”时,它与执行“/voltiac about”是一样的。
这里是错误
[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.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 论坛上得到了这个答案,但我该如何按照他说的去做?
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
at ml.voltiac.bukkit.Main.onCommand(Main.java:48) ~[?:?]
You are trying to get a value from an array that does not contain any values (basic Java problem). Make sure there are objects in the array 'args' before getting any values.
(顺便说一句,第 48 行是 "sender.sendMessage(TextFormat.playerOnly());")
很可能堆栈跟踪指出了错误的行,如果 class 在编译后以任何方式被操纵,就会发生这种情况。
查看日志输出您执行的命令/voltiac
。由于您没有提供任何参数,因此 args
参数将是一个空数组。
然后您尝试访问 args
的第一个元素,而不先检查它是否有任何参数:
if (args[0].equalsIgnoreCase("help")) {
这可能是您的异常来源。
这意味着它可能可以通过在任何子命令之前首先进行 if (args.length == 0)
检查来修复。
当我执行命令“/voltiac”时,它会在控制台中打印此错误,并在 minecraft 中显示 "An internal error occurred while attempting to perform this command"。但是当我执行命令“/voltiac help”和“/voltiac about”时,它们工作得很好。我试图让插件做的是当我执行命令“/voltiac”时,它与执行“/voltiac about”是一样的。
这里是错误
[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.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 论坛上得到了这个答案,但我该如何按照他说的去做?
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0 at ml.voltiac.bukkit.Main.onCommand(Main.java:48) ~[?:?]
You are trying to get a value from an array that does not contain any values (basic Java problem). Make sure there are objects in the array 'args' before getting any values.
(顺便说一句,第 48 行是 "sender.sendMessage(TextFormat.playerOnly());")
很可能堆栈跟踪指出了错误的行,如果 class 在编译后以任何方式被操纵,就会发生这种情况。
查看日志输出您执行的命令/voltiac
。由于您没有提供任何参数,因此 args
参数将是一个空数组。
然后您尝试访问 args
的第一个元素,而不先检查它是否有任何参数:
if (args[0].equalsIgnoreCase("help")) {
这可能是您的异常来源。
这意味着它可能可以通过在任何子命令之前首先进行 if (args.length == 0)
检查来修复。