Skip to content

gitmylo/java-module-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java Module Library

Readme contents

Information

Java module library is a lightweight library that allows for a modular extensive design

Usage

Api

Intro

The api is a class used by both the main application and modules (plugins). This class should have direct or indirect access to anything you want to be accessible in the external modules.

Loader redirect

An example on how to create a bridge to the loader:

package com.mylo.usedlibrary;

import com.mylo.modlib.loader.SimpleLoader;
import java.util.List;

/*
        A really basic mod loader
 */
public class LoadMods {
    public static List<CustomMod> loadMods(){
        return new SimpleLoader<CustomMod>().loadModules().getLoadedMods();
    }
}

Mod base class

An example mod base class:

package com.mylo.usedlibrary;

import com.mylo.modlib.modbase.Mod;

/*
        a custom mod class using an interface,
        so you could even have this extend a
        class made by you.
 */
public abstract class CustomMod implements Mod {
    public abstract String anotherString();
}

Custom loader

To make a custom loader you start by extending "com.mylo.modlib.loader.SimpleLoader". You could simply change modRoot (the mod folder to check) and modMainLink (the mod's .mod file by default).

Main app

Intro

Begin by importing the api you made at the previous steps.

Mounting the modules

Main class loading all modules and calling functions example:

package com.mylo.ModuleExample;

import com.mylo.usedlibrary.CustomMod;
import com.mylo.usedlibrary.LoadMods;

public class Main {
  public static void main(String[] args){
    System.out.println("Starting module load");
    for (CustomMod mod: LoadMods.loadMods()){//from before, loadmods gives a list of CustomMod
      System.out.println("Loaded Module: " + mod.getName());//mod.getName is built in, used for sorting on your end
      System.out.println("Module's custom string: " + mod.anotherString());//mod.anotherString was added with the CustomMod
      System.out.println("-----------------------------------------------------------");
    }
    System.out.println("Done");
  }
}

Modules (Plugins)

Intro

All modules require the api you made at the previous steps

Custom mod ModMain

package com.mylo.exampleMod;

import com.mylo.modlib.modbase.ModMain;

import java.util.Arrays;
import java.util.List;

public class Main extends ModMain {
    @Override
    public List getModules() {
        return Arrays.asList(new TestMod());
        //this just instantiates the test mod created in the next step
    }
}

Custom mod Module classes

package com.mylo.exampleMod;

import com.mylo.usedlibrary.CustomMod;

public class TestMod extends CustomMod {
    @Override
    public String getName() {//getName is from the base mod class
        return "TestMod";
    }

    @Override
    public String anotherString() {//anotherString is from CustomMod
        return "Another String Lmao";
    }
}

To then install the module, grab the compiled .class files, put them in a folder with a .mod file in the root with the path to the mod loader class. As would be in a meta-inf/manifest.mf file, but this isn't a jar, so you simply specify it in a .mod file. if you want a version that loads mods from a jar or a zip, you're free to make a fork.

License

Licensed under MIT License.

About

A system that allows external modules to easily be loaded in during runtime

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages