Skip to content

Creating your first module

codestech edited this page Jul 12, 2026 · 1 revision

To create a Module all you need to do is to create a class extending the Module class, as in the following example.

import net.hypejet.modules.Module;

public final class FirstModule extends Module<ExampleEnvironment> {
    @Override
    protected void load() {
        // Called when the module gets loaded, not required
    }
    
    @Override
    protected void unload() {
        // Called when the module gets unloaded, not required
    }
}

Caution

You should NEVER call Module's load and unload methods yourself. Modules can be loaded and unloaded only via the module manager, which loads the dependencies first, and then injects itself into the module, just before the module gets loaded, so that your modules can access the module manager and its environment while they're loaded.

You don't need to override the load and unload methods, but in most cases, it is the proper thing to do, because we expect that you fully unload all components of the module.

Getting the module manager

If you wish to access the module manager that loaded the module, you can do so by calling the getModuleManager() method using the module instance. Note that it is safe only as long as the module is loaded, so from the moment the load method starts its execution, to the moment the unload method stops executing.

However, you should be safe in most cases, as we expect that you don't use module instances that aren't loaded. If you're not sure whether the module is loaded, you can use the Module#isLoaded method - it is safe to be called at any time.

An example of module accessing the module manager:

import net.hypejet.modules.Module;

public final class FirstModule extends Module<ExampleEnvironment> {
    @Override
    protected void load() {
        ModuleManager<ExampleEnvironment> moduleManager = this.getModuleManager();
        // You can use the module manager here
    }
    
    @Override
    protected void unload() {
        // You can safely get the module manager here as well, but not after execution of the unload method finishes
    }
}

Environment

As you might have noticed, you need to specify an environment type. It represents what environment your module runs on and can be whatever you want, as long as you can provide an instance of it. This allows you to register and unregister components specific to your environment.

For example, if you run a Minecraft server using Paper, and you want to divide your plugin into modules, you might want to make your own environment type, which provides a way to register listeners, commands, etc. You can even pass JavaPlugin as an environment.

If you have such kind of environment (let's call it PaperEnvironment), you can access it in the following way:

import net.hypejet.modules.Module;

public final class FirstModule extends Module<PaperEnvironment> {
    @Override
    protected void load() {
        PaperEnvironment environment = this.getModuleManager().getEnvironment();
        environment.registerListener(...);
        environment.registerCommand(...);
    }
    
    @Override
    protected void unload() {
        PaperEnvironment environment = this.getEnvironment(); // This is equal to getting the environment from the module manager
        environment.doSomething();
    }
}

You need to provide the environment instance while creating the module manager, which stores the provided instance. After you provide the environment, it cannot be changed. Every module loaded by certain module manager has access to the same module manager, which means that the same environment instance is returned for each module.

It's worth pointing out that the specified environment type doesn't need to be exactly the same as the one specified in module manager. Let's say you have a MinecraftEnvironment, which represents any Minecraft-related environment, and two implementations: PaperEnvironment and VelocityEnvironment. If you want your module to run on any kind of Minecraft-related environment, you can specify MinecraftEnvironment as the environment type in your module class. You'll be able to register that module on both PaperEnvironment and VelocityEnvironment.

Clone this wiki locally