Adding multiple variables to commands

Discussion in 'Plugin Development' started by Krotass, Dec 6, 2013.

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

    Krotass

    *SOLVED* Thanks for helping me

    Hey guys. I'm currently trying to code a plugin with different commands.

    I know how to add a command such like /plugin help

    let's say I wanted to add a kit and I have the command /plugin kit

    how do I specify a variable after the kit such as "knight"
    What I'm looking for is how to do /plugin kit knight

    Current code:
    Code:
                    if (cmd.getName().equalsIgnoreCase("plugin")) {
                        if(args.length == 0) {
                            player.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN + "Plugin" + ChatColor.GOLD + "] " + ChatColor.YELLOW + "Please run the " + ChatColor.RED + "/plugin help " + ChatColor.YELLOW + "command");
                            return true;
                        } else {
                         
                            if(args[0].equalsIgnoreCase("kit")) {
                                //Permission for the info command
                                if (!sender.hasPermission("plugin.info")) {
                                    sender.sendMessage(ChatColor.RED + "You do not have the permission to exucute this command");
                                    return true;
                                }
                             
                                player.sendMessage(ChatColor.YELLOW + "Current Kits: " + ChatColor.GREEN + "Knight");
                                return true;
     
                             
                            }
    }
    Note: This part works fine. All I need is a 3'rd variable
     
  2. Offline

    Wolfey

    Code:
    if(args.length == 3) { }
    
    Is that what you're looking for?

    Or
    Code:
    if(args[1].equalsIgnoreCase("kit") { } // second argument
    if(args[2].equalsIgnoreCase("knight") { } // third argument
    
    What you're trying to do is this:
    Code:
    Player p = (Player) sender;
    if(cmd.getName().equalsIgnoreCase("plugin")) {
    if(args.length == 0) {
    p.sendMessage("Do /plugin help for help");
    return true;
    }
    if(args.length == 1) {
    if(args[0].equalsIgnoreCase("help")) {
    p.sendMessage("command list help");
    return true;
    }
    }
    if(args.length == 2) {
    if(args[0].equalsIgnoreCase("kit")) {
    if(args[1].equalsIgnoreCase("knight")) {
    // give them knight kit
    }
    }
    }
    }
    
     
  3. Offline

    Wingzzz

    Okay no problem!

    First off, let's go over the arguments of a command alright? You can consider these to be the, words, or better yet- information as it is not only limited to words (of course).

    So, let's say you're given the variable 'args' which has a return type of 'String[]'. So you know it's an array of strings, essentially a group of words. How do we find out how many words we have in this group though? Well simply check the length!
    Code:java
    1. int amountOfWords = args.length;

    Awesome! Now we know how to extract how many words have been provided to us. We can use this to determine how many words in a command have been type (I say words, but these can be any string- of course 1, 2, 3 aren't words but numbers, although they are strings when typed but we can convert those to integers later- anyways...) to handle them accordingly.

    Let's go over an example shall we?

    Code:java
    1. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    2. if(sender instanceof Player) {
    3. Player player = (Player) sender;
    4. if(command.getName().equalsIgnoreCase("plugin")) {
    5. switch(args.length) {
    6. case 0:
    7. // send message
    8. return true;
    9. case 1:
    10. if(args[0].equalsIgnoreCase("kit")) {
    11. // send information on all available kits and how to use the command properly
    12. }
    13. case 2:
    14. if(args[0].equalsIgnoreCase("kit")) {
    15. if(args[1].equalsIgnoreCase("")) {
    16. /*
    17.   * now, here you can either handle each and every kit via string the same way here with
    18.   * if-else of with a switch- OR if you have any class that represents a kit or anything like
    19.   * that you can have a collection of those kits and you can just say if the collection contains
    20.   * a kit with the name that equalsIgnoreCase(args[1]);
    21.   *
    22.   * I'd recommend just handling each kit with if-else's or a switch statement if you're newer to this :)
    23.   */
    24. }
    25. }
    26. default:
    27. // inform them on how to use the command- as at this point they entered an invalid amount of arguments
    28. }
    29. }
    30. }
    31. }

    So, as you can see when we want to retrieve an element from the array, we simply input its index number. Remember, java starts at 0, so the first element is at index 0... Which would be the "/plugin", then the second is at 1 (yet a length of 2), "/plugin kit", then the third is at index 2 (when the array has a length of 3) "/plugin kit knight"...

    If I didn't explain it properly let me know- this might look a bit confusing to those other than me (I think things through in my head sometimes and forget to type it out... err)
     
  4. Offline

    Krotass



    This works, thanks XD

    Yeah it looks a bit derped :D Thanks for helping me tho XD

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

    Wingzzz

    Well all you need to know about command arguments ie: /plugin arg arg arg arg... is just about arrays (as long as it's about the default Bukkit onCommand() command handling). So if you every run into any troubles just be sure to understand arrays thoroughly ;) Good luck!
     
  6. Offline

    Krotass



    The /plugin kit knight works perfect... Found one problem


    typing /plugin kit knight somecrapgoeshere would still give you the kit.

    I fixed this by adding an else saying please run the /plugin help command

    BUT! I've now added a command saying /plugin kit ranger

    /plugin kit ranger somecrapgoeshere would works and I can't add an else statement since this would display at the knight one.

    My final question is:

    How do I add more than one command which has the same input such like /plugin kit <kit>

    I need 5 commands in total:

    /plugin
    /plugin kit
    /plugin kit list
    /plugin kit ranger
    /plugin kit knight

    I can make 3/5 commands work
    I cannot add more than 1 kit since the codes would get loaded if you have the same arg in the codes
    I cannot add the /plugin kit command since I have it goes into the kit knight code.

    I'm bad at explaining stuff :(
     
  7. Offline

    Wolfey

    Code:
    Player p = (Player) sender;
    if(cmd.getName().equalsIgnoreCase("plugin")) {
    if(args.length == 0) {
    p.sendMessage("Do /plugin help for help");
    return true;
    }
    if(args.length == 1) {
    if(args[0].equalsIgnoreCase("help")) {
    p.sendMessage("command list help");
    return true;
    }
    }
    if(args.length == 2) {
    if(args[0].equalsIgnoreCase("kit")) {
    if(args[1].equalsIgnoreCase("knight")) {
    // give them knight kit
    } else if(args[1].equalsIgnoreCase("otherKitName")) {
    // give them kit
    } 
    }
    }
    }
    
    and then when you dont have anymore args[1]'s to put, write an else statement, this is if they don't write /plugin kit knight, or /plugin kit otherKitName

    Also no it wouldn't give you the kit if you wrote /plugin kit knight sdhsiodhs, cause that would be 3 args, not 2, and would therefor cause an error. You can prevent this by doing if(args.length > 2) { }
     
  8. Offline

    Wingzzz

    You can move over to pure if-else statements if you don't need to handle many arguments... So you could just say:


    Code:java
    1. if(command.getName().equalsIgnoreCase("plugin")) {
    2. if(args.length == 0) {
    3. // -> /plugin
    4. } else if(args.length == 1) {
    5. // -> /plugin arg
    6. if(args[1].equalsIgnoreCase("kit")) {
    7. // inform on how to properly use the command
    8. }
    9. } else if(args.length == 2) {
    10. // -> /plugin arg arg
    11. if(args[1].equalsIgnoreCase("kit")) {
    12. if(args[2].equalsIgnoreCase("knight")) {
    13. // do something
    14. }
    15. }
    16. } else if(args.length > 2) {
    17. // -> This will handle anything not mentioned, so 3, 4, 5, 300...
    18. }
    19. }



    Switches are just faster- especially when you have a larger amount of possibilities.

    EDIT: Ninja'd
     
  9. Offline

    Krotass



    haha, thanks for helping me :D
     
    Wingzzz likes this.
Thread Status:
Not open for further replies.

Share This Page