For the complete documentation index, see llms.txt. This page is also available as Markdown.

Working with the API

Brief introduction to Sonar's API and event system

Sonar is supposed to be an easy-to-use plugin; therefore, the API has been designed to give a developer almost full control over the behavior of the plugin.

Using the API

Before you are actually able to use the API, you will have to add the dependency to your project.

maven(url = "https://repo.jonesdev.xyz/releases/")
compileOnly("xyz.jonesdev.sonar:sonar-api:2.1.46")

Now you are able to access all the main API functions by using Sonar.get(). Replace 2.1.46 with the version of Sonar you are using.

Events

Using Sonar's built-in event system is also not hard. First, you need to create an event listener:

import xyz.jonesdev.sonar.api.event.SonarEvent;
import xyz.jonesdev.sonar.api.event.SonarEventListener;
import xyz.jonesdev.sonar.api.event.impl.UserVerifySuccessEvent;

public final class TestListener implements SonarEventListener {

  @Override
  public void handle(final SonarEvent event) {
    if (event instanceof UserVerifySuccessEvent successEvent) {
      // ... some example code ...
      System.out.printf("Test: %s (%s) (took %d ms to verify)%n",
        successEvent.getUsername(), successEvent.getOfflineUuid(), successEvent.getTimeTakenToVerify());
      System.out.println("You can also get the raw user data: " + successEvent.getUser());
    }
    // ...
  }
}

Then, you need to register your event listener using the Sonar API:

Have fun developing!

Last updated