Args ArrayIndexOutOfBoundsException Error

Discussion in 'Plugin Development' started by Dead_Skeleton, Oct 9, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Dead_Skeleton

    Hey guys, it's me again. I'm having trouble with my plugin. I'm having trouble with the paying part of the plugin. Everything else is fine. Here's my code:

    Code:java
    1. @SuppressWarnings({ "deprecation", "null" })
    2. public boolean onCommand(CommandSender sender, Command cmd,
    3. String CommandLabel, String[] args) {
    4. if (cmd.getName().equalsIgnoreCase("parkour")) {
    5. if (args.length == 0) {
    6. if (!(sender instanceof Player)) {
    7. sender.sendMessage(ChatColor.RED
    8. + "Parkour Plugin v1.0 by Dead_Skeleton");
    9. sender.sendMessage(ChatColor.BLUE + "Commands:");
    10. sender.sendMessage(ChatColor.BLACK + "______________");
    11. sender.sendMessage(ChatColor.RED + " ");
    12. sender.sendMessage(ChatColor.AQUA
    13. + "/p helm - Hold any item / block and ");
    14. sender.sendMessage(ChatColor.AQUA
    15. + "execute this command and you will wear it!");
    16. sender.sendMessage(ChatColor.RED + " ");
    17. sender.sendMessage(ChatColor.AQUA
    18. + "/p points - This command allows you to see your ");
    19. sender.sendMessage(ChatColor.AQUA + "points for parkour!");
    20. sender.sendMessage(ChatColor.AQUA + " ");
    21. sender.sendMessage(ChatColor.AQUA
    22. + "/p points send [player] [points] - Send other");
    23. sender.sendMessage(ChatColor.AQUA + "players points!");
    24. return true;
    25. }
    26. Player p = (Player) sender;
    27. p.sendMessage(ChatColor.RED
    28. + "Parkour Plugin v1.0 by Dead_Skeleton");
    29. p.sendMessage(ChatColor.BLUE + "Commands:");
    30. p.sendMessage(ChatColor.BLACK + "______________");
    31. p.sendMessage(ChatColor.RED + " ");
    32. p.sendMessage(ChatColor.AQUA
    33. + "/p helm - Hold any item / block and ");
    34. p.sendMessage(ChatColor.AQUA
    35. + "execute this command and you will wear it!");
    36. p.sendMessage(ChatColor.RED + " ");
    37. p.sendMessage(ChatColor.AQUA
    38. + "/p points - This command allows you to see your ");
    39. p.sendMessage(ChatColor.AQUA + "points for parkour!");
    40. p.sendMessage(ChatColor.AQUA + " ");
    41. p.sendMessage(ChatColor.AQUA
    42. + "/p points send [player] [points] - Send other");
    43. p.sendMessage(ChatColor.AQUA + "players points!");
    44. return true;
    45. }
    46. if (args.length > 0) {
    47. if (args[0].equalsIgnoreCase("helm")) {
    48. if (!(sender instanceof Player)) {
    49. sender.sendMessage(ChatColor.RED
    50. + "The console doesn't even have an armor slot!!");
    51. return true;
    52. }
    53. Player p = (Player) sender;
    54. if (p.getInventory().getItemInHand().getTypeId() == Material.AIR
    55. .getId()) {
    56. p.sendMessage(ChatColor.RED
    57. + "Please actually hold an item!");
    58. return true;
    59. }
    60. ItemStack itemInHand = p.getItemInHand();
    61. ItemStack helmet = p.getInventory().getHelmet();
    62. p.getInventory().setHelmet(itemInHand);
    63. p.getInventory().setItemInHand(helmet);
    64. p.sendMessage(ChatColor.GREEN
    65. + "You have put on the helmet!! :D");
    66. return true;
    67. }
    68. if (args[0].equalsIgnoreCase("points")) {
    69. if (!(sender instanceof Player)) {
    70. sender.sendMessage(ChatColor.RED
    71. + "The console does not have any stats!! Silly console!");
    72. return true;
    73. }
    74. Player p = (Player) sender;
    75. if (getConfig().contains(p.getName() + ".stats.money")) {
    76. int pConfig = (int) getConfig().getInt(
    77. p.getName() + ".stats.money");
    78. p.sendMessage(ChatColor.GREEN + "You have " + pConfig
    79. + " parkour point(s)!");
    80. }
    81. if (!(getConfig().contains(p.getName() + ".stats.money"))) {
    82. getConfig().addDefault(p.getName() + ".stats.money", 1);
    83. getConfig().set(p.getName() + ".stats.money", 1);
    84. }
    85. Player target = Bukkit.getServer().getPlayer(args[2]);
    86. if (args[1].length() >= 2 && args[2].length() >= 2 && args[3].length() >= 1 && args[0].equalsIgnoreCase("points")
    87. && args[1].equalsIgnoreCase("send")
    88. && args[2].equals(target)) {
    89. if (isInt(args[3])) {
    90. int args3 = Integer.parseInt(args[3]);
    91. if(args3 > getConfig().getInt(p.getName() + ".stats.money")) {
    92. p.sendMessage(ChatColor.RED + "You do not have that many points!");
    93. return true;
    94. }
    95. if (target != null) {
    96. int playerConfig = Integer.parseInt(p
    97. .getName() + ".stats.money");
    98. int targetConfig = Integer.parseInt(target
    99. .getName() + ".stats.money");
    100. getConfig().set(
    101. target.getName() + ".stats.money",
    102. targetConfig + args3);
    103. getConfig().set(
    104. p.getName() + ".stats.money",
    105. playerConfig - args3);
    106. target.sendMessage(ChatColor.GREEN
    107. + args[3]
    108. + " points received from "
    109. + p.getName() + "!");
    110. p.sendMessage(ChatColor.GREEN + "Sent "
    111. + args[3] + " points to "
    112. + target.getName() + "!");
    113. }
    114. if (target == null) {
    115. p.sendMessage(ChatColor.RED
    116. + "Could not find player "
    117. + target.getName() + "!");
    118. return true;
    119. }
    120. }
    121. } else {
    122. return true;
    123. }
    124. } else {
    125. Player p = (Player) sender;
    126. p.sendMessage(ChatColor.RED
    127. + "Parkour Plugin v1.0 by Dead_Skeleton");
    128. p.sendMessage(ChatColor.BLUE + "Commands:");
    129. p.sendMessage(ChatColor.BLACK + "______________");
    130. p.sendMessage(ChatColor.RED + " ");
    131. p.sendMessage(ChatColor.AQUA
    132. + "/p helm - Hold any item / block and ");
    133. p.sendMessage(ChatColor.AQUA
    134. + "execute this command and you will wear it!");
    135. p.sendMessage(ChatColor.RED + " ");
    136. p.sendMessage(ChatColor.AQUA
    137. + "/p points - This command allows you to see your ");
    138. p.sendMessage(ChatColor.AQUA + "stats for parkour!");
    139. p.sendMessage(ChatColor.AQUA + " ");
    140. p.sendMessage(ChatColor.AQUA
    141. + "/p points send [player] [points] - Send other");
    142. p.sendMessage(ChatColor.AQUA + "players points!");
    143. return true;
    144. }
    145. }
    146. }
    147. return true;
    148. }


    Here's the error:

    Code:
    2013-10-09 21:41:08 [INFO] Starting minecraft server version 1.6.4
    2013-10-09 21:41:08 [INFO] Loading properties
    2013-10-09 21:41:08 [INFO] Default game type: SURVIVAL
    2013-10-09 21:41:08 [INFO] Generating keypair
    2013-10-09 21:41:09 [INFO] Starting Minecraft server on 10.0.1.39:25565
    2013-10-09 21:41:09 [INFO] This server is running CraftBukkit version git-Bukkit-1.6.2-R1.0-3-g9532cb6-b2887jnks (MC: 1.6.4) (Implementing API version 1.6.4-R0.1-SNAPSHOT)
    2013-10-09 21:41:10 [INFO] [FullOnPvPKits] Loading FullOnPvPKits v1.0
    2013-10-09 21:41:10 [INFO] [Parkour] Loading Parkour v1.0
    2013-10-09 21:41:10 [INFO] Preparing level "world"
    2013-10-09 21:41:10 [INFO] Preparing start region for level 0 (Seed: -793209349851589360)
    2013-10-09 21:41:10 [INFO] ----- Bukkit Auto Updater -----
    2013-10-09 21:41:10 [INFO] It appears that you're running a Development Build, when you've specified in bukkit.yml that you prefer to run Recommended Builds.
    2013-10-09 21:41:10 [INFO] If you would like to be kept informed about new Development Build releases, it is recommended that you change 'preferred-channel' in your bukkit.yml to 'dev'.
    2013-10-09 21:41:10 [INFO] With that set, you will be told whenever a new version is available for download, so that you can always keep up to date and secure with the latest fixes.
    2013-10-09 21:41:10 [INFO] If you would like to disable this warning, simply set 'suggest-channels' to false in bukkit.yml.
    2013-10-09 21:41:10 [INFO] ----- ------------------- -----
    2013-10-09 21:41:11 [INFO] Preparing spawn area: 31%
    2013-10-09 21:41:12 [INFO] Preparing start region for level 1 (Seed: 269478325867081726)
    2013-10-09 21:41:13 [INFO] Preparing spawn area: 45%
    2013-10-09 21:41:13 [INFO] Preparing start region for level 2 (Seed: 269478325867081726)
    2013-10-09 21:41:14 [INFO] [FullOnPvPKits] Enabling FullOnPvPKits v1.0
    2013-10-09 21:41:14 [INFO] [Parkour] Enabling Parkour v1.0
    2013-10-09 21:41:14 [INFO] Server permissions file permissions.yml is empty, ignoring it
    2013-10-09 21:41:14 [INFO] Done (4.164s)! For help, type "help" or "?"
    2013-10-09 21:41:18 [INFO] Dead_Skeleton[/76.30.77.248:41291] logged in with entity id 418 at ([world] 493.53854935608535, 95.10490908500188, 688.4251485762018)
    2013-10-09 21:41:24 [INFO] Dead_Skeleton issued server command: /p points
    2013-10-09 21:41:24 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'p' in plugin Parkour v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:523)
        at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:959)
        at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:877)
        at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:834)
        at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
        at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
        at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
        at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
        at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
        at me.Dead_Skeleton.main.Main.onCommand(Main.java:134)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    
    Thanks for the help! Probably going to be a simple little thing that I forgot, but I can't quite seem to solve it! Hope you can help!
     
  2. Offline

    chasechocolate

    Checking if args.length > 0 could still throw an exception if args.length is 2. Whenever you need args[1] you need to check if args.length == 2, args[2] you will need to check if args.length == 3, etc.
     
  3. Offline

    The_Doctor_123

    You are not putting in a third argument when typing in the command, which is supposedly the player's name.
    Code:java
    1. Player target = Bukkit.getServer().getPlayer(args[2]);
     
  4. Offline

    Dead_Skeleton

    I still cannot figure out what to do to the code. I changed it an now it is giving a different error. Instead of ArrayIndexOutOfBoundsException: 2, It's doing ArrayIndexOutOfBounds: 1

    Here's the modified code:

    Code:java
    1. if (!args[0].isEmpty() && !args[1].isEmpty()
    2. && !args[2].isEmpty() && !args[3].isEmpty()) {
    3. if (args[0].equalsIgnoreCase("points")
    4. && args[1].equalsIgnoreCase("send")
    5. && !args[2].isEmpty()) {
    6. Player target = Bukkit.getServer().getPlayer(args[2]);
    7. if (isInt(args[3])) {
    8. int args3 = Integer.parseInt(args[3]);
    9. if (args3 > getConfig().getInt(
    10. p.getName() + ".stats.money")) {
    11. p.sendMessage(ChatColor.RED
    12. + "You do not have that many points!");
    13. return true;
    14. }
    15. if (target != null) {
    16. int playerConfig = Integer.parseInt(p
    17. .getName() + ".stats.money");
    18. int targetConfig = Integer.parseInt(target
    19. .getName() + ".stats.money");
    20. getConfig().set(
    21. target.getName() + ".stats.money",
    22. targetConfig + args3);
    23. getConfig().set(
    24. p.getName() + ".stats.money",
    25. playerConfig - args3);
    26. target.sendMessage(ChatColor.GREEN
    27. + args[3]
    28. + " points received from "
    29. + p.getName() + "!");
    30. p.sendMessage(ChatColor.GREEN + "Sent "
    31. + args[3] + " points to "
    32. + target.getName() + "!");
    33. }
    34. if (target == null) {
    35. p.sendMessage(ChatColor.RED
    36. + "Could not find player "
    37. + target.getName() + "!");
    38. return true;
    39. }
    40. }
    41. } else {
    42. return true;
    43. }
    44. } else {
    45. return true;


    Sorry, I'm new to it and would appreciate your help! Thanks!
     
  5. Offline

    The_Doctor_123

    Dead_Skeleton
    That's because you're not typing in enough arguments.......

    When you type your command, you should look like this:

    Code:
    /myCommand args0 args1 args2 args3
    
     
  6. Offline

    Dead_Skeleton

    lol, I'm so stupid... thanks. Gonna try and go on my own without any help, I think it will help me get better :)
     
Thread Status:
Not open for further replies.

Share This Page