Register new command listener for subcommands?

Discussion in 'Plugin Development' started by EnvisionRed, Jul 7, 2012.

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

    EnvisionRed

    I'd like to know if it's possible to, say, register a commandExecutor for the command "/testcommand test" and then a different one for "/testcommand foo" and then yet another one for "/testcommand bar"

    It's purely for convenience in my plugin, so not hugely necessary. If anyone knows how to do it, please post it here.
     
  2. you need to check the arguments for that task
     
  3. Offline

    DocRedstone

    you could do something like this

    PHP:
    if (cmdLabel.equalsIgnoreCase("testcommand")) {
    if(
    args[0].equalsIgnoreCase("test")) {
    Bukkit.broadcastMessage("This is a test.");
    } else if (
    args[0].equalsIgnoreCase("foo")) {
    Bukkit.broadcastMessage("This is a foo.");
    }
    }
     
  4. Offline

    EnvisionRed

    Sorry, I don't think you understood the question, DocRedstone. I wanted to register a CommandExecutor for those. I'm not sure what led you to think I didn't know what an if statement is.
     
  5. Offline

    DocRedstone


    Nah I though you were trying to figure out how to do a sub command under a primary command. That's the method that I use.
     
  6. Offline

    EnvisionRed

    I can do that, but it still gets hella messy if there's a lot of subcommands.
     
  7. You can't use a CommandExecutor for sub-commands but you can use diferent methods...

    Quick example:
    Code:
    if(label.equalsIgnoreCase("testcommand"))
    {
        if(args.length >= 1)
        {
            if(args[0].equalsIgnoreCase("foo"))
                return commandFoo(sender, args);
            else if(args[0].equalsIgnoreCase("bar")
                return commandBar(sender, args);
        }
    
        return true;
    }
    
    // ...
    
    private boolean commandFoo(CommandSender sender, String[] args)
    {
         if(args.length >= 2)
         {
             // arg[1] is the 2nd argument in /testcommand foo arg2
         }
         else
             sender.sendMessage("Not enough arguments");
    
         return true; // for the command executor
    }
    You could even make an interface for your subcommands and call them from diferent classes if you want :}
     
    ferrybig and EnvisionRed like this.
  8. Offline

    Sagacious_Zed Bukkit Docs

    Digi
    You can actually reuse the CommandExecutor interface for that purpose.
     
  9. Offline

    EnvisionRed

    Theoretically, I could do something like
    Code:
    if cmd.getName().equalsIgnoreCase("someCommand"){
        if (args[0].equalsIgnoreCase("someArg"){
            cmd.setExecutor(someCommandExecutor);
        }
    }
    right?
     
  10. Offline

    Kaikz

    Feel free to decompile my InstaServer. I think my subcommand setup works rather well.
    I'll put the source up soon, I just don't have my Git login, lol.
     
  11. Offline

    EnvisionRed

    Meh, I'll piece something together. I don't feel like getting a decompiler, then looking through all your classes unless I really can't get my code working.
     
  12. Offline

    Kaikz

  13. Offline

    deltahat

    Not quite. When your plugin starts up, you register your CommandExecutor implementations.
    Code:
    public void onEnable() {
        getCommand("summon").setExecutor(new SummonAnimalExecutor());
    }
    
    In your CommandExecutor's onCommand implementation you use the String[] args parameter to determine what arguments the user passed in. Don't change the executor here or it will cause conflicts if two players use the same command at the same time. Instead, do what Digi says and call a different method depending on what parameters are passed in.
    Code:
    public class SummonAnimalExecutor {
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (args.length == 0) {
            printUsage(sender);
        } else if (args[0] == "creeper") {
            summonCreeper(sender);
        } else if (args[0] == "chicken") {
            summonChicken(sender);
        } else if (args[0] == "zombie") {
            summonZombie(sender);
        } 
    }
    }
    
    Finally, don't forget to register your command in the Plugin.yml file where you can control your help text. You can list the sub-commands in Plugin.yml with additional help text for each sub-command.
    Code:
    commands:
      summon:
        description: Summons monsters.
        usage: <command> creeper|chicken|zombie
      summon creeper:
        description: Summons a creeper.
      summon chicken:
        description: Summons a chicken.
      summon zombie:
        description: Summons a zombie.
    
     
Thread Status:
Not open for further replies.

Share This Page