From 6da07833e524b7f8bc1a59f801293b20c296148b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Sun, 26 Feb 2023 23:14:56 +0800 Subject: [PATCH] [6.0.10] update chat module --- .../taboolib/module/chat/ComponentToString.kt | 70 +++++++++++++++++++ .../module/chat/impl/DefaultComponent.kt | 29 ++++---- 2 files changed, 85 insertions(+), 14 deletions(-) create mode 100644 module/module-chat/src/main/kotlin/taboolib/module/chat/ComponentToString.kt diff --git a/module/module-chat/src/main/kotlin/taboolib/module/chat/ComponentToString.kt b/module/module-chat/src/main/kotlin/taboolib/module/chat/ComponentToString.kt new file mode 100644 index 000000000..057c8ed69 --- /dev/null +++ b/module/module-chat/src/main/kotlin/taboolib/module/chat/ComponentToString.kt @@ -0,0 +1,70 @@ +package taboolib.module.chat + +import net.md_5.bungee.api.ChatColor +import net.md_5.bungee.api.chat.* +import taboolib.common.reflect.Reflex.Companion.invokeMethod + +/** + * TabooLib + * taboolib.module.chat.ComponentToJson + * + * @author 坏黑 + * @since 2023/2/26 22:25 + */ +object ComponentToString { + + fun toLegacyString(vararg components: BaseComponent): String { + val builder = StringBuilder() + val newArray: Array = components + val size = components.size + for (i in 0 until size) { + builder.append(toLegacyString(newArray[i])) + } + return builder.toString() + } + + private fun toLegacyString(component: BaseComponent): String { + val builder = StringBuilder() + toLegacyString1(component, builder) + return builder.toString() + } + + private fun toLegacyString1(component: BaseComponent, builder: StringBuilder): String { + if (component is TranslatableComponent) { + // 逻辑复杂,直接调用原始方法 + // 以防出现版本兼容问题 + component.invokeMethod("toLegacyText", builder) + } else { + addFormat(component, builder) + when (component) { + is TextComponent -> builder.append(component.text) + is KeybindComponent -> builder.append(component.keybind) + is ScoreComponent -> builder.append(component.value) + is SelectorComponent -> builder.append(component.selector) + } + } + component.extra?.forEach { toLegacyString1(it, builder) } + return builder.toString() + } + + private fun addFormat(component: BaseComponent, builder: StringBuilder) { + if (component.colorRaw != null) { + builder.append(component.color) + } + if (component.isBold) { + builder.append(ChatColor.BOLD) + } + if (component.isItalic) { + builder.append(ChatColor.ITALIC) + } + if (component.isUnderlined) { + builder.append(ChatColor.UNDERLINE) + } + if (component.isStrikethrough) { + builder.append(ChatColor.STRIKETHROUGH) + } + if (component.isObfuscated) { + builder.append(ChatColor.MAGIC) + } + } +} \ No newline at end of file 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 55d638fb9..251ffad4b 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 @@ -9,10 +9,7 @@ import net.md_5.bungee.chat.ComponentSerializer import taboolib.common.platform.ProxyCommandSender import taboolib.common.platform.ProxyPlayer import taboolib.common.platform.function.onlinePlayers -import taboolib.module.chat.ClickAction -import taboolib.module.chat.ComponentText -import taboolib.module.chat.Decoration -import taboolib.module.chat.StandardColors +import taboolib.module.chat.* import java.awt.Color /** @@ -30,23 +27,27 @@ class DefaultComponent() : ComponentText { private val left = arrayListOf() private val latest = arrayListOf() - private val components: BaseComponent - get() = TextComponent(*(left + latest).toTypedArray()) + private val component: BaseComponent + get() = when { + left.isEmpty() && latest.size == 1 -> latest[0] + latest.isEmpty() && left.size == 1 -> left[0] + else -> TextComponent(*(left + latest).toTypedArray()) + } init { color(StandardColors.RESET) } override fun toRawMessage(): String { - return ComponentSerializer.toString(components) + return ComponentSerializer.toString(component) } override fun toLegacyText(): String { - return TextComponent.toLegacyText(components) + return ComponentToString.toLegacyString(*(left + latest).toTypedArray()) } override fun toPlainText(): String { - return TextComponent.toPlainText(components) + return TextComponent.toPlainText(*(left + latest).toTypedArray()) } override fun broadcast() { @@ -86,7 +87,7 @@ class DefaultComponent() : ComponentText { override fun append(other: ComponentText): ComponentText { other as? DefaultComponent ?: error("Unsupported component type.") flush() - latest += other.components + latest += other.component return this } @@ -96,7 +97,7 @@ class DefaultComponent() : ComponentText { override fun appendTranslation(text: String, obj: List): ComponentText { flush() - latest += TranslatableComponent(text, obj.map { if (it is DefaultComponent) it.components else it }) + latest += TranslatableComponent(text, obj.map { if (it is DefaultComponent) it.component else it }) return this } @@ -125,9 +126,9 @@ class DefaultComponent() : ComponentText { override fun hoverText(text: ComponentText): ComponentText { text as? DefaultComponent ?: error("Unsupported component type.") try { - latest.forEach { it.hoverEvent = HoverEvent(HoverEvent.Action.SHOW_TEXT, Text(arrayOf(text.components))) } + latest.forEach { it.hoverEvent = HoverEvent(HoverEvent.Action.SHOW_TEXT, Text(arrayOf(text.component))) } } catch (ex: NoClassDefFoundError) { - latest.forEach { it.hoverEvent = HoverEvent(HoverEvent.Action.SHOW_TEXT, arrayOf(text.components)) } + latest.forEach { it.hoverEvent = HoverEvent(HoverEvent.Action.SHOW_TEXT, arrayOf(text.component)) } } return this } @@ -153,7 +154,7 @@ class DefaultComponent() : ComponentText { override fun hoverEntity(id: String, type: String?, name: ComponentText?): ComponentText { try { - val component = if (name is DefaultComponent) name.components else null + val component = if (name is DefaultComponent) name.component else null latest.forEach { it.hoverEvent = HoverEvent(HoverEvent.Action.SHOW_ENTITY, Entity(type, id, component)) } } catch (_: NoClassDefFoundError) { TODO("Unsupported hover entity for this version.")