Welcome to Episode Three of our Minecraft plugin development series.
Click here to see the second episode where I show you how to release and compile your first Minecraft plugin.
In this episode, we’re going to cover:
- Events and listeners — How Bukkit/Spigot/Paper server fires “events”.
- All events list — Where to find all available events on the server.
- Event priorities — Resolving plugin conflicts and understanding event order.
- Cancelling and modifying events — How to properly cancel and edit stuff inside an event listener.
Source Codes:
Links:
- All events list (Paper Javadocs) (click Javadoc and then search for “event”)
What Are Events
Events — They are actions that happen in the game, or from another plugins. You can see a list of events at https://papermc.io/javadocs
Sync/Async — Most events happen on the main thread in the heartbeat (see first video), some events such as chat can happen on another threads.
Event listeners —The server will scan all classes which implement Listener for event methods.
@EventHandler — To listen for a specific event, create a method that is annotated with @EventHandler. The method does not return anything, and has a single parameter which is the event itself.
Registering Event Listeners
The server needs to know where to look for events, so you always need to register each and every listener class.
How to do it — In onEnable(), call Bukkit.getPluginManager().registerEvents() and pass in the listener class instance and your plugin instance.
Event Priorities
Bukkit came up with a priority system to help resolve conflicts in plugins listening to the same event.
The server calls event methods from the lowest priority up. If you need to have the last say in your plugin, listen on HIGHEST.
If you don’t specify a priority, it will be NORMAL.
The MONITOR priority is used for logging plugins etc., but not to change the event.
The image above illustrates all available event priorities, starting from LOWEST to MONITOR. Bukkit calls methods with LOWEST first, and with MONITOR as last.
The image below illustrates the priority system, where the first method having a normal priority gets called first, and the second one with the highest priority comes afterwards.
Cancelling Events
Cancellable events — Some events can be cancelled (i.e. block break event), preventing the given action from being completed. These events implement Cancellable.
You can ignore canceled events in the event handler annotation or by checking event#isCancelled().
Please note that other plugins or you can also “uncancel” cancelled events, that is why priorities take place.
Final Words
Creating Minecraft plugins can be confusing, with most YouTube tutorials being painfully outdated, disorganized and offering no live support.
If you want to learn from the best of the pack and create truly customized plugins, check out Project Orion.
It’s a full fledged training tried and tested by 2,000+ people showing step-by-step on how to make plugins, advanced systems (minigames, custom mobs, antipiracy, dungeons, claims etc.) and best yet, I am doing live calls multiple times each week and connect with you in our community to answer any and all questions you have.
It’s just so much better experience than any blog post or video since I can pour my time and heart into it with greater depth and our platform is custom coded with features we need for it specifically. Click here to learn more!