Standards for onCommand

Discussion in 'Plugin Development' started by Ne0nx3r0, Dec 20, 2012.

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

    Ne0nx3r0

    A lot of times onCommand's for plugins tend to get very crowded with if statements for various sub-commands, and I was wondering, how do others deal with this?
     
  2. Offline

    zeeveener

    I dealt with it by creating classes solely for each family of commands. I had about 4 classes. Each was a CommandExecutor. In onEnable, I just spread out the commands manually.

    Then, within each class, the onCommand was simply a delegator. It caught the command, decided which one it was, then continued on into a separate method within that class. This way, you can localize the code for each command and you know, a little more specifically, what causes any errors.

    For the methods, I had the parameters as, CommandSender sender, String[] args. I handled the player-getting and args parsing within the individual methods.
     
  3. Offline

    EnvisionRed

    I create a different method for each subcommand and call that from the onCommand.
     
  4. Offline

    Ne0nx3r0

    I'm curious, what do you guys think of this?

    Code:
            if(args.length == 0)
            {
                return this.usage(cs,args);
            }
            else
            {
                try {
                    return (Boolean) Class.forName("SomeCommandExecutor")
                            .getMethod(args[0].toLowerCase()).invoke(this,cs,args);
                }
                catch(Exception e)
                {
                    return false;
                }
            }
    
    Then you would just create methods named after the first arg.

    I suppose it lacks in support of first parameter aliases, but it seemed like an idea worth trying out.

    *edit* fixing code
     
    fireblast709 likes this.
  5. Offline

    EnvisionRed

    Just make sure to put the arg into lowercase first, and make all your methods's names lowercase.
     
  6. Offline

    Ne0nx3r0

    Oh good point, heh.
     
  7. Offline

    fireblast709

    Mmh nice, short, and functional. Me likes
     
  8. Offline

    Lolmewn

    If's everywhere :D
     
  9. Offline

    Ne0nx3r0

    [​IMG]

    Practically speaking I don't know if there's much of a difference, the If's are probably even faster... But after trying this method out, I like the organization of it.
     
    ZeusAllMighty11 likes this.
  10. Offline

    Ranzdo

  11. Offline

    fireblast709

    Ranzdo I don't see the point of using that many lines while you can do in way less lines, using less RAM and taking (probably) less time than your excessive method
     
Thread Status:
Not open for further replies.

Share This Page