diff --git a/module/module-chat/src/main/kotlin/taboolib/module/chat/ComponentText.kt b/module/module-chat/src/main/kotlin/taboolib/module/chat/ComponentText.kt index 84a1c199f..f7a979b2a 100644 --- a/module/module-chat/src/main/kotlin/taboolib/module/chat/ComponentText.kt +++ b/module/module-chat/src/main/kotlin/taboolib/module/chat/ComponentText.kt @@ -1,5 +1,6 @@ package taboolib.module.chat +import net.md_5.bungee.api.chat.BaseComponent import java.awt.Color /** @@ -135,6 +136,9 @@ interface ComponentText : Source { /** 移除颜色 */ fun uncolor(): ComponentText + /** 转换为 Spigot 对象 */ + fun toSpigotObject(): BaseComponent + companion object { fun empty(): ComponentText { diff --git a/module/module-chat/src/main/kotlin/taboolib/module/chat/impl/DefaultComponent.kt b/module/module-chat/src/main/kotlin/taboolib/module/chat/impl/DefaultComponent.kt index 251ffad4b..110b46558 100644 --- a/module/module-chat/src/main/kotlin/taboolib/module/chat/impl/DefaultComponent.kt +++ b/module/module-chat/src/main/kotlin/taboolib/module/chat/impl/DefaultComponent.kt @@ -311,6 +311,10 @@ class DefaultComponent() : ComponentText { return this } + override fun toSpigotObject(): BaseComponent { + return component + } + /** 释放缓冲区 */ fun flush() { left.addAll(latest) diff --git a/platform/platform-bukkit/src/main/kotlin/taboolib/platform/util/BukkitTellrawJson.kt b/platform/platform-bukkit/src/main/kotlin/taboolib/platform/util/BukkitTellrawJson.kt index 247b80618..c0597203b 100644 --- a/platform/platform-bukkit/src/main/kotlin/taboolib/platform/util/BukkitTellrawJson.kt +++ b/platform/platform-bukkit/src/main/kotlin/taboolib/platform/util/BukkitTellrawJson.kt @@ -7,12 +7,13 @@ import org.tabooproject.reflex.Reflex.Companion.getProperty import org.tabooproject.reflex.Reflex.Companion.invokeMethod import taboolib.common.Isolated import taboolib.common.util.unsafeLazy -import taboolib.module.chat.TellrawJson +import taboolib.module.chat.ComponentText +import taboolib.module.chat.RawMessage -fun TellrawJson.hoverItem(itemStack: ItemStack): TellrawJson { - val nmsItemStack = classCraftItemStack.invokeMethod("asNMSCopy", itemStack, isStatic = true)!! +fun ItemStack.toNMSKeyAndItemData(): Pair { + val nmsItemStack = classCraftItemStack.invokeMethod("asNMSCopy", this, isStatic = true)!! val nmsKey = try { - itemStack.type.key.key + type.key.key } catch (ex: NoSuchMethodError) { val nmsItem = nmsItemStack.invokeMethod("getItem")!! val name = nmsItem.getProperty("name")!! @@ -26,7 +27,17 @@ fun TellrawJson.hoverItem(itemStack: ItemStack): TellrawJson { } key } - return hoverItem(nmsKey, nmsItemStack.invokeMethod("getTag")?.toString() ?: "{}") + return nmsKey to (nmsItemStack.invokeMethod("getTag")?.toString() ?: "{}") +} + +fun ComponentText.hoverItem(itemStack: ItemStack): ComponentText { + val (key, data) = itemStack.toNMSKeyAndItemData() + return hoverItem(key, data) +} + +fun RawMessage.hoverItem(itemStack: ItemStack): RawMessage { + val (key, data) = itemStack.toNMSKeyAndItemData() + return hoverItem(key, data) } private val classCraftItemStack by unsafeLazy {