How to manage lots of commands

Discussion in 'Plugin Development' started by MCMastery, Aug 13, 2016.

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

    MCMastery

    Hi, for my plugin each command has its own class (there will be many, many commands). Also, I don't register them with the plugin.yml, I listen to command preprocess events and make my own auto-complete.

    Now I just have a list in a command manager class that contains instances of every command. This is really annoying, since every time I make a new command a have to go into the command manager class and add it to the list.

    If I didn't have a separate class for each command, I would just use an enum (but that would be 1000's of lines long, and hard to navigate). I was also thinking of using reflection, to find every class that implements the command interface. But that is pretty complicated...

    Does anyone have any ideas on how I could do this?
     
  2. Offline

    Zombie_Striker

    @MCMastery
    Create an interface. All of your command classes will extend this class. Inside this new class, create a static hashmap where the keys are a string (the command) and the values are the command classes. Inside each commandclass constructor, add the command to the hashmap. By doing this, you only need one line to the new class to add the command .
     
  3. Offline

    MCMastery

    But where would I call the command constructors
     
  4. Offline

    Zombie_Striker

    @MCMastery
    I don't think "call" is the word you're looking for, constructors are only called when a class if first created.

    [edit] just saw the problem with my idea. You would either have to create a new instance of the class is some other method (e.g. the onEnable) or add the following lines to the command class
    Code:
    static{
    new CommandClass();
    }
     
  5. Offline

    mythbusterma

    MCMastery likes this.
Thread Status:
Not open for further replies.

Share This Page