mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-08-28 08:25:56 +02:00
Rename Dialog (#113)
Add a rename dialog for longer possible rename text Also has a rename fix
This commit is contained in:
commit
f0d53a6ffa
16 changed files with 485 additions and 27 deletions
|
|
@ -61,6 +61,13 @@ object ConfigOptions {
|
|||
const val ENCHANT_LIMIT_ROOT = "enchant_limits"
|
||||
const val ENCHANT_VALUES_ROOT = "enchant_values"
|
||||
|
||||
// Dialog menu rename
|
||||
const val DIALOG_RENAME_ENABLED = "enable_dialog_rename"
|
||||
const val DIALOG_MAX_SIZE = "dialog_rename_max_size"
|
||||
const val DIALOG_RENAME_USE_PERMISSION = "permission_needed_for_dialog_rename"
|
||||
const val DIALOG_KEEP_USER_TEXT = "dialog_rename_keep_user_text"
|
||||
|
||||
// Others
|
||||
const val DISABLE_MERGE_OVER_ROOT = "disable-merge-over"
|
||||
|
||||
const val IMMUTABLE_ENCHANTMENT_LIST = "immutable_enchantments"
|
||||
|
|
@ -107,6 +114,12 @@ object ConfigOptions {
|
|||
private const val DEFAULT_DEBUG_LOG = false
|
||||
private const val DEFAULT_VERBOSE_DEBUG_LOG = false
|
||||
|
||||
// Dialog menu rename
|
||||
const val DEFAULT_DIALOG_RENAME_ENABLED = false
|
||||
const val DEFAULT_DIALOG_MAX_SIZE = 256
|
||||
const val DEFAULT_DIALOG_RENAME_USE_PERMISSION = false
|
||||
const val DEFAULT_DIALOG_KEEP_USER_TEXT = true
|
||||
|
||||
// -------------
|
||||
// Config Ranges
|
||||
// -------------
|
||||
|
|
@ -131,6 +144,9 @@ object ConfigOptions {
|
|||
@JvmField
|
||||
val USE_OF_COLOR_COST_RANGE = 0..1000
|
||||
|
||||
@JvmField
|
||||
val DIALOG_MAX_SIZE_RANGE = 0..Int.MAX_VALUE
|
||||
|
||||
// Valid range for an enchantment limit
|
||||
const val ENCHANT_LIMIT = 255
|
||||
|
||||
|
|
@ -416,6 +432,48 @@ object ConfigOptions {
|
|||
.getBoolean(VERBOSE_DEBUG_LOGGING, DEFAULT_VERBOSE_DEBUG_LOG)
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the dialog menu for rename enabled
|
||||
*/
|
||||
val doRenameDialog: Boolean
|
||||
get() {
|
||||
return ConfigHolder.DEFAULT_CONFIG
|
||||
.config
|
||||
.getBoolean(DIALOG_RENAME_ENABLED, DEFAULT_DIALOG_RENAME_ENABLED)
|
||||
}
|
||||
|
||||
/**
|
||||
* Do the dialog menu require permission
|
||||
*/
|
||||
val doRenameDialogUsePermission: Boolean
|
||||
get() {
|
||||
return ConfigHolder.DEFAULT_CONFIG
|
||||
.config
|
||||
.getBoolean(DIALOG_RENAME_USE_PERMISSION, DEFAULT_DIALOG_RENAME_USE_PERMISSION)
|
||||
}
|
||||
|
||||
/**
|
||||
* Do the dialog menu require permission
|
||||
*/
|
||||
val renameDialogMaxSize: Int
|
||||
get() {
|
||||
return ConfigHolder.DEFAULT_CONFIG
|
||||
.config
|
||||
.getInt(DIALOG_MAX_SIZE, DEFAULT_DIALOG_MAX_SIZE)
|
||||
.takeIf { it in DIALOG_MAX_SIZE_RANGE }
|
||||
?: Int.MAX_VALUE
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import org.bukkit.event.Listener
|
|||
import org.bukkit.event.inventory.InventoryCloseEvent
|
||||
import org.bukkit.inventory.AnvilInventory
|
||||
import xyz.alexcrea.cuanvil.dependency.packet.PacketManager
|
||||
import xyz.alexcrea.cuanvil.util.dialog.AnvilRenameDialogUtil
|
||||
|
||||
class AnvilCloseListener(private val packetManager: PacketManager) : Listener {
|
||||
|
||||
|
|
@ -18,6 +19,7 @@ class AnvilCloseListener(private val packetManager: PacketManager) : Listener {
|
|||
packetManager.setInstantBuild(player, false)
|
||||
}
|
||||
|
||||
AnvilRenameDialogUtil.anvilRenameDialog.closeInventory(player)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -21,11 +21,14 @@ import org.bukkit.inventory.AnvilInventory
|
|||
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
|
||||
import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair
|
||||
import xyz.alexcrea.cuanvil.util.dialog.AnvilRenameDialogUtil
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
|
||||
/**
|
||||
|
|
@ -41,6 +44,8 @@ class PrepareAnvilListener : Listener {
|
|||
const val ANVIL_OUTPUT_SLOT = 2
|
||||
|
||||
var IS_EMPTY_TEST = false
|
||||
|
||||
private const val RENAME_DIALOG_PERMISSION = "ca.rename.dialog"
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -80,6 +85,8 @@ class PrepareAnvilListener : Listener {
|
|||
return
|
||||
}
|
||||
|
||||
tryRenameDialog(player, event)
|
||||
|
||||
// Test if the event should bypass custom anvil.
|
||||
if (DependencyManager.tryEventPreAnvilBypass(event, player)) {
|
||||
// even if we got bypassed we still want to set price
|
||||
|
|
@ -117,6 +124,36 @@ class PrepareAnvilListener : Listener {
|
|||
|
||||
}
|
||||
|
||||
private fun tryRenameDialog(
|
||||
player: HumanEntity,
|
||||
event: PrepareAnvilEvent
|
||||
) {
|
||||
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
|
||||
|
||||
|
|
@ -208,11 +245,8 @@ class PrepareAnvilListener : Listener {
|
|||
var useColor = false
|
||||
if (ConfigOptions.renameColorPossible && renameText != null) {
|
||||
val component = AnvilColorUtil.handleColor(
|
||||
renameText, player,
|
||||
ConfigOptions.permissionNeededForColor,
|
||||
ConfigOptions.allowColorCode, ConfigOptions.allowHexadecimalColor, ConfigOptions.allowMinimessage,
|
||||
AnvilColorUtil.ColorUseType.RENAME
|
||||
)
|
||||
renameText,
|
||||
AnvilColorUtil.renamePermission(player))
|
||||
|
||||
if (component != null) {
|
||||
renameText = MiniMessageUtil.legacy_mm.serialize(component)
|
||||
|
|
@ -230,8 +264,13 @@ class PrepareAnvilListener : Listener {
|
|||
else ChatColor.stripColor(it.displayName)
|
||||
|
||||
|
||||
if (!displayName.contentEquals(renameText) && !(displayName == null && renameText == "")) {
|
||||
if (!displayName.contentEquals(renameText) && !(displayName == null &&
|
||||
renameText == "" ||
|
||||
//TODO on recent paper check effective name instead
|
||||
renameText == CasedStringUtil.snakeToUpperSpacedCase(resultItem.type.name.lowercase())
|
||||
)) {
|
||||
it.setDisplayName(renameText)
|
||||
processDialogPCD(it, player)
|
||||
resultItem.itemMeta = it
|
||||
|
||||
sumCost += ConfigOptions.itemRenameCost
|
||||
|
|
|
|||
|
|
@ -60,25 +60,11 @@ object AnvilColorUtil {
|
|||
return ColorPermissions(canUseColorCode, canUseHexColor, canUseMinimessage, player)
|
||||
}
|
||||
|
||||
/**
|
||||
* Color a string depending on allowed color type, color use type and player permissions
|
||||
* @return colored component or null if nothing has been colored
|
||||
*/
|
||||
fun handleColor(
|
||||
textToColorText: String,
|
||||
player: Permissible,
|
||||
usePermission: Boolean,
|
||||
allowColorCode: Boolean,
|
||||
allowHexadecimalColor: Boolean,
|
||||
allowMinimessage: Boolean,
|
||||
useType: ColorUseType
|
||||
): Component? {
|
||||
val permission = calculatePermissions(
|
||||
player, usePermission,
|
||||
allowColorCode, allowHexadecimalColor, allowMinimessage,
|
||||
useType
|
||||
)
|
||||
return handleColor(textToColorText, permission)
|
||||
fun renamePermission(player: Permissible): ColorPermissions {
|
||||
return calculatePermissions(player,
|
||||
ConfigOptions.permissionNeededForColor,
|
||||
ConfigOptions.allowColorCode, ConfigOptions.allowHexadecimalColor, ConfigOptions.allowMinimessage,
|
||||
ColorUseType.RENAME)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,51 @@
|
|||
package xyz.alexcrea.cuanvil.util.dialog
|
||||
|
||||
import io.delilaheve.CustomAnvil
|
||||
import io.delilaheve.util.ConfigOptions
|
||||
import org.bukkit.entity.HumanEntity
|
||||
import org.bukkit.event.inventory.PrepareAnvilEvent
|
||||
import xyz.alexcrea.cuanvil.dependency.util.PlatformUtil
|
||||
import xyz.alexcrea.cuanvil.dialog.AnvilRenameDialog
|
||||
import xyz.alexcrea.cuanvil.dialog.AnvilRenameDialogImpl
|
||||
import xyz.alexcrea.cuanvil.update.UpdateUtils
|
||||
import xyz.alexcrea.cuanvil.util.AnvilColorUtil
|
||||
|
||||
object AnvilRenameDialogUtil {
|
||||
|
||||
val anvilRenameDialog: AnvilRenameDialog;
|
||||
|
||||
init {
|
||||
val version = UpdateUtils.currentMinecraftVersion()
|
||||
anvilRenameDialog = (if(!PlatformUtil.isPaper ||
|
||||
(version.major <= 1 && version.minor <= 21 && version.patch <= 6)) {
|
||||
NoImplAnvilRenameDialog()
|
||||
} else {
|
||||
AnvilRenameDialogImpl({ player, component -> AnvilColorUtil.revertColorSmallest(
|
||||
component, AnvilColorUtil.renamePermission(player)
|
||||
) },
|
||||
{ ConfigOptions.shouldKeepRenameText },
|
||||
{ ConfigOptions.renameDialogMaxSize },
|
||||
CustomAnvil.instance,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
class NoImplAnvilRenameDialog: AnvilRenameDialog {
|
||||
|
||||
override fun canSendDialog(): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
override fun tryShowDialog(
|
||||
player: HumanEntity,
|
||||
event: PrepareAnvilEvent
|
||||
) {}
|
||||
|
||||
override fun closeInventory(player: HumanEntity) {}
|
||||
|
||||
override fun currentText(player: HumanEntity): String? {
|
||||
return null
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue