Skip to content

Commit

Permalink
[6.0.10] update chat module
Browse files Browse the repository at this point in the history
  • Loading branch information
Bkm016 committed Feb 26, 2023
1 parent d3ec393 commit 6da0783
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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<out BaseComponent> = 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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -30,23 +27,27 @@ class DefaultComponent() : ComponentText {

private val left = arrayListOf<BaseComponent>()
private val latest = arrayListOf<BaseComponent>()
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() {
Expand Down Expand Up @@ -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
}

Expand All @@ -96,7 +97,7 @@ class DefaultComponent() : ComponentText {

override fun appendTranslation(text: String, obj: List<Any>): 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
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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.")
Expand Down

0 comments on commit 6da0783

Please sign in to comment.