[Tutorial] How to create a Sponge Plugin

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
    }
}
5 Likes

Updated the post, to be more of a tutorial than just a random thread pointing to a random GitHub.

Will link this, since @Tux2 's post is somewhat similar, but goes over more of the event handlers (It may be outdated, although I’m not sure)

You should update @SpongeEventHandler to @Subscribe.

1 Like

I noticed in your repository that you log when your example plugin enables and disables. I’m assuming that in a finished state that Sponge will handle this for all plugins; making the inclusion of those lines a little verbose. Lots of people included this in their Bukkit plugins (likely originating from some old outdated YouTube tutorials), which probably isn’t the best habit to carry over.

I know it’s a little early to start picking up on things like this, but it’s probably still best to start off fresh without any of those habits ;D

I actually never log when my plugin starts. This is a placeholder.

There is no ‘disable’ for Sponge plugins at runtime. Your code is mixing up two things: PreInit only runs once while the server-start and server-stop events can run multiple times (e.g. SP testing integrated server)

Oh I see. Thanks for the info!