Hello community,
I noticed a few threads asking for help in plugin development, so I thought I would create a GitHub repository with an example plugin for everyone to look at, learn by and modify.
Even though I hate posting the first tutorial of millions… I feel it’s necessary as more and more people come to try and develop Sponge plugins.
Warning: There is pseudo-code! Nope. Not any longer :3
However, if you’d still like to have a look at it,
here it is: GitHub - MrMysteri0us/SpongePlugin
So how does this code work?
Well, first, we have to register our plugin, we use Annotations for that.
@Plugin
Now our plugin needs a unique id (may be human readable or not), a name (human readable) and a version:
id = "SpongePlugin", name = "SpongePlugin", version = "1.0"
Our code should look like this:
@Plugin(id = "SpongePlugin", name = "SpongePlugin", version = "1.0")
Optionally, you can add dependencies like:
require-after:plugin-name
Plugin x has to be present, load our plugin after plugin x
after:plugin-name
Plugin x can be present, if so, load our plugin after plugin x
before:plugin-name
Plugin x can be present, if so, load our plugin before plugin x
require-before:plugin-name
Plugin x has to be present, load our plugin before plugin x
@Plugin(id = "SpongePlugin", name = "SpongePlugin", version = "1.0", dependencies = "after:a;before:b")
Our plugin now needs a start method and a stop method, right?
We can add those in the form of SpongeEvents:
First, we have to mark our method as an event handler:
@Subscribe
Now we have to build that method:
public void onEvent(SpongeEvent event) {}
We have to do that for:
a) PreInitializationEvent - similar to Bukkits onEnable()
b) ServerStoppingEvent - similar to Bukkits onDisable()
In the end, our code would look like this:
@Plugin(id = "SpongePlugin", name = "SpongePlugin", version = "1.0", dependencies = "after:b;before:c")
public class SpongePlugin {
@Subscribe
public void onInit(PreInitializationEvent event) {
// TODO -> start plugin: load config, assign variables
}
@Subscribe
public void onStop(ServerStoppingEvent event) {
// TODO -> stop plugin: save config (if changed), clean up
}
}