Again, question about arguments.

Discussion in 'Plugin Development' started by ZeusAllMighty11, Jul 13, 2012.

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

    ZeusAllMighty11 Retired Staff

    I need some help.

    I want to have a basic command like : /monkey add <player>

    I know that /monkey gets registered as the command. All others are args of it.
    So add would be args[0] would be add, and args[1] would be player?

    Confused, please help.
     
  2. Offline

    Eballer48


    Code:
     
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
     {
       if (!(sender instanceof Player)) {
         HcMain.getInstance();
    HcMain.log.info("[HungerCraft] This command can only be accessed by players!");
         return true;
       }
       Player p = (Player)sender;
       if (cmd.getName().equalsIgnoreCase("start")) {
         if ((p.hasPermission("admin.start")) || (p.hasPermission("admin.*"))) {
           if (HcMain.getInstance().DENY_LOGIN)
             HcChat.playerChat(p, "The game has already begun!");
           else
             HcMain.getInstance().startGame();
         }
         else {
           HcChat.playerChat(p, "You are not allowed to do this.");
         }
         return true;
       }
     
        if (cmd.getName().equalsIgnoreCase("kitinfo")) {
            if (args.length != 1) {
              HcChat.playerChat(p, "Must include a kit name! (/kitinfo [kitName])");
              return true;
            }
            HcChat.kitInfo(p, args[0]);
            return true;
          }
     
          if (cmd.getName().equalsIgnoreCase("kit")) {
            if (HcMain.getInstance().DENY_LOGIN) {
              if (args.length != 1) {
                HcChat.kitChat(p);
                return true;
              }
              HcChat.playerChat(p, "The game has already started!");
              return true;
            }
            if (args.length != 1) {
           HcChat.kitChat(p);
              return true;
            }
            HcKit.setKit(p, args[0]);
            return true;
          }
     
    

    here is a command I have, maybe you can get what you need from it?
     
  3. Offline

    Pandemoneus

    Yes, it is as you think.
    You actually might have gotten the answer on your own by printing out what the args are. :)
     
    ZeusAllMighty11 likes this.
  4. Offline

    ZeusAllMighty11 Retired Staff

    Thanks, but doesn't really help me. I need an example with the random command I provided for my own reasons. (No, I did not make a /monkey command, I just will understand more if you use that)
     
  5. Offline

    EnvisionRed

    Yep, arguments is an array of strings of everything that comes after the command.

    The index of any array in java is 0, so the first args[0], second is args[1].
    Please note that trying to use an argument that is nonexistant (per se if there are only 2 arguments and you try to fetch args[2]) will yield an ArrayIndexOutOfBounds Exception, so always check the length of the arguments.

    So for the command /money add player, args[0] = "add", and args[1] = "player".

    I hope this helped
     
    ZeusAllMighty11 likes this.
  6. Offline

    ZeusAllMighty11 Retired Staff

    kthanks mate.

    How would I do /something add+remove <player>
    EnvisionRed

    I want a command to add a player to a list, or remove. So the command would be:
    /police add <player>
    AND
    /police remove <player>

    Adding them adds them to a list( which I already have defined)
    removing them removes them from the lsit

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

    EnvisionRed

    Simple:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if (args.length < 2){
    sender.sendMessage("Proper usage: /police <add/remove> <player>");
    return true;
    }
    if (args[0].equalsIgnoreCase("add") || args[0].equalsIgnoreCase("remove");){
    if (args[0].equalsIgnoreCase("add"){
    //add args[1] (the player's name) to the list, check if he is there first also. I don't feel like typing the code out for it
    return true;
    }else{
    //remove args[1] (the player's name) from the list, check if he is there first. I still don't feel like typing it out.
    return true;
    }else{
    sender.sendMessage("Proper usage: /police <add/remove> <player>");
    return true;
    }
    }
     
    ZeusAllMighty11 likes this.
Thread Status:
Not open for further replies.

Share This Page