Creating a CommandExecutor class?

Discussion in 'Plugin Development' started by kreashenz, Mar 21, 2013.

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

    kreashenz

    How would I start a class to hold all the executors for commands, and make it a command executor, so I don't need to make it all in the onEnable of the main class. Any help, or examples of CommandExecutors that people can show me?
     
  2. Offline

    DatCookiez

    I only know of sepearate ones, this would be interesting to find out!
     
  3. Offline

    kreashenz

    realiez Try reading the edited version of my post, I kinda screwed up what I meant in it./
     
  4. Offline

    stuntguy3000

    From the wiki:

    Using a separate CommandExecutor class

    The examples above just put the onCommand() method into the plugin's main class. For small plugins, this is fine, but if you're writing something more extensive, it may make sense to put your onCommand() method into its own class. Fortunately, this isn't too hard:
    • Create a new class within your plugin's package. Call it something like MyPluginCommandExecutor (although of course replacing MyPlugin with your plugin's actual name). That class must implement the Bukkit CommandExecutor interface.
    • In your plugin's onEnable() method, you need to create an instance of your new command executor class, and then make a call like getCommand("basic").setExecutor(myExecutor);, where "basic" is the command we want to handle, and myExecutor is the instance we created.
    Best explained by example:
    MyPlugin.java (the main plugin class):
    @Override
    public void onEnable() {
    // ...

    // This will throw a NullPointException if you don't have the command defined in your plugin.yml file!
    getCommand("basic").setExecutor(new MyPluginCommandExecutor(this));

    // ...
    }
    MyPluginCommandExecutor.java:
    public class MyPluginCommandExecutor implements CommandExecutor {

    private MyPlugin plugin; // pointer to your main class, unrequired if you don't need methods from the main class

    public MyPluginCommandExecutor(MyPlugin plugin) {
    this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    // implementation exactly as before...
    }
    }
    Notice how we pass an instance of the main plugin to the constructor for MyPluginCommandExecutor. This allows us easy access to the main plugin's methods if we need to.
    By doing this, we can better organise our code - if the main onCommand() method is large and complex, it can be split into submethods without cluttering up the plugin's main class.
    Note that if your plugin has multiple commands, you will need set the command executor for each command individually.
     
  5. Offline

    DatCookiez

    What he said xD
     
  6. Offline

    stuntguy3000

    realiez please do not spam the forums with useless posts...
     
  7. Offline

    kreashenz

    stuntguy3000 , realiez , That's not what I wanted, I wanted a whole new class, just for getCommand("command").setExecutor(new command()); and no booleans in it.
     
  8. Offline

    stuntguy3000

    What do you mean?
     
  9. Offline

    Burnett1

    If im correct (probably not) you need to make a new class and make a new method with an appropriate name. Put the code from your onEnable that you want in the other class in the method (maybe needs tweaked) and then in your onEnable call upon that method.

    stuntguy3000
    He means when you register the commands
    He wants all that in another class.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  10. Offline

    kreashenz

    Burnett1 That's what I mean, can you gimme an example of what the method would be ?
     
  11. I wouldn't suggest you to write an additional class only to register your commands. If you want to cleanup your onEnable method, you can extract all the registration commands to a separate method like registerCommands in your plugin class and call that method from onEnable. You should define the new method as private. You can do that very easily by marking all the register lines and use the refactoring tools of your ide (netbeans or eclipse, doesn't matter). The refactoring operation is named "extract method". Your ide will do the rest.
     
  12. Offline

    kreashenz

    hapm So just make a simple little thing like :
    Code:
    onEnable() {
    // all the other crap
    a;
    }
    private void a() {
    // register commands and shiz here
    } 
    ?
    [OffTopic] Please, get on Skype, and Saros, I have a job to do with Economy stuff, and its not working for some gay reason ..( Also 2 more commands using economy stuff).
     
  13. Offline

    AmShaegar

    If I got it right, try this code I am using in my polugin:
    Code:
    PluginMain:
        @Override
        public void onEnable() {
            ve = new VoteExecutor(this);
        }
     
    VoteExecutor:
        public VoteExecutor(JavaPlugin plugin) {
            plugin.getCommand("vote").setExecutor(this);
            plugin.getCommand("yes").setExecutor(this);
            plugin.getCommand("no").setExecutor(this);
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
     
        }
     
  14. Offline

    kreashenz

    AmShaegar Example please ? This seems easier than making a whole new class..
     
  15. Offline

    AmShaegar

    This IS the example :confused: VoteExecutor is a new CommandExecutor class but it's all in one. Make sure to add extends CommandExecutor.
     
  16. Offline

    kreashenz

    AmShaegar OOHH, that's what you meant.. Lol. Ok.. But I get an error over getCommand, is there anything else I need to add to the new class?
     
  17. kreashenz
    It would be a(); in onEnable but the rest is correct. I prefer this solution over letting the CommandExecutor register it self as you will need to create an instance of it in your onEnable anyway, so you don't cleanup but scatter it over multiple methods. BTW giving this to other objects as long as the constructor isn't finished yet, can get very ugly.

    [OffTopic] can't get to saros atm as I'm at work.
     
Thread Status:
Not open for further replies.

Share This Page