diff --git a/nms/nms-common/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/util/PaperSpigotUtil.kt b/nms/nms-common/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/util/PaperSpigotUtil.kt index 3f709c7f..20972bfa 100644 --- a/nms/nms-common/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/util/PaperSpigotUtil.kt +++ b/nms/nms-common/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/util/PaperSpigotUtil.kt @@ -37,7 +37,7 @@ object PlatformUtil { // Lore fun ItemMeta.componentLore(): MutableList { val lore: List? - if(isPaper){ + if (isPaper) { lore = this.lore() } else { val legacyLores = this.lore ?: return ArrayList() @@ -52,13 +52,15 @@ object PlatformUtil { } fun ItemMeta.setComponentLore(lore: List) { - if(isPaper){ + if (isPaper) { this.lore(lore) } else { val legacyLore = ArrayList(lore.size) for (component in lore) { - legacyLore.add(if(component == null) null - else legacy_mm.serialize(component)) + legacyLore.add( + if (component == null) null + else legacy_mm.serialize(component) + ) } this.lore = legacyLore @@ -66,35 +68,34 @@ object PlatformUtil { } // Display name - private val useCustomName = hasMethod(ItemStack::class.java, "customName") + private val useCustomName = hasMethod(ItemMeta::class.java, "customName") fun ItemMeta.componentDisplayName(): Component? { - if(useCustomName){ - if(!this.hasCustomName()) return null + if (useCustomName) { + if (!this.hasCustomName()) return null return this.customName() - }else if(isPaper){ - if(!this.hasDisplayName()) return null - return this.displayName() - } else { - if(!this.hasDisplayName()) return null + } + if (!this.hasDisplayName()) return null - val legacy = this.displayName - return legacy_mm.deserialize(legacy) + return if (isPaper) { + this.displayName() + } else { + legacy_mm.deserialize(this.displayName) } } - fun ItemMeta.setComponentDisplayName(component: Component?) { - if(useCustomName){ + fun ItemMeta.setComponentDisplayName(component: Component?, fallback: String? = null) { + if (useCustomName) { this.customName(component) - }else if(isPaper){ + } else if (isPaper) { this.displayName(component) } else { - if(component == null){ + if (component == null) { this.setDisplayName(null) return } - val legacy = legacy_mm.serialize(component) + val legacy = fallback ?: legacy_mm.serialize(component) this.setDisplayName(legacy) } } diff --git a/src/main/kotlin/io/delilaheve/util/ItemUtil.kt b/src/main/kotlin/io/delilaheve/util/ItemUtil.kt index b883f3e9..e0cd2c3b 100644 --- a/src/main/kotlin/io/delilaheve/util/ItemUtil.kt +++ b/src/main/kotlin/io/delilaheve/util/ItemUtil.kt @@ -82,6 +82,6 @@ object ItemUtil { * The two items should either be the same type, or, the [other] is a book */ fun ItemStack.canMergeWith( - other: ItemStack? - ) = (other != null) && (customType == other.customType || (other.isEnchantedBook())) + other: ItemStack + ) = customType == other.customType || (other.isEnchantedBook()) } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/anvil/AnvilMergeLogic.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/anvil/AnvilMergeLogic.kt index e5e5bc7f..a1c411a3 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/anvil/AnvilMergeLogic.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/anvil/AnvilMergeLogic.kt @@ -6,6 +6,7 @@ import io.delilaheve.util.EnchantmentUtil.combineWith import io.delilaheve.util.ItemUtil.isEnchantedBook import io.delilaheve.util.ItemUtil.repairFrom import io.delilaheve.util.ItemUtil.unitRepair +import net.kyori.adventure.text.Component import org.bukkit.ChatColor import org.bukkit.Material import org.bukkit.entity.HumanEntity @@ -16,6 +17,7 @@ import org.bukkit.inventory.view.AnvilView import org.bukkit.persistence.PersistentDataType import xyz.alexcrea.cuanvil.api.EnchantmentApi import xyz.alexcrea.cuanvil.dependency.DependencyManager +import xyz.alexcrea.cuanvil.dependency.util.PlatformUtil.setComponentDisplayName import xyz.alexcrea.cuanvil.dialog.AnvilRenameDialog import xyz.alexcrea.cuanvil.enchant.CAEnchantment import xyz.alexcrea.cuanvil.recipe.AnvilCustomRecipe @@ -138,11 +140,12 @@ object AnvilMergeLogic { private fun handleRename(resultItem: ItemStack, view: AnvilView, player: HumanEntity): Int { // Can be null var renameText = ChatColor.stripColor(view.renameText) + var component: Component? = null var sumCost = 0 var useColor = false if (ConfigOptions.renameColorPossible && renameText != null) { - val component = AnvilColorUtil.handleColor( + component = AnvilColorUtil.handleColor( renameText, AnvilColorUtil.renamePermission(player) ) @@ -169,7 +172,11 @@ object AnvilMergeLogic { renameText == CasedStringUtil.snakeToUpperSpacedCase(resultItem.type.name.lowercase()) ) ) { - it.setDisplayName(renameText) + if (component == null) + component = if (renameText == null || renameText.isEmpty()) null + else Component.text(renameText) + it.setComponentDisplayName(component, renameText) + processDialogPCD(it, player) resultItem.itemMeta = it diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt index 22821589..7f9ef676 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt @@ -31,6 +31,7 @@ import xyz.alexcrea.cuanvil.listener.PrepareAnvilListener.Companion.ANVIL_INPUT_ import xyz.alexcrea.cuanvil.listener.PrepareAnvilListener.Companion.ANVIL_INPUT_RIGHT import xyz.alexcrea.cuanvil.listener.PrepareAnvilListener.Companion.ANVIL_OUTPUT_SLOT import xyz.alexcrea.cuanvil.util.CustomRecipeUtil +import xyz.alexcrea.cuanvil.util.MaterialUtil.isAir import xyz.alexcrea.cuanvil.util.MiniMessageUtil import xyz.alexcrea.cuanvil.util.anvil.AnvilLoreEditUtil import xyz.alexcrea.cuanvil.util.anvil.AnvilXpUtil @@ -92,7 +93,7 @@ class AnvilResultListener : Listener { } // Rename - if (rightItem == null) { + if (rightItem.isAir) { val result = AnvilMergeLogic.doRenaming(view, player, leftItem) if (result.isEmpty()) return @@ -106,7 +107,7 @@ class AnvilResultListener : Listener { } // Merge - val canMerge = leftItem.canMergeWith(rightItem) + val canMerge = leftItem.canMergeWith(rightItem!!) if (canMerge) { val result = AnvilMergeLogic.doMerge(view, player, leftItem, rightItem)