Skip to content

Commit

Permalink
fix: translatable/emote-support for itemnames & lore
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed May 30, 2024
1 parent 8f02e21 commit e7a886c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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) })
})
}
}
)
}
Expand Down

0 comments on commit e7a886c

Please sign in to comment.