-
Notifications
You must be signed in to change notification settings - Fork 50
Creating a Module
In this guide, you will learn how to create a module using Prison-Core.
Note: As with any project, the first step is to add your dependencies. For now, you must download the Prison-Core JAR file from the Releases page of this repository. A Maven repository is coming soon.
The first thing to do in all Java projects is to create your main class. For Prison modules, each module's main class must extend tech.mcprison.prison.modules.Module
. Once you import and extend Module, you will find that it requires a default constructor. The default constructor takes a name, a version, and a target API level for your module. You must set those within your super()
parameters. Here is an example.
package tech.mcprison.prison.demo;
import tech.mcprison.prison.modules.Module;
/**
* @author Faizaan A. Datoo
*/
public class DemoModule extends Module {
public DemoModule(String version) {
super("Demo", version, 30);
}
}
Notice that we did left the version as a parameter. We will go over how to set this later on in this guide.
You'll usually also want to override the enable()
and disable()
methods. These are called, as you may have assumed, when the module is enabled or disabled. In the enable()
method, you will include your initialization code. In the disable()
method, you will perform the final clean-up operations oft he module before the server closes.
package tech.mcprison.prison.demo;
import tech.mcprison.prison.modules.Module;
/**
* @author Faizaan A. Datoo
*/
public class DemoModule extends Module {
public DemoModule(String version) {
super("Demo", version, 30);
}
@Override public void enable() {
// Initialization code
}
@Override public void disable() {
// Clean-up code
}
}
Your main class is now complete. Let's look at how to load this module up in the game.
While modules don't use a server mod API directly, they are installed on modified servers alongside Prison-Core. Therefore, you must create the classes and files required by your target server mod in order to make your module loadable.
Prison-Core currently has implementations for both Spigot and Sponge. As a result, modules have the ability to run on either Spigot or Sponge using the same JAR file. This is possible using a dual-mod setup, which will be shown here.
Note: If you are creating a module that only targets either Spigot or Sponge, you may just implement the classes and files pertaining to your server mod.
Warning: We will be including Sponge and Spigot as dependencies in your module. Unless you are only targeting one server mod, do not use any Sponge or Spigot APIs in your module!
To make your module loadable on Spigot, we will create the JavaPlugin
sub-class and plugin.yml
file as normal.
package tech.mcprison.prison.demo;
import org.bukkit.plugin.java.JavaPlugin;
import tech.mcprison.prison.Prison;
/**
* @author Faizaan A. Datoo
*/
public class DemoSpigot extends JavaPlugin {
@Override public void onEnable() {
Prison.get().getModuleManager().registerModule(new DemoModule(getDescription().getVersion()
));
}
}
All that the onEnable()
method requires is a simple one-liner that tells Prison's module manager to register your module. We also pass in the version string, because we're setting the version in plugin.yml
:
name: PrisonDemo
version: 1.0.0
main: tech.mcprison.prison.demo.DemoSpigot
description: The tutorial module for Prison.
depend: [Prison]
It is important that you depend on the Prison plugin so that all its APIs load before your module.
If you are creating your module only to run on Spigot, then you may stop here. Otherwise, continue to learn how to make your module loadable in Sponge.
Making a module in Sponge is a bit easier. You only have to create one class - your main class.
package tech.mcprison.prison.demo;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameStartedServerEvent;
import org.spongepowered.api.plugin.Plugin;
import tech.mcprison.prison.Prison;
@Plugin(id = "prison-demo", name = "PrisonDemo", version = "1.0.0", dependencies = {
@Dependency(id = "prison")}) public class DemoSponge {
@Listener public void onServerStart(GameStartedServerEvent event) {
Prison.get().getModuleManager().registerModule(new DemoModule("1.0.0")); // See the note
}
}
Note: There is no quick way to get your plugin's version in Sponge. We are just setting the version number raw here, so be sure to keep it synchronized with the version value in your
@Plugin
annotation.
Now that we have made our module loadable for Spigot and Sponge, we are ready to test it out. Compile and package your project into a JAR as you would any plugin, and install it on your Spigot or Sponge test server. To see if your module is loaded, run the /prison modules
command.
It works! Congratulations. You've successfully created your first Prison-Core module.