From 800ca782b04a7aa99c14a55476d8348af104ffb3 Mon Sep 17 00:00:00 2001 From: Boy Date: Thu, 30 May 2024 13:56:39 +0200 Subject: [PATCH] refactor: restructure how anvil renaming is handled --- .../emojy/nms/v1_20_R4/EmojyListener.kt | 14 ++------ .../emojy/nms/v1_20_R4/EmojyNMSHandler.kt | 34 +++++++++++-------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/v1_20_R4/src/main/kotlin/com/mineinabyss/emojy/nms/v1_20_R4/EmojyListener.kt b/v1_20_R4/src/main/kotlin/com/mineinabyss/emojy/nms/v1_20_R4/EmojyListener.kt index ec88073..97114b8 100644 --- a/v1_20_R4/src/main/kotlin/com/mineinabyss/emojy/nms/v1_20_R4/EmojyListener.kt +++ b/v1_20_R4/src/main/kotlin/com/mineinabyss/emojy/nms/v1_20_R4/EmojyListener.kt @@ -71,18 +71,10 @@ class EmojyListener : Listener { @EventHandler fun PrepareAnvilEvent.onAnvil() { - if (result?.itemMeta?.hasDisplayName() != true || inventory.renameText == null) { - result?.editItemMeta { persistentDataContainer.remove(ORIGINAL_ITEM_RENAME_TEXT) } - return - } - val displayName = inventory.renameText?.miniMsg()?.transformEmotes(null, false)?.unescapeEmoteIds() ?: run { - result?.editItemMeta { persistentDataContainer.remove(ORIGINAL_ITEM_RENAME_TEXT) } - return - } - result = result?.editItemMeta { - persistentDataContainer.set(ORIGINAL_ITEM_RENAME_TEXT, DataType.STRING, inventory.renameText!!) - displayName(displayName) + if (inventory.renameText == null || result?.itemMeta?.hasDisplayName() != true) { + persistentDataContainer.remove(ORIGINAL_ITEM_RENAME_TEXT) } + else persistentDataContainer.set(ORIGINAL_ITEM_RENAME_TEXT, DataType.STRING, inventory.renameText!!) } } } diff --git a/v1_20_R4/src/main/kotlin/com/mineinabyss/emojy/nms/v1_20_R4/EmojyNMSHandler.kt b/v1_20_R4/src/main/kotlin/com/mineinabyss/emojy/nms/v1_20_R4/EmojyNMSHandler.kt index 5b9f407..7bf6df0 100644 --- a/v1_20_R4/src/main/kotlin/com/mineinabyss/emojy/nms/v1_20_R4/EmojyNMSHandler.kt +++ b/v1_20_R4/src/main/kotlin/com/mineinabyss/emojy/nms/v1_20_R4/EmojyNMSHandler.kt @@ -79,19 +79,21 @@ class EmojyNMSHandler(emojy: EmojyPlugin) : IEmojyNMSHandler { } ?: it }) is ClientboundContainerSetSlotPacket -> ClientboundContainerSetSlotPacket(packet.containerId, packet.stateId, packet.slot, packet.item.transformItemNameLore()) - is ClientboundContainerSetContentPacket -> { - val player = connection.player.bukkitEntity - (player.openInventory.topInventory as? AnvilInventory)?.let { - ClientboundContainerSetContentPacket(packet.containerId, packet.stateId, - NonNullList.of(packet.items.first(), *packet.items.map { - CraftItemStack.asNMSCopy(CraftItemStack.asBukkitCopy(it).editItemMeta { - setDisplayName(persistentDataContainer.get(ORIGINAL_ITEM_RENAME_TEXT, DataType.STRING) ?: return@editItemMeta) - }) - }.toTypedArray()), packet.carriedItem) - } ?: ClientboundContainerSetContentPacket(packet.containerId, packet.stateId, - NonNullList.of(packet.items.first(), *packet.items.map { it.transformItemNameLore() }.toTypedArray()), - packet.carriedItem) - } + is ClientboundContainerSetContentPacket -> ClientboundContainerSetContentPacket( + packet.containerId, packet.stateId, NonNullList.of(packet.items.first(), + *packet.items.map { + val inv = connection.player.bukkitEntity.openInventory.topInventory + val bukkit = CraftItemStack.asBukkitCopy(it) + + // If item is firstItem in AnvilInventory we want to set it to have the plain-text displayname + if (inv is AnvilInventory && inv.firstItem == bukkit) + bukkit.itemMeta?.persistentDataContainer?.get(ORIGINAL_ITEM_RENAME_TEXT, DataType.STRING)?.let { og -> + CraftItemStack.asNMSCopy(bukkit.editItemMeta { + setDisplayName(og) + }) + } ?: it.transformItemNameLore() + else it.transformItemNameLore() + }.toTypedArray()), packet.carriedItem) else -> packet }, promise ) @@ -105,10 +107,14 @@ class EmojyNMSHandler(emojy: EmojyPlugin) : IEmojyNMSHandler { } private fun ItemStack.transformItemNameLore(): ItemStack { - val locale = connection.locale() + val player = connection.player.bukkitEntity + val locale = player.locale() return CraftItemStack.asNMSCopy(CraftItemStack.asBukkitCopy(this).editItemMeta { itemName(if (hasItemName()) itemName().transformEmotes(locale) else null) lore(lore()?.map { l -> l.transformEmotes(locale) }) + persistentDataContainer.get(ORIGINAL_ITEM_RENAME_TEXT, DataType.STRING)?.let { + displayName(it.miniMsg().escapeEmoteIDs(player).transformEmotes(locale).unescapeEmoteIds()) + } }) } }