From c8b89d08cc1a91350233cab53d1d331d8bea0134 Mon Sep 17 00:00:00 2001 From: Boy Date: Tue, 19 Dec 2023 22:22:42 +0100 Subject: [PATCH] fix: anvil renaming not working when color-codes are in the renameText --- .../kotlin/com/mineinabyss/emojy/EmojyListener.kt | 15 +++++++++++++++ .../com/mineinabyss/emojy/nms/EmojyNMSHandlers.kt | 14 ++++++++++---- .../emojy/nms/v1_20_R2/EmojyNMSHandler.kt | 4 ++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/core/src/main/kotlin/com/mineinabyss/emojy/EmojyListener.kt b/core/src/main/kotlin/com/mineinabyss/emojy/EmojyListener.kt index 619cc03..4903bf5 100644 --- a/core/src/main/kotlin/com/mineinabyss/emojy/EmojyListener.kt +++ b/core/src/main/kotlin/com/mineinabyss/emojy/EmojyListener.kt @@ -1,15 +1,21 @@ package com.mineinabyss.emojy import com.mineinabyss.emojy.nms.EmojyNMSHandlers +import com.mineinabyss.idofront.items.editItemMeta import com.mineinabyss.idofront.messaging.logError import com.mineinabyss.idofront.messaging.logVal +import com.mineinabyss.idofront.textcomponents.miniMsg import com.mineinabyss.idofront.textcomponents.serialize import io.papermc.paper.event.player.AsyncChatDecorateEvent import io.papermc.paper.event.player.AsyncChatEvent +import org.bukkit.Bukkit +import org.bukkit.entity.Player import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority import org.bukkit.event.Listener +import org.bukkit.event.inventory.PrepareAnvilEvent import org.bukkit.event.player.PlayerJoinEvent +import org.bukkit.inventory.AnvilInventory @Suppress("UnstableApiUsage") class EmojyListener : Listener { @@ -24,5 +30,14 @@ class EmojyListener : Listener { fun AsyncChatDecorateEvent.onPlayerChat() { result(result().transform(player(), true, true)) } + + @EventHandler + fun PrepareAnvilEvent.onAnvil() { + val inventory = inventory as? AnvilInventory ?: return + val displayName = (inventory.renameText?.miniMsg() ?: inventory.firstItem?.itemMeta?.displayName())?.transform(inventory.viewers.first() as? Player, false) + result = result?.editItemMeta { + displayName(displayName) + } + } } diff --git a/core/src/main/kotlin/com/mineinabyss/emojy/nms/EmojyNMSHandlers.kt b/core/src/main/kotlin/com/mineinabyss/emojy/nms/EmojyNMSHandlers.kt index 1e99e28..54249de 100644 --- a/core/src/main/kotlin/com/mineinabyss/emojy/nms/EmojyNMSHandlers.kt +++ b/core/src/main/kotlin/com/mineinabyss/emojy/nms/EmojyNMSHandlers.kt @@ -7,6 +7,7 @@ import com.mineinabyss.idofront.textcomponents.miniMsg import com.mineinabyss.idofront.textcomponents.serialize import com.mineinabyss.idofront.textcomponents.toPlainText import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer import org.bukkit.Bukkit import org.bukkit.entity.Player @@ -34,16 +35,21 @@ object EmojyNMSHandlers { } private val gson = GsonComponentSerializer.gson() - fun JsonObject.formatString() : String { + private val plain = PlainTextComponentSerializer.plainText() + //TODO toPlainText fixes the anvil issue with tags being escaped + // It does break all other formatting everywhere else though by removing tags + // serialize() doesnt but keeps the escaped tag from gson + // anvil goes like this, inputItem -> readUtf -> renameField -> writeNbt from raw string, which is why it breaks + fun JsonObject.formatString(player: Player? = null) : String { return if (this.has("args") || this.has("text") || this.has("extra") || this.has("translate")) { - gson.serialize(gson.deserialize(this.toString()).toPlainText().miniMsg().transform(null, true)) + gson.serialize(gson.deserialize(this.toString())/*.toPlainText()*/.serialize().miniMsg().transform(player, true)) } else this.toString() } - fun transformer() = { string: String -> + fun transformer(player: Player? = null) = { string: String -> runCatching { val element = JsonParser.parseString(string) - if (element.isJsonObject) element.asJsonObject.formatString() + if (element.isJsonObject) element.asJsonObject.formatString(player) else string }.getOrNull() ?: string } diff --git a/v1_20_R2/src/main/kotlin/com/mineinabyss/emojy/nms/v1_20_R2/EmojyNMSHandler.kt b/v1_20_R2/src/main/kotlin/com/mineinabyss/emojy/nms/v1_20_R2/EmojyNMSHandler.kt index e018d8c..8fb1d39 100644 --- a/v1_20_R2/src/main/kotlin/com/mineinabyss/emojy/nms/v1_20_R2/EmojyNMSHandler.kt +++ b/v1_20_R2/src/main/kotlin/com/mineinabyss/emojy/nms/v1_20_R2/EmojyNMSHandler.kt @@ -174,7 +174,7 @@ class EmojyNMSHandler : IEmojyNMSHandler { override fun readNbt(): CompoundTag? { return super.readNbt()?.apply { - transform(this, EmojyNMSHandlers.transformer()) + transform(this, EmojyNMSHandlers.transformer(player)) } } @@ -232,7 +232,7 @@ class EmojyNMSHandler : IEmojyNMSHandler { val packetID = customDataSerializer.readVarInt() val attribute = ctx.channel().attr(Connection.ATTRIBUTE_SERVERBOUND_PROTOCOL) val packet = attribute.get().createPacket(packetID, customDataSerializer) ?: throw IOException("Bad packet id $packetID") - + //ObfHelper.INSTANCE.deobfClassName(packet.javaClass.name).logVal("Packet: ") out += when { customDataSerializer.readableBytes() > 0 -> throw IOException("Packet $packetID ($packet) was larger than I expected, found ${customDataSerializer.readableBytes()} bytes extra whilst reading packet $packetID") packet is ServerboundChatPacket -> {