Skip to content
This repository has been archived by the owner on Jul 4, 2022. It is now read-only.

ListenerAPI

Nico edited this page Nov 1, 2020 · 19 revisions

Listener API

Dependencies required:

The API of the ListenerAPI system used in SimplixCore can be found in the simplixcore-common-api module. To just make use of the Listener API, no other dependencies than described in getting started are required. However, the specific events (that will be called automatically for by the SimplixCore-Plugin) can be found in the simplixcore-minecraft-api package.

<!-- Minecraft events -->
<dependency>
  <groupId>dev.simplix.core</groupId>
  <artifactId>simplixcore-minecraft-api</artifactId>
  <version>1.0-SNAPSHOT</version>
  <scope>provided</scope>
</dependency>

Basic structure

The Simplix platform makes use of its own event and listener system. It is needed for our own standalone applications as well as for our plugins. It also contains some abstracted and therefore platform independent events from Spigot/BungeeCord. The SimplixCore handles calling these events on the given platform. You can just start listening for the given events from the simplixcore-minecraft-api module.

Creating listeners

Hello world listener:

import dev.simplix.core.common.listener.Listener;
import dev.simplix.core.common.listener.Listeners;

public class HelloWorldListener implements Listener<HelloWorldEvent> {

  public void register() {
    Listeners.register(this);
  }

  @Override
  public Class<HelloWorldEvent> type() {
    return HelloWorldEvent.class;
  }

  @Override
  public void handleEvent(HelloWorldEvent event) {
  }
}

Standard methods in an event

Listening for a ChatEvent in a platform-independent way.

Real-world example: ChatBlocker

import dev.simplix.core.common.listener.Listener;
import dev.simplix.core.minecraft.api.events.ChatEvent;
import java.util.Set;

// Will work for BungeeCord & Spigot if needed
public class BadWordBlocker implements Listener<ChatEvent> {

  private final Set<String> badWords;

  public BadWordBlocker(final Set<String> badWords) {
    this.badWords = badWords;
  }

  @Override
  public Class<ChatEvent> type() {
    return ChatEvent.class;
  }

  @Override
  public void handleEvent(ChatEvent event) {
    final String message = event.message();
    System.out.println("Received " + message);
    for (String badWord : this.badWords) {
      if (!message.contains(badWord)) {
        continue;
      }
      event.canceled(true);
      event.cancelReason("Bad word");
    }
  }
}

Creating own events

import dev.simplix.core.common.event.AbstractEvent;
import java.util.UUID;

public class TierUpgradeEvent extends AbstractEvent {

  // Properties
  private final UUID playerUUID;
  private int nextTier;
  private int oldTier;

  public TierUpgradeEvent(UUID playerUuid, int nextTier, int oldTier) {
    this.nextTier = nextTier;
    this.oldTier = oldTier;
    this.playerUUID = playerUuid;
  }

  // Accessors
  public UUID playerUuid() {
    return this.playerUUID;
  }

  public int nextTier() {
    return this.nextTier;
  }

  public void nextTier(int nextTier) {
    this.nextTier = nextTier;
  }

  public int oldTier() {
    return this.oldTier;
  }

  public void oldTier(int oldTier) {
    this.oldTier = oldTier;
  }
}
import dev.simplix.core.common.listener.Listener;
import dev.simplix.core.common.listener.Listeners;

public class TierUpgradeListener implements Listener<TierUpgradeEvent> {

  private boolean someConditionHere;

  public void register() {
    // The Listener class stores all of our Listener instances
    Listeners.register(this);
  }

  @Override
  public Class<TierUpgradeEvent> type() {
    return TierUpgradeEvent.class;
  }

  @Override
  public void handleEvent(TierUpgradeEvent event) {
    //Every event is cancellable by default
    event.canceled(this.someConditionHere);
    //Every event has a cancel reason by default
    event.cancelReason("Example Cancel-Reason");
  }
}

Calling own events

import dev.simplix.core.common.event.Events;
import java.util.UUID;

public class CallEventExample {

  public void upgradeTier(UUID playerUuid, int oldTier, int nextTier) {
    // Upgrading work here
    final TierUpgradeEvent call = Events.call(new TierUpgradeEvent(playerUuid, oldTier, nextTier));
    if (call.canceled()) {
      // Handle cancellation here

      // Reading properties...
      final String cancelReason = call.cancelReason();
    }
  }
}