Command args

Discussion in 'Plugin Development' started by Techno, Mar 28, 2014.

Thread Status:
Not open for further replies.
  1. I know this is a noob'ish question but, how would I get the commands args for a player?
    Like:
    /command <player>

    I know that I would get the args.length but what would I do after that?
    Please answer my question! Thanks!
     
  2. Offline

    ShadowLAX

    Techno A parameter for the onCommand method is a String[], which then you can get by just using <parametername>[0] to get the first argument. Then just parse it to a player.
     
  3. ShadowLAX
    So...
    Code:java
    1.  
    2. if (args.length == 1) {
    3.  
    4. }
    5.  


    And how would I parse it? Sorry, i'm not a pro (yet) at java..
     
  4. Offline

    Trevor1134

    Techno That is checking is player actually typed /command <something> to retrieve the value, do:

    Code:java
    1. String s = args[0];


    Remeber, in arrays, integers start at 0, so /command <something> would be the 0 argument.
     
  5. Offline

    Opacification

    Lets say you want to get a target,

    You could do something like.

    Code:java
    1. Bukkit target = Bukkit.getServer().getPlayer(args[0));
     
  6. Opacification
    Okay, so then I would do:
    Code:
    if (args.length == target) {
     
    }
    
    ?

    chasechocolate
    Can you help? Your a pro..

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

    justin_393

    Techno Opacification just showed you the best way to get a player. Since java starts counting at 0 you want to make sure that args[0] starts with 0. To extend off of what he said you would want to check if the player is online by doing.
    Code:java
    1. Player target = Bukkit.getServer().getPlayer(args[0));
    2.  
    3. if (target == null) {
    4.  
    5. sender.sendMessage(args[0] + "is currently offline!");
    6.  
    7. }
    8.  



    This would tell you if they are offline
     
    iiHeroo likes this.
  8. justin_393
    Then I would just add an else { } on to the end, to run the code when the playername is entered as an arg?
     
  9. Offline

    unon1100

    I have a wonderful tutorial on how Arrays work, I do/will use Java lingo when doing my tutorials as I do prefer people to learn the basics of Java before learning bukkit, but here it is regardless.

     
  10. Offline

    justin_393

    Techno Yep, you can do
    Code:java
    1. Player target = Bukkit.getServer().getPlayer(args[0]);
    2. if(target == null) {
    3. sender.sendMessage(args[0] + "is currently offline");
    4. }else{
    5. //your code here
    6. }
     
  11. Offline

    Opacification

    Techno

    Follow what I and justin_393 told you.

    Mine was the base of getting a player.

    His was checking if the player didn't exist or was offline.
     
    justin_393 likes this.
Thread Status:
Not open for further replies.

Share This Page