Skip to content

Commit

Permalink
[6.2.0] 优化 "nmsProxy" 函数
Browse files Browse the repository at this point in the history
  • Loading branch information
Bkm016 committed Oct 16, 2024
1 parent 5ba69b7 commit f99bcfa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group=taboolib
version=6.2.0-beta18
version=6.2.0-beta19
kotlin.incremental=true
kotlin.incremental.java=true
kotlin.caching.enabled=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,29 @@ fun <T> nmsProxy(clazz: Class<T>, bind: String = "{name}Impl", parent: List<Stri
if (nmsProxyInstanceMap.containsKey(key)) {
return nmsProxyInstanceMap[key] as T
}
// 获取合适的构造函数并创建实例
fun <T> createInstance(clazz: Class<T>, parameters: Array<out Any>): T {
// 遍历所有构造函数
val constructors = clazz.declaredConstructors
for (constructor in constructors) {
val parameterTypes = constructor.parameterTypes
if (parameterTypes.size != parameters.size) continue
var isMatch = true
for (i in parameterTypes.indices) {
if (!parameterTypes[i].isAssignableFrom(parameters[i].javaClass)) {
isMatch = false
break
}
}
if (isMatch) {
constructor.isAccessible = true
return constructor.newInstance(*parameters) as T
}
}
throw NoSuchMethodException("没有找到匹配的构造函数: ${clazz.name}")
}
// 获取代理类并实例化
val newInstance = nmsProxyClass(clazz, bind, parent).getDeclaredConstructor(*parameter.map { it.javaClass }.toTypedArray()).newInstance(*parameter)
val newInstance = createInstance(nmsProxyClass(clazz, bind, parent), parameter)
// 缓存实例
nmsProxyInstanceMap[key] = newInstance!!
return newInstance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ object ProtocolHandler : OpenListener {
* 卸载 LightInjector,需要其他插件立刻顶替
*/
@Awake(LifeCycle.DISABLE)
private fun onDisable() {
private fun onDisable(s: String) {
if (TabooLib.isStopped()) {
return
}
Expand All @@ -147,8 +147,14 @@ object ProtocolHandler : OpenListener {
instance?.close()
// 通知其他插件立刻接管
val next = containers.firstOrNull { it.name != pluginId }
if (next != null && next.call(PACKET_LISTENER_EJECT, arrayOf()).isSuccessful) {
debug("LightInjector closed, current packet listener is taken over by ${next.name}.")
if (next != null) {
try {
if (next.call(PACKET_LISTENER_EJECT, arrayOf()).isSuccessful) {
debug("LightInjector closed, current packet listener is taken over by ${next.name}.")
}
} catch (ex: IllegalStateException) {
if (ex.message != "zip file closed") ex.printStackTrace()
}
}
}
}
Expand Down

0 comments on commit f99bcfa

Please sign in to comment.