Skip to content

How to listen for more events

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 toolbar-processor to your project’s build file using your preferred build system:

pom.xml
<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>

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.

Once you build the project, the library will handle the rest of the bothersome work:

  1. The toolbar-processor module will generate a bukkit-based listener based on the given event, annotate it with @GeneratedListener and call ListenerService#callEventIfPresent, which would result your toolbar ToolEventListener#onTrigger to be invoked.
  2. The toolbar-core module will search for classes that extend @GeneratedListener during the enable stage and register the event listener (PluginManager#registerEvents(listener, plugin)).

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;
@GeneratedListener
public class PlayerMoveEventListener implements Listener {
private final ListenerService listenerService = ToolbarAPI.getInstance().listenerService();
@EventHandler
public void on(PlayerMoveEvent event) {
listenerService.callEventIfPresent(event);
}
}