Proper way to do Commands?

Discussion in 'Plugin Development' started by apexearth, Jan 28, 2011.

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

    apexearth

    Hi,

    What is the proper way to create a command in Bukkit?

    /help me! [​IMG]

    Thanks
     
  2. Offline

    Schirf

  3. Offline

    apexearth

    Thanks,

    I watched that and he does it the same way that I have chosen to. Although I thought I saw some people doing it another way and storing the info in a yml file. I've also seen a Command class in bukkit. I thought they might be implementing something so they can group all commands together to populate a bukkit help list or something along those lines...
     
  4. Offline

    Raphfrk

    You can use the yml file. However, if you do that the commands arrive in your JavaPlugin class rather than the PlayerListener class.

    Here is an example:

    This goes in your main JavaPlugin class:

    Code:
        @Override
        public boolean onCommand(Player player, Command command, String commandLabel, String[] args) {
            String[] split = args;
    
            //player.sendMessage(commandLabel + " " + args[0]);
     
            if( commandLabel.equalsIgnoreCase("event")) {
                if( split.length > 1 && split[0].equalsIgnoreCase("invite")) {
    
                    <some code>
                    return true;
    
                }
    
                if( split.length > 0 && split[0].equalsIgnoreCase("list")) {
    
                    int start = 0;
    
                    if( split.length > 1 ) {
                        <more code>
                    }
                    return true;
    
                }
            }
    
            return false;
    
        }
    
    This is the yml:

    Code:
    name: EventLink
    main: com.raphfrk.bukkit.eventlink.EventLink
    version: 0.1
    commands:
      event:
        description: Used to configure eventlink
        usage: |
          Incorrect usage of command /<command>
          /<command> invite hostname:port - attempts connection
          /<command> delete servername   - deletes connection
          /<command> list             - lists all trusted servers
          /<command> list <num>      - lists all trusted servers
    
    If you use the yml, it will give proper errors when the user doesn't enter the command correctly.
     
  5. Offline

    apexearth

    Thank you, that's exactly what I was looking for. [​IMG]
     
  6. Offline

    Schirf

    But if you use the yml, the commands are (currently) case sensitive. You can always write out the error message to the player yourself.
     
  7. Offline

    Snowl

    Or you could do
    Code:
        @Override
        public boolean onCommand(Player player, Command command, String commandLabel, String[] args) {
            String[] split = args;
    
            //player.sendMessage(commandLabel + " " + args[0]);
            if( commandLabel.equalsIgnoreCase("event")) {
                if( split.length > 1 && split[0].equalsIgnoreCase("invite")) {
    
                    <some code>
                	event.setCancelled(true);
    
                }
    
                if( split.length > 0 && split[0].equalsIgnoreCase("list")) {
    
                    int start = 0;
    
                    if( split.length > 1 ) {
                        <more code>
                    }
                	event.setCancelled(true);
    
                }
               else
               {
                  //error code here
               }
            }
        }
    
     
Thread Status:
Not open for further replies.

Share This Page