Resource Create module system

Discussion in 'Spigot Plugin Development' started by WailRoth, Jul 12, 2021.

  1. Hello, now i will present to you HOW TO CREATE MODULE SYSTEM. Is very useful, for Core plguin ore things like that.
    Note: this code was made in 2019, so you can upgrade lot of thing in !

    Now, let's go !

    First, you will create 2 Maps: for commands and listeners

    Code (Java):
    public Map<Module, Command> commandList = new HashMap<>();
        public Map<Module, Listener> listenerList = new HashMap<>();

    Next, create Module abstract class, for implements it to next class, don't worry about "Getter" and "Setter" annotations, it's lombok, it replace physical getter and setter.

    Register command with paramters will register your command, same for listener.
    Wale is my main class !!!
    Code (Java):
    public abstract class Module {


        @Getter
        @Setter
        protected String name;
        @Getter
        protected boolean isEnabled;
        @Getter
        @Setter
        protected String description;
        protected String modulePrefix;


        public abstract void registerCommands();

        public abstract void registerListeners();

        public abstract void registerOthers();


        public void registerCommand(Command executor, String name) {
            System.out.println(Texts.message(ConsoleColors.GREEN + " [COMMANDS] " + executor.getName() + " was registered"));
            ((CraftServer) Wale.getInstance().getServer()).getCommandMap().register(name, executor);
            Wale.getInstance().getCommandList().put(this, executor);
        }

        public void registerListener(Listener listener) {
            PluginManager pm = Wale.getInstance().getServer().getPluginManager();
            Wale.getInstance().getListenerList().put(this, listener);
            pm.registerEvents(listener, Wale.getInstance());
            System.out.println(Texts.message(ConsoleColors.GREEN + " [LISTENERS] " + listener.getClass().getSimpleName() + " was registered"));
        }


    }

    Next, create ModuleManager, to manage all modules.
    Close don't work in this code, you can easily update it.
    And please, don't use Sysout. Instead of use Bukkit.getLogger();

    We create List for store all modules, to register them and remove them from list.
    I store all my modules in yml file, and i retrieve them from file.


    Code (Java):
    public class ModuleManager {


        protected List<Module> moduleList = new ArrayList<>();


        public List<Module> getModuleList() {
            return moduleList;
        }

        public void registerModule(Module module) {
            moduleList.add(module);
        }

        public void removeModule(Module module) {
            moduleList.remove(module);
        }

        public void loadModules() {
            for (Module module : Wale.getInstance().getModuleManager().getModuleList()) {

                module.isEnabled = Wale.getInstance().getModuleFile().getBoolean(module.getName());

                if (!module.isEnabled) {
                    System.out.println(Texts.message(ConsoleColors.RED_BOLD + " [MODULES] " + module.getName() + " was not registered"));
                } else {
                    System.out.println(Texts.message(ConsoleColors.GREEN_BOLD + " [MODULES] " + module.getName() + " was registered"));
                    module.registerCommands();
                    module.registerListeners();
                    module.registerOthers();
                }


            }
        }


        public void reloadModule(Module module) throws IllegalAccessException, InstantiationException, IOException {
            getModuleList().remove(module);
            close(module);
            registerModule(module.getClass().newInstance());
            System.out.println(module.getName() + " was reloaded. It is: " + module.isEnabled);
        }


        public Module getByName(String module) {
            Module moduleSelected = null;
            for (Module mods : getModuleList()) {
                if (mods.getName().equals(module)) {
                    moduleSelected = mods;
                    return moduleSelected;
                }
            }
            throw new NullPointerException("Module cannot be found");
        }

        public void close(Module module) {
            SimpleCommandMap simpleCommandMap = new SimpleCommandMap(Wale.getInstance().getServer());
            simpleCommandMap.getCommands().remove(Wale.getInstance().getCommandList().get(module));
            System.out.println("Commands of " + module.getName() + " were unregistred");
            Wale.getInstance().getCommandList().remove(module);

            HandlerList handlerList = new HandlerList();
            handlerList.unregister(Wale.getInstance().getListenerList().get(module));
            Wale.getInstance().getListenerList().remove(module);
        }


    }
     

    Next, we will register modules in our main class
    You can see lot of modules, don't worry about that.

    Code (Java):
        public void registerModules(){
    ModuleManager moduleManager = new ModuleManager()
            moduleManager.registerModule(new TopluckModule(this));
            moduleManager.registerModule(new PacketsModule(this));
            moduleManager.registerModule(new WebModule());

            moduleManager.loadModules();

        }

    Now, we will create our module. Create class like MyModule and follow :
    We extends our class by "Module" abstract class, we give it a name, a description...
    Code (Java):
    public class WebModule extends Module {


        public WebModule() {
            this.name = "Web";
            this.description = "Web side for wale.";
            this.modulePrefix = "{Web}";
        }

        @Override
        public void registerCommands() {

        }

        @Override
        public void registerListeners() {

        }

        @Override
        public void registerOthers() {
            createWebFolder();
            WebServer server = new WebServer();
            server.createServer();
        }

        public void createWebFolder() {
            File file = new File(Wale.getInstance().getDataFolder() + "/web/");
            if (!file.exists()) {
                file.mkdirs();
            }
        }


    }

    Done, your module is good !

    I hope this helped, sorry for that bad english, i'm french. All this code is not perfect, but it can be very useful :p
     
    • Like Like x 2
  2. Very interesting!
     
    • Like Like x 1