How to listen for more events
The Problem
Section titled “The Problem”Currently, the toolbar-core module only listens to two player events:
PlayerInteractEntityEvent- called when a player right-clicks an entity.PlayerInteractEvent- called when a player interacts with an object or air.
But you may want to listen for more events. For example, PlayerMoveEvent or PlayerTeleportEvent.
This is the purpose of this guide. We will teach you how to listen for more events.
Add Dependency
Section titled “Add Dependency”Add toolbar-processor to your project’s build file using your preferred build system:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.16.0</version> <configuration> <!-- 1. Add the annotation processor as an annotation processor dependency --> <annotationProcessorPaths> <path> <groupId>com.tofpu.toolbar</groupId> <artifactId>toolbar-processor</artifactId> <version>VERSION</version> </path> </annotationProcessorPaths> <!-- 2. Specify the path of the annotation processor (Required for JDK 23+) --> <annotationProcessors> <annotationProcessor>com.tofpu.toolbar.processor.ListenerProcessor</annotationProcessor> </annotationProcessors> <compilerArgs> <!-- 3. Sets the path where the listeners will be generated at --> <arg>-AlistenerPackage=your.org.plugin.generated</arg> </compilerArgs> </configuration> </plugin> </plugins></build>plugins { java}
dependencies { // 1. Add the annotation processor as an annotation processor dependency annotationProcessor("com.tofpu.toolbar:toolbar-processor:VERSION")}
tasks.withType<JavaCompile>({ options.apply { // 2. Specify the path of the annotation processor (Required for JDK 23+) compilerArgs.addAll(listOf("-processor", "com.tofpu.toolbar.processor.ListenerProcessor"))
// 3. Sets the path where the listeners will be generated at compilerArgs.add("-AlistenerPackage=your.org.plugin.generated") }})Add the Listener
Section titled “Add the Listener”Add your class (e.g. YourEventListener), let it implement ToolEventListener<YourEvent>, and annotate the class with
ListenerWrapper(type = YourEvent.class):
package your.org.plugin.generated;
import com.tofpu.toolbar.api.listener.ListenerWrapper;import com.tofpu.toolbar.api.listener.ToolEventListener;import org.bukkit.event.Event;import org.bukkit.event.player.PlayerMoveEvent;
@ListenerWrapper(type = PlayerMoveEvent.class)public class PlayerMoveEventListener implements ToolEventListener<PlayerMoveEvent> {
@Override public void onTrigger(PlayerMoveEvent event, ToolActivator<Event> toolActivator) { toolActivator.activate(event, event.getPlayer().getItemInHand()); }
@Override public Class<PlayerMoveEvent> type() { return PlayerMoveEvent.class; }}And, that is it! Your items can now successfully listen to this event and act accordingly.
Extra: Behind the scenes
Section titled “Extra: Behind the scenes”Once you build the project, the library will handle the rest of the bothersome work:
- The
toolbar-processormodule will generate a bukkit-based listener based on the given event, annotate it with@GeneratedListenerand callListenerService#callEventIfPresent, which would result your toolbarToolEventListener#onTriggerto be invoked. - The
toolbar-coremodule will search for classes that extend@GeneratedListenerduring the enable stage and register the event listener (PluginManager#registerEvents(listener, plugin)).
Generated Listener Class
Section titled “Generated Listener Class”This is the class that the toolbar-processor module will generate in your behalf:
package your.org.plugin.generated;
import com.tofpu.toolbar.api.listener.ListenerService;import com.tofpu.toolbar.api.listener.GeneratedListener;import com.tofpu.toolbar.api.ToolbarAPI;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.player.PlayerMoveEvent;
@GeneratedListenerpublic class PlayerMoveEventListener implements Listener { private final ListenerService listenerService = ToolbarAPI.getInstance().listenerService();
@EventHandler public void on(PlayerMoveEvent event) { listenerService.callEventIfPresent(event); }}