Bukkit uses an event based system that allows plugin developers to interact with and modify the server and specific actions that occur in the world.
Event handlers are methods that get called when their event occurs. They are generally public and void as well as named on{EventNameStem}
by convention. All handlers however, must have the @EventHandler
annotation, as well as contain its event as the ONLY parameter. Here is an example of an event handler for the PlayerJoinEvent
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event){
//Run when a player joins
}
Note: The naming format for Bukkit events is {Source}{Action}({Target})Event
. Some examples of these event names are: PlayerInteractEvent
or BlockBreakEvent
or PlayerJoinEvent
. A list of all events can be found on the Spigot Javadocs
Merely creating an event handler is not enough to allow Bukkit to start sending event calls to your method. You must also register it through the PluginManager interface.
The most common way to register events is to create a class that implements the Listener interface and use it wrap your event handlers.
public class EventListener implements Listener { //Implements the Listener interface
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event){
//Run when a player joins
}
}
This listener class and all of its events can then be registered in your main plugin class like this:
@Override
public void onEnable(){
Bukkit.getPluginManager().registerEvents(new EventListener(), this); //Register your listener and its event handlers
}