Skip to content

Latest commit

 

History

History
112 lines (83 loc) · 3.62 KB

README.md

File metadata and controls

112 lines (83 loc) · 3.62 KB

JModules

Current Version Maven Central Build Status Total Downloads Issues License

A simple module framework for Java

Introduction

JModules is a simple Java framework that makes creating additional modules easy. All you have to do is implement a class and let JModules do the rest.

How to use JModules

Using JModules in your project is as easy as 1, 2, 3.

  1. Implement the Module class
  2. Create a ModuleManager
  3. Add some packages

Then you can access, enable or disable modules to your hearts content!

Implementing the Module class

Each module is represented by a simple interface, meaning implementing it is super simple.

public class MyModule implements Module {
    
    @Override
    public void onEnable() {
        // do some enabling
    }
    
    @Override
    public void onDisable() {
        // do some disabling
    }
}

You can also define a custom name for a module and define other modules that this module depends on.

public class MyAdvancedModule implements Module {
    
    @Override
    public void onEnable() {
        // do some enabling
    }
    
    @Override
    public void onDisable() {
        // do some disabling
    }
    
    @Override
    public String getName() {
        return "CustomName";
    }
       
    @Override
    public String[] getDependencies() {
        return new String[]{"MyModule"};
    }
}

Creating a ModuleManager

The ModuleManager class is the main class used in JModules. Firstly, you need to create your own instance of the ModuleManager like so:

ModuleManager myManager = new ModuleManager();

You can also add any amount of runnables to be executed whenever a module is added. This can be used to store each module or register them in some other external system.

Each ModuleInterfaceRunnable is added to the ModuleManager during instantiation.

ModuleManager myManager = new ModuleManager(new ModuleInterfaceRunnable(MyInterface.class) {
    
    @Override
    public void run(Module module) {
        // do some more things with the module
    }
});

Adding the modules

JModules works by searching a package and adding any Modules found to the ModuleManager. This is done by supplying the ModuleManager with a package to search, a ClassLoader to search through. You can also ask the ModuleManager to ignore a set of classes if you like.

myManager.addModulesFromPackage("com.mypackage", System.classLoader(), IgnoreMe.class);

You can also add modules by yourself using the addModule method.

Finishing up

Once you are done you can enable and disable modules using the methods supplied in the ModuleManager.

Final bits

Thanks for using JModules! I originally created this because I am lazy and couldn't be bothered to implement something like this for each project I would use it in, but I decided to upload it to the world in the hopes someone else would find it useful!

If you need any help or have any issues or suggestions, please let me know!