Arguments problem with my commands

Discussion in 'Plugin Development' started by Jonni, Apr 9, 2011.

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

    Jonni

    Hey everybody.

    I got some trouble with my commands.
    Since last time, I don't exactly know, but today it really started annoying me.
    I have this Error message when trying to use a command, but the arguments are not exactly, what is needed:
    In the plugin.yml my commands are listed like the following:
    Code:
    commands:
      thecommand:
        description: mydescription
        usage: /<command> <args[1]> //here i use for every command <command> as the first argument and the seccond or so is named otherwise (not args[1])
    Well. I will now post an example of my /kill command...
    When i use it with the right arguments, the command works, but when i only type "/kill" i get the Error message i posted above.

    Code:
            if (cmd.getName().toLowerCase().equals("kill"))
            {
                Player target = this.getServer().getPlayer(args[0]);
                
                if (p.isOp() == false)
                {
                    p.sendMessage("Du musst Admin sein um den Befehl zu nutzen!");
                    return true;
                }
                else
                {
                    if (checkPlayer(target) == false)
                    {
                        p.sendMessage("Der Spieler existiert nicht!");
                        return true;
                    }
                    else
                    {
                        target.setHealth(0);
                        p.sendMessage("Du hast " + target.getName() + " gekillt!");
                        target.sendMessage(p.getName() + " hat dich gekillt!");
                    }
                }
                
                return true;
            }
    I really hope that you can help me :S

    BTW: i'm german, so the /kill command is documented in german.

    Greets
     
  2. Offline

    Sammy

    Do this:
    Code:
    if (cmd.getName().toLowerCase().equals("kill") && args.length>0)
    
    Now if you use /kill nothing happens

    The problem was, even without args[0] you were doing the:
    Code:
     Player target = this.getServer().getPlayer(args[0]);
    
     
  3. Offline

    Jonni

    Okay thanks :)
    Now: is it possible to show the usage when a command isnt used the right way?

    Example:
    /kill
    -> Usage: /kill <name>

    And this without coding it into every single Command but with the plugin.yml
     
  4. Offline

    askmeaboutlo0m

    You could check if either args[0] equals an empty string ("") or check if the array length of args[] is greater than 0, but you have to see which one works.
     
  5. Offline

    Jonni

    But its still the same thing. The command will just be skipped but there is no message with the description or something like that...
     
  6. Offline

    Sammy

    Sure you can, just do an if like this one:

    Code:
    if (args.length <=0){
    return false;
    }
    
     
  7. Offline

    Jonni

    Okay thanks mate.
    Works for almost all my commands.
    But i think the remaining errors (same Errormessage) are depending on something else.
     
Thread Status:
Not open for further replies.

Share This Page