mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 08:14:00 +02:00
add pcd keept rename name
This commit is contained in:
parent
91d2cce8cc
commit
5265d81176
5 changed files with 68 additions and 14 deletions
|
|
@ -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?
|
||||
|
||||
}
|
||||
|
|
@ -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]
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue