Appearance
Events
Listen to events with @Subscribe methods. The method's single parameter decides which event it gets.
java
public final class Speedometer extends TextHudWidget {
@Subscribe
public void onTick(ClientTickEvent event) {
// once per game tick
}
@Subscribe(priority = 10)
public void onKey(KeyEvent event) {
if (event.key() == Keys.R && event.action() == KeyEvent.Action.PRESS && !event.inScreen()) {
reset();
event.setCancelled(true); // Minecraft does not see this key press
}
}
}Modules are subscribed automatically while they are enabled. For other objects call lynx.events().register(listener) and unregister(listener) yourself.
Built-in events
| Event | When | Cancellable |
|---|---|---|
ClientTickEvent | Once per game tick, at its end | no |
KeyEvent | Key or mouse button pressed, released or repeated (not while typing in a text field) | yes |
ClientTickEvent is one shared instance, do not keep it. Ticks are normally 20 per second, but after lag or while loading Minecraft runs extra ticks to catch up. For time, use System.nanoTime(), not a tick counter.
Keys
KeyEvent.key() uses LynxClient's own key codes from Keys, the same on every Minecraft version (GLFW codes; 1.8.9 is translated). Mouse buttons use the same codes: Keys.mouse(0) left, 1 right, 2 middle, 3/4 side buttons.
Priority and cancelled events
- Higher
priorityruns first (default 0). - Once an event is cancelled, later listeners do not get it, unless they set
@Subscribe(receiveCancelled = true).
Your own events
Any class implementing Event (or Cancellable) can be posted:
java
public final class LapEvent implements Event {
public final long millis;
public LapEvent(long millis) { this.millis = millis; }
}
lynx.events().post(new LapEvent(lapTime));Other addons that depend on yours can listen to it. Posting does not allocate on LynxClient's side, so it is fine every frame.