In this project, you can register Bukkit listeners and add one or more requirements. A "requirement" is a condition that must be fulfilled for this event to be executed.
<dependencies>
<dependency>
<groupId>world.avionik</groupId>
<artifactId>event-requirement-manager</artifactId>
<version>1.0.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
dependencies {
compileOnly 'world.avionik:event-requirement-manager:1.0.2'
}
Bukkit.getPluginManager().registerEvents(PlayerMoveListener(), javaPlugin) {
it is PlayerEvent // Here you must return a Boolean that specifies whether you want to call the event or not.
}
Bukkit.getPluginManager().registerEvents(
PlayerMoveListener(), // The listener class to register
javaPlugin,
FirstEventRequirementHandler(), // Here you can add all requerements
SecondEventRequirementHandler()
)
EventRequirement.registerEvents(
PlayerMoveListener(), // The listener class to register
javaPlugin,
FirstEventRequirementHandler(), // Here you can add all requerements
SecondEventRequirementHandler()
)
Whenever a listener is registered, this triggers a Bukkit event called ListenerRegisterEvent. This event contains the registered listener.
You can use these methods to automatically cancel events. Of course, you can also set a requerement here.
Bukkit.getPluginManager().registerCancelledEvent<PlayerMoveEvent>(EventPriority.HIGHEST, javaPlugin) { // PlayerMoveEvent is the event that is cancelled
it is Cancelled // Here you must return a Boolean that specifies whether you want to call the event or not.
}
Bukkit.getPluginManager().registerCancelledEvent<PlayerMoveEvent>( // PlayerMoveEvent is the event that is cancelled
EventPriority.HIGHEST,
javaPlugin,
FirstEventRequirementHandler(), // Here you can add all requerements
SecondEventRequirementHandler()
)
EventRequirement.registerCancelledEvent(
PlayerMoveEvent::class.java, // The event to cancel
EventPriority.HIGHEST,
javaPlugin,
FirstEventRequirementHandler(), // Here you can add all requerements
SecondEventRequirementHandler()
)
There is an EventRequirementHandler class. You can implement this class in your code to have a more elegant way of creating requirements.
class FirstEventRequirementHandler : EventRequirementHandler {
/**
* This method is executed when an event with this requirement is called.
* @param event that is about to be executed
* @return whether this event should be executed or not
*/
override fun execute(event: Event): Boolean {
if (event !is PlayerEvent)
return false
return event.player.gameMode == GameMode.SPECTATOR
}
}