Skip to content

Repository files navigation

EventBus

A lightweight Kotlin event bus with listener priorities, event cancellation and annotation-based subscription.

Install

Maven Central

repositories { mavenCentral() }

dependencies {
    implementation("org.noamm:eventbus:1.0.2")
}

Usage

Define events by extending Event:

class PlayerJoinEvent(val name: String) : Event()
class DamageEvent : Event(cancelable = true)

Create a bus and post events:

val bus = bus { setErrorHandler { throwable -> throw throwable } }

bus.post(PlayerJoinEvent("Notch"))
bus.post(DamageEvent())

Annotated listeners

Mark methods with @SubscribeEvent and register the subscriber object:

val playerTracker = object {
    @SubscribeEvent(priority = EventPriority.HIGH)
    fun onJoin(event: PlayerJoinEvent) = println("${event.name} joined")
}

bus.subscribe(playerTracker)
bus.unsubscribe(playerTracker)

Lambda listeners

Register inline listeners for an event class:

bus.register<DamageEvent> { event ->
    event.isCanceled = true
}

bus.once<PlayerJoinEvent> { }

register returns the EventListener so you can unregister() it later.

Behavior

  • Listeners run from HIGHEST to LOWEST priority.
  • Canceling an event skips listeners not registered with receiveCancelled = true.
  • Exceptions thrown by listeners are routed to the error handler configured on the EventBusBuilder (rethrown by default).

License

CC0 1.0 Universal

About

Kotlin Eventbus

Resources

Stars

1 star

Watchers

0 watching

Forks

Used by

Contributors

Languages

Generated from Noamm-Dev/SimpleKotlin