Skip to content

Loading modules

codestech edited this page Jul 12, 2026 · 4 revisions

After you've created your first module, you're probably wondering how to load it. For that, you need a module manager.

Module manager is the heart of the module system. It's responsible for loading and unloading registered modules, after defining their load order.

Module registry

To create a module manager, you firstly must use ModuleRegistry. It's a definition of modules to be loaded by a single module manager. It also resolves dependencies early. Creating a module registry is as simple as writing the following line.

ModuleRegistry<ExampleEnvironment> registry = new ModuleRegistry<>();

Note that you have to specify an environment type here. It's the type of environment you're going to pass to the module manager.

Registering modules

Alright, we created the module registry. Now how do we register modules? It's also pretty easy:

registry.register(ExampleModule.class, ExampleModule::new); // Works only for modules with empty constructors
registry.register(AnotherModule.class, () -> new AnotherModule());
registry.register(ThirdModule.class); // Works only for modules with empty constructors

You can also use the module registry as a builder:

registry.register(ExampleModule.class, ExampleModule::new)
    .register(AnotherModule.class, () -> new AnotherModule())
    .register(ThirdModule.class);

Important

The class you specify in the register method must be exactly the same as the class of the returned module instance. You cannot return a module which is a subclass of the specified class. Otherwise, loading your module will fail.

As previously said, all of these modules are going to be loaded by the same module manager.

Creating the module manager

Now let's say you have registered all the modules you want, so you finally can create the module manager.

ModuleManager<ExampleEnvironment> moduleManager = registry.createModuleManager(new ExampleEnvironment());

As you can see, you need to provide an environment instance to be used by the module manager. It doesn't have to be of the same type as the one specified in the registry, but has to implement it. This is the same environment your modules will use. For more information on environments, check out the previous page.

Important

Creating the module manager can throw an exception if circular module loading was detected. Read this page for more information.

Caution

All calls to the module manager will throw an exception until it gets loaded. The same applies if the module manager is unloaded.

Loading the module manager

You managed to create the module manager, but now you wonder how to finally load the modules. All you need to do is to call the ModuleManager#load method.

manager.load();

It loads all the modules that were successfully registered in the module registry. If any module fails to load, don't worry, it'll be skipped. However, if there are any modules that require the module that failed to load, all of the dependants will be skipped as well. Of course, if anything fails, the module manager is going to print detailed errors to the console.

Unloading the module manager

To unload your modules, simply use the unload method, which is fail-safe as well.

manager.unload();

Now all your modules are unloaded.

Clone this wiki locally