Solved Briefly explain why this value is 0 and not 4...

Discussion in 'Plugin Development' started by croc122, Oct 14, 2014.

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

    croc122

    I am trying to learn Java in order to develop Bukkit plugins. I found some open source plugins to dissect and learn from the code, but I don't understand why this line is like it is.

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    2. {
    3. if ((cmd.getName().equalsIgnoreCase("Menu")) &&
    4. ((sender instanceof Player)))
    5. {
    6. Player player = (Player)sender;
    7. if (args.length == 0)
    8. {
    9. player.openInventory(Menu.menu);
    10. return true;
    11. }


    In the line "if (args.length == 0)" (line 7), why does it equal zero and not four (because the /menu command is 4 letters)??? Unless .length means something else entirely, I'm completely stumped. Thanks for your help!
     
  2. Offline

    nuno1212sss

    Args is a String array, that means, it stores strings, the .length is the amount of Strings stored in that array, but if you do string.length() that is the amount of letters
     
  3. Offline

    Fuzzybear04

    Okay, so it works like, this (I see where you're coming from btw :D)
    The command starts with a "/", after that is it arg 0, so it'd be "/arg0", and after that it adds to it, so "/arg0 arg1 arg2" ect
    Basically, the command is checking if that there are no extra arguments provided, just the command, so it can open the inventory.
    Hope that explains it ;)
     
  4. Offline

    CraftCreeper6

    croc122
    Hello! Welcome to bukkit :)

    First of all, in Java, numbers start from 0. So 0,1,2,3,4 etc....

    Secondly, "args" stands for arguments, yes, it may seem like the it's meant for Menu but it is not :) It is simply to check weather the player has supplied 1 argument.

    Say you typed in chat:

    /This is a test

    That would be args[2]. You use [] to show that it's a list.

    The arguments are:

    CommandLabel / Command: This
    [0] is
    [1] a
    [2] test

    Therefore, if(args.length...) is checking weather you have supplied just one argument. As the first point states about Java starting at 0.
     
    croc122 likes this.
  5. Offline

    croc122


    Okay thanks so much, I feel dumb for not knowing that. nuno1212sss
     
  6. Offline

    fireblast709

    croc122 args is the an array which contains every argument that is found in the command. For example:
    Code:
    /menu args[0] args[1]
    Now in this example, cmd.getName() would return 'menu', so would label.

    args however, is the remaining part split over whitespace (" "). This means that the first element in the array is "args[0]", and the second is "args[1]". There are two elements, thus in this example args.length == 2. With /menu, there are no such arguments, thus the length == 0.
     
    croc122 likes this.
Thread Status:
Not open for further replies.

Share This Page