Issue w/ Multiple Command Arguments

Discussion in 'Plugin Development' started by LegitJava, Oct 24, 2013.

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

    LegitJava

    Hi there,

    I'm having trouble with multiple commands arguments. So, I already have a subcommand which is for example /birthdaycraft change <playername> <month> <day>. But, I have a separate if statement that handles if the argument is more than 2 if they type the command. So, the subcommands conflict with the regular if the argument is more than 2 but with no sub command. So, how do I make it not conflict? I know it's hard to explain but hopefully you can see what I mean in my code below.
    Code:java
    1. if (args.length == 1) {
    2. if (args[0].equalsIgnoreCase("check")) {
    3. if (!(p.hasPermission("birthdaycraft.check"))) {
    4. MessageManager.getInstance().msg(p, MessageType.BAD, "You do not have permission to use BirthdayCraft!");
    5. return true;
    6. }
    7.  
    8. if (args.length < 1) {
    9. MessageManager.getInstance().msg(p, MessageType.BAD, "You must specify a player name!");
    10. return true;
    11. }
    12.  
    13. Player target = Bukkit.getServer().getPlayer(args[1]);
    14.  
    15. if (target == null) {
    16. MessageManager.getInstance().msg(p, MessageType.BAD, "The player " + args[1] + " is not online!");
    17. return true;
    18. }
    19.  
    20. int targetMonth = settings.getBirthdays().getInt(target + ".month");
    21. int targetDay = settings.getBirthdays().getInt(target + ".day");
    22.  
    23. MessageManager.getInstance().msg(p, MessageType.GOOD, args[1] + "'s birthday is: " + targetMonth + "/" + targetDay + "!");
    24. return true;
    25. }
    26.  
    27. if (args[0].equalsIgnoreCase("change")) {
    28. if (!(p.hasPermission("birthdaycraft.change"))) {
    29. MessageManager.getInstance().msg(p, MessageType.BAD, "You do not have permission to use BirthdayCraft!");
    30. return true;
    31. }
    32.  
    33. if (args.length < 3) {
    34. MessageManager.getInstance().msg(p, MessageType.BAD, "You did not specify enough arguments!");
    35. MessageManager.getInstance().msg(p, MessageType.BAD, "Usage: /birthdaycraft change <player> <month> <day>");
    36. return true;
    37. }
    38.  
    39. Player target = Bukkit.getServer().getPlayer(args[1]);
    40. int targetMonth = -1;
    41. int targetDay = -1;
    42.  
    43. if (target == null) {
    44. MessageManager.getInstance().msg(p, MessageType.BAD, "The player " + args[1] + " could not be found!");
    45. return true;
    46. }
    47.  
    48. try { targetMonth = Integer.parseInt(args[2]); }
    49. catch (Exception e) { MessageManager.getInstance().msg(p, MessageType.BAD, "You did not specify a valid number!"); return true; }
    50.  
    51. try { targetDay = Integer.parseInt(args[3]); }
    52. catch (Exception e) { MessageManager.getInstance().msg(p, MessageType.BAD, "You did not specify a valid number!"); return true; }
    53.  
    54. if (targetMonth < 1 || targetMonth > 12) {
    55. MessageManager.getInstance().msg(p, MessageType.BAD, "You did not specify a valid month!");
    56. return true;
    57. }
    58.  
    59. if (targetDay < 1 || targetDay > 31) {
    60. MessageManager.getInstance().msg(p, MessageType.BAD, "You did not specify a valid day!");
    61. return true;
    62. }
    63.  
    64. settings.getBirthdays().set(target.getName() + ".month", targetMonth);
    65. settings.getBirthdays().set(target.getName() + ".day", targetDay);
    66. settings.saveBirthdays();
    67.  
    68. MessageManager.getInstance().msg(p, MessageType.GOOD, "You have successfully changed " + args[1] + "'s birthday to: " + targetMonth + "/" + targetDay + "!");
    69. }
    70.  
    71. else if (args[0] != "change" && args[0] != "update" && args[0] != "check"){
    72. MessageManager.getInstance().msg(p, MessageType.BAD, "You did not specify enough arguments!");
    73. MessageManager.getInstance().msg(p, MessageType.BAD, "Usage: /birthdaycraft <month> <day>");
    74. }
    75. }
    76.  
    77. if (args.length == 2) {
    78. int month = -1, day = -1;
    79.  
    80. int monthConfig = settings.getBirthdays().getInt(p.getName() + ".month");
    81. int dayConfig = settings.getBirthdays().getInt(p.getName() + ".day");
    82.  
    83. try { month = Integer.parseInt(args[0]); }
    84. catch (Exception e) { MessageManager.getInstance().msg(p, MessageType.BAD, "You did not specify a valid number!"); return true; }
    85.  
    86. try { day = Integer.parseInt(args[1]); }
    87. catch (Exception e) { MessageManager.getInstance().msg(p, MessageType.BAD, "You did not specify a valid number!"); return true; }
    88.  
    89. if (month < 1 || month > 12) {
    90. MessageManager.getInstance().msg(p, MessageType.BAD, "You did not specify a valid month!");
    91. return true;
    92. }
    93.  
    94. if (day < 1 || day > 31) {
    95. MessageManager.getInstance().msg(p, MessageType.BAD, "You did not specify a valid day!");
    96. return true;
    97. }
    98.  
    99. if (monthConfig != 0 && dayConfig != 0 || monthConfig != 0 || dayConfig != 0) {
    100. MessageManager.getInstance().msg(p, MessageType.GOOD, "Your birthday is already set as: " + monthConfig + "/" + dayConfig);
    101. return true;
    102. }
    103.  
    104. settings.getBirthdays().set(p.getName() + ".month", month);
    105. settings.getBirthdays().set(p.getName() + ".day", day);
    106. settings.saveBirthdays();
    107.  
    108. MessageManager.getInstance().msg(p, MessageType.GOOD, "You have successfully set your birthday to: " + month + "/" + day + "!");
    109. }
     
    Fight_Or_Die likes this.
  2. Offline

    1Rogue

    args.length is the number of arguments, in your case 4.

    so instead of:
    Code:java
    1. if (args.length < 3) {
    2. // error out
    3. }
    4. //other code with args[2], args[3]


    You would want anything with less than 4 arguments
    Code:java
    1. if (args.length < 4) {
    2. // error out
    3. }
    4. // other code up to args[3]
     
  3. Offline

    LegitJava

    1Rogue

    Doesn't work, it doesn't even show me any error message in console or in game chat. This is quite odd.
     
  4. Offline

    1Rogue

    if you log something in the start of the command, does it show up in console?

    Code:java
    1. plugin.getLogger().log(Level.INFO, "check subcommand running!");
     
  5. Offline

    LegitJava

    1Rogue Nope, it doesn't log to the console. I put that after if (args.length < 4) { }
     
  6. Offline

    1Rogue

    (args.length < 4) is an error catch, so it shouldn't be there. Put it directly after the check for the subcommand:

    Code:java
    1. if ("check".equalsIgnoreCase(args[0])) {
    2. plugin.getLogger().log(/* ... */);
    3. // other code
    4. }
     
  7. Offline

    LegitJava

    1Rogue


    Nope, it doesn't display that in the logger. But, if I type only check with no other arguments, I get a stack trace:

    10 10:38:18 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    24.10 10:38:18 [Server] INFO at me.legitmodern.birthdaycraft.Birthdaycraft.onCommand(Birthdaycraft.java:239)
    24.10 10:38:18 [Server] INFO Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    24.10 10:38:18 [Server] INFO at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    24.10 10:38:18 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
    24.10 10:38:18 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
    24.10 10:38:18 [Server] INFO at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
    24.10 10:38:18 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
    24.10 10:38:18 [Server] INFO at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
    24.10 10:38:18 [Server] INFO at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
    24.10 10:38:18 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
    24.10 10:38:18 [Server] INFO at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
    24.10 10:38:18 [Server] INFO at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
    24.10 10:38:18 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:834)
    24.10 10:38:18 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:877)
    24.10 10:38:18 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:959)
    24.10 10:38:18 [Server] INFO at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:523)
    24.10 10:38:18 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
    24.10 10:38:18 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    24.10 10:38:18 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'birthdaycraft' in plugin BirthdayCraft v1.0
    24.10 10:38:18 [Server] SEVERE null
     
  8. Offline

    1Rogue

    Oh silly me, you have "if (args.length == 1)" at the top of all your commands, so it will only execute with a single argument. Change that to > 0.

    What's line 239 of Birthdaycraft.java?
     
  9. Offline

    LegitJava

    1Rogue Thanks, I'll let you know if that fixed. Line 239 is:

    Code:java
    1. Player target = Bukkit.getServer().getPlayer(args[1]);
     
  10. Offline

    1Rogue

    You're checking too low again:

    Code:java
    1. if (args.length < 1) {
    2. MessageManager.getInstance().msg(p, MessageType.BAD, "You must specify a player name!");
    3. return true;
    4. }


    Should be:

    Code:java
    1. if (args.length < 2) {
    2. MessageManager.getInstance().msg(p, MessageType.BAD, "You must specify a player name!");
    3. return true;
    4. }
     
  11. Offline

    LegitJava

    1Rogue Thank you! The commands are fixed now. So, I always had 1 less check because I thought that the command itself didn't count as an argument. For the future, I count the /command as an argument as well?
     
  12. Offline

    1Rogue


    No. An array with 5 arguments has a length of 5:

    args[0], args[1], args[2], args[3], and args[4]. That's 5.
     
  13. Offline

    LegitJava

    1Rogue
    Ah right, silly me. Thanks again for the help. :)
     
  14. Offline

    JPG2000

    LegitJava Well, it looks like you fixed it. If you didn't, or is still confussed with Args, I created a tutorial on it. Check my signature!
     
  15. Offline

    LegitJava

    It's fixed now, but I'll still read over your tutorial. Thanks!
     
    JPG2000 likes this.
Thread Status:
Not open for further replies.

Share This Page