[URGRENT] Unhandled Exception Executing Command.

Discussion in 'Plugin Development' started by Thiorum, Jan 23, 2014.

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

    Thiorum

    So I am trying to make a basic economy plugin for my server, when I type /economy it is meant to display "wrong arguments" but it keeps giving errors, also when players join it doesn't create a balance file for that player, I uploaded the command error as well as the eclipse project file so people can explore and fix, thank you, help fast please :3 !
    Code:java
    1. [23:51:14 WARN]: Unexpected exception while parsing console command "economy"
    2. org.bukkit.command.CommandException: Unhandled exception executing command 'economy' in plugin GoldEconomy v0.1
    3. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    4. at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    5. at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:542) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    6. at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchServerCommand(CraftServer.java:529) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    7. at net.minecraft.server.v1_7_R1.DedicatedServer.aw(DedicatedServer.java:286) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    8. at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:251) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    9. at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:541) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    10. at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:453) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    11. at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    12. Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    13. at Kriptus.Plugins.GoldEconomy.Commands.onCommand(Commands.java:20) ~[?:?]
    14. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    15. ... 8 more
    16. >
    17.  

    ------------------------------------------------------------
    Commands.java VvvV
    Code:java
    1. package Kriptus.Plugins.GoldEconomy;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8.  
    9. public class Commands implements CommandExecutor
    10. {
    11. //economy args[0], args[1], args[2]
    12. //<command> <Add/Remove/Set> <PlayerName> <Amount>
    13.  
    14. @Override
    15. public boolean onCommand(CommandSender p, Command cmd, String label,
    16. String[] args)
    17. {if(cmd.getName().equalsIgnoreCase("economy"))
    18. {
    19. //ShortLitnks
    20. String tp = Bukkit.getServer().getPlayerExact(args[1]).getName();
    21. double amount = 0;
    22. //ShortLinks
    23.  
    24. if(args.length != 3)
    25. {
    26. p.sendMessage(ChatColor.RED + "Incorrect Usage");
    27. p.sendMessage(ChatColor.RED + cmd.getUsage());
    28. }
    29. if(args.length == 3){
    30. if(args[0].equalsIgnoreCase("add"))
    31. {
    32. if(!Manager.hasAccount(args[1]))
    33. {
    34. p.sendMessage(ChatColor.RED+"Error: Player does not have an account");
    35. return true;
    36. }
    37. try
    38. {
    39. amount = Double.parseDouble(args[2]);
    40. }
    41. catch (Exception e)
    42. {
    43. p.sendMessage(ChatColor.RED+"Error: Please enter a valid number.");
    44. return true;
    45. }
    46.  
    47. Manager.setBalance(args[1], Manager.getBalance(args[1]) + amount);
    48. p.sendMessage(ChatColor.GREEN + tp + "'s account has " + args[2] + " added to it." );
    49. }
    50.  
    51. if(args[0].equalsIgnoreCase("remove"))
    52. {
    53. if(!Manager.hasAccount(args[1]))
    54. {
    55. p.sendMessage(ChatColor.RED+"Error: Player does not have an account");
    56. return true;
    57. }
    58. try
    59. {
    60. amount = Double.parseDouble(args[2]);
    61. }
    62. catch (Exception e)
    63. {
    64. p.sendMessage(ChatColor.RED+"Error: Please enter a valid number.");
    65. return true;
    66. }
    67.  
    68. Manager.setBalance(args[1], Manager.getBalance(args[1]) - amount);
    69. p.sendMessage(ChatColor.GREEN + tp + "'s account has " + args[2] + " removed from it." );
    70. }
    71.  
    72. if(args[0].equalsIgnoreCase("set"))
    73. {
    74. if(!Manager.hasAccount(args[1]))
    75. {
    76. p.sendMessage(ChatColor.RED+"Error: Player does not have an account");
    77. return true;
    78. }
    79. try
    80. {
    81. amount = Double.parseDouble(args[2]);
    82. }
    83. catch (Exception e)
    84. {
    85. p.sendMessage(ChatColor.RED+"Error: Please enter a valid number.");
    86. return true;
    87. }
    88.  
    89. Manager.setBalance(args[1], amount);
    90. p.sendMessage(ChatColor.GREEN + tp + "'s account has been set to " + args[2] + "." );
    91. }
    92. if(args[0].equalsIgnoreCase("balance"))
    93. {
    94. Manager.getBalance(args[1]);
    95. return true;
    96. }
    97. }
    98. }
    99. return true;
    100. }
    101.  
    102. }
     

    Attached Files:

  2. Offline

    random_username


    Code:
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
        at Kriptus.Plugins.GoldEconomy.Commands.onCommand(Commands.java:20)
    Could you please post your Commands class? Also, take a look at line 20.
     
  3. Offline

    Thiorum

    I posted a new thread with the download, also this was a mis-uploaded thread as I hadn't completed half the details >.<
     
  4. Offline

    Tirelessly

  5. Offline

    Thiorum

    I couldn't see anything wrong with it, no errors.
     
  6. Offline

    random_username

    Can you please post the class? :)
     
  7. Offline

    Shockwave317

    I don't like having commands that way...
     
  8. Offline

    xTrollxDudex

    Shockwave317
    You need to check your args, and by the way, arrays indexes start at 0. Learn Java please.
     
  9. Offline

    Shockwave317

    xTrollxDudex hrm I do it this way...
    edit: oh and I was talking about his cmd. etc...
    Code:java
    1. @Override
    2. public void onEnable() {
    3. getCommand("command").setExecutor(this);
    4. PluginManager pm = this.getServer().getPluginManager();
    5. pm.registerEvents(this, this);
    6.  
    7. }
    8.  
    9.  
    10.  
    11. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    12. if (args.length == 0) {
    13. //do something
    14. }
    15. if (args.length == 1) {
    16. if (args[0].equalsIgnoreCase("check")) {
    17. //do something
    18.  
    19. }
    20.  
    21. }
    22. }
     
  10. Offline

    Thiorum

    Bump, still need help >.<
     
  11. Offline

    Mrawesomecookie

    You need to check the argument count FIRST.
     
  12. Offline

    Thiorum

    what do you mean?
     
  13. Offline

    random_username


    Code:java
    1. if(args.length == 1){
    2. //code
    3. }

    That would be if the command is:
    /somecommand argument
    and the word argument would be args[0]
    Edit:
    I'll explain a bit more. When you made the command, you only typed "/economy". You checked for args[1] before checking the args length. As you didn't have any arguments, you got the ArrrayIndexOutOfBoundsException. Also, in java, you start counting array indexes at 0, not 1. So if you have /economy something, the word something would be args[0].
     
Thread Status:
Not open for further replies.

Share This Page