How to make a neat command executor?

Discussion in 'Plugin Development' started by LucasEmanuel, May 12, 2012.

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

    LucasEmanuel

  2. Offline

    ItsHarry

    You could make methods for everything, although I don't think it's that ugly.
     
  3. Offline

    LucasEmanuel

    Thats a good idea :)
     
  4. Offline

    ItsHarry

    :p
     
  5. Offline

    Crast

    I designed a factory-style pattern for handling sub-commands of a single command which encapsulates a number of things such as permission checking, auto-generated list of the sub commands, checking for minimum quantity of arguments, and the like. Also, the subcommand lookup is via a hash so it's O(1).

    It lets me do something like:
    PHP:
        addSub("allow""mondochest.use")
                .
    setMinArgs(1)
                .
    setUsage("<player>")
                .
    setDescription("Allow users to access a MondoChest")
                .
    setHandler(new SubHandler() {
                        public 
    void handle(CallInfo callthrows MondoMessage {
                                
    listener.allowAccess(callcall.getArg(1));
                        }
                });
    Here's part of where I define the commands here:
    https://bitbucket.org/crast/mondoch...crast/mondochest/command/Executor.java#cl-114

    And the implementation of the subcommand factory/class here:

    https://bitbucket.org/crast/mondoch...c/us/crast/mondochest/command/SubCommand.java

    The output of the auto-generated help looks like this:
    [​IMG]


    I thought about splitting this out into its own library, but I've been too busy to do so; however the source is open, you're free to steal it.

    As for tips for your own implementation, even if you don't implement a command registry type system, you could split some of the implementations out into their own methods and even into other classes with some thinking.
     
Thread Status:
Not open for further replies.

Share This Page