Skip to content

Introduction

BrokenEarth edited this page Apr 2, 2019 · 2 revisions

In this page, you'll learn more about how the EventBus works. You'll also learn about event methods and listeners. If you have any issues with the wiki, don't hesitate to create a new issue here

Understanding EventBus

EventBus is an API that helps you create and call events. An event is a class that can be called after an occurrence or event. Suppose you create a class called PlayerEatEvent. In that class, you have two getters - getPlayer and getFood. PlayerEatEvent is considered an event class. When you detect a player eating something in a game, you'll have to call PlayerEatEvent. When it is called, all methods listening to that event will be called.

Event methods, sometimes known as listener methods, are methods that will be called if an event is called. Usually, there are different event methods listening to different events. If an event is called, event methods listening to the event will be called.

Summary

An EventBus is an API that helps you create an call events.

Event Methods and Listeners

As stated above, an event method is a method listening to an event. If that event is called, the event method will be called. A listener, on the other hand, is a class containing at least an event method. There are few requirements that are needed to be met before a method is considered an event method:

  1. The method is public and not static.
  2. They only have one parameter requiring an object. That object is the event class.
  3. Should be annotated with SubscribeEvent.

When an event or its subclass is called, an event method expecting the event class in their parameter will be called.

Summary

An event method is a method listening to an event. A listener is a class containing an event method. There are few requirements that are needed to be met before a method is considered an event method.

Glossary

Term Meaning
EventBus an API that helps you create and call events.
Event a class that can be called after an occurrence or event
Event Methods methods that will be called if an event is called
Listener a class containing at least an event method
Clone this wiki locally