How to give optionals args in commands?

Discussion in 'Plugin Development' started by Smex, Oct 11, 2011.

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

    Smex

    Let's say, in a plugin, which has a command like /showme <playername>.
    How would I define this <playername> in the method/command whatever.
    This is a little bit of code, how would I implement this?:

    Code:
                    if(args[0].equals("n")) {
                        if (sender.hasPermission("whomode.use")){
    
                        //stuff
    
                        return true;
      
                        }else{
                            sender.sendMessage(red + "Wannabe admin?");
                        }
                }
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    You would define this in the OnCommand method of your command Executor...
     
  3. Offline

    Smex

    Eh and how would I do this?
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    You would go into the existing onCommand method that handles showme and past the above code, above what is currently in the onCommand. Then put in a return statement at the end of each code brach. Return true if it was correct, false and it provides the usage in the yaml. This way if it has an argument it will run what is in the if statement. else it runs what the command originally was.
     
  5. Offline

    Smex

    I think you are not getting what I want, correct me if Im wrong:
    I want in my plugin to store the <playername> argument from the command /wm n <playername> into a string( the string already exists and is called wmplayername),
    than reference that string for something in my database..here's my code:
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    
            if(cmd.getName().equalsIgnoreCase("wm")) {
    
                if(args.length>0){
    
                    //wm log -> displays every player gamemode
                    if(args[0].equalsIgnoreCase("log")) {
                    if (sender.hasPermission("whomode.use")){
    
                    for( String index: WhoMode.database.getIndices().keySet()) {
                        WhoMode.ent = WhoMode.database.getArguments(index);
                        sender.sendMessage(red + WhoMode.ent.toString().toLowerCase());
                        }
    
                    return true;
    
                    }else{
                        sender.sendMessage(red + "Wannabe admin?");
                    }
                }
    
                    //wm n <name> -> displays the player from <name> + gamemode
                    if(args[0].equalsIgnoreCase("n")) {    <--- Here comes the <playername> thing in the game
                        if (sender.hasPermission("whomode.use")){
    
                        // HERE COMES MY METHOD FOR THE DATABASE
    
                        return true;
    
                        }else{
                            sender.sendMessage(red + "Wannabe admin?");
                        }
                }
    
            }else{
                sender.sendMessage(red + "Use /wm log");
            }
            }
            return false;
        }
     
  6. if (args.length >= 2) {
    wmplayername = args[1];
    }
     
  7. Offline

    Smex

    I finally understand the args and stuff, yes!
    thank you guys for helping.
     
Thread Status:
Not open for further replies.

Share This Page