Skip to content
Faizaan edited this page Jun 21, 2016 · 5 revisions

Prison uses Google Guava's Event Bus feature as its event system. Events don't have to extend or implement anything, and listening to an event is almost identical to Bukkit's event API.

Creating an Event

Here is an example event.

public class MyEvent {

}

That's all you have to do! You can add variables and methods as you please, just as you would a normal class.

Calling an Event

With the event bus, calling an event is also known as "posting" an event. So, let's post the event we just created.

// ...
Prison.getInstance().getEventBus().post(new MyEvent());

Creating a Listener

A listener is what contains the method that is triggered when a certain event is posted. Listeners can listen to as many events as they want.

public class MyListener {

      @Subscribe
      public void onMyEvent(MyEvent e) {
           // Respond to the event here.
      }

}

As you can see, it is very similar to Bukkit's listener API.

Registering a Listener

Here is how to register a listener:

// ...
Prison.getInstance().getEventBus().register(new MyListener());

Now, when MyEvent is posted, the onMyEvent method in your listener will be called.

Drawbacks of the EventBus

With the EventBus API from Guava, there is no concept of cancelling an event or setting a listener's priority. However, Prison currently has no need for it and therefore it has been a nonissue so far.