Command Executing, the proper way?

Discussion in 'Plugin Development' started by bdubz4552, May 3, 2014.

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

    bdubz4552

    For the longest time, I had a single command executor that would take the given command label, and based on that, call a method contained in a different class, via static methods (eww). As I have become more fluent in Java, I have realized that this is not really the best practice. So, my question is, for all eleven commands I have, should I give them all their own executors, or have one executor? If only one, how would I go about calling code, other than static methods, or is there no other way? I tried earlier today to call new instances of classes that contained the executing code:
    Code:java
    1. if (commandLabel.equalsIgnoreCase("somecommand")) {
    2. new SomeCommand(arguments, suchas, player, etc);
    3. }

    but this only created nullPointerExceptions with every attempt to run a command:
    Yes, it says 1.7.2-R0.3 in the stack trace, but this is a 1.7.9 dev build:
    Edit: Looking back at it, the line that the stack trace says is causing errors references the message output system. The class that is created here extends from a base class that contains this reference. Could that alone be the issue?

    From the plugin tutorials I have seen, I have only seen executors with all the executing code contained inside, and with the volume of executing code I have, this seems like an organizational nightmare. All opinions are welcome. :)
     
  2. Offline

    oaschi

    bdubz4552
    If your plugin is not too minimalistic, I would recommend creating a new command executor class for each command.
    When registering your commands in your plugin class, you can hand over the plugin itself, like this:
    Code:java
    1. getCommand("myCommand").setExecutor(new MyCommandExecutor(this));


    Now there is no need to use static methods to access your plugin anymore.
    Just save the plugin handed over in an instance variable in your command executor.

    I don't know if this is best practice, but it seems good to me.
     
Thread Status:
Not open for further replies.

Share This Page