From 80ab673cfd2268ed143f9dfc669c95e3b5efe245 Mon Sep 17 00:00:00 2001 From: Moritz Zwerger Date: Thu, 29 Feb 2024 23:03:47 +0100 Subject: [PATCH] fix nbt chat insertion with translations + tests This mainly affects 1.20+ and weird data thanks to nbt --- .../minosoft/data/text/BaseComponent.kt | 4 +-- .../protocol/buffers/play/PlayInByteBuffer.kt | 6 +++- .../minosoft/data/text/ChatComponentTest.kt | 29 +++++++++++++++---- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/data/text/BaseComponent.kt b/src/main/java/de/bixilon/minosoft/data/text/BaseComponent.kt index b1fe830ea..9205c1a35 100644 --- a/src/main/java/de/bixilon/minosoft/data/text/BaseComponent.kt +++ b/src/main/java/de/bixilon/minosoft/data/text/BaseComponent.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2023 Moritz Zwerger + * Copyright (C) 2020-2024 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. * @@ -58,7 +58,7 @@ class BaseComponent : ChatComponent, Iterable { val clickEvent = json["clickEvent", "click_event"]?.toJsonObject()?.let { click -> ClickEvents.build(click, restricted) } ?: parent?.clickEvent val hoverEvent = json["hoverEvent", "hover_event"]?.toJsonObject()?.let { hover -> HoverEvents.build(hover, restricted) } ?: parent?.hoverEvent - val text = json["text"]?.nullCast() ?: "" + val text = json["text", ""]?.nullCast() ?: "" val component = TextComponent( message = text, diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/buffers/play/PlayInByteBuffer.kt b/src/main/java/de/bixilon/minosoft/protocol/protocol/buffers/play/PlayInByteBuffer.kt index 44bdf5a7a..0a3fd6e23 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/buffers/play/PlayInByteBuffer.kt +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/buffers/play/PlayInByteBuffer.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2023 Moritz Zwerger + * Copyright (C) 2020-2024 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. * @@ -55,6 +55,7 @@ import de.bixilon.minosoft.protocol.protocol.buffers.InByteBuffer import de.bixilon.minosoft.protocol.protocol.encryption.CryptManager import de.bixilon.minosoft.recipes.Ingredient import de.bixilon.minosoft.util.KUtil +import de.bixilon.minosoft.util.json.Jackson import de.bixilon.minosoft.util.logging.Log import de.bixilon.minosoft.util.logging.LogLevels import de.bixilon.minosoft.util.logging.LogMessageType @@ -116,6 +117,9 @@ class PlayInByteBuffer : InByteBuffer { return readChatComponent() } val nbt = readNBT() + if (DebugOptions.LOG_RAW_CHAT) { + Log.log(LogMessageType.CHAT_IN, LogLevels.VERBOSE) { TextComponent(Jackson.MAPPER.writeValueAsString(nbt)) } + } return ChatComponent.of(nbt, connection.language, null, restricted = true) } diff --git a/src/test/java/de/bixilon/minosoft/data/text/ChatComponentTest.kt b/src/test/java/de/bixilon/minosoft/data/text/ChatComponentTest.kt index f3899ec47..6d87cca8d 100644 --- a/src/test/java/de/bixilon/minosoft/data/text/ChatComponentTest.kt +++ b/src/test/java/de/bixilon/minosoft/data/text/ChatComponentTest.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2023 Moritz Zwerger + * Copyright (C) 2020-2024 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. * @@ -287,14 +287,33 @@ internal class ChatComponentTest { val hover = EntityHoverEvent("0d2dc333-f629-4b59-bdf9-074f58b99c06".toUUID(), minecraft("item"), name = TextComponent("item.item.slimeball")) val expected = BaseComponent( - TextComponent("[").color(ChatColors.GRAY).italic(), - TextComponent("Bixilon").color(ChatColors.GRAY).italic(), - TextComponent(": ").color(ChatColors.GRAY).italic(), + TextComponent("[").color(ChatColors.GRAY).italic(), + TextComponent("Bixilon").color(ChatColors.GRAY).italic(), + TextComponent(": ").color(ChatColors.GRAY).italic(), BaseComponent( TextComponent("Killed ").color(ChatColors.GRAY).italic(), TextComponent("item.item.slimeball").color(ChatColors.GRAY).italic().hoverEvent(hover), ), - TextComponent("]").color(ChatColors.GRAY).italic(), + TextComponent("]").color(ChatColors.GRAY).italic(), + ) + assertEquals(text, expected) + } + + @Test + fun `nbt solo text`() { + val language = LanguageFile("en_US", Namespaces.MINECRAFT, mutableMapOf( + "chat.type.admin" to "[%s: %s]" + )) + val text = ChatComponent.of("""{"with":[{"color":"red","extra":[{"color":"red","text":"[Admins] "},{"":"Bixilon"},{"":""}],"insertion":"Bixilon"," text":""},{"text":"test"}],"color":"gray","italic":1,"translate":"chat.type.admin"}""", translator = language) + val expected = BaseComponent( + TextComponent("[").color(ChatColors.GRAY).italic(), + BaseComponent( + TextComponent("[Admins] ").color(ChatColors.RED).italic(), + TextComponent("Bixilon").color(ChatColors.RED).italic(), + ), + TextComponent(": ").color(ChatColors.GRAY).italic(), + TextComponent("test").color(ChatColors.GRAY).italic(), + TextComponent("]").color(ChatColors.GRAY).italic(), ) assertEquals(text, expected) }