onCommand example

Discussion in 'Plugin Development' started by Lavamike, Feb 26, 2011.

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

    Lavamike

    Hi! I've just been learning most of this API as well as java in general lately and, being used to the onPlayerCommand, I was wondering if someone could provide an example of a simple command as I'm not really understanding the transition.

    I downloaded the source of a different plugin to try and figure it out but it seems like it's not working.
    From what I understand the onCommand goes into the 'public class ... extends JavaPlugin'

    So I currently have it in there like this:

    Code:
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    {
    	String[] split = args;
    	String commandName = cmd.getName().toLowerCase();
    	
    	if(sender instanceof Player)
    	{
    		System.out.println("Command cmd = " + sender);
    		System.out.println("Args = " + args);
    		Player player = (Player) sender;
    		// zone command
    		if(commandName.equals("zone"))
    		{
    			System.out.println("IT'S HERE!");
    			// zone create
    			if(split[0].equalsIgnoreCase("create"))
    			{
    				if(player.isOp())
    				{
    					System.out.println("Wow! IT WORKS!??!?");
    				}
    			}
    		}
    	return false;
    }
    

    Hope someone can help me out,
    -Mike
     
  2. Offline

    phondeux

    I do the same thing! The BigBrother source code has a nice example of using onCommand;
    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
            try {
                String[] split = args;
                String commandName = command.getName().toLowerCase();
    ... etc
     
  3. Offline

    fullwall

    Have you registered the zone command in the plugin.yml?
     
  4. Offline

    Lavamike

    Uhhh i'm not really sure how to do that, what do I have to add in plugin.yml for that?
     
  5. Offline

    darknesschaos

    to make it so that when the command is issued by the player or other source it gets sent to the appropriate plugin.
     
  6. Offline

    fullwall

    It goes like this (add this at the end of the plugin.yml)

    commands:
    zone:
    description: A description
    usage: |
    /<command> - usage
     
  7. Offline

    darknesschaos

    what is "zone" for? is it new?
     
  8. Offline

    fullwall

    if(commandName.equals("zone"))
    I based it off this line.
     
  9. Offline

    darknesschaos

    ah, I thought it was something different then.
     
  10. Offline

    Raphfrk

    You can leave all the fields blank if you wish, but they have to appear. This is the version used for ServerPort

    Code:
    name: Server Port
    main: com.raphfrk.bukkit.serverport.ServerPortBukkit
    version: 261
    commands:
      worldlist:
        description:
        usage:
      pos:
        description:
        usage:
      release:
        description:
        usage:
      getinv:
        description:
        usage:
      serverport:
        description:
        usage:
      drawgate:
        description:
        usage:
      cancelredirect:
        description:
        usage:
      regengates:
        description:
        usage:
      stopcircle:
        description:
        usage:
      circleload:
        description:
        usage:
    
    Ofc, I probably should include the fields when I am less lazy :)
    --- merged: Feb 27, 2011 1:07 PM ---
    Bukkit will only send you commands if you have registered them.
     
  11. Offline

    Lavamike

    Got it working! Big thanks to all you guys =]
     
Thread Status:
Not open for further replies.

Share This Page