From 7f1ce11d9d7b71b03fc0d2bbd9ab40edc8c82efb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=91?= Date: Sun, 22 Sep 2024 00:27:47 +0800 Subject: [PATCH] =?UTF-8?q?[6.2.0][dev]=20=E6=95=B0=E6=8D=AE=E5=8C=85?= =?UTF-8?q?=E5=BA=95=E5=B1=82=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../taboolib/common/platform/function/IO.kt | 10 +- .../util/{Executor.kt => SyncExecutor.kt} | 0 .../main/java/taboolib/common/OpenAPI.java | 2 + .../java/taboolib/common/OpenContainer.java | 2 +- .../java/taboolib/common/OpenListener.java | 2 +- .../main/java/taboolib/common/OpenResult.java | 6 +- .../kotlin/taboolib/common/io/ProjectInfo.kt | 2 + .../common/util/{Executor.kt => Execution.kt} | 0 gradle.properties | 2 +- .../taboolib/module/nms/LightInjector.java | 4 +- .../module/nms/LightInjectorImpl.java | 28 ++- .../kotlin/taboolib/module/nms/Exchanges.kt | 14 +- .../module/nms/MinecraftServerUtil.kt | 7 - .../kotlin/taboolib/module/nms/PacketImpl.kt | 2 +- .../taboolib/module/nms/PacketReceiveEvent.kt | 7 +- .../taboolib/module/nms/PacketSendEvent.kt | 7 +- .../taboolib/module/nms/ProtocolHandler.kt | 183 ++++++++++++++++-- .../module/lang/inline/InlineLanguage.kt | 1 - .../module/kether/RemoteScriptProperty.kt | 4 +- .../platform/type/AfyBrokerContainer.kt | 24 ++- .../taboolib/platform/type/BukkitContainer.kt | 24 ++- .../taboolib/platform/type/BungeeContainer.kt | 24 ++- .../platform/type/VelocityContainer.kt | 24 ++- settings.gradle.kts | 1 - 24 files changed, 284 insertions(+), 96 deletions(-) rename common-platform-api/src/main/kotlin/taboolib/common/util/{Executor.kt => SyncExecutor.kt} (100%) rename common-util/src/main/kotlin/taboolib/common/util/{Executor.kt => Execution.kt} (100%) diff --git a/common-platform-api/src/main/kotlin/taboolib/common/platform/function/IO.kt b/common-platform-api/src/main/kotlin/taboolib/common/platform/function/IO.kt index 9ffdeb292..18289d1f0 100644 --- a/common-platform-api/src/main/kotlin/taboolib/common/platform/function/IO.kt +++ b/common-platform-api/src/main/kotlin/taboolib/common/platform/function/IO.kt @@ -1,7 +1,7 @@ package taboolib.common.platform.function import taboolib.common.PrimitiveIO -import taboolib.common.io.isDevelopmentMode +import taboolib.common.io.isDebugMode import taboolib.common.io.newFile import taboolib.common.io.runningResources import taboolib.common.platform.PlatformFactory @@ -18,14 +18,12 @@ fun server(): T { } /** - * 打印开发者日志 + * 打印调试信息 * * @param message 日志内容 */ -fun dev(vararg message: Any?) { - if (isDevelopmentMode) { - message.filterNotNull().forEach { PrimitiveIO.debug(it) } - } +fun debug(vararg message: Any?) { + if (isDebugMode) message.filterNotNull().forEach { PrimitiveIO.debug(it) } } /** diff --git a/common-platform-api/src/main/kotlin/taboolib/common/util/Executor.kt b/common-platform-api/src/main/kotlin/taboolib/common/util/SyncExecutor.kt similarity index 100% rename from common-platform-api/src/main/kotlin/taboolib/common/util/Executor.kt rename to common-platform-api/src/main/kotlin/taboolib/common/util/SyncExecutor.kt diff --git a/common-util/src/main/java/taboolib/common/OpenAPI.java b/common-util/src/main/java/taboolib/common/OpenAPI.java index 43ed034d4..fa720a096 100644 --- a/common-util/src/main/java/taboolib/common/OpenAPI.java +++ b/common-util/src/main/java/taboolib/common/OpenAPI.java @@ -15,10 +15,12 @@ public class OpenAPI { @NotNull public static OpenResult call(String name, Object[] data) { + // PrimitiveIO.debug("OpenAPI call {0}", name); for (Map.Entry entry : TabooLib.getAwakenedClasses().entrySet()) { if (entry.getValue() instanceof OpenListener) { OpenResult result = ((OpenListener) entry.getValue()).call(name, data); if (result.isSuccessful()) { + // PrimitiveIO.debug(" Handle by {0}", entry.getKey()); return result; } } diff --git a/common-util/src/main/java/taboolib/common/OpenContainer.java b/common-util/src/main/java/taboolib/common/OpenContainer.java index 47971cf35..0d9951929 100644 --- a/common-util/src/main/java/taboolib/common/OpenContainer.java +++ b/common-util/src/main/java/taboolib/common/OpenContainer.java @@ -25,5 +25,5 @@ public interface OpenContainer { * @param args 参数 */ @NotNull - OpenResult call(String name, @NotNull Object[] args); + OpenResult call(String name, Object[] args); } diff --git a/common-util/src/main/java/taboolib/common/OpenListener.java b/common-util/src/main/java/taboolib/common/OpenListener.java index bdeaad6e5..62c6d0757 100644 --- a/common-util/src/main/java/taboolib/common/OpenListener.java +++ b/common-util/src/main/java/taboolib/common/OpenListener.java @@ -18,5 +18,5 @@ public interface OpenListener { * @param data 数据 * @return boolean 是否注册成功 */ - OpenResult call(@NotNull String name, @NotNull Object[] data); + OpenResult call(@NotNull String name, Object[] data); } diff --git a/common-util/src/main/java/taboolib/common/OpenResult.java b/common-util/src/main/java/taboolib/common/OpenResult.java index 9daf3038c..4ab656622 100644 --- a/common-util/src/main/java/taboolib/common/OpenResult.java +++ b/common-util/src/main/java/taboolib/common/OpenResult.java @@ -2,7 +2,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.tabooproject.reflex.AnalyseMode; import org.tabooproject.reflex.Reflex; public class OpenResult { @@ -74,7 +73,10 @@ public static OpenResult failed() { return new OpenResult(false, null); } - public static OpenResult deserialize(Object source) { + /** + * 从其他插件的 OpenResult 转换为当前插件的 OpenResult + */ + public static OpenResult cast(Object source) { Object successful = Reflex.Companion.getLocalProperty(source, "successful"); Object value = Reflex.Companion.getLocalProperty(source, "value"); return new OpenResult(Boolean.TRUE.equals(successful), value); diff --git a/common-util/src/main/kotlin/taboolib/common/io/ProjectInfo.kt b/common-util/src/main/kotlin/taboolib/common/io/ProjectInfo.kt index ebe931fb0..333f8a4ea 100644 --- a/common-util/src/main/kotlin/taboolib/common/io/ProjectInfo.kt +++ b/common-util/src/main/kotlin/taboolib/common/io/ProjectInfo.kt @@ -39,6 +39,8 @@ val taboolibPath: String /** * 特定类的组名 + * 传入:org.tabooproject.taboolib.platform.BukkitPlugin + * 传出:org.tabooproject */ val Class<*>.groupId: String get() = name.substringBefore(taboolibId).dropLast(1) \ No newline at end of file diff --git a/common-util/src/main/kotlin/taboolib/common/util/Executor.kt b/common-util/src/main/kotlin/taboolib/common/util/Execution.kt similarity index 100% rename from common-util/src/main/kotlin/taboolib/common/util/Executor.kt rename to common-util/src/main/kotlin/taboolib/common/util/Execution.kt diff --git a/gradle.properties b/gradle.properties index 21e452938..01679f038 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ group=taboolib -version=6.2.0-beta10 +version=6.2.0-beta11 kotlin.incremental=true kotlin.incremental.java=true kotlin.caching.enabled=true diff --git a/module/bukkit-nms/src/main/java/taboolib/module/nms/LightInjector.java b/module/bukkit-nms/src/main/java/taboolib/module/nms/LightInjector.java index 854258ced..5064acbca 100644 --- a/module/bukkit-nms/src/main/java/taboolib/module/nms/LightInjector.java +++ b/module/bukkit-nms/src/main/java/taboolib/module/nms/LightInjector.java @@ -41,6 +41,7 @@ import taboolib.common.PrimitiveIO; import taboolib.common.reflect.ClassHelper; +import java.io.Closeable; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.*; @@ -61,7 +62,7 @@ * * @author fren_gor */ -public abstract class LightInjector { +public abstract class LightInjector implements Closeable { private static final Class SERVER_CLASS = getNMSClass("MinecraftServer", "server"); private static final Class SERVER_CONNECTION_CLASS = getNMSClass("ServerConnection", "server.network"); @@ -337,6 +338,7 @@ public final void receivePacket(@NotNull Channel channel, @NotNull Object packet * If this injector is already closed then invoking this method has no effect. * 如果此注入器已经关闭,则调用此方法无效。 */ + @Override public final void close() { if (closed.getAndSet(true)) { return; diff --git a/module/bukkit-nms/src/main/java/taboolib/module/nms/LightInjectorImpl.java b/module/bukkit-nms/src/main/java/taboolib/module/nms/LightInjectorImpl.java index a3f96009a..88fd9bc79 100644 --- a/module/bukkit-nms/src/main/java/taboolib/module/nms/LightInjectorImpl.java +++ b/module/bukkit-nms/src/main/java/taboolib/module/nms/LightInjectorImpl.java @@ -34,12 +34,18 @@ public LightInjectorImpl(@NotNull Plugin plugin) { protected @Nullable Object onPacketReceiveAsync(@Nullable Player sender, @NotNull Channel channel, @NotNull Object packet) { if (sender != null) { PacketReceiveEvent event = new PacketReceiveEvent(sender, new PacketImpl(packet)); - event.call(); - return event.getPacket().getSource(); + if (event.callIf()) { + return ProtocolHandler.INSTANCE.handlePacket(ProtocolHandler.PACKET_RECEIVE, sender, channel, event.getPacket().getSource()); + } else { + return null; + } } else { PacketReceiveEvent.Handshake event = new PacketReceiveEvent.Handshake(channel, new PacketImpl(packet)); - event.call(); - return event.getPacket().getSource(); + if (event.callIf()) { + return ProtocolHandler.INSTANCE.handlePacket(ProtocolHandler.PACKET_RECEIVE, null, channel, event.getPacket().getSource()); + } else { + return null; + } } } @@ -47,12 +53,18 @@ public LightInjectorImpl(@NotNull Plugin plugin) { protected @Nullable Object onPacketSendAsync(@Nullable Player receiver, @NotNull Channel channel, @NotNull Object packet) { if (receiver != null) { PacketSendEvent event = new PacketSendEvent(receiver, new PacketImpl(packet)); - event.call(); - return event.getPacket().getSource(); + if (event.callIf()) { + return ProtocolHandler.INSTANCE.handlePacket(ProtocolHandler.PACKET_SEND, receiver, channel, event.getPacket().getSource()); + } else { + return null; + } } else { PacketSendEvent.Handshake event = new PacketSendEvent.Handshake(channel, new PacketImpl(packet)); - event.call(); - return event.getPacket().getSource(); + if (event.callIf()) { + return ProtocolHandler.INSTANCE.handlePacket(ProtocolHandler.PACKET_SEND, null, channel, event.getPacket().getSource()); + } else { + return null; + } } } } diff --git a/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/Exchanges.kt b/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/Exchanges.kt index 5173fdf43..21c81df65 100644 --- a/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/Exchanges.kt +++ b/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/Exchanges.kt @@ -23,8 +23,13 @@ import java.util.logging.Logger @Suppress("UNCHECKED_CAST") object Exchanges { + // Minecraft 语言文件缓存 const val MINECRAFT_LANGUAGE = "minecraft_language" + + // Spigot 映射表 const val MAPPING_SPIGOT = "mapping_spigot" + + // Paper 映射表 const val MAPPING_PAPER = "mapping_paper" private val map: MutableMap @@ -53,6 +58,13 @@ object Exchanges { return map[key] as T } + /** + * 读取数据,如果不存在则写入默认值 + */ + fun getOrPut(key: String, defaultValue: () -> T): T { + return map.getOrPut(key) { defaultValue()!! } as T + } + /** * 写入数据 */ @@ -151,7 +163,7 @@ object Exchanges { } override fun getName(): String { - return "TabooLibExchange" + return "TabooLibExchange/v1" } } // endregion diff --git a/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/MinecraftServerUtil.kt b/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/MinecraftServerUtil.kt index 32b511e08..c98f8e01f 100644 --- a/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/MinecraftServerUtil.kt +++ b/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/MinecraftServerUtil.kt @@ -75,13 +75,6 @@ fun nmsClass(name: String): Class<*> { } } -/** - * 禁用数据包监听器 - */ -fun disablePacketListener() { - ProtocolHandler.disable() -} - @Synchronized fun nmsProxy(clazz: Class, bind: String = "{name}Impl", vararg parameter: Any): T { return nmsProxy(clazz, bind, emptyList(), *parameter) diff --git a/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/PacketImpl.kt b/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/PacketImpl.kt index 71975aa14..e48dddf17 100644 --- a/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/PacketImpl.kt +++ b/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/PacketImpl.kt @@ -58,6 +58,6 @@ class PacketImpl(override var source: Any) : Packet() { companion object { - val spigotNameCache = ConcurrentHashMap>() + val spigotNameCache = Exchanges.getOrPut("packet_spigot_name_cache") { ConcurrentHashMap>() } } } \ No newline at end of file diff --git a/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/PacketReceiveEvent.kt b/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/PacketReceiveEvent.kt index 7825170a6..7d59f5ee0 100644 --- a/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/PacketReceiveEvent.kt +++ b/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/PacketReceiveEvent.kt @@ -5,11 +5,8 @@ import org.bukkit.entity.Player import taboolib.common.event.CancelableInternalEvent /** - * TabooLib - * taboolib.module.nms.PacketReceiveEvent - * - * @author sky - * @since 2021/6/24 5:38 下午 + * 数据包接收事件,可用于拦截数据包 + * 自 6.2 版本起,不能放行已被拦截的数据包(即使用 isCancelled = false) */ class PacketReceiveEvent(val player: Player, val packet: Packet) : CancelableInternalEvent() { diff --git a/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/PacketSendEvent.kt b/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/PacketSendEvent.kt index 6cb51848e..8313e3e7b 100644 --- a/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/PacketSendEvent.kt +++ b/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/PacketSendEvent.kt @@ -5,11 +5,8 @@ import org.bukkit.entity.Player import taboolib.common.event.CancelableInternalEvent /** - * TabooLib - * taboolib.module.nms.PacketSendEvent - * - * @author sky - * @since 2021/6/24 5:38 下午 + * 数据包发送事件,可用于拦截数据包 + * 自 6.2 版本起,不能放行已被拦截的数据包(即使用 isCancelled = false) */ class PacketSendEvent(val player: Player, val packet: Packet) : CancelableInternalEvent() { diff --git a/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/ProtocolHandler.kt b/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/ProtocolHandler.kt index 94a392acf..0b31bbb1c 100644 --- a/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/ProtocolHandler.kt +++ b/module/bukkit-nms/src/main/kotlin/taboolib/module/nms/ProtocolHandler.kt @@ -1,13 +1,18 @@ package taboolib.module.nms -import taboolib.common.Inject -import taboolib.common.LifeCycle -import taboolib.common.TabooLib +import io.netty.channel.Channel +import org.bukkit.entity.Player +import org.bukkit.event.server.PluginDisableEvent +import org.bukkit.event.server.PluginEnableEvent +import taboolib.common.* +import taboolib.common.event.InternalEventBus import taboolib.common.platform.Awake import taboolib.common.platform.Platform import taboolib.common.platform.PlatformSide -import taboolib.common.platform.function.dev -import taboolib.common.platform.function.isListened +import taboolib.common.platform.event.SubscribeEvent +import taboolib.common.platform.function.debug +import taboolib.common.platform.function.getOpenContainers +import taboolib.common.platform.function.pluginId import taboolib.platform.BukkitPlugin /** @@ -15,10 +20,21 @@ import taboolib.platform.BukkitPlugin * @since 2018-10-28 14:34 */ @Inject +@Awake @PlatformSide(Platform.BUKKIT) -object ProtocolHandler { +object ProtocolHandler : OpenListener { - private var isDisabled = false + // 数据包监听器 + const val PACKET_LISTENER = "packet_listener/v1" + + // 数据包监听器注销 + const val PACKET_LISTENER_EJECT = "packet_listener_eject/v1" + + // 数据包接收 + const val PACKET_RECEIVE = "packet_receive/v1" + + // 数据包发送 + const val PACKET_SEND = "packet_send/v1" /** * 使用 LightInjector 作为 TabooLib 数据包实现。 @@ -30,33 +46,166 @@ object ProtocolHandler { private var instance: LightInjector? = null /** - * 禁用 Channel 注入 + * 当前所有启用数据包共享的 OpenContainer 缓存 + * 当插件加载或卸载时,缓存会被更新 + */ + private var containers = listOf() + + /** + * 更新 OpenContainer 缓存 + */ + private fun updateContainer() { + containers = getOpenContainers().filter { Exchanges.contains(PACKET_LISTENER + "/plugin/" + it.name) } + } + + /** + * 当前插件是否已经注入数据包监听器 + */ + fun isInjected(): Boolean { + return instance != null + } + + /** + * 向其他 TabooLib 插件共享数据包事件 + * + * @param method API 名,指定为 PACKET_RECEIVE 或 PACKET_SEND + * @param sender 数据包发送者或接收者 + * @param channel 数据包通道 + * @param packet 数据包原始对象 + * @return 返回值将会替换原始数据包,如果为 null 则表示已被拦截 + */ + fun handlePacket(method: String, sender: Player?, channel: Channel, packet: Any): Any? { + var current = packet + var isCancelled = false + containers.forEach { + val result = it.call(method, arrayOf(sender, channel, packet)) + if (result.isSuccessful) { + current = result.value ?: current + } else { + isCancelled = true + // debug("Packet ${packet.javaClass.name} cancelled by ${it.name}.") + } + } + return if (isCancelled) null else current + } + + + /** + * 当前插件是否监听了数据包事件 */ - fun disable() { - isDisabled = true + private fun isPacketEventListened(): Boolean { + return InternalEventBus.isListening(PacketReceiveEvent::class.java) + || InternalEventBus.isListening(PacketReceiveEvent.Handshake::class.java) + || InternalEventBus.isListening(PacketSendEvent::class.java) + || InternalEventBus.isListening(PacketSendEvent.Handshake::class.java) } /** - * 数据包事件是否被当前插件监听 + * 立刻向服务端注入数据包监听器 + * 并且更新 OpenContainer 缓存 */ - fun isPacketEventListened(): Boolean { - return PacketSendEvent::class.java.isListened() || PacketReceiveEvent::class.java.isListened() + private fun injectPacketListener() { + instance = LightInjectorImpl(BukkitPlugin.getInstance()) + Exchanges[PACKET_LISTENER] = pluginId + updateContainer() + debug("LightInjector initialized.") } + /** + * 初始化 LightInjector + */ @Awake(LifeCycle.ENABLE) private fun onEnable() { - if (TabooLib.isStopped() || isDisabled || !isPacketEventListened()) { + if (TabooLib.isStopped()) { return } - instance = LightInjectorImpl(BukkitPlugin.getInstance()) - dev("LightInjector initialized.") + // 所有 TabooLib 6.2 只注入一个数据包监听器 + // 借助 Exchanges 和 OpenAPI 减少资源损耗 + if (Exchanges.contains(PACKET_LISTENER)) { + // 是否监听了数据包相关事件 + // 只有在 Exchanges 中标记本插件,才会收到共享的数据包事件 + if (isPacketEventListened()) { + Exchanges["$PACKET_LISTENER/plugin/$pluginId"] = true + debug("LightInjector already initialized by other plugin.") + } + } else { + injectPacketListener() + } } + /** + * 卸载 LightInjector,需要其他插件立刻顶替 + */ @Awake(LifeCycle.DISABLE) private fun onDisable() { if (TabooLib.isStopped()) { return } - instance?.close() + if (instance != null) { + // 注销数据包监听器 + instance?.close() + // 通知其他插件立刻接管 + containers.firstOrNull()?.call(PACKET_LISTENER_EJECT, arrayOf()) + // 提示新的接管者 + debug("LightInjector closed, current packet listener is taken over by ${Exchanges.get(PACKET_LISTENER)}.") + } + } + + @Awake(LifeCycle.ACTIVE) + private fun onActive() { + if (instance != null) { + updateContainer() + } + } + + @SubscribeEvent + private fun onEnabled(e: PluginEnableEvent) { + if (instance != null) { + updateContainer() + } + } + + @SubscribeEvent + private fun onDisable(e: PluginDisableEvent) { + if (instance != null) { + updateContainer() + } + } + + override fun call(name: String, data: Array): OpenResult { + when (name) { + // 数据包接收 + PACKET_RECEIVE -> { + val player = data[0] as? Player + val channel = data[1] as Channel + val packet = data[2] as Any + if (player != null) { + val event = PacketReceiveEvent(player, PacketImpl(packet)) + return if (event.callIf()) OpenResult.successful(event.packet.source) else OpenResult.failed() + } else { + val event = PacketReceiveEvent.Handshake(channel, PacketImpl(packet)) + return if (event.callIf()) OpenResult.successful(event.packet.source) else OpenResult.failed() + } + } + // 数据包发送 + PACKET_SEND -> { + val player = data[0] as? Player + val channel = data[1] as Channel + val packet = data[2] as Any + if (player != null) { + val event = PacketSendEvent(player, PacketImpl(packet)) + return if (event.callIf()) OpenResult.successful(event.packet.source) else OpenResult.failed() + } else { + val event = PacketSendEvent.Handshake(channel, PacketImpl(packet)) + return if (event.callIf()) OpenResult.successful(event.packet.source) else OpenResult.failed() + } + } + // 数据包监听器注销 + PACKET_LISTENER_EJECT -> { + injectPacketListener() + return OpenResult.successful() + } + } + return OpenResult.failed() } } \ No newline at end of file diff --git a/module/minecraft/minecraft-i18n/src/main/kotlin/taboolib/module/lang/inline/InlineLanguage.kt b/module/minecraft/minecraft-i18n/src/main/kotlin/taboolib/module/lang/inline/InlineLanguage.kt index 70818e693..3ef7b9533 100644 --- a/module/minecraft/minecraft-i18n/src/main/kotlin/taboolib/module/lang/inline/InlineLanguage.kt +++ b/module/minecraft/minecraft-i18n/src/main/kotlin/taboolib/module/lang/inline/InlineLanguage.kt @@ -1,6 +1,5 @@ package taboolib.module.lang.inline -import taboolib.common.platform.function.dev import taboolib.library.configuration.ConfigurationSection import taboolib.module.configuration.util.getStringColored import taboolib.module.configuration.util.getStringListColored diff --git a/module/minecraft/minecraft-kether/src/main/kotlin/taboolib/module/kether/RemoteScriptProperty.kt b/module/minecraft/minecraft-kether/src/main/kotlin/taboolib/module/kether/RemoteScriptProperty.kt index 852101eee..6f9e8f3ae 100644 --- a/module/minecraft/minecraft-kether/src/main/kotlin/taboolib/module/kether/RemoteScriptProperty.kt +++ b/module/minecraft/minecraft-kether/src/main/kotlin/taboolib/module/kether/RemoteScriptProperty.kt @@ -14,10 +14,10 @@ import taboolib.common.OpenResult class RemoteScriptProperty(val remote: OpenContainer, val source: Any, id: String) : ScriptProperty(id) { override fun read(instance: Any, key: String): OpenResult { - return OpenResult.deserialize(source.invokeMethod("read", instance, key, remap = false)) + return OpenResult.cast(source.invokeMethod("read", instance, key, remap = false)) } override fun write(instance: Any, key: String, value: Any?): OpenResult { - return OpenResult.deserialize(source.invokeMethod("write", instance, key, value, remap = false)) + return OpenResult.cast(source.invokeMethod("write", instance, key, value, remap = false)) } } \ No newline at end of file diff --git a/platform/platform-afybroker/src/main/kotlin/taboolib/platform/type/AfyBrokerContainer.kt b/platform/platform-afybroker/src/main/kotlin/taboolib/platform/type/AfyBrokerContainer.kt index ff9e9a296..d045dda3b 100644 --- a/platform/platform-afybroker/src/main/kotlin/taboolib/platform/type/AfyBrokerContainer.kt +++ b/platform/platform-afybroker/src/main/kotlin/taboolib/platform/type/AfyBrokerContainer.kt @@ -1,9 +1,13 @@ package taboolib.platform.type import net.afyer.afybroker.server.plugin.Plugin +import org.tabooproject.reflex.Reflex.Companion.invokeLocalMethod import org.tabooproject.reflex.Reflex.Companion.invokeMethod import taboolib.common.OpenContainer import taboolib.common.OpenResult +import taboolib.common.io.groupId +import taboolib.common.io.isDebugMode +import taboolib.common.io.taboolibId /** * TabooLib @@ -12,24 +16,26 @@ import taboolib.common.OpenResult * @author Ling556 * @since 2024/5/09 23:51 */ -class AfyBrokerContainer(plugin: Plugin) : OpenContainer { +class AfyBrokerContainer(val plugin: Plugin) : OpenContainer { - private val name = plugin.description.name - private val main = plugin.description.main!! - private val clazz = try { - Class.forName(main.substring(0, main.length - "platform.AfyBrokerPlugin".length) + "common.OpenAPI") - } catch (ignored: Throwable) { + // 获取目标插件的 OpenAPI 类 + private val api = try { + Class.forName("${plugin::class.java.groupId}.$taboolibId.common.OpenAPI") + } catch (ex: ClassNotFoundException) { + // 在调试模式下输出错误信息 + if (isDebugMode) ex.printStackTrace() null } override fun getName(): String { - return name + return plugin.description.name } override fun call(name: String, args: Array): OpenResult { return try { - OpenResult.deserialize(clazz?.invokeMethod("call", name, args, isStatic = true, remap = false) ?: return OpenResult.failed()) - } catch (ignored: NoSuchMethodException) { + OpenResult.cast(api?.invokeMethod("call", name, args, isStatic = true, remap = false) ?: error("OpenAPI not found in ${getName()}")) + } catch (ex: NoSuchMethodException) { + if (isDebugMode) ex.printStackTrace() OpenResult.failed() } } diff --git a/platform/platform-bukkit-impl/src/main/kotlin/taboolib/platform/type/BukkitContainer.kt b/platform/platform-bukkit-impl/src/main/kotlin/taboolib/platform/type/BukkitContainer.kt index f823ac7dd..3d0049f63 100644 --- a/platform/platform-bukkit-impl/src/main/kotlin/taboolib/platform/type/BukkitContainer.kt +++ b/platform/platform-bukkit-impl/src/main/kotlin/taboolib/platform/type/BukkitContainer.kt @@ -1,9 +1,13 @@ package taboolib.platform.type import org.bukkit.plugin.Plugin +import org.tabooproject.reflex.Reflex.Companion.invokeLocalMethod import org.tabooproject.reflex.Reflex.Companion.invokeMethod import taboolib.common.OpenContainer import taboolib.common.OpenResult +import taboolib.common.io.groupId +import taboolib.common.io.isDebugMode +import taboolib.common.io.taboolibId /** * TabooLib @@ -12,24 +16,26 @@ import taboolib.common.OpenResult * @author sky * @since 2021/7/3 1:44 上午 */ -class BukkitContainer(plugin: Plugin) : OpenContainer { +class BukkitContainer(val plugin: Plugin) : OpenContainer { - private val name = plugin.name - private val main = plugin.description.main - private val clazz = try { - Class.forName(main.substring(0, main.length - "platform.BukkitPlugin".length) + "common.OpenAPI") - } catch (ignored: Throwable) { + // 获取目标插件的 OpenAPI 类 + private val api = try { + Class.forName("${plugin::class.java.groupId}.${taboolibId}.common.OpenAPI") + } catch (ex: ClassNotFoundException) { + // 在调试模式下输出错误信息 + if (isDebugMode) ex.printStackTrace() null } override fun getName(): String { - return name + return plugin.name } override fun call(name: String, args: Array): OpenResult { return try { - OpenResult.deserialize(clazz?.invokeMethod("call", name, args, isStatic = true, remap = false) ?: return OpenResult.failed()) - } catch (ignored: NoSuchMethodException) { + OpenResult.cast(api?.invokeMethod("call", name, args, isStatic = true, remap = false) ?: error("OpenAPI not found in ${getName()}")) + } catch (ex: NoSuchMethodException) { + if (isDebugMode) ex.printStackTrace() OpenResult.failed() } } diff --git a/platform/platform-bungee-impl/src/main/kotlin/taboolib/platform/type/BungeeContainer.kt b/platform/platform-bungee-impl/src/main/kotlin/taboolib/platform/type/BungeeContainer.kt index e290247df..1c96556ee 100644 --- a/platform/platform-bungee-impl/src/main/kotlin/taboolib/platform/type/BungeeContainer.kt +++ b/platform/platform-bungee-impl/src/main/kotlin/taboolib/platform/type/BungeeContainer.kt @@ -1,9 +1,13 @@ package taboolib.platform.type import net.md_5.bungee.api.plugin.Plugin +import org.tabooproject.reflex.Reflex.Companion.invokeLocalMethod import org.tabooproject.reflex.Reflex.Companion.invokeMethod import taboolib.common.OpenContainer import taboolib.common.OpenResult +import taboolib.common.io.groupId +import taboolib.common.io.isDebugMode +import taboolib.common.io.taboolibId /** * TabooLib @@ -12,24 +16,26 @@ import taboolib.common.OpenResult * @author sky * @since 2021/7/3 1:44 上午 */ -class BungeeContainer(plugin: Plugin) : OpenContainer { +class BungeeContainer(val plugin: Plugin) : OpenContainer { - private val name = plugin.description.name - private val main = plugin.description.main!! - private val clazz = try { - Class.forName(main.substring(0, main.length - "platform.BungeePlugin".length) + "common.OpenAPI") - } catch (ignored: Throwable) { + // 获取目标插件的 OpenAPI 类 + private val api = try { + Class.forName("${plugin::class.java.groupId}.$taboolibId.common.OpenAPI") + } catch (ex: ClassNotFoundException) { + // 在调试模式下输出错误信息 + if (isDebugMode) ex.printStackTrace() null } override fun getName(): String { - return name + return plugin.description.name } override fun call(name: String, args: Array): OpenResult { return try { - OpenResult.deserialize(clazz?.invokeMethod("call", name, args, isStatic = true, remap = false) ?: return OpenResult.failed()) - } catch (ignored: NoSuchMethodException) { + OpenResult.cast(api?.invokeMethod("call", name, args, isStatic = true, remap = false) ?: error("OpenAPI not found in ${getName()}")) + } catch (ex: NoSuchMethodException) { + if (isDebugMode) ex.printStackTrace() OpenResult.failed() } } diff --git a/platform/platform-velocity-impl/src/main/kotlin/taboolib/platform/type/VelocityContainer.kt b/platform/platform-velocity-impl/src/main/kotlin/taboolib/platform/type/VelocityContainer.kt index 64933d69b..70e01a4c6 100644 --- a/platform/platform-velocity-impl/src/main/kotlin/taboolib/platform/type/VelocityContainer.kt +++ b/platform/platform-velocity-impl/src/main/kotlin/taboolib/platform/type/VelocityContainer.kt @@ -1,9 +1,13 @@ package taboolib.platform.type import com.velocitypowered.api.plugin.PluginContainer +import org.tabooproject.reflex.Reflex.Companion.invokeLocalMethod import org.tabooproject.reflex.Reflex.Companion.invokeMethod import taboolib.common.OpenContainer import taboolib.common.OpenResult +import taboolib.common.io.groupId +import taboolib.common.io.isDebugMode +import taboolib.common.io.taboolibId /** * TabooLib @@ -12,24 +16,26 @@ import taboolib.common.OpenResult * @author sky * @since 2021/7/3 1:44 上午 */ -class VelocityContainer(plugin: PluginContainer) : OpenContainer { +class VelocityContainer(val plugin: PluginContainer) : OpenContainer { - private val name = plugin.description.id - private val main = plugin.instance.get().javaClass.name - private val clazz = try { - Class.forName(main.substring(0, main.length - "platform.VelocityPlugin".length) + "common.OpenAPI") - } catch (ignored: Throwable) { + // 获取目标插件的 OpenAPI 类 + private val api = try { + Class.forName("${plugin::class.java.groupId}.$taboolibId.common.OpenAPI") + } catch (ex: ClassNotFoundException) { + // 在调试模式下输出错误信息 + if (isDebugMode) ex.printStackTrace() null } override fun getName(): String { - return name + return plugin.description.id } override fun call(name: String, args: Array): OpenResult { return try { - OpenResult.deserialize(clazz?.invokeMethod("call", name, args, isStatic = true, remap = false) ?: return OpenResult.failed()) - } catch (ignored: NoSuchMethodException) { + OpenResult.cast(api?.invokeMethod("call", name, args, isStatic = true, remap = false) ?: error("OpenAPI not found in ${getName()}")) + } catch (ex: NoSuchMethodException) { + if (isDebugMode) ex.printStackTrace() OpenResult.failed() } } diff --git a/settings.gradle.kts b/settings.gradle.kts index f29b9b4ca..75ca7e881 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -4,7 +4,6 @@ include( // 基础工具 "module:basic:basic-configuration", "module:basic:basic-submit-chain", - "module:basic:basic-sql", // 针对 Bukkit 平台的常规工具 "module:bukkit:bukkit-fake-op",