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
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?
}

View file

@ -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<HumanEntity, Component?, String?>,
val keepUserPreviousDialog: Supplier<Boolean>,
val maxLength: Supplier<Int>,
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]
}
}