A simple library for dispatching events and subscribing to them
Subscription and event dispatch
EventBus eventBus = new EventBusImpl();
eventBus.subscribe(ChatMessageEvent.class, event -> {
System.out.println("Received ChatMessageEvent: " + event.getMessage());
if (shouldCancel) {
event.cancel();
}
});
eventBus.subscribe(new AsyncChatListener());
eventBus.call(new ChatMessageEvent("Chat message"));Subscribing to events using the @Subscribe annotation
public class AsyncChatListener {
@Subscribe(priority = EventPriorities.FIRST, handleCancelled = true, async = true)
public void onChatMessage(ChatMessageEvent event) {
System.out.println("Async chat event: " + event);
}
}Unsubscribe from events
// Subscribing to events
Subscriber<ChatMessageEvent> subscriber = eventBus.subscribe(ChatMessageEvent.class, event -> {
System.out.println("Received ChatMessageEvent: " + event.getMessage());
});
AsyncChatListener asyncChatListener = new AsyncChatListener();
eventBus.subscribe(asyncChatListener);
eventBus.call(new ChatMessageEvent("Chat message"));
// Unsubscribe from events
eventBus.unsubscribe(asyncChatListener);
eventBus.unsubscribe(subscriber);Working with event result
CallResult result = eventBus.callSilently(new ChatMessageEvent("Chat message"));
System.out.println("Exceptions: " + result.getExceptions());
System.out.println("Is success: " + result.isSuccess());
System.out.println("Is cancelled: " + result.isCancelled());Creating an own event
public class ChatMessageEvent implements Event, Cancellable {
private final String message;
private boolean cancelled;
public ChatMessageEvent(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}Adding repo:
<repositories>
<repository>
<id>luminiadev-repository-snapshots</id>
<url>https://repo.luminiadev.com/snapshots</url>
</repository>
</repositories>Adding a library api:
<dependency>
<groupId>com.luminiadev.eventbus</groupId>
<artifactId>api</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>Adding a library implementation:
<dependency>
<groupId>com.luminiadev.eventbus</groupId>
<artifactId>core</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>Adding repo:
maven {
name = "luminiadevRepositorySnapshots"
url = uri("https://repo.luminiadev.com/snapshots")
}Adding a library api:
implementation "com.luminiadev.eventbus:api:1.0.0-SNAPSHOT"Adding a library implementation:
implementation "com.luminiadev.eventbus:core:1.0.0-SNAPSHOT"Inspired by the AllayMC event system, I took some code from here.