chat component: proper read parent and click event with legacy formatting

This commit is contained in:
Bixilon 2023-02-27 10:15:37 +01:00
parent eef7d6ce1d
commit e018694df7
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 42 additions and 38 deletions

View File

@ -44,28 +44,6 @@ class BaseComponent : ChatComponent {
}
constructor(translator: Translator? = null, parent: TextComponent? = null, json: Map<String, Any>, restrictedMode: Boolean = false) {
var currentParent: TextComponent? = null
var currentText = ""
fun parseExtra() {
json["extra"].toJsonList()?.let {
for (data in it) {
this += ChatComponent.of(data, translator, currentParent, restrictedMode)
}
}
}
json["text"]?.nullCast<String>()?.let {
if (it.indexOf(ProtocolDefinition.TEXT_COMPONENT_FORMATTING_PREFIX) != -1) {
// TODO: That is wrong, clickEvent, hoverEvent is ignored
this += ChatComponent.of(it, translator, parent, restrictedMode)
parseExtra()
return
}
currentText = it
}
val color = json["color"]?.nullCast<String>()?.toColor() ?: parent?.color
val formatting = parent?.formatting?.toMutableSet() ?: mutableSetOf()
@ -76,20 +54,37 @@ class BaseComponent : ChatComponent {
formatting.addOrRemove(PreChatFormattingCodes.STRIKETHROUGH, json["strikethrough"]?.toBoolean())
formatting.addOrRemove(PreChatFormattingCodes.OBFUSCATED, json["obfuscated"]?.toBoolean())
val clickEvent = json["clickEvent", "click_event"]?.toJsonObject()?.let { click -> ClickEvents.build(click, restrictedMode) }
val hoverEvent = json["hoverEvent", "hover_event"]?.toJsonObject()?.let { hover -> HoverEvents.build(hover, restrictedMode) }
val clickEvent = parent?.clickEvent ?: json["clickEvent", "click_event"]?.toJsonObject()?.let { click -> ClickEvents.build(click, restrictedMode) }
val hoverEvent = parent?.hoverEvent ?: json["hoverEvent", "hover_event"]?.toJsonObject()?.let { hover -> HoverEvents.build(hover, restrictedMode) }
val textComponent = TextComponent(
message = currentText,
val text = json["text"]?.nullCast<String>() ?: ""
val component = TextComponent(
message = text,
color = color,
formatting = formatting,
clickEvent = clickEvent,
hoverEvent = hoverEvent,
)
if (currentText.isNotEmpty()) {
this += textComponent
fun parseExtra() {
json["extra"].toJsonList()?.let {
for (data in it) {
this += ChatComponent.of(data, translator, component, restrictedMode)
}
}
}
if (text.indexOf(ProtocolDefinition.TEXT_COMPONENT_FORMATTING_PREFIX) != -1) {
this += ChatComponent.of(text, translator, component, restrictedMode)
parseExtra()
return
}
if (text.isNotEmpty()) {
this += component
}
currentParent = textComponent
parseExtra()
@ -100,7 +95,7 @@ class BaseComponent : ChatComponent {
with.add(part ?: continue)
}
}
this += translator?.translate(it.toResourceLocation(), currentParent, restrictedMode, *with.toTypedArray()) ?: ChatComponent.of(json["with"], translator, currentParent, restrictedMode)
this += translator?.translate(it.toResourceLocation(), component, restrictedMode, *with.toTypedArray()) ?: ChatComponent.of(json["with"], translator, component, restrictedMode)
}
}

View File

@ -32,7 +32,7 @@ private typealias PartList = MutableList<ChatComponent>
object LegacyComponentReader {
private fun PartList.push(sequence: SequenceBuilder, restricted: Boolean) {
private fun PartList.push(sequence: SequenceBuilder, parent: TextComponent?, restricted: Boolean) {
if (sequence.text.isEmpty()) return
val split = sequence.text.split(' ')
@ -51,15 +51,15 @@ object LegacyComponentReader {
}
if (text.isNotEmpty()) {
// an url follows, push the previous part
this += TextComponent(text, sequence.color, sequence.formatting.toMutableSet())
this += TextComponent(text, sequence.color, sequence.formatting.toMutableSet(), parent?.clickEvent, parent?.hoverEvent)
text.clear()
}
this += TextComponent(part, sequence.color, sequence.formatting.toMutableSet(), event)
this += TextComponent(part, sequence.color, sequence.formatting.toMutableSet(), parent?.clickEvent ?: event, parent?.hoverEvent)
}
if (text.isNotEmpty()) {
// data that was not pushed yet
this += TextComponent(text, sequence.color, sequence.formatting.toMutableSet())
this += TextComponent(text, sequence.color, sequence.formatting.toMutableSet(), parent?.clickEvent, parent?.hoverEvent)
}
sequence.reset() // clear it up again for next usage
@ -100,13 +100,13 @@ object LegacyComponentReader {
val color = ChatColors.VALUES.getOrNull(Character.digit(formattingChar, 16))
if (color != null) {
parts.push(sequence, restricted) // try push previous, because this is a color change
parts.push(sequence, parent, restricted) // try push previous, because this is a color change
sequence.color = color
continue
}
val formatting = ChatFormattingCodes.getChatFormattingCodeByChar(formattingChar)
if (formatting != null) {
parts.push(sequence, restricted) // try push previous, because this is a formatting change
parts.push(sequence, parent, restricted) // try push previous, because this is a formatting change
if (formatting != PostChatFormattingCodes.RESET) {
// a reset means resetting, this is done by the previous push
@ -116,7 +116,7 @@ object LegacyComponentReader {
}
}
parts.push(sequence, restricted)
parts.push(sequence, parent, restricted)
return when {
parts.isEmpty() -> EmptyComponent

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -43,6 +43,15 @@ class TextHoverEvent(
return true
}
override fun hashCode(): Int {
return text.hashCode()
}
override fun equals(other: Any?): Boolean {
if (other !is TextHoverEvent) return false
return text == other.text
}
companion object : HoverEventFactory<TextHoverEvent> {
override val name: String = "show_text"