Skip to content

EventManager

OxideWaveLength edited this page Mar 16, 2020 · 10 revisions

Introduction

We are now ready to listen for events in a custom class! This means that you can listen for any event, from any class, and here is how!

Notes: In this guide we expect you haven't renamed any package/class in the project. If you did, just follow along with the names you now have (BaseClient is the Main client's class). Also, to have an example on how the event listeners work in action, you can check out any of the following classes: CommandManager (with its Command class) and ModuleManager (with its Module class)

Index

  1. Events list
  2. Create an event listener

Event list

Event Name Parameters Cancellable Progress Working
KeyPressedEvent int key yes 100% yes
MessageReceivedEvent String message yes 100% yes
MessageSentEvent String message yes 100% yes
PacketReceivedEvent Packet packet yes 100% yes
PacketSentEvent Packet packet yes 100% yes
MouseScrollEvent int button yes 100% yes
ServerConnectingEvent String addr, int port yes 100% yes
PlayerSpawnEvent EntityOtherPlayerMP yes 100% yes
BlockRenderEvent Block yes 100% yes
FluidRenderEvent Block yes 100% yes
RenderLivingLabelEvent Entity, String yes 100% yes
CollideEvent Entity, AxisAlignedBB, Block yes 100% yes
BlockBrightnessRequestEvent Block no 100% yes
ServerJoinEvent ServerData data no 100% yes
ServerLeaveEvent ServeData data, String reason, IChatComponent message no 100% yes
MouseClickEvent int button no 100% yes
PreMotionEvent double x, double y, double z, float yaw, float pitch, boolean ground no 100% yes
PostMotionEvent None no 100% yes
Render2DEvent int width, int height no 100% yes
Render3DEvent float partialTicks no 100% yes
UpdateEvent None no 100% yes

Create an event listener

Any class can become an event listener, you just need to extend the class EventListener! For example, the "Module" class is an EventListener, because the module needs to listen for the updateEvent, same for the ModuleManager, since it has to listen for the key presses!

How can we do that though?

First things first, we need to create a class, give it whatever name you want. After that is done, open the class, and right after the class name, before the open bracket, type extends EventListener, making sure to import me.wavelength.baseclient.event.EventListener just like so:

package me.wavelength.baseclient.example

import me.wavelength.baseclient.event.EventListener;

public class ExampleClass extends EventListener {

}

And now you can start listening for events! How can we do that though?

Every event has their method, every method starts with on, that means that if you get into the class, and type "on", then press Ctrl+Space (this works only on Eclipse as far as I know), every method that starts with "on" will appear and can be generated, so you just need to scroll with your arrow keys and press enter on the one you want (or double click on it). Of course you could type "onMessage" and press Ctrl+Space to get the same menu appear with only the Messages events.

Here is an example:

package me.wavelength.baseclient.example

import me.wavelength.baseclient.event.EventListener;

public class ExampleClass extends EventListener {

	on // Once Ctrl+Space is pressed (At least on Eclipse) a window will appear

}

And this is what it will look like:

Now we're ready to...

Register a listener!

Our event listener will not work yet, we need to register it first.

Once an event listener is registered, it will continuously listen, all the time, until it's finally unregistered.

To register an event listener, we use this code: BaseClient.instance.getEventManager().registerListener(new ExampleClass());, where BaseClient is the client's main class, and ExampleClass is your event listener class.

You can register the event when the client starts, or when a module is activated, you can register it at any given time.

Unregister a listener

Let's say that we registered a listener, but that now we don't need it anymore, maybe we rendered something on the screen for 5 seconds, but now we are done; we could of course just not render anything anymore, but it's preferred to unregister the listeners once they are not needed anymore (since the event listeners work with a for-loop of every registered listener, when reaching a certain amount of registered listeners it might cause some lag).

So, how do we unregister a listener? It's pretty easy, just type BaseClient.instance.getEventManager().unregisterListener(listener); in any part of the code, where BaseClient is the client's main class, and listener is the listener's instance. You can also use BaseClient.instance.getEventManager().unregisterListener(class);, where BaseClient is the client's main class, and class is the listener's class. (Keep in mind though that this will unregister every listener of that class. That means that if you have a class for toasters (notifications drawn on-screen) and unregister it through the class all of the toasters are going to be unregistered. If your code acts weird after making this change do NOT open an issue, revert the changes)