How To Use ?
1. Registering Events Without a Listener Class
With BasePlugin.registerEvent, you can register events dynamically without creating a dedicated Listener class. This allows you to quickly define inline behavior for events and apply advanced configurations like limits, filters, and expiration.
Example: Registering an Event Dynamically
BasePlugin.registerEvent(PlayerJoinEvent.class) //registers PlayerJoinEvent
.priority(EventPriority.HIGHEST) //sets priority as HIGHEST
.expire(10, TimeUnit.HOURS) //this event will expire in 10 hours
.filter(event -> event.getPlayer().hasPermission("example.permission")) //adds filter to work this listener
.limit(10) //after 10 usage, this listener will unregister (Events that fail the filter do not affects the limit!)
.consume(event -> { //when the event is triggered, consume will work
event.getPlayer().sendMessage("test");
event.getPlayer().sendMessage("test");
event.getPlayer().sendMessage("test");
});
2. Registering Events with a Listener Class
With EventUtils.registerEvent, you can register a Listener class to handle multiple events in one place. This method ensures the listener is not registered twice and handles lifecycle management.
EventUtils.registerEvent(new Listener() {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
event.getPlayer().sendMessage("Welcome to the server!");
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
event.getPlayer().sendMessage("Goodbye!");
}
}, BasePlugin.getInstance());
3. Manually Calling Events
Using EventUtils.callEvent, you can programmatically trigger events. This is useful for custom event logic or when triggering Bukkit events in custom workflows.
Example: Triggering a Custom Event
CustomEvent customEvent = new CustomEvent();
boolean notCancelled = EventUtils.callEvent(customEvent);
if (notCancelled) {
Bukkit.broadcastMessage("The custom event executed successfully!");
}
That's all