Plugin Tutorial: Discord Integration
Tutorial covering the development of a simple game server plugin
Let's write a simple game server plugin to log player messages sent in-game to a Discord channel using a webhook. No prior knowledge is needed for this tutorial (obviously, beyond Java).
Baby steps
Start by including the module containing the Irminsul API into your project.
Create your main plugin class and extend the GamePlugin
class located in io.irminsul.common.plugin
. Implement the required onEnable
and onDisable
methods.
Your plugin must have a public constructor that takes in no parameters. Java creates this implicitly if you have no other constructor.
As you may have noticed in the onEnable
and onDisable
methods in this example, you have access to an SLF4J logger belonging to your plugin. You also have access to the GameServer
to which your plugin is running on through the this.server
field.
When exactly a plugin is instantiated, and what state the server is in at that time, is unspecified and up to the implementation. Our default game server makes no guarantees of safety or nullability at the time plugins are loaded. Place your logic in onEnable
, not your constructor!
Next, we must create a plugin.properties
file to provide some basic information about our plugin.
You can also try using the plugins
command to verify that your plugin is loaded, and to see some information on it.
It works! It's not very interesting though. Let's make our plugin actually do something.
Events
Most of your plugin logic will be based around subscribing to, and handling, events. The game server fires events for basically everything. You can subscribe to events, add your own logic on them, manipulate them, or even cancel them altogether.
Irminsul's event bus is simple. Register an object as an event subscriber, and methods annotated with @EventHandler
will be called intelligently based on the event in the method parameters. Let's set up a basic handler for the PlayerLoginEvent
event.
Let's try it out!
It works! We can get all sorts of information on the player joining through the event. Since PlayerLoginEvent
does not extend CancellableEvent
, we aren't able to cancel the player login, since it's not really something we want to cancel.
Let's add a second event handler for the PlayerChatEvent
event and see what we can do with it.
Let's try it out and send some messages to the server console!
It works! We did it! We wrote a simple plugin to log player chat messages to a Discord webhook. We've began to explore the potentials of the Irminsul plugin API.
Last updated