Commands with Args

Discussion in 'Plugin Development' started by BetaNyan, Jun 17, 2014.

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

    BetaNyan

    I am making a plugin and I want to add commands with a base command with arguments.

    Here is what i want
    /sprotect (Basecommand)
    /sprotect example
    /sprotect example1

    Heres my code
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
               
                Player p = (Player) sender;
            if(args.length == 0) {
                if(cmd.getName().equalsIgnoreCase("sprotect")) {
                    p.sendMessage(ChatColor.GREEN + "/sp enablebreak" + ChatColor.AQUA + " Enables block breaking protection");
                    p.sendMessage(ChatColor.GREEN + "/sp enableplace" + ChatColor.AQUA + " Enables block placing protection");
                    p.sendMessage(ChatColor.GREEN + "/sp disablebreak" + ChatColor.AQUA + " Disables block breaking protection");
                    p.sendMessage(ChatColor.GREEN + "/sp disableplace" + ChatColor.AQUA + " Disables block placing protection");
                    p.sendMessage(ChatColor.GREEN + "/sp reload" + ChatColor.AQUA + " Reloads the plugin. Do this after using any /sp enable commands.");
                }
          else {
                    if(args.length == 1) {
               
                        if(args[1] == "enablebreak") {
                            if(!p.hasPermission("simpleprotect.enablebreak") || p.hasPermission("simpleprotect.*")) {
                                p.sendMessage(ChatColor.DARK_RED + "You do not have permission to enable break protection.");
                            } else {
                                p.sendMessage(ChatColor.AQUA + "Block Break protection has been " + ChatColor.GREEN + "enabled" + ChatColor.AQUA + ".");
                                getConfig().set("BreakProtect", true);
                            }
                        }
                    }
               
                        if(args[0] == "enableplace") {
                            if(!p.hasPermission("simpleprotect.enableplace") || p.hasPermission("simpleprotect.*")) {
                                p.sendMessage(ChatColor.DARK_RED + "You do not have permission to enable place protection.");
                            } else {
                                p.sendMessage(ChatColor.AQUA + "Block Place protection has been " + ChatColor.GREEN + "enabled" + ChatColor.AQUA + ".");
                                getConfig().set("PlaceProtect", true);
                            }
                        }
                       
                        if(args[0] == "disablebreak") {
                            if(!p.hasPermission("simpleprotect.disablebreak") || p.hasPermission("simpleprotect.*")) {
                                p.sendMessage(ChatColor.DARK_RED + "You do not have permission to disable break protection.");
                            } else {
                                p.sendMessage(ChatColor.AQUA + "Block Break protection has been " + ChatColor.RED + "disabled" + ChatColor.AQUA + ".");
                                getConfig().set("BreakProtect", false);
                            }
                        }
                       
                        if(args[0] == "disableplace") {
                            if(!p.hasPermission("simpleprotect.disableplace") || p.hasPermission("simpleprotect.*")) {
                                p.sendMessage(ChatColor.DARK_RED + "You do not have permission to disable place protection.");
                            } else {
                                p.sendMessage(ChatColor.AQUA + "Block Place protection has been " + ChatColor.RED + "disabled" + ChatColor.AQUA + ".");
                                getConfig().set("PlaceProtect", false);
                            }
                        }
               
                        if(args[0] == "reload") {
                            if(!p.hasPermission("simpleprotect.reload") || p.hasPermission("simpleprotect.*")) {
                                p.sendMessage(ChatColor.DARK_RED + "You do not have permission to reload SimpleProtection");
                            } else {
                                instance.reloadConfig();
                                instance.saveConfig();
                            }
                        }
            return false;
            }
            return false;
            }
            return false;
        }
    So as you can see /sprotect sends you all of the help menu. That part works. But /sprotect enablebreak, /sprotect enableplace, etc doesn't work.

    Please help!
     
  2. Offline

    macboinc

    Paste plugin.yml?
     
  3. Offline

    BetaNyan

    Code:
    main: me.BetaNyan.SimpleProtection.Main
    version: 1.0
    name: SimpleProtection
     
    commands:
        sprotect:
     
  4. Offline

    macboinc

    Try:
    Code:
         
    name: SimpleProtection
    version: 1.0
    author: BetaNyan
    main: me.BetaNyan.SimpleProtection.Main
    description: Your description!
     
    commands:
        sprotect:   
            usage: /<command>
            description: Description!
                       
              
     
  5. Offline

    BetaNyan

    Whenever I do /sprotect or /sprotect <args> it just shows the help menu and or it says /sprotect <args>
     
  6. Offline

    LordVakar

    BetaNyan
    In the plugin.yml remove the usage part.
    All of these have to be else if
    Code:java
    1. if(args[0] == "enableplace") {
    2. if(!p.hasPermission("simpleprotect.enableplace") || p.hasPermission("simpleprotect.*")) {
    3. p.sendMessage(ChatColor.DARK_RED + "You do not have permission to enable place protection.");
    4. } else {
    5. p.sendMessage(ChatColor.AQUA + "Block Place protection has been " + ChatColor.GREEN + "enabled" + ChatColor.AQUA + ".");
    6. getConfig().set("PlaceProtect", true);
    7. }
    8. }
    9.  
    10. else if(args[0] == "disablebreak") {
    11. if(!p.hasPermission("simpleprotect.disablebreak") || p.hasPermission("simpleprotect.*")) {
    12. p.sendMessage(ChatColor.DARK_RED + "You do not have permission to disable break protection.");
    13. } else {
    14. p.sendMessage(ChatColor.AQUA + "Block Break protection has been " + ChatColor.RED + "disabled" + ChatColor.AQUA + ".");
    15. getConfig().set("BreakProtect", false);
    16. }
    17. }
    18.  
    19. else if(args[0] == "disableplace") {
    20. if(!p.hasPermission("simpleprotect.disableplace") || p.hasPermission("simpleprotect.*")) {
    21. p.sendMessage(ChatColor.DARK_RED + "You do not have permission to disable place protection.");
    22. } else {
    23. p.sendMessage(ChatColor.AQUA + "Block Place protection has been " + ChatColor.RED + "disabled" + ChatColor.AQUA + ".");
    24. getConfig().set("PlaceProtect", false);
    25. }
    26. }
    27.  
    28. else if(args[0] == "reload") {
    29. if(!p.hasPermission("simpleprotect.reload") || p.hasPermission("simpleprotect.*")) {
    30. p.sendMessage(ChatColor.DARK_RED + "You do not have permission to reload SimpleProtection");
    31. } else {
    32. instance.reloadConfig();
    33. instance.saveConfig();
    34. }
    35. }


    So it would be

    else if(args[0] == "disableplace") {
    The onyl one if just if(args[0] would be enableplace
     
  7. Offline

    BetaNyan

    Didn't work! Heres my code
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2.  
    3. Player p = (Player) sender;
    4. if(args.length == 0) {
    5. if(cmd.getName().equalsIgnoreCase("sprotect")) {
    6. p.sendMessage(ChatColor.GREEN + "/sp enablebreak" + ChatColor.AQUA + " Enables block breaking protection");
    7. p.sendMessage(ChatColor.GREEN + "/sp enableplace" + ChatColor.AQUA + " Enables block placing protection");
    8. p.sendMessage(ChatColor.GREEN + "/sp disablebreak" + ChatColor.AQUA + " Disables block breaking protection");
    9. p.sendMessage(ChatColor.GREEN + "/sp disableplace" + ChatColor.AQUA + " Disables block placing protection");
    10. p.sendMessage(ChatColor.GREEN + "/sp reload" + ChatColor.AQUA + " Reloads the plugin. Do this after using any /sp enable commands.");
    11. }
    12. else if(args.length == 1) {
    13.  
    14. if(args[0] == "enableplace") {
    15. if(!p.hasPermission("simpleprotect.enableplace") || p.hasPermission("simpleprotect.*")) {
    16. p.sendMessage(ChatColor.DARK_RED + "You do not have permission to enable place protection.");
    17. } else {
    18. p.sendMessage(ChatColor.AQUA + "Block Place protection has been " + ChatColor.GREEN + "enabled" + ChatColor.AQUA + ".");
    19. getConfig().set("PlaceProtect", true);
    20. }
    21. }
    22.  
    23. else if(args[0] == "enableplace") {
    24. if(!p.hasPermission("simpleprotect.enableplace") || p.hasPermission("simpleprotect.*")) {
    25. p.sendMessage(ChatColor.DARK_RED + "You do not have permission to enable place protection.");
    26. } else {
    27. p.sendMessage(ChatColor.AQUA + "Block Place protection has been " + ChatColor.GREEN + "enabled" + ChatColor.AQUA + ".");
    28. getConfig().set("PlaceProtect", true);
    29. }
    30. }
    31.  
    32. else if(args[0] == "disablebreak") {
    33. if(!p.hasPermission("simpleprotect.disablebreak") || p.hasPermission("simpleprotect.*")) {
    34. p.sendMessage(ChatColor.DARK_RED + "You do not have permission to disable break protection.");
    35. } else {
    36. p.sendMessage(ChatColor.AQUA + "Block Break protection has been " + ChatColor.RED + "disabled" + ChatColor.AQUA + ".");
    37. getConfig().set("BreakProtect", false);
    38. }
    39. }
    40.  
    41. else if(args[0] == "disableplace") {
    42. if(!p.hasPermission("simpleprotect.disableplace") || p.hasPermission("simpleprotect.*")) {
    43. p.sendMessage(ChatColor.DARK_RED + "You do not have permission to disable place protection.");
    44. } else {
    45. p.sendMessage(ChatColor.AQUA + "Block Place protection has been " + ChatColor.RED + "disabled" + ChatColor.AQUA + ".");
    46. getConfig().set("PlaceProtect", false);
    47. }
    48. }
    49.  
    50. else if(args[0] == "reload") {
    51. if(!p.hasPermission("simpleprotect.reload") || p.hasPermission("simpleprotect.*")) {
    52. p.sendMessage(ChatColor.DARK_RED + "You do not have permission to reload SimpleProtection");
    53. } else {
    54. instance.reloadConfig();
    55. instance.saveConfig();
    56. }
    57. }
    58. }
    59. }
    60. return false;
    61. }
    62. }
     
  8. Offline

    LordVakar

    BetaNyan
    Is there any stracktrace?
    What exactly doesn't work?
    Try:
    else if (args[0].equalsIgnoreCase("stuffhere"))
    Add the equalsIgnoreCase
    Try removing this line:
    else if(args.length == 1) {
     
  9. Offline

    es359

    don't use == with strings.

    BetaNyan

    Do it like this.

    Code:java
    1. if(cmd.getName().equalsIgnoreCase("MainCommand.")) {
    2. if(args.lenth == 0) {
    3. //do whatever.
    4. }else if(args.lenth == 1){
    5. if(args[0].equalsIgnoreCase("arg1")) {
    6. //do arg1.
    7. return;
    8. }else if(args[0].equalsIgnoreCase("arg2")) {
    9. //do arg2.
    10.  
    11. // you get the idea.
    12. }
    13. }
    14. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  10. Offline

    AoH_Ruthless

    BetaNyan
    Look at your if logic.

    if args length is 0 -
    -if command is /sprotect
    --run code
    if args length is 1
    -if args[0] is X
    --run code

    You should check if the command is /sprotect, then the args.

    If I used /abcdefghijklmnopqrstuvwxyz enableplace, the code would run.
    Also, you have the enableplace argument twice ... makes no sense. Proofread your code man, it's not that difficult.

    Also, you are unsafely casting sender to player. If you try to run the command from command block or console, you will get an internal server error.
     
    es359 likes this.
  11. Offline

    es359

    Or just use a
    HTML:
    switch() {
    //yourcasehere
    }
    Statement.
     
  12. Offline

    AoH_Ruthless

    es359 That's also incorrect. Your brackets are misplaced. Read your code again and update the post accordingly :)

    (Didn't see your switch post, I mean Post #10)
     
  13. Offline

    es359

    Um... no?
     
    AoH_Ruthless likes this.
  14. Offline

    AoH_Ruthless

    es359
    ****, I can't read I suppose. My bad :(. Man, I'm on a stupid post spree today [sheep]
     
    es359 likes this.
  15. Offline

    es359

    AoH_Ruthless Lol You had me scared for a second. I panicked and put it in eclipse just to make sure. XD
     
  16. Offline

    AoH_Ruthless

    es359
    Yeah know, I thought the else if (args[0].equalsIgnoreCase("arg2")) {} actually said else if (args.length == 2) {}

    Like I said, I clearly can't read :)

    Anyways lets not make fun of me too much longer (we have to keep the topic on track, right? ;))
     
    es359 likes this.
  17. Offline

    es359

    Sorry, didn't see your last post lol.


    Usually, I don't like doing it like that, because, it can confuse people do the the elseifs() right after the statement.

    I try to space them out, or just use the switch() statement with args[0]. (It's easer to read. I think anyway lol.)


    EDIT: I was using Sublime 2 texteditor at the time, so it doesn't have any of the actual java syntax in it.

    Updated code. BetaNyan. Like AoH_Ruthless said, Make sure to properly check your commands arguments, and your logic. Or it will backfire on you. Also, I recommend you add a safe to your player cast, so you don't get that nasty console error.
    Code:java
    1.  
    2. if(cmd.getName().equalsIgnoreCase("MainCommand.")) {
    3. if(args.length == 0) {
    4. //do whatever.
    5. }else if(args.length == 1){
    6. if(args[0].equalsIgnoreCase("arg1")) {
    7. //do arg1.
    8. return true;
    9. }else if(args[0].equalsIgnoreCase("arg2")) {
    10. //do arg2.
    11.  
    12. // you get the idea.
    13. }
    14. }
    15. }
    16.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  18. Offline

    AoH_Ruthless

    es359
    Instead of lots of embedded ifs, else ifs, and elses, I just return at the end of each body.

    Code:java
    1.  
    2. if (args.length == 1) {
    3. if (args[0].equalsIgnoreCase("a")) {
    4. // code
    5. return true;
    6. }
    7. if ... {
    8. // code
    9. return true;
    10. }
    11. }
    12.  
    13. if (args.length == 0 {
    14. // code
    15. return true;
    16. }
    17.  

    But I guess it is to each his own.
    Technically, that is slightly less efficient than else if and I guess is a bad practice, but I think it looks better.

    However, you are also correct. Switch statements are optimized by the compiler, providing more efficiency and maybe even a bit of eloquence in your code :D
     
    es359 likes this.
  19. Offline

    es359

    Might be the java course I'm taking lol... Instructor tends to favor nested elseif() statements. AoH_Ruthless
     
  20. Offline

    AoH_Ruthless

    es359
    I would not say elseif() statements are bad. They have a wide variety of purposes and it's good that your Java course is teaching them (hopefully properly).

    Case statements are more preferable when there is a long chain of if statements. Let me explain:

    With a chain of say, 5 if statements, you compiler basically is forced to check each one in a row until a match is found. If statements can also have tons of things to check. You could have to check if string s is equal to AoH_Ruthless, boolean a == boolean b, etc. Switch statements force you to only check one thing at a time.

    But else ifs are obviously more dynamic, because they allow you to check multiple things at once, albeit giving up a few nanoseconds of speed.
     
    es359 likes this.
Thread Status:
Not open for further replies.

Share This Page