Multiple Command Arguments

Discussion in 'Plugin Development' started by tommykins20, Dec 15, 2011.

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

    tommykins20

    Hello,
    How can I make a command allow multiple arguments for it, for example:
    /mr chicken pop
    What method can I use to make this possible?
    Thanks,

    Happy crafting,
    -tommykins20
     
  2. Offline

    Kierrow

    If your plugin was to listen for the command "/hello" and
    would not check for any arguments, if you typed "/hello there" it would
    do the same thing as before.

    That is because, if you want to have arguments in your command, you don't have to declare them.

    In your onCommand method, there is a String[] array passed, usually called something like args.
    It is located in the fourth spot:

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, [B]String[] args[/B]) {
        return false; // -> your code
    }
    Now that args array contains all the arguments you as a player have passed.
    For example, if you were to type "/hello there stranger", args[0] = "there" and args[1] = "stranger".

    I hope I was able to help you.
     
  3. Offline

    Perdog

    @tommykins20
    Just to give you a basic example of how one would add the arguments to the command, I will use your example
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. if (sender instanceof Player) {
    3. player = (Player) sender;
    4. }
    5. if (cmd.getName().equalsIgnoreCase("hello")) {
    6. if (args.length == 2) {
    7. if (args[0].equalsIgnoreCase("there")) {
    8. if (args[1].equalsIgnoreCase("stranger")) {
    9. //Do something, perhaps send them a message?
    10. player.sendMessage("Hello to you too!");
    11. return true;
    12. }
    13. }
    14. }
    15. }
    16. return false;
    17. }
     
    Resolver and Steffion like this.
  4. Offline

    tommykins20

    wow! This is really helpfull thank you guys!! :D
     
  5. Offline

    Steffion

    Thanks helped me too :)!
     
  6. Offline

    TGameCo

    I understand this, but how do i have a string then an int?
    Example:
    /heal <Player> [amount]
     
  7. Offline

    BajanAmerican

     
  8. Offline

    Rockett8855


    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. if (sender instanceof Player) {
    4. Player p = (Player) sender;
    5. if (cmd.getName().equalsIgnoreCase("heal")) {
    6. if (args.length == 2) {
    7. Player target = Bukkit.getServer().getPlayer(args[0]);
    8. if (target == null) {
    9. p.sendMessage("That player isn't online");
    10. } else {
    11. if (isInt(args[1]) == true){
    12. target.setHealth(20);
    13. target.sendMessage("You have been healed");
    14. p.sendMessage("You have healed " + target.getName());
    15. } else {
    16. p.sendMessage("That is not a valid health value");
    17. }
    18. }
    19. }
    20. }
    21. } else {
    22. sender.sendMessage("You must be a player");
    23. }
    24. return false;
    25. }
    26.  
    27. public boolean isInt(String s) {
    28. try {
    29. Integer.parseInt(s);
    30. return true;
    31. } catch (NumberFormatException e) {
    32. return false;
    33. }
    34. }
    35.  


    I like to use this integer parser boolean for more than one thing.
     
  9. Offline

    TGameCo



    Thanks for that! I will be updating one of my plugins soon because of this!
     
  10. Offline

    Rockett8855


    One change, if you want it to set the health of the amount instead of
    Code:java
    1. target.setHealth(20);

    use
    Code:java
    1. target.setHealth(args[2]);
     
    sara4012 likes this.
  11. Offline

    sara4012

    Ditto!
     
  12. Offline

    Bloxcraft


    You need to parse the int/double/float whatever. It's still a string, and to my knowledge, setHealth only takes a double.

    Code:java
    1. target.setHealth(Double.parseDouble(args[2]));
     
  13. Offline

    Rockett8855

    Yeah, wrote this a while ago, you are correct.
     
  14. Offline

    KewCrew

    lol i watched ur utube videos and now i see u commenting on thread XD
     
Thread Status:
Not open for further replies.

Share This Page