From 5265d81176160fbeadf360389503b8441598731d Mon Sep 17 00:00:00 2001 From: alexcrea Date: Thu, 21 May 2026 20:36:16 +0200 Subject: [PATCH] add pcd keept rename name --- .../cuanvil/dialog/AnvilRenameDialog.kt | 7 +++++ .../cuanvil/dialog/AnvilRenameDialogImpl.kt | 25 ++++++++++++---- .../io/delilaheve/util/ConfigOptions.kt | 10 +++++++ .../cuanvil/listener/PrepareAnvilListener.kt | 30 ++++++++++++++++--- .../util/dialog/AnvilRenameDialogUtil.kt | 10 +++---- 5 files changed, 68 insertions(+), 14 deletions(-) diff --git a/nms/nms-common/src/main/kotlin/xyz/alexcrea/cuanvil/dialog/AnvilRenameDialog.kt b/nms/nms-common/src/main/kotlin/xyz/alexcrea/cuanvil/dialog/AnvilRenameDialog.kt index a50af23..62e1cf0 100644 --- a/nms/nms-common/src/main/kotlin/xyz/alexcrea/cuanvil/dialog/AnvilRenameDialog.kt +++ b/nms/nms-common/src/main/kotlin/xyz/alexcrea/cuanvil/dialog/AnvilRenameDialog.kt @@ -1,14 +1,21 @@ package xyz.alexcrea.cuanvil.dialog +import org.bukkit.NamespacedKey import org.bukkit.entity.HumanEntity import org.bukkit.event.inventory.PrepareAnvilEvent interface AnvilRenameDialog { + companion object { + val PCD_KEEP_RENAME_TEXT_KEY = NamespacedKey.fromString("customanvil:last_rename_text")!! + } + fun canSendDialog(): Boolean fun tryShowDialog(player: HumanEntity, event: PrepareAnvilEvent) fun closeInventory(player: HumanEntity) + fun currentText(player: HumanEntity): String? + } \ No newline at end of file diff --git a/nms/nms-paper/src/main/kotlin/xyz/alexcrea/cuanvil/dialog/AnvilRenameDialogImpl.kt b/nms/nms-paper/src/main/kotlin/xyz/alexcrea/cuanvil/dialog/AnvilRenameDialogImpl.kt index 00491e0..603a215 100644 --- a/nms/nms-paper/src/main/kotlin/xyz/alexcrea/cuanvil/dialog/AnvilRenameDialogImpl.kt +++ b/nms/nms-paper/src/main/kotlin/xyz/alexcrea/cuanvil/dialog/AnvilRenameDialogImpl.kt @@ -19,16 +19,17 @@ import org.bukkit.craftbukkit.inventory.view.CraftAnvilView import org.bukkit.entity.HumanEntity import org.bukkit.event.inventory.PrepareAnvilEvent import org.bukkit.inventory.ItemStack +import org.bukkit.persistence.PersistentDataType import org.bukkit.plugin.Plugin -import java.util.UUID +import java.util.* import java.util.function.BiFunction import java.util.function.Consumer import java.util.function.Supplier -import kotlin.collections.set @Suppress("UnstableApiUsage") class AnvilRenameDialogImpl( val fromFormated: BiFunction, + val keepUserPreviousDialog: Supplier, val maxLength: Supplier, val plugin: Plugin, ): AnvilRenameDialog { @@ -118,9 +119,19 @@ class AnvilRenameDialogImpl( } private fun nameFromItem(player: HumanEntity, item: ItemStack?): String? { - return if(item?.hasItemMeta() != true || !item.itemMeta.hasCustomName()) - PLAIN_TEXT_SERIALIZER.serializeOrNull(item?.effectiveName()) - else fromFormated.apply(player, item.effectiveName()) + // Already has text + if(item?.hasItemMeta() != true || !item.itemMeta.hasCustomName()) + return PLAIN_TEXT_SERIALIZER.serializeOrNull(item?.effectiveName()) + + if(keepUserPreviousDialog.get() && item.hasItemMeta()) { + val lastName = item.itemMeta.persistentDataContainer.get( + AnvilRenameDialog.PCD_KEEP_RENAME_TEXT_KEY, + PersistentDataType.STRING) + + if(lastName != null) return lastName + } + + return fromFormated.apply(player, item.effectiveName()) } private fun tryShowDialogScheduled(player: HumanEntity, event: PrepareAnvilEvent) { @@ -178,4 +189,8 @@ class AnvilRenameDialogImpl( runTaskMap.remove(player.uniqueId)?.cancel() } + override fun currentText(player: HumanEntity): String? { + return lastNames[player.uniqueId] + } + } \ No newline at end of file diff --git a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt index c30853a..f34b9ef 100644 --- a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt +++ b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt @@ -464,6 +464,16 @@ object ConfigOptions { ?: DEFAULT_DIALOG_MAX_SIZE } + /** + * Should the text used for rename should be kept in the item's pdc + */ + val shouldKeepRenameText: Boolean + get() { + return ConfigHolder.DEFAULT_CONFIG + .config + .getBoolean(DIALOG_KEEP_USER_TEXT, DEFAULT_DIALOG_KEEP_USER_TEXT) + } + /** * Get the given [enchantment]'s limit */ diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt index b7bedb3..0525a85 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt @@ -18,11 +18,12 @@ import org.bukkit.event.EventPriority import org.bukkit.event.Listener import org.bukkit.event.inventory.PrepareAnvilEvent import org.bukkit.inventory.AnvilInventory -import org.bukkit.inventory.InventoryView import org.bukkit.inventory.ItemStack import org.bukkit.inventory.meta.EnchantmentStorageMeta import org.bukkit.inventory.meta.ItemMeta +import org.bukkit.persistence.PersistentDataType import xyz.alexcrea.cuanvil.dependency.DependencyManager +import xyz.alexcrea.cuanvil.dialog.AnvilRenameDialog import xyz.alexcrea.cuanvil.enchant.CAEnchantment import xyz.alexcrea.cuanvil.util.* import xyz.alexcrea.cuanvil.util.MaterialUtil.isAir @@ -44,7 +45,7 @@ class PrepareAnvilListener : Listener { var IS_EMPTY_TEST = false - const val RENAME_DIALOG_PERMISSION = "ca.rename.dialog" + private const val RENAME_DIALOG_PERMISSION = "ca.rename.dialog" } /** @@ -127,12 +128,32 @@ class PrepareAnvilListener : Listener { player: HumanEntity, event: PrepareAnvilEvent ) { - if(!ConfigOptions.doRenameDialog || !AnvilRenameDialogUtil.anvilRenameDialog.canSendDialog()) return - if(ConfigOptions.doRenameDialogUsePermission && !player.hasPermission(RENAME_DIALOG_PERMISSION)) return + if(!canUseRenameDialog(player)) return AnvilRenameDialogUtil.anvilRenameDialog.tryShowDialog(player, event) } + private fun canUseRenameDialog(player: HumanEntity): Boolean { + if(!ConfigOptions.doRenameDialog || !AnvilRenameDialogUtil.anvilRenameDialog.canSendDialog()) return false + if(ConfigOptions.doRenameDialogUsePermission && !player.hasPermission(RENAME_DIALOG_PERMISSION)) return false + + return true + } + + private fun processDialogPCD(it: ItemMeta, player: HumanEntity) { + val keepDialog = canUseRenameDialog(player) && ConfigOptions.shouldKeepRenameText + + val pdc = it.persistentDataContainer + if(!keepDialog) + pdc.remove(AnvilRenameDialog.PCD_KEEP_RENAME_TEXT_KEY) + else { + val text = AnvilRenameDialogUtil.anvilRenameDialog.currentText(player) + if(text == null || text.isBlank()) + pdc.remove(AnvilRenameDialog.PCD_KEEP_RENAME_TEXT_KEY) + else pdc.set(AnvilRenameDialog.PCD_KEEP_RENAME_TEXT_KEY, PersistentDataType.STRING, text) + } + } + private fun isImmutable(item: ItemStack?): Boolean { if (item.isAir) return false @@ -249,6 +270,7 @@ class PrepareAnvilListener : Listener { renameText == CasedStringUtil.snakeToUpperSpacedCase(resultItem.type.name.lowercase()) )) { it.setDisplayName(renameText) + processDialogPCD(it, player) resultItem.itemMeta = it sumCost += ConfigOptions.itemRenameCost diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/util/dialog/AnvilRenameDialogUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/dialog/AnvilRenameDialogUtil.kt index c72ddf3..dbff77b 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/util/dialog/AnvilRenameDialogUtil.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/util/dialog/AnvilRenameDialogUtil.kt @@ -2,7 +2,6 @@ package xyz.alexcrea.cuanvil.util.dialog import io.delilaheve.CustomAnvil import io.delilaheve.util.ConfigOptions -import net.kyori.adventure.text.Component import org.bukkit.entity.HumanEntity import org.bukkit.event.inventory.PrepareAnvilEvent import xyz.alexcrea.cuanvil.dependency.util.PlatformUtil @@ -24,6 +23,7 @@ object AnvilRenameDialogUtil { AnvilRenameDialogImpl({ player, component -> AnvilColorUtil.revertColorSmallest( component, AnvilColorUtil.renamePermission(player) ) }, + { ConfigOptions.shouldKeepRenameText }, { ConfigOptions.renameDialogMaxSize }, CustomAnvil.instance, ) @@ -39,12 +39,12 @@ object AnvilRenameDialogUtil { override fun tryShowDialog( player: HumanEntity, event: PrepareAnvilEvent - ) { + ) {} - } - - override fun closeInventory(player: HumanEntity) { + override fun closeInventory(player: HumanEntity) {} + override fun currentText(player: HumanEntity): String? { + return null } }