Solved Command throwing errors

Discussion in 'Plugin Help/Development/Requests' started by kember007, Mar 29, 2015.

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

    kember007

    I'm not quite sure what I've done wrong this time but when I type a command without any arguments on my plugin it throws an error
    Error:
    Code:
    [12:52:08 INFO]: eKember issued server command: /class
    [12:52:08 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'clas
    s' in plugin SpellsAndClasses v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got.jar:git-Spigot-1649]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    1) ~[spigot.jar:git-Spigot-1649]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServe
    r.java:767) ~[spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerCon
    nection.java:1043) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java
    :880) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat
    .java:65) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186
    ) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java
    :81) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:7
    34) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    89) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    84) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :490) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [spigot.jar:git-Spigot-1649]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at me.kemsly.spells.spells.onCommand(spells.java:205) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got.jar:git-Spigot-1649]
            ... 13 more
    
    Here is the code:
    Code:Java
    1.  
    2. public int resetPrice = 500;
    3. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    4. {
    5. if(cmd.getName().equalsIgnoreCase("spell"))
    6. {
    7. Player player = (Player) sender;
    8. if(args.length == 0 || args[0].equalsIgnoreCase(null))
    9. {
    10. try {
    11. if(this.mysql.getClass(player).equalsIgnoreCase("Warrior"))
    12. {
    13. player.sendMessage(ChatColor.RED + "[" + ChatColor.GRAY + "Spells" + ChatColor.RED + "]" + ChatColor.AQUA + " You can use the Punch spell.");
    14. player.sendMessage(ChatColor.AQUA + "You can buy spells and wands with /spell buy.");
    15. }
    16. if(this.mysql.getClass(player).equalsIgnoreCase("Assassin"))
    17. {
    18. player.sendMessage(ChatColor.RED + "[" + ChatColor.GRAY + "Spells" + ChatColor.RED + "]" + ChatColor.AQUA + " You can use the Vanish spell.");
    19. player.sendMessage(ChatColor.AQUA + "You can buy spells and wands with /spell buy.");
    20. }
    21. } catch (ClassNotFoundException e) {
    22. // TODO Auto-generated catch block
    23. e.printStackTrace();
    24. } catch (SQLException e) {
    25. // TODO Auto-generated catch block
    26. e.printStackTrace();
    27. }
    28. }
    29. if(args[0].equalsIgnoreCase("buy") && args[1].equalsIgnoreCase(null))
    30. {
    31. player.sendMessage(ChatColor.RED + "[" + ChatColor.GRAY + "Spells" + ChatColor.RED + "]" + ChatColor.AQUA + " With /spell buy you can buy in game items including wands, and potions");
    32. player.sendMessage(ChatColor.AQUA + "You can currently buy a wand, a potion of healing I, or a potion of healing II.");
    33. player.sendMessage(ChatColor.AQUA + "Use '/spell buy heal one' or '/spell buy heal two' to buy healing potions and '/spell buy wand' to buy a wand.");
    34. }
    35. if(args[0].equalsIgnoreCase("buy") && args[1].equalsIgnoreCase("wand"))
    36. {
    37. int wandPrice = 350;
    38. String playername = player.getName();
    39. if(economy.getBalance(playername)>=wandPrice)
    40. {
    41. EconomyResponse er = economy.withdrawPlayer(playername, wandPrice);
    42. if(er.transactionSuccess())
    43. {
    44. ItemStack wand = new ItemStack(Material.BLAZE_ROD, 1, (short)1);
    45. ItemMeta wandMeta = wand.getItemMeta();
    46. wandMeta.setDisplayName(ChatColor.GRAY + "Wand");
    47. player.getInventory().addItem(wand);
    48. }
    49. else
    50. {
    51. player.sendMessage(ChatColor.RED + "[" + ChatColor.GRAY + "Spells" + ChatColor.RED + "]" + ChatColor.AQUA + " Transaction failed!");
    52. }
    53. }
    54. else
    55. {
    56. player.sendMessage(ChatColor.RED + "[" + ChatColor.GRAY + "Spells" + ChatColor.RED + "]" + ChatColor.AQUA + " You do not have enough money to buy a wand, you need $350!");
    57. }
    58. }
    59. if(args[0].equalsIgnoreCase("buy") && args[1].equalsIgnoreCase("heal"))
    60. {
    61. int halfHealPrice = 850;
    62. int fullHealPrice = 950;
    63. String playername = player.getName();
    64. if(args[2].equalsIgnoreCase("one") || args[1].equalsIgnoreCase("I"))
    65. {
    66. if(economy.getBalance(playername)>=halfHealPrice)
    67. {
    68. EconomyResponse er = economy.withdrawPlayer(playername, halfHealPrice);
    69. if(er.transactionSuccess())
    70. {
    71. Potion healI = new Potion(PotionType.INSTANT_HEAL, 1);
    72. ItemStack HealI = healI.toItemStack(1);
    73. ItemMeta healIMeta = HealI.getItemMeta();
    74. healIMeta.setDisplayName(ChatColor.GOLD + "Heal I");
    75. player.getInventory().addItem(HealI);
    76. }
    77. else
    78. {
    79. player.sendMessage(ChatColor.RED + "[" + ChatColor.GRAY + "Spells" + ChatColor.RED + "]" + ChatColor.AQUA + " Transaction failed!");
    80. }
    81. }
    82. else
    83. {
    84. player.sendMessage(ChatColor.RED + "[" + ChatColor.GRAY + "Spells" + ChatColor.RED + "]" + ChatColor.AQUA + " You do not have enough money to buy a half healstone, you need $850!");
    85. }
    86. }
    87. if(args[2].equalsIgnoreCase("two") || args[1].equalsIgnoreCase("II"))
    88. {
    89. if(economy.getBalance(playername)>=fullHealPrice)
    90. {
    91. EconomyResponse er = economy.withdrawPlayer(playername, fullHealPrice);
    92. if(er.transactionSuccess())
    93. {
    94. Potion healII = new Potion(PotionType.INSTANT_HEAL, 2);
    95. ItemStack HealII = healII.toItemStack(1);
    96. ItemMeta healIIMeta = HealII.getItemMeta();
    97. healIIMeta.setDisplayName(ChatColor.GOLD + "Heal II");
    98. player.getInventory().addItem(HealII);
    99. }
    100. else
    101. {
    102. player.sendMessage(ChatColor.RED + "[" + ChatColor.GRAY + "Spells" + ChatColor.RED + "]" + ChatColor.AQUA + " Transaction failed!");
    103. }
    104. }
    105. else
    106. {
    107. player.sendMessage(ChatColor.RED + "[" + ChatColor.GRAY + "Spells" + ChatColor.RED + "]" + ChatColor.AQUA + " You do not have enough money to buy a wand, you need $950!");
    108. }
    109. }
    110. }
    111. }
    112. if(cmd.getName().equalsIgnoreCase("class"))
    113. {
    114. Player player = (Player) sender;
    115. if(args.length == 0 || args[0].equalsIgnoreCase(null))
    116. {
    117. try {
    118. player.sendMessage(ChatColor.RED + "[" + ChatColor.GRAY + "Class" + ChatColor.RED + "]" + ChatColor.AQUA + " Your class is currently '" + this.mysql.getClass(player) + "'");
    119. } catch (ClassNotFoundException e) {
    120. // TODO Auto-generated catch block
    121. e.printStackTrace();
    122. } catch (SQLException e) {
    123. // TODO Auto-generated catch block
    124. e.printStackTrace();
    125. }
    126. }
    127. if(args[0].equalsIgnoreCase("reset"))
    128. {
    129. String playername = player.getName();
    130. try {
    131. if(this.mysql.getClass(player).equalsIgnoreCase("Warrior") || this.mysql.getClass(player).equalsIgnoreCase("Assassin") || this.mysql.getClass(player).equalsIgnoreCase("Archer"))
    132. {
    133. if(economy.getBalance(playername)>=resetPrice)
    134. {
    135. EconomyResponse er = economy.withdrawPlayer(playername, resetPrice);
    136. if(er.transactionSuccess())
    137. {
    138. this.mysql.resetClass(player);
    139. cooldown.remove(player);
    140. player.sendMessage(ChatColor.RED + "[" + ChatColor.GRAY + "Class" + ChatColor.RED + "]" + ChatColor.AQUA + " Class reset, you may now choose a different class.");
    141. }
    142. }
    143. else
    144. {
    145. player.sendMessage(ChatColor.RED + "[" + ChatColor.GRAY + "Class" + ChatColor.RED + "]" + ChatColor.AQUA + " You do not have enough money to reset your class, you require $500!");
    146. }
    147. }
    148. else
    149. {
    150. player.sendMessage(ChatColor.RED + "[" + ChatColor.GRAY + "Class" + ChatColor.RED + "]" + ChatColor.AQUA + " You are not in a class already, you must be in a class to reset your class.");
    151. }
    152. } catch (ClassNotFoundException e) {
    153. // TODO Auto-generated catch block
    154. e.printStackTrace();
    155. } catch (SQLException e) {
    156. // TODO Auto-generated catch block
    157. e.printStackTrace();
    158. }
    159. }
    160. else
    161. {
    162. try {
    163. player.sendMessage(ChatColor.RED + "[" + ChatColor.GRAY + "Class" + ChatColor.RED + "]" + ChatColor.AQUA + " Your class is currently '" + this.mysql.getClass(player) + "'");
    164. } catch (ClassNotFoundException e) {
    165. // TODO Auto-generated catch block
    166. e.printStackTrace();
    167. } catch (SQLException e) {
    168. // TODO Auto-generated catch block
    169. e.printStackTrace();
    170. }
    171. }
    172. }
    173. return false;
    174. }
    175.  

    Thanks for any help :) also, please (if you have time) lightly explain what I did wrong (no need to go really in depth).
     
  2. Offline

    nverdier

  3. Offline

    Zombie_Striker

    What is line 205? The exception is an ArrayOutOfBounds! Try learning how to read the stack trace, will help you when errors occur.
     
  4. Offline

    kember007

    Umm, line 205 is just a closing bracket, I don't think that would cause any issue because it has to be there...
     
  5. Offline

    nverdier

    @kember007 Then you have updated your code since that stack trace. Re-export your plugin. Re-run it in the server. Post the new error log.
     
  6. Offline

    Zombie_Striker

    Are you using the correct formatting? Do you stack two statements on the same line? If you're using Eclipse, do you use Control-Shift-F ?
     
  7. Offline

    kember007

    Well the stacktrace now says the error is this line "
    if(args[0].equalsIgnoreCase("reset"))
    "
    see anything wrong?
     
  8. Offline

    nverdier

    @kember007 Yes. There is nothing in 'args'. You have to add a check to make sure that the String[] isn't empty.
     
  9. Offline

    kember007

    I was thinking that aswell but I have this and shouldn't it fix my issue?
    Code:
    if(args.length == 0 || args[0].equalsIgnoreCase(null))
    
     
  10. Offline

    nverdier

    @kember007 No. When you do args[0], if the length isn't at least 1, you will get an error.
     
  11. Offline

    kember007

    Okay so to be clear if I enter a command without any args it will return an error? how do I solve this issue?

    Ah, I fixed it.
    if you where wondering the solution you need to do something like this...
    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    3. {
    4. if(cmd.getName().equalsIgnoreCase("enterCommandHere"))
    5. {
    6. if(args.length == 0)
    7. {
    8. //do stuff
    9. return false; // <-- that was where the error was!
    10. }
    11. }
    12. return false;
    13. }
    14.  

    so you can see I forgot to have the return false in the if statement.
     
    Last edited by a moderator: Mar 29, 2015
Thread Status:
Not open for further replies.

Share This Page