mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +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
|
|
@ -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