Skip to content

Commit

Permalink
Merge pull request #480 from Ling556/dev/6.2.0
Browse files Browse the repository at this point in the history
AfyBroker事件监听器注册
  • Loading branch information
Bkm016 authored Oct 19, 2024
2 parents 404a956 + 31f170f commit dbeff95
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class EventBus : ClassVisitor(-1) {
Platform.BUKKIT -> registerBukkit(method, optionalEvent, anno, obj)
Platform.BUNGEE -> registerBungee(method, optionalEvent, anno, obj)
Platform.VELOCITY -> registerVelocity(method, optionalEvent, anno, obj)
Platform.AFYBROKER -> registerAfyBroker(method, optionalEvent, anno, obj)
else -> {}
}
}
Expand Down Expand Up @@ -94,7 +95,19 @@ class EventBus : ClassVisitor(-1) {
registerBungeeListener(listenType, level, ignoreCancelled) { invoke(obj, method, it) }
}
}

private fun registerAfyBroker(method: ClassMethod, optionalBind: Class<*>?, event: ClassAnnotation, obj: Any?) {
val annoLevel = event.property("level", -1)
val level = if (annoLevel != 0) annoLevel else event.enum("priority", EventPriority.NORMAL).level
val ignoreCancelled = event.property("ignoreCancelled", false)
val listenType = method.parameterTypes[0]
if (listenType == OptionalEvent::class.java) {
if (optionalBind != null) {
registerAfyBrokerListener(optionalBind, level, ignoreCancelled) { invoke(obj, method, it, true) }
}
} else {
registerAfyBrokerListener(listenType, level, ignoreCancelled) { invoke(obj, method, it) }
}
}
private fun registerVelocity(method: ClassMethod, optionalBind: Class<*>?, event: ClassAnnotation, obj: Any?) {
val postOrder = event.enum("postOrder", PostOrder.NORMAL)
val listenType = method.parameterTypes[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ fun <T> registerBungeeListener(
}
}

/**
* 注册一个 AfyBroker 监听器
*
* @param event 事件
* @param level 优先级
* @param ignoreCancelled 是否忽略取消事件
* @param func 事件处理函数
* @return 监听器
*/
fun <T> registerAfyBrokerListener(
event: Class<T>,
level: Int = 0,
ignoreCancelled: Boolean = false,
func: Closeable.(T) -> Unit,
): ProxyListener {
listenEvents += event
val closeableListener = CloseableListener()
return PlatformFactory.getService<PlatformListener>().registerListener(event, level, ignoreCancelled) { func(closeableListener, it) }.also {
closeableListener.proxyListener = it
}
}

/**
* 注册一个 Velocity 监听器
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import taboolib.common.platform.Platform
import taboolib.common.platform.PlatformSide
import taboolib.common.platform.event.EventPriority
import taboolib.common.platform.event.ProxyListener
import taboolib.common.platform.function.info
import taboolib.common.platform.service.PlatformListener
import taboolib.common.util.unsafeLazy
import java.lang.reflect.Method
Expand Down Expand Up @@ -39,9 +40,9 @@ class AfyBrokerListener : PlatformListener {
@Suppress("UNCHECKED_CAST")
override fun <T> registerListener(event: Class<T>, level: Int, ignoreCancelled: Boolean, func: (T) -> Unit): ProxyListener {
val listener = AfyBrokerListener(event, level) { func(it as T) }
val priority = byListenerAndPriority.computeIfAbsent(event) { HashMap() }
val listenerMap = priority.computeIfAbsent(level.toByte()) { HashMap() }
listenerMap[listener] = arrayOf(AfyBrokerListener.handleMethod)
val prioritiesMap = byListenerAndPriority.computeIfAbsent(event) { HashMap() }
val currentPriorityMap = prioritiesMap.computeIfAbsent(level.toByte()) { HashMap() }
currentPriorityMap[listener] = arrayOf(AfyBrokerListener.handleMethod)
eventBus.invokeMethod<Void>("bakeHandlers", event)
return listener
}
Expand Down

0 comments on commit dbeff95

Please sign in to comment.