A simple library for dispatching events and subscribing to them
Subscription and event dispatch
EventBuseventBus = newEventBusImpl();
eventBus.subscribe(ChatMessageEvent.class, event -> {
System.out.println("Received ChatMessageEvent: " + event.getMessage());
if (shouldCancel) {
event.cancel();
}
});
eventBus.subscribe(newAsyncChatListener());
eventBus.call(newChatMessageEvent("Chat message"));Registering custom subscription annotation resolver
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MySubscribe {
intpriority() default0;
booleanasync() defaultfalse;
booleanhandleCancelled() defaulttrue;
}
eventBus.registerAnnotationResolver(
SubscriptionAnnotationResolver.of(
MySubscribe.class,
annotation -> newSubscriptionAnnotationInfo(
annotation.priority(),
annotation.async(),
annotation.handleCancelled()
)
)
);
eventBus.removeAnnotationResolver(MySubscribe.class);Subscribing to events using the @Subscribe annotation
publicclassAsyncChatListener {
@Subscribe(priority = EventPriorities.FIRST, handleCancelled = true, async = true)
publicvoidonChatMessage(ChatMessageEventevent) {
System.out.println("Async chat event: " + event);
}
}Unsubscribe from events
// Subscribing to eventsSubscriber<ChatMessageEvent> subscriber = eventBus.subscribe(ChatMessageEvent.class, event -> {
System.out.println("Received ChatMessageEvent: " + event.getMessage());
});
AsyncChatListenerasyncChatListener = newAsyncChatListener();
eventBus.subscribe(asyncChatListener);
eventBus.call(newChatMessageEvent("Chat message"));
// Unsubscribe from eventseventBus.unsubscribe(asyncChatListener);
eventBus.unsubscribe(subscriber);Working with event result
CallResultresult = eventBus.callSilently(newChatMessageEvent("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
publicclassChatMessageEventimplementsEvent, Cancellable {
privatefinalStringmessage;
privatebooleancancelled;
publicChatMessageEvent(Stringmessage) {
this.message = message;
}
publicStringgetMessage() {
returnmessage;
}
@OverridepublicbooleanisCancelled() {
returncancelled;
}
@OverridepublicvoidsetCancelled(booleancancelled) {
this.cancelled = cancelled;
}
}Adding repository:
<repositories>
<repository>
<id>densy-repository-snapshots</id>
<url>https://repo.densy.org/snapshots</url>
</repository>
</repositories>Adding a library api:
<dependency>
<groupId>org.densy.eventbus</groupId>
<artifactId>api</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>Adding a library implementation:
<dependency>
<groupId>org.densy.eventbus</groupId>
<artifactId>core</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>Adding repository:
maven {
name ="densyRepositorySnapshots"
url = uri("https://repo.densy.org/snapshots")
}Adding a library api:
implementation "org.densy.eventbus:api:1.0.0-SNAPSHOT"Adding a library implementation:
implementation "org.densy.eventbus:core:1.0.0-SNAPSHOT"Inspired by the AllayMC event system, I took some code from here.