Command Whit More Than One Args? /command args

Discussion in 'Plugin Development' started by MariusAlexanderWinsjansen, Feb 28, 2012.

Thread Status:
Not open for further replies.
  1. Greetings,

    I wonder how I can make one command whit more than one args!

    EG: /tell joke

    I only know how to make things happens when they type /tell, but i want it to be possible to type /tell joke /tell story /tell something etc!

    This is just an stupid eg, I am not making that plugin! haha!

    Hope someone can help me!

    Best Regards,
    Marius
     
  2. Offline

    Sorroko

    MariusAlexanderWinsjansen (I cant believe i typed that name, its too long! :p) Heres an example of the onCommand handler:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        Player player = null;
        if (sender instanceof Player) {
            player = (Player) sender;
        }
       
        if(cmd.getName().equalsIgnoreCase("mycmd")){
            if(args.length == 0){
                //do something this is root command
                return true;
            }
            if(args.length > 0){ //check to see if there were sub commands
               
                if(args[0].equalsIgnoreCase("test")){
       
                  //sub command for /mycmd test
       
                } else if(args[0].equalsIgnoreCase("test2")) {
                    if(player != null){
                        //player only sub command - /mycmd test2
                    }
            } else {
                    //return a message like 'Sorry that sub command does not exist'
                return false;
            }
        }
    }
     
    Ice_Sword likes this.
  3. Offline

    Ice_Sword

    You'd probably check the number of arguemets first. Then you'd use an if statement to check what arg[1] is.

    This is sudo code, don't try to copy it:

    If (arg[1] is "joke")
    {Tell Joke}
    Else if (arg[1] is "story")
    {Tell story.}
    etc.


    EDIT: I was too slow. The post above gave you the actual code.
     
    ryan7745 likes this.
  4. Offline

    theguynextdoor

    Possibly something along the lines of:
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("tell")){
    2. if(args[0].equalsIgnoreCase("joke")){
    3. //Haha funny man here
    4. }
    5. else if(args[0].equalsIgnoreCase("story")){
    6. // Insert bed time story here
    7. }
    8. else if(args[0].equalsIgnoreCase("something")){
    9. // Insert something here
    10. }
    11. }


    The main focus really is that to get the first thing after the initial command is referred to as args[0].
    And so if you wanted to expand on that, you could also check if args[1] equals something, and so on.
     
    Ice_Sword likes this.
  5. Thanks, ryan7745 Ice_Sword theguynextdoor !:D

    I really needed that! :)

    If someoff you have the time, also read this thread I have, where I am looking for some partner help in my new plugin! :)

    http://forums.bukkit.org/threads/lf-developer-partner-for-plugin-gungame.62501/

    Best Regards,
    Marius!

    Hi, again,

    I just have a guestion for the expanded args, so if i want to say "/tell story short"

    It should tell a short story, not just a noral story you get buy typing "/tell story"

    When i try this, it ends up whit just telling me the normal story! Here is my code:

    Code:
            if(commandLabel.equalsIgnoreCase("tell")){
                if(args.length == 0){
                player.sendMessage("You Have To Type /tell story");
                }else if(args[0].equelsIgnoreCase("story")){
                player.sendMessage("Once Upon A Time..");
                }else if(args[0].equelsIgnoreCase("story")){
                if(args[1].eguelsIgnoreCase("short")){
                player.sendMessage("Once Upon A Time.. In A Far Of Land");
                }else if{
                player.sendMessage("dude, you typed something wrong");
                }
            }
        }
    Hope you can help me!

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

    theguynextdoor

    probably due to languages or whatever, but i changed the spelling of equelsIgnoreCase to equalsIgnoreCase

    Code:java
    1. if (cmd.getName().equalsIgnoreCase("tell")) {
    2.  
    3. Player player = (Player) sender;
    4.  
    5. if (args.length == 0) {
    6. player.sendMessage("You Have To Type /tell story");
    7. }
    8. if (args[0].equalsIgnoreCase("story")) {
    9. if (args[1].equalsIgnoreCase("short")) {
    10. player.sendMessage("Once Upon A Time.. In A Far Of Land");
    11. }
    12. else {
    13. player.sendMessage("Once Upon A Time..");
    14. }
    15. }
    16. else {
    17. player.sendMessage("dude, you typed something wrong");
    18. }
    19. }


    Try this, basically i checked that if their arg[0] is story, then after that i checked if args[1] was short, if it was then i would send them the short version, otherwise it would send them the other message
     
  7. Thansk alot, again!! Worked for me! :)
     
Thread Status:
Not open for further replies.

Share This Page