Skip to content

Commit

Permalink
feat: Update event listener (#1)
Browse files Browse the repository at this point in the history
* Update event listener to more easy

* Update README.md
  • Loading branch information
LeagueLugas authored Aug 29, 2024
1 parent 5c307fa commit f4bdb78
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 30 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ class TestCommand : SpringCommand {
}
```
### Event Listener
You need to implement the SpringListener<T : Event> interface, and if this interface is not present, an error will occur when loading the plugin.
You need to implement the SpringListener interface, and if this interface is not present, an error will occur when loading the plugin.
```kotlin
@EventListener
class TestListener : SpringListener<InventoryOpenEvent> {
override fun onEvent(event: InventoryOpenEvent) {
class TestListener : SpringListener {
@EventHandler // org.bukkit.event
fun onEvent(event: InventoryOpenEvent) {
val player = event.player
player.sendMessage(MessageUtil.color("Opened inventory"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import io.github.leaguelugas.springmc.SpringMC
import io.github.leaguelugas.springmc.di.BeanResolver
import io.github.leaguelugas.springmc.di.annotations.EventListener
import io.github.leaguelugas.springmc.event.SpringListener
import org.bukkit.event.Event

class EventListenerResolver(
private val plugin: SpringMC,
Expand All @@ -14,34 +13,14 @@ class EventListenerResolver(
annotation: EventListener,
): Any {
val listenerClass = "${instance.javaClass.`package`}.${instance.javaClass.simpleName}"
if (instance !is SpringListener<*>) {
if (instance !is SpringListener) {
throw IllegalArgumentException(
"Event-Listener ($listenerClass) must implement SpringListener interface",
)
}

val eventClass: Class<out Event> =
instance.javaClass.declaredMethods
.find { it.name == "onEvent" && it.parameters.isNotEmpty() }
?.parameters
?.let {
it.forEach { p ->
runCatching {
return@let p.type.asSubclass(Event::class.java)
}
}
return@let null
}
?: throw IllegalArgumentException(
"Event-Listener ($listenerClass) must have a method named 'onEvent' with at least one parameter extending Event",
)

plugin.afterLoadingJob.add {
plugin.server.pluginManager.registerEvent(eventClass, instance, annotation.priority, { listener, event ->
if (listener is SpringListener<Event>) {
listener.onEvent(event)
}
}, plugin)
plugin.server.pluginManager.registerEvents(instance, plugin)
}

return instance
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package io.github.leaguelugas.springmc.event

import org.bukkit.event.Event
import org.bukkit.event.Listener

interface SpringListener<out T : Event> : Listener {
fun onEvent(event: @UnsafeVariance T)
}
interface SpringListener : Listener

0 comments on commit f4bdb78

Please sign in to comment.