onCommand does not work

Discussion in 'Plugin Development' started by Zero9195, May 16, 2011.

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

    Zero9195

    Hi Guys, I have a small problem. I want to trigger something when someone writes "/cw". But everytime I try it out is says the command doesn't exist, anyone knows why?
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
         {
             if(sender instanceof Player)
             {
                 Player player = (Player) sender;
                 player.sendMessage("hi");
                 String command = cmd.getName();
                 String[] arguments =  null;
                 String helpMessage = "ChangeWeather is activ. Try out rain/sun.";
                 if(command.equals("cw") && args.length > 0)
                 {
                       //What to do
                 }
             }
             return true;
         }
     
  2. Offline

    lycano

    @Zero9195: you probably typed something like /Cw or /CW ..
    cmd.getName() needs to be lowercased. In addition you plugins.yml must contain this command.

    Code:
    plugin.yml
    ..
    ..
    commands:
        cw:
            description: This does something useful
            usage: |
                /<command> syntax - Desribe me
    
    ---
    
    String command = cmd.getName().toLowerCase();
    if (command.equals("cw") && args.length > 0) {
        // do something
    } else if (command.equals("cw") && !args.length) {
        // show help
    }
    
     
  3. Offline

    Daniel Heppner

    To fix the capitalization issues, use .equalsIgnoreCase().
     
  4. Offline

    Zero9195

    Thanks, didn't Know about the Plugin.yml ;)
     
Thread Status:
Not open for further replies.

Share This Page