mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-24 00:26:16 +02:00
add immutable list
This commit is contained in:
parent
b67d956e39
commit
f4ce267e44
2 changed files with 93 additions and 30 deletions
|
|
@ -2,6 +2,7 @@ package io.delilaheve.util
|
||||||
|
|
||||||
import io.delilaheve.CustomAnvil
|
import io.delilaheve.CustomAnvil
|
||||||
import io.delilaheve.util.EnchantmentUtil.enchantmentName
|
import io.delilaheve.util.EnchantmentUtil.enchantmentName
|
||||||
|
import org.bukkit.NamespacedKey
|
||||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||||
import xyz.alexcrea.cuanvil.config.WorkPenaltyType
|
import xyz.alexcrea.cuanvil.config.WorkPenaltyType
|
||||||
import xyz.alexcrea.cuanvil.config.WorkPenaltyType.WorkPenaltyPart
|
import xyz.alexcrea.cuanvil.config.WorkPenaltyType.WorkPenaltyPart
|
||||||
|
|
@ -51,6 +52,8 @@ object ConfigOptions {
|
||||||
|
|
||||||
const val DISABLE_MERGE_OVER_ROOT = "disable-merge-over"
|
const val DISABLE_MERGE_OVER_ROOT = "disable-merge-over"
|
||||||
|
|
||||||
|
const val IMMUTABLE_ENCHANTMENT_LIST = "immutable_enchantments"
|
||||||
|
|
||||||
// Keys for specific enchantment values
|
// Keys for specific enchantment values
|
||||||
private const val KEY_BOOK = "book"
|
private const val KEY_BOOK = "book"
|
||||||
private const val KEY_ITEM = "item"
|
private const val KEY_ITEM = "item"
|
||||||
|
|
@ -478,4 +481,17 @@ object ConfigOptions {
|
||||||
.takeIf { it in ENCHANT_LIMIT_RANGE }
|
.takeIf { it in ENCHANT_LIMIT_RANGE }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun isImmutable(key: NamespacedKey): Boolean {
|
||||||
|
val immutables = ConfigHolder.DEFAULT_CONFIG.config.getStringList(IMMUTABLE_ENCHANTMENT_LIST)
|
||||||
|
|
||||||
|
// We need to ignore case so can't just check "contain"
|
||||||
|
for (ench in immutables) {
|
||||||
|
if (ench.equals(key.toString(), ignoreCase = true) ||
|
||||||
|
ench.equals(key.key, ignoreCase = true)
|
||||||
|
)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,8 @@ 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.ItemStack
|
import org.bukkit.inventory.ItemStack
|
||||||
|
import org.bukkit.inventory.meta.EnchantmentStorageMeta
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta
|
||||||
import xyz.alexcrea.cuanvil.dependency.DependencyManager
|
import xyz.alexcrea.cuanvil.dependency.DependencyManager
|
||||||
import xyz.alexcrea.cuanvil.util.*
|
import xyz.alexcrea.cuanvil.util.*
|
||||||
import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair
|
import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair
|
||||||
|
|
@ -52,8 +54,16 @@ class PrepareAnvilListener : Listener {
|
||||||
val first = inventory.getItem(ANVIL_INPUT_LEFT) ?: return
|
val first = inventory.getItem(ANVIL_INPUT_LEFT) ?: return
|
||||||
val second = inventory.getItem(ANVIL_INPUT_RIGHT)
|
val second = inventory.getItem(ANVIL_INPUT_RIGHT)
|
||||||
|
|
||||||
|
|
||||||
if (!player.hasPermission(CustomAnvil.affectedByPluginPermission)) return
|
if (!player.hasPermission(CustomAnvil.affectedByPluginPermission)) return
|
||||||
|
|
||||||
|
if (isImmutable(first) || isImmutable(second)) {
|
||||||
|
CustomAnvil.verboseLog("Skipping anvil process as one of the two item is immutable")
|
||||||
|
|
||||||
|
event.result = null
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Test custom recipe
|
// Test custom recipe
|
||||||
if (testCustomRecipe(event, inventory, player, first, second)) return
|
if (testCustomRecipe(event, inventory, player, first, second)) return
|
||||||
|
|
||||||
|
|
@ -80,10 +90,38 @@ class PrepareAnvilListener : Listener {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun isImmutable(item: ItemStack?): Boolean {
|
||||||
|
if (item == null) return false
|
||||||
|
|
||||||
|
val meta = item.itemMeta
|
||||||
|
return meta != null &&
|
||||||
|
(hasImmutableEnchants(meta) || hasImmutableStoredEnchants(meta))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun hasImmutableEnchants(meta: ItemMeta): Boolean {
|
||||||
|
if (!meta.hasEnchants()) return false
|
||||||
|
|
||||||
|
for (enchant in meta.enchants.keys) {
|
||||||
|
if (ConfigOptions.isImmutable(enchant.key)) return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun hasImmutableStoredEnchants(meta: ItemMeta): Boolean {
|
||||||
|
if (meta !is EnchantmentStorageMeta || !meta.hasStoredEnchants()) return false
|
||||||
|
|
||||||
|
for (enchant in meta.storedEnchants.keys) {
|
||||||
|
if (ConfigOptions.isImmutable(enchant.key)) return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// return true if a custom recipe exist with these ingredients
|
// return true if a custom recipe exist with these ingredients
|
||||||
private fun testCustomRecipe(event: PrepareAnvilEvent, inventory: AnvilInventory,
|
private fun testCustomRecipe(
|
||||||
|
event: PrepareAnvilEvent, inventory: AnvilInventory,
|
||||||
player: HumanEntity,
|
player: HumanEntity,
|
||||||
first: ItemStack, second: ItemStack?): Boolean {
|
first: ItemStack, second: ItemStack?
|
||||||
|
): Boolean {
|
||||||
val recipe = CustomRecipeUtil.getCustomRecipe(first, second)
|
val recipe = CustomRecipeUtil.getCustomRecipe(first, second)
|
||||||
CustomAnvil.verboseLog("custom recipe not null? ${recipe != null}")
|
CustomAnvil.verboseLog("custom recipe not null? ${recipe != null}")
|
||||||
if (recipe == null) return false
|
if (recipe == null) return false
|
||||||
|
|
@ -105,8 +143,10 @@ class PrepareAnvilListener : Listener {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun doRenaming(event: PrepareAnvilEvent, inventory: AnvilInventory,
|
private fun doRenaming(
|
||||||
player: HumanEntity, first: ItemStack) {
|
event: PrepareAnvilEvent, inventory: AnvilInventory,
|
||||||
|
player: HumanEntity, first: ItemStack
|
||||||
|
) {
|
||||||
val resultItem = first.clone()
|
val resultItem = first.clone()
|
||||||
var anvilCost = handleRename(resultItem, inventory, player)
|
var anvilCost = handleRename(resultItem, inventory, player)
|
||||||
|
|
||||||
|
|
@ -134,10 +174,12 @@ class PrepareAnvilListener : Listener {
|
||||||
if (ConfigOptions.renameColorPossible && inventoryName != null) {
|
if (ConfigOptions.renameColorPossible && inventoryName != null) {
|
||||||
val resultString = StringBuilder(inventoryName)
|
val resultString = StringBuilder(inventoryName)
|
||||||
|
|
||||||
useColor = AnvilColorUtil.handleColor(resultString, player,
|
useColor = AnvilColorUtil.handleColor(
|
||||||
|
resultString, player,
|
||||||
ConfigOptions.permissionNeededForColor,
|
ConfigOptions.permissionNeededForColor,
|
||||||
ConfigOptions.allowColorCode, ConfigOptions.allowHexadecimalColor,
|
ConfigOptions.allowColorCode, ConfigOptions.allowHexadecimalColor,
|
||||||
AnvilColorUtil.ColorUseType.RENAME)
|
AnvilColorUtil.ColorUseType.RENAME
|
||||||
|
)
|
||||||
|
|
||||||
if (useColor) {
|
if (useColor) {
|
||||||
inventoryName = resultString.toString()
|
inventoryName = resultString.toString()
|
||||||
|
|
@ -165,9 +207,11 @@ class PrepareAnvilListener : Listener {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun doMerge(event: PrepareAnvilEvent, inventory: AnvilInventory,
|
private fun doMerge(
|
||||||
|
event: PrepareAnvilEvent, inventory: AnvilInventory,
|
||||||
player: HumanEntity,
|
player: HumanEntity,
|
||||||
first: ItemStack, second: ItemStack) {
|
first: ItemStack, second: ItemStack
|
||||||
|
) {
|
||||||
val newEnchants = first.findEnchantments()
|
val newEnchants = first.findEnchantments()
|
||||||
.combineWith(second.findEnchantments(), first, player)
|
.combineWith(second.findEnchantments(), first, player)
|
||||||
val resultItem = first.clone()
|
val resultItem = first.clone()
|
||||||
|
|
@ -201,8 +245,10 @@ class PrepareAnvilListener : Listener {
|
||||||
}
|
}
|
||||||
|
|
||||||
// return true if there is a valid unit repair with these ingredients
|
// return true if there is a valid unit repair with these ingredients
|
||||||
private fun testUnitRepair(event: PrepareAnvilEvent, inventory: AnvilInventory, player: HumanEntity,
|
private fun testUnitRepair(
|
||||||
first: ItemStack, second: ItemStack): Boolean {
|
event: PrepareAnvilEvent, inventory: AnvilInventory, player: HumanEntity,
|
||||||
|
first: ItemStack, second: ItemStack
|
||||||
|
): Boolean {
|
||||||
val unitRepairAmount = first.getRepair(second) ?: return false
|
val unitRepairAmount = first.getRepair(second) ?: return false
|
||||||
|
|
||||||
val resultItem = first.clone()
|
val resultItem = first.clone()
|
||||||
|
|
@ -228,16 +274,17 @@ class PrepareAnvilListener : Listener {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun testLoreEdit(event: PrepareAnvilEvent, inventory: AnvilInventory, player: HumanEntity,
|
private fun testLoreEdit(
|
||||||
first: ItemStack, second: ItemStack): Boolean {
|
event: PrepareAnvilEvent, inventory: AnvilInventory, player: HumanEntity,
|
||||||
|
first: ItemStack, second: ItemStack
|
||||||
|
): Boolean {
|
||||||
val type = second.type
|
val type = second.type
|
||||||
var result: ItemStack? = null
|
var result: ItemStack? = null
|
||||||
|
|
||||||
val xpCost = AtomicInteger()
|
val xpCost = AtomicInteger()
|
||||||
if (Material.WRITABLE_BOOK == type) {
|
if (Material.WRITABLE_BOOK == type) {
|
||||||
result = AnvilLoreEditUtil.tryLoreEditByBook(player, first, second, xpCost)
|
result = AnvilLoreEditUtil.tryLoreEditByBook(player, first, second, xpCost)
|
||||||
}
|
} else if (Material.PAPER == type) {
|
||||||
else if(Material.PAPER == type) {
|
|
||||||
result = AnvilLoreEditUtil.tryLoreEditByPaper(player, first, second, xpCost)
|
result = AnvilLoreEditUtil.tryLoreEditByPaper(player, first, second, xpCost)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue