Event Basics
All events extend theorg.bukkit.event.Event class and are called by the PluginManager.
The Event Class
Fromorg.bukkit.event.Event:
Creating an Event Listener
1
Create a Listener Class
Create a class that implements the The
Listener interface:Listener interface is a marker interface with no methods (from org.bukkit.event.Listener):2
Register the Listener
Register your listener in the plugin’s
onEnable() method:The second parameter is your plugin instance, which is used to track which plugin registered the listener.
The @EventHandler Annotation
The@EventHandler annotation marks methods as event handlers. From org.bukkit.event.EventHandler:
Event Priority
Control the order in which your event handler is called:org.bukkit.event.EventPriority):
LOWEST- Called first, for early modificationsLOW- Called earlyNORMAL- Default priorityHIGH- Called lateHIGHEST- Called very late, final modificationsMONITOR- Read-only, for observation (don’t modify events!)
Ignore Cancelled Events
Skip cancelled events with theignoreCancelled parameter:
Common Events
Player Events
Listen for player-related events:PlayerJoinEvent Example
Fromorg.bukkit.event.player.PlayerJoinEvent:
Block Events
Entity Events
Cancellable Events
Many events can be cancelled to prevent the action from occurring:Calling Events
You can create and call custom events:Event class:
Asynchronous Events
Some events are fired asynchronously (off the main thread):Best Practices
-
Use the correct priority:
LOWEST/LOWfor early cancellationNORMALfor most modificationsHIGH/HIGHESTfor final modificationsMONITORonly for observation
-
Check event state:
-
Performance considerations:
-
Unregister when done:
-
Use ignoreCancelled wisely:
- Set to
truewhen you don’t want to process cancelled events - Set to
falsewhen you need to see all events regardless of cancellation
- Set to
Event Reference
Common event packages:org.bukkit.event.player.*- Player eventsorg.bukkit.event.block.*- Block eventsorg.bukkit.event.entity.*- Entity eventsorg.bukkit.event.inventory.*- Inventory eventsorg.bukkit.event.world.*- World eventsorg.bukkit.event.server.*- Server events