Events
Overview
Apollo provides its own listener based event system, similar to the existing event system found in Bukkit.
List of available Apollo events
ApolloRegisterPlayerEvent
ApolloUnregisterPlayerEvent
ApolloReceivePacketEvent
ApolloSendPacketEvent
ApolloUpdateOptionEvent
ApolloUpdateOptionEvent
Called when an option is updated.
Field | Description |
---|---|
Options container | The container that the option is in. |
ApolloPlayer player | The Apollo player the option was updated for, or null if it was a global option. |
Option<?, ?, ?> option | The option that was updated. |
Object value | The new value of the option. |
This event is cancellable.
Integration
Sample Code (Method 1)
public class GeneralExample1 implements ApolloListener {
public GeneralExample1() {
EventBus.getBus().register(this);
}
@Listen
public void onApolloRegister(ApolloRegisterPlayerEvent event) {
((Player) event.getPlayer()).sendMessage("You have joined using LunarClient!");
}
}
Sample Code (Method 2)
public class GeneralExample2 implements ApolloListener {
public GeneralExample2() {
this.handle(ApolloRegisterPlayerEvent.class, this::onApolloRegister);
}
public void onApolloRegister(ApolloRegisterPlayerEvent event) {
((Player) event.getPlayer()).sendMessage("You have joined using LunarClient!");
}
}
Creating Apollo events
Creating the event class
// Normal event
public class CoolApolloEvent implements Event {
private final ApolloPlayer player;
public CoolApolloEvent(ApolloPlayer player) {
this.player = player;
}
public ApolloPlayer getPlayer() {
return this.player;
}
}
// Cancellable event
public class CoolApolloCancellableEvent implements EventCancellable {
private final ApolloPlayer player;
private boolean cancelled;
public CoolApolloCancellableEvent(ApolloPlayer player) {
this.player = player;
}
public ApolloPlayer getPlayer() {
return this.player;
}
@Override
public boolean isCancelled() {
return this.cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}
Calling the created event
// Calling a normal event
public void callCoolApolloEvent(ApolloPlayer player) {
CoolApolloEvent event = new CoolApolloEvent(player);
EventBus.EventResult<CoolApolloEvent> result = EventBus.getBus().post(event);
for (Throwable throwable : result.getThrowing()) {
throwable.printStackTrace();
}
}
// Calling a cancellable event
public void callCoolApolloCancellableEvent(ApolloPlayer player) {
CoolApolloCancellableEvent event = new CoolApolloCancellableEvent(player);
EventBus.EventResult<CoolApolloCancellableEvent> result = EventBus.getBus().post(event);
if (!result.getEvent().isCancelled()) {
// Do some action if the event is not cancelled
}
for (Throwable throwable : result.getThrowing()) {
throwable.printStackTrace();
}
}