From e7a886c7944a8f5aabdaf2edf6d32c709b81ae83 Mon Sep 17 00:00:00 2001 From: Boy Date: Thu, 30 May 2024 13:33:00 +0200 Subject: [PATCH] fix: translatable/emote-support for itemnames & lore --- .../emojy/translator/EmojyTranslator.kt | 2 +- .../emojy/nms/v1_20_R4/EmojyNMSHandler.kt | 21 ++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/core/src/main/kotlin/com/mineinabyss/emojy/translator/EmojyTranslator.kt b/core/src/main/kotlin/com/mineinabyss/emojy/translator/EmojyTranslator.kt index 21972fd..75a6f17 100644 --- a/core/src/main/kotlin/com/mineinabyss/emojy/translator/EmojyTranslator.kt +++ b/core/src/main/kotlin/com/mineinabyss/emojy/translator/EmojyTranslator.kt @@ -20,7 +20,7 @@ class EmojyTranslator : Translator { override fun translate(component: TranslatableComponent, locale: Locale): Component? { val mmString = emojy.languages.find { it.locale == locale }?.keys?.get(component.key()) ?: return null - val resultingComponent = mmString.miniMsg(EmojyArgumentTag(component.args()).takeIf { component.args().isNotEmpty() } ?: IdofrontTextComponents.globalResolver) + val resultingComponent = mmString.miniMsg(EmojyArgumentTag(component.arguments()).takeIf { component.arguments().isNotEmpty() } ?: IdofrontTextComponents.globalResolver) return when { component.children().isEmpty() -> resultingComponent else -> resultingComponent.children(component.children()) 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 0b86c85..5b9f407 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 @@ -7,6 +7,7 @@ import com.mineinabyss.emojy.* import com.mineinabyss.emojy.config.SPACE_PERMISSION import com.mineinabyss.emojy.nms.IEmojyNMSHandler import com.mineinabyss.idofront.items.editItemMeta +import com.mineinabyss.idofront.messaging.broadcastVal import com.mineinabyss.idofront.messaging.logVal import com.mineinabyss.idofront.plugin.listeners import com.mineinabyss.idofront.textcomponents.miniMsg @@ -29,6 +30,8 @@ import net.minecraft.network.Connection import net.minecraft.network.protocol.game.* import net.minecraft.network.syncher.EntityDataSerializer import net.minecraft.network.syncher.SynchedEntityData +import net.minecraft.world.item.ItemStack +import org.bukkit.Bukkit import org.bukkit.NamespacedKey import org.bukkit.craftbukkit.inventory.CraftItemStack import org.bukkit.entity.Player @@ -75,15 +78,19 @@ class EmojyNMSHandler(emojy: EmojyPlugin) : IEmojyNMSHandler { ) } ?: it }) + is ClientboundContainerSetSlotPacket -> ClientboundContainerSetSlotPacket(packet.containerId, packet.stateId, packet.slot, packet.item.transformItemNameLore()) is ClientboundContainerSetContentPacket -> { - (connection.player.bukkitEntity.openInventory.topInventory as? AnvilInventory)?.let { + 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.copy()).editItemMeta { + CraftItemStack.asNMSCopy(CraftItemStack.asBukkitCopy(it).editItemMeta { setDisplayName(persistentDataContainer.get(ORIGINAL_ITEM_RENAME_TEXT, DataType.STRING) ?: return@editItemMeta) }) }.toTypedArray()), packet.carriedItem) - } ?: packet + } ?: ClientboundContainerSetContentPacket(packet.containerId, packet.stateId, + NonNullList.of(packet.items.first(), *packet.items.map { it.transformItemNameLore() }.toTypedArray()), + packet.carriedItem) } else -> packet }, promise @@ -96,6 +103,14 @@ class EmojyNMSHandler(emojy: EmojyPlugin) : IEmojyNMSHandler { else -> packet }) } + + private fun ItemStack.transformItemNameLore(): ItemStack { + val locale = connection.locale() + return CraftItemStack.asNMSCopy(CraftItemStack.asBukkitCopy(this).editItemMeta { + itemName(if (hasItemName()) itemName().transformEmotes(locale) else null) + lore(lore()?.map { l -> l.transformEmotes(locale) }) + }) + } } ) }