Search code examples
javaarraysminecraftbukkit

Minecraft Bukkit Java Error when doing command


When i do the command "/voltiac" it prints this error in the console and says "An internal error occurred while attempting to perform this command" in minecraft. But when i do the commands "/voltiac help" and "/voltiac about" they work perfectly. What i was trying to get the plugin to do was when i did the command "/voltiac" it was the same as doing "/voltiac about".

Here is the error

[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

Here's my code

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;

}
}

I got this answer on the bukkit forums, but how do i do what he's saying?

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.

(BTW line 48 is "sender.sendMessage(TextFormat.playerOnly());")


Solution

  • In all likelihood the stacktrace is pointing out the wrong line, this can happen if the class is manipulated after compilation in any way.

    Looking at the log output the command you've executed it /voltiac. Since you havent provided any arguments the args parameter will be an empty array.

    You then try to access the first element of args without first checking if it has any arguments here:

    if (args[0].equalsIgnoreCase("help")) { 
    

    which probably is the source of your exception. This means it can probably be fixed by first doing an if (args.length == 0) check before any of the subcommands.