add pcd keept rename name

This commit is contained in:
alexcrea 2026-05-21 20:36:16 +02:00
parent 91d2cce8cc
commit 5265d81176
Signed by: alexcrea
GPG key ID: E346CD16413450E3
5 changed files with 68 additions and 14 deletions

View file

@ -1,14 +1,21 @@
package xyz.alexcrea.cuanvil.dialog package xyz.alexcrea.cuanvil.dialog
import org.bukkit.NamespacedKey
import org.bukkit.entity.HumanEntity import org.bukkit.entity.HumanEntity
import org.bukkit.event.inventory.PrepareAnvilEvent import org.bukkit.event.inventory.PrepareAnvilEvent
interface AnvilRenameDialog { interface AnvilRenameDialog {
companion object {
val PCD_KEEP_RENAME_TEXT_KEY = NamespacedKey.fromString("customanvil:last_rename_text")!!
}
fun canSendDialog(): Boolean fun canSendDialog(): Boolean
fun tryShowDialog(player: HumanEntity, event: PrepareAnvilEvent) fun tryShowDialog(player: HumanEntity, event: PrepareAnvilEvent)
fun closeInventory(player: HumanEntity) fun closeInventory(player: HumanEntity)
fun currentText(player: HumanEntity): String?
} }

View file

@ -19,16 +19,17 @@ import org.bukkit.craftbukkit.inventory.view.CraftAnvilView
import org.bukkit.entity.HumanEntity import org.bukkit.entity.HumanEntity
import org.bukkit.event.inventory.PrepareAnvilEvent import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.inventory.ItemStack import org.bukkit.inventory.ItemStack
import org.bukkit.persistence.PersistentDataType
import org.bukkit.plugin.Plugin import org.bukkit.plugin.Plugin
import java.util.UUID import java.util.*
import java.util.function.BiFunction import java.util.function.BiFunction
import java.util.function.Consumer import java.util.function.Consumer
import java.util.function.Supplier import java.util.function.Supplier
import kotlin.collections.set
@Suppress("UnstableApiUsage") @Suppress("UnstableApiUsage")
class AnvilRenameDialogImpl( class AnvilRenameDialogImpl(
val fromFormated: BiFunction<HumanEntity, Component?, String?>, val fromFormated: BiFunction<HumanEntity, Component?, String?>,
val keepUserPreviousDialog: Supplier<Boolean>,
val maxLength: Supplier<Int>, val maxLength: Supplier<Int>,
val plugin: Plugin, val plugin: Plugin,
): AnvilRenameDialog { ): AnvilRenameDialog {
@ -118,9 +119,19 @@ class AnvilRenameDialogImpl(
} }
private fun nameFromItem(player: HumanEntity, item: ItemStack?): String? { private fun nameFromItem(player: HumanEntity, item: ItemStack?): String? {
return if(item?.hasItemMeta() != true || !item.itemMeta.hasCustomName()) // Already has text
PLAIN_TEXT_SERIALIZER.serializeOrNull(item?.effectiveName()) if(item?.hasItemMeta() != true || !item.itemMeta.hasCustomName())
else fromFormated.apply(player, item.effectiveName()) 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) { private fun tryShowDialogScheduled(player: HumanEntity, event: PrepareAnvilEvent) {
@ -178,4 +189,8 @@ class AnvilRenameDialogImpl(
runTaskMap.remove(player.uniqueId)?.cancel() runTaskMap.remove(player.uniqueId)?.cancel()
} }
override fun currentText(player: HumanEntity): String? {
return lastNames[player.uniqueId]
}
} }

View file

@ -464,6 +464,16 @@ object ConfigOptions {
?: DEFAULT_DIALOG_MAX_SIZE ?: 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 * Get the given [enchantment]'s limit
*/ */

View file

@ -18,11 +18,12 @@ import org.bukkit.event.EventPriority
import org.bukkit.event.Listener import org.bukkit.event.Listener
import org.bukkit.event.inventory.PrepareAnvilEvent import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.inventory.AnvilInventory import org.bukkit.inventory.AnvilInventory
import org.bukkit.inventory.InventoryView
import org.bukkit.inventory.ItemStack import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.EnchantmentStorageMeta import org.bukkit.inventory.meta.EnchantmentStorageMeta
import org.bukkit.inventory.meta.ItemMeta import org.bukkit.inventory.meta.ItemMeta
import org.bukkit.persistence.PersistentDataType
import xyz.alexcrea.cuanvil.dependency.DependencyManager import xyz.alexcrea.cuanvil.dependency.DependencyManager
import xyz.alexcrea.cuanvil.dialog.AnvilRenameDialog
import xyz.alexcrea.cuanvil.enchant.CAEnchantment import xyz.alexcrea.cuanvil.enchant.CAEnchantment
import xyz.alexcrea.cuanvil.util.* import xyz.alexcrea.cuanvil.util.*
import xyz.alexcrea.cuanvil.util.MaterialUtil.isAir import xyz.alexcrea.cuanvil.util.MaterialUtil.isAir
@ -44,7 +45,7 @@ class PrepareAnvilListener : Listener {
var IS_EMPTY_TEST = false 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, player: HumanEntity,
event: PrepareAnvilEvent event: PrepareAnvilEvent
) { ) {
if(!ConfigOptions.doRenameDialog || !AnvilRenameDialogUtil.anvilRenameDialog.canSendDialog()) return if(!canUseRenameDialog(player)) return
if(ConfigOptions.doRenameDialogUsePermission && !player.hasPermission(RENAME_DIALOG_PERMISSION)) return
AnvilRenameDialogUtil.anvilRenameDialog.tryShowDialog(player, event) 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 { private fun isImmutable(item: ItemStack?): Boolean {
if (item.isAir) return false if (item.isAir) return false
@ -249,6 +270,7 @@ class PrepareAnvilListener : Listener {
renameText == CasedStringUtil.snakeToUpperSpacedCase(resultItem.type.name.lowercase()) renameText == CasedStringUtil.snakeToUpperSpacedCase(resultItem.type.name.lowercase())
)) { )) {
it.setDisplayName(renameText) it.setDisplayName(renameText)
processDialogPCD(it, player)
resultItem.itemMeta = it resultItem.itemMeta = it
sumCost += ConfigOptions.itemRenameCost sumCost += ConfigOptions.itemRenameCost

View file

@ -2,7 +2,6 @@ package xyz.alexcrea.cuanvil.util.dialog
import io.delilaheve.CustomAnvil import io.delilaheve.CustomAnvil
import io.delilaheve.util.ConfigOptions import io.delilaheve.util.ConfigOptions
import net.kyori.adventure.text.Component
import org.bukkit.entity.HumanEntity import org.bukkit.entity.HumanEntity
import org.bukkit.event.inventory.PrepareAnvilEvent import org.bukkit.event.inventory.PrepareAnvilEvent
import xyz.alexcrea.cuanvil.dependency.util.PlatformUtil import xyz.alexcrea.cuanvil.dependency.util.PlatformUtil
@ -24,6 +23,7 @@ object AnvilRenameDialogUtil {
AnvilRenameDialogImpl({ player, component -> AnvilColorUtil.revertColorSmallest( AnvilRenameDialogImpl({ player, component -> AnvilColorUtil.revertColorSmallest(
component, AnvilColorUtil.renamePermission(player) component, AnvilColorUtil.renamePermission(player)
) }, ) },
{ ConfigOptions.shouldKeepRenameText },
{ ConfigOptions.renameDialogMaxSize }, { ConfigOptions.renameDialogMaxSize },
CustomAnvil.instance, CustomAnvil.instance,
) )
@ -39,12 +39,12 @@ object AnvilRenameDialogUtil {
override fun tryShowDialog( override fun tryShowDialog(
player: HumanEntity, player: HumanEntity,
event: PrepareAnvilEvent event: PrepareAnvilEvent
) { ) {}
} override fun closeInventory(player: HumanEntity) {}
override fun closeInventory(player: HumanEntity) {
override fun currentText(player: HumanEntity): String? {
return null
} }
} }