-
-
Notifications
You must be signed in to change notification settings - Fork 49
EventManager
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)
Event Name | Parameters | Cancellable | Progress | Working |
---|---|---|---|---|
KeyPressedEvent |
int key |
yes | 100% | yes |
MessageReceivedEvent |
String message |
yes | 100% | yes |
MessageSentEvent |
String message |
yes | 100% | yes |
MouseClickEvent |
int button |
not yet | 97% | yes |
MouseScrollEvent |
int button |
yes | 100% | yes |
PacketReceivedEvent |
Packet packet |
yes | 0% | no |
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 |
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 to listen for the key presses!
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...
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 you finally unregister it
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.
Let's say that we registered a listener, but 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 not needed (since the event listeners work with a for-loop of every registered listener, when reaching a certain amount if listeners registered it will start to lag).
So, how do we unregister a listener? It's pretty easy, 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. (there are drawbacks in using this, if your code acts weird after making this change do NOT open an issue, revert the changes)