Solved Help with command arguements

Discussion in 'Plugin Development' started by MrDplugins, Jan 24, 2016.

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

    MrDplugins

    Hi Ive done some googling and researching and I can't the answer I need for this problem I have.
    I want my command to create a heist like so

    /createheist <name> <time> <reward> <level> <cops>
    How would I code that?
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
               if (sender instanceof Player) {
                    Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("createheist")) {
               // Name, time, reward, cops, level, etc
               
    
            }
    
                return true;
            }
            return false;
        }
    Thanks in advance!
     
  2. Offline

    Caedus

    Judging by the stage at which your code is at, i'm not sure whether you want someone to hand you code or not.

    Assuming there has to be 5 arguments, no more and no less, you would want to check to make sure the argument length = 5, like:
    Code:
    if(args.length == 5)
    From there on, depending on what you want to do with the arguments, you can refer to them as args[0], args[1], args[2], args[3] & args[4].

    Not sure how helpful to you this is, but i'm pretty sure there are some useful tutorials around command arguments online.
     
  3. Offline

    MrDplugins

    @Caedus this is only the command part I have all the methods and config storage setup. So would it look like this?

    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
               if (sender instanceof Player) {
                    Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("createheist")) {
      if(args.length==0){
    //Name for heist
    } 
    
      if(args.length==1){
    //Time for heist
    }
    
      if(args.length==3){
    //level for heist
    }
    etc
    
    
    
    
    
     
  4. Offline

    Zombie_Striker

    @MrDplugins
    Not really. You want to check if the length is greater than or equal to 5. What that is doing is checking if it has no args, one arg, or 3 args.

    After that, use Integer#parseInt to turn those strings into integer (time,reward,ect)
     
  5. Offline

    MrDplugins

    @Zombie_Striker Okay I know this is like the worst thing to ask but, can you spoon feed me the code? I'm just having trouble understanding how to make multiple arguments.
     
  6. Offline

    Caedus

  7. Offline

    BobTheHamster9

    As Zombie Striker said you would check if the length is equivalent to 5 as you have 5 "arguments"
    Name, Time, Reward, Level, Cops
    Indexes start at 0
    So if you wanted to set a String name to the first argument "name"
    Code:
    String name = args[0];
    If you wanted to set the time to an int
    Code:
     int time = Integer.parseInt(args[1]);
     
    Last edited: Jan 24, 2016
  8. Offline

    jimisdam

    @MrDplugins
    Actually, when we execute args[number], we are talking about Computer Numbers,(Starting from 0),
    But with args.length is the Human Numbers (starting from 1)
     
Thread Status:
Not open for further replies.

Share This Page