[Tutorial] Command Arguments

Discussion in 'Resources' started by JPG2000, Oct 23, 2013.

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

    JPG2000

    Hello!

    This tutorial will be on command arguments. Basicly, how to do a command with multiple arguments.

    Requested by: @PieMan456

    Alright, before we dig into some simple code, I need to explain a concept.

    Lets pretend for a second. Someone in a mall tells a person, and a computer to count to 5. How do you think they will turn out?

    I bet for the person you though they would say "1, 2, 3, 4, 5". Thats normal. But you were probalyl wrong about computers. In java, it would normally say "0, 1, 2, 3, 4". Thats how java counts. Its very odd.

    Thats basically what Im going to be referring to as the weird java counting rule.

    Now, one other thing is arguments. Heres what I'll be refering to:

    /basecommand arg0 arg2 arg2 arg3....

    The basecomamnd is the command that you usually write as with commandLabel, or in my examples, alias. The arguments are any separate arguments, not including base commands.
    Now onto some real code!

    In your onCommand method, usually used for commands, you have a parameter, usually named args.
    Code:java
    1. public void onCommand(ComamndSender sender, Command command, String alias, String[] args) {
    2. ^
    3. ^
    4. }


    That is a String array variable named args. When used as an array, it returns a string. It will return the argument that you 'put' in the array. See here:
    Code:
    /hello my name is jake
     
    In this case, here are the string array args:
     
    args[0] is 'my'
    args[1] is 'name'
    args[2] is 'is'
    and args[3] is 'jake'
    
    So basicly, you can access the argument by 'putting' a number inside of the array. The number is the argument number. NOTE: This does not include the base command. It only counts arguments. This does use the silly java counting rule. args[0] is the first argument and so on.

    Now, then, you can compare arguments using the same 'syntax' as using a base command:
    Code:java
    1. //Somewhere in your onCommand...
    2. if (alias.equalsIgnoreCase("basecommand") {
    3. if (args[0].equalsIgnoreCase("arg1")) {
    4. //Argument 1 is 'arg1'
    5. }
    6. }


    Now, that isn't the only thing you have to think about. You have to think "What if the person only types the basecommand and NO arguments". If that is the case with the example above, then you will get an errror. There is no args[0]. It will return null.

    To get around this, we use args.length. Key things to remember about this:

    - args.length represents the arguments - does not include base
    - args.length will return 0 if no arguments are typed

    So, lets see this in action:
    Code:java
    1. if (alias.equalsIgnoreCase("hello") {
    2. if (args.length == 0) { //Sender only typed '/hello' and nothing else
    3. sender.sendMessage("You only typed hello!");
    4.  
    5. } else { //Sender typed more then 1 argument, so args[0] can't be null.
    6. if (args[0].equalsIgnoreCase("sir") { //Sender typed '/hello sir'
    7. sender.sendMessage("You typed hello sir!");
    8. } else { //Sender had attest 1 argument, but didn't type sir as a second one!
    9. //args[0] also returns string, so lets send the message right back to them!
    10. sender.sendMessage("Your first argument was: " + args[0]);
    11.  
    12. }
    13. }



    That was pretty basic. The only new thing here is that we blast the players second argument back at them in the code section where the player typed a argument but not sir.

    Last thing:
    One thing you should be carefull about, your plugin.yml. ONLY REGISTER THE BASE COMMAND!!! IN YOUR plugin.yml you don't need to register the arguments...you can't. Just register the base command.


    SUMMING UP!
    • args[number_here] returns a string with that arg, does not count base command
    • check the length before comparing args. It will be null if no args where typed!
    • args.length uses the silly java number rule. args.length will be 0 if none, and 1 if 1 argument
    • only register base command in plugin.yml
    Here is the last piece of summing up code:
    Code:java
    1. public boolean onCommand(CommandSender sender, Comammand command, String alias, String[] args) {
    2. Player player = (Player) sender;
    3. if (alias.equalsIgnoreCase("heal") {
    4. if (args.length == 0) {
    5. //Player only typed '/heal' so lets heal them back!
    6. player.setHealth(20);
    7. } else {
    8. //Player typed something more
    9. Player target = Bukkit.getPlayerExact(args[0]);
    10. if (target == null) {
    11. //Target is not online
    12. player.sendMessage("Your target " + args[0] + " is not online!");
    13. } else {
    14. //Targets online
    15. player.sendMessage("You've healed " + args[0]);
    16. target.setHealth(20);
    17. }
    18. }
    19. }
    20. }


    Okay, so I know this is a lot for a very very simple concept, but some people weren't getting it. So I hope this helps!
     
  2. Offline

    PieMan456

    Hey thank you for writing this but when I try to add another command if says there is a syntax error on else. Do you know why?
     
  3. Offline

    JPG2000

  4. There is standing Comamnd ;)
     
  5. Offline

    Tehmaker

    Why does this need to be posted? If you don't know Java, you shouldn't be playing with Bukkit... If you know Java, you know this....
     
    Bart likes this.
  6. Some people (like me) have some trouble with arguments.
    If you don't like it why did you clicked the tread? just to post a comment?\

    EDIT: and about your "words of knowledge" I started with bukkit before I had learned java, I had no problems
     
  7. Offline

    Pitazzo

    Good! Really useful!
     
    nelson2tm likes this.
  8. Offline

    MrInspector

    I know this is kinda of over a 1 month necropost, but it's in the resource section so I dunno, but there is some errors in the code, here's a fixed up version with your code if anyone is really new to development and can't figure out how to fix, it's simple fixes really.
    Code:java
    1. public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
    2. Player player = (Player) sender;
    3. if (alias.equalsIgnoreCase("heal")) {
    4. if (args.length == 0) {
    5. //Player only typed '/heal' so lets heal them back!
    6. player.setHealth(20.0);
    7. } else {
    8. //Player typed something more
    9. Player target = Bukkit.getPlayerExact(args[0]);
    10. if (target == null) {
    11. //Target is not online
    12. player.sendMessage("Your target " + args[0] + " is not online!");
    13. } else {
    14. //Targets online
    15. player.sendMessage("You've healed " + args[0]);
    16. target.setHealth(20.0);
    17. }
    18. }
    19. }
    20. }
     
    97waterpolo likes this.
  9. Offline

    Wolfey

    Kind of a bump, but...
    MrInspector It's also good to check if the sender is a player before casting it as one.
     
  10. Offline

    MrInspector

    This was based off his tutorial, I was just fixing his code so new developers didn't have to reply and wait for an answer. :)
     
  11. Offline

    Wolfey

    If you were fixing his tutorial, then you should have fixed the casting one too.
     
  12. Offline

    MrInspector

    I mean I was fixing the errors in his tutorial
     
  13. Offline

    GalaxyBeatzz

    Good one I only knew this half... Thanks really helped me out in my project.
     
  14. Offline

    JoeyDevs

    Thanks for helping, Now i underzstand commands using Arguments !
     
  15. Offline

    PratamaJr

    JPG2000 Thanks for tuttorial!!
     
  16. Offline

    mickedplay

    JPG2000 You wrote' ComamndSender' instead of' CommandSender' in your first post in the first code :p
     
  17. Offline

    stoneminer02

    Could you show how to have like 'infinite' args?
    For a command like /kick Bob I wanna kick peepz :3 And I can have more args than one :D
     
Thread Status:
Not open for further replies.

Share This Page