mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +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,13 +54,21 @@ 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
|
||||||
|
|
||||||
// Test rename lonely item
|
// Test rename lonely item
|
||||||
if(second == null) {
|
if (second == null) {
|
||||||
doRenaming(event, inventory, player, first)
|
doRenaming(event, inventory, player, first)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -70,23 +80,51 @@ class PrepareAnvilListener : Listener {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test for unit repair
|
// Test for unit repair
|
||||||
if(testUnitRepair(event, inventory, player, first, second)) return
|
if (testUnitRepair(event, inventory, player, first, second)) return
|
||||||
|
|
||||||
// Test for lore edit
|
// Test for lore edit
|
||||||
if(testLoreEdit(event, inventory, player, first, second)) return
|
if (testLoreEdit(event, inventory, player, first, second)) return
|
||||||
|
|
||||||
CustomAnvil.log("no anvil fuse type found")
|
CustomAnvil.log("no anvil fuse type found")
|
||||||
event.result = null
|
event.result = null
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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(
|
||||||
player: HumanEntity,
|
event: PrepareAnvilEvent, inventory: AnvilInventory,
|
||||||
first: ItemStack, second: ItemStack?): Boolean {
|
player: HumanEntity,
|
||||||
|
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
|
||||||
|
|
||||||
val amount = CustomRecipeUtil.getCustomRecipeAmount(recipe, first, second)
|
val amount = CustomRecipeUtil.getCustomRecipeAmount(recipe, first, second)
|
||||||
|
|
||||||
|
|
@ -94,7 +132,7 @@ class PrepareAnvilListener : Listener {
|
||||||
resultItem.amount *= amount
|
resultItem.amount *= amount
|
||||||
|
|
||||||
event.result = resultItem
|
event.result = resultItem
|
||||||
if(DependencyManager.tryTreatAnvilResult(event, resultItem)) return true
|
if (DependencyManager.tryTreatAnvilResult(event, resultItem)) return true
|
||||||
|
|
||||||
// Maybe add an option on custom craft to ignore/not ignore penalty ??
|
// Maybe add an option on custom craft to ignore/not ignore penalty ??
|
||||||
var xpCost = recipe.xpCostPerCraft * amount
|
var xpCost = recipe.xpCostPerCraft * amount
|
||||||
|
|
@ -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)
|
||||||
|
|
||||||
|
|
@ -118,7 +158,7 @@ class PrepareAnvilListener : Listener {
|
||||||
}
|
}
|
||||||
|
|
||||||
event.result = resultItem
|
event.result = resultItem
|
||||||
if(DependencyManager.tryTreatAnvilResult(event, resultItem)) return
|
if (DependencyManager.tryTreatAnvilResult(event, resultItem)) return
|
||||||
|
|
||||||
anvilCost += AnvilXpUtil.calculatePenalty(first, null, resultItem, AnvilUseType.RENAME_ONLY)
|
anvilCost += AnvilXpUtil.calculatePenalty(first, null, resultItem, AnvilUseType.RENAME_ONLY)
|
||||||
|
|
||||||
|
|
@ -131,18 +171,20 @@ class PrepareAnvilListener : Listener {
|
||||||
|
|
||||||
var sumCost = 0
|
var sumCost = 0
|
||||||
var useColor = false
|
var useColor = false
|
||||||
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()
|
||||||
|
|
||||||
sumCost+= ConfigOptions.useOfColorCost
|
sumCost += ConfigOptions.useOfColorCost
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -165,9 +207,11 @@ class PrepareAnvilListener : Listener {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun doMerge(event: PrepareAnvilEvent, inventory: AnvilInventory,
|
private fun doMerge(
|
||||||
player: HumanEntity,
|
event: PrepareAnvilEvent, inventory: AnvilInventory,
|
||||||
first: ItemStack, second: ItemStack) {
|
player: HumanEntity,
|
||||||
|
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()
|
||||||
|
|
@ -195,14 +239,16 @@ class PrepareAnvilListener : Listener {
|
||||||
|
|
||||||
// Finally, we set result
|
// Finally, we set result
|
||||||
event.result = resultItem
|
event.result = resultItem
|
||||||
if(DependencyManager.tryTreatAnvilResult(event, resultItem)) return
|
if (DependencyManager.tryTreatAnvilResult(event, resultItem)) return
|
||||||
|
|
||||||
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, anvilCost)
|
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, anvilCost)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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()
|
||||||
|
|
@ -222,26 +268,27 @@ class PrepareAnvilListener : Listener {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
event.result = resultItem
|
event.result = resultItem
|
||||||
if(DependencyManager.tryTreatAnvilResult(event, resultItem)) return true
|
if (DependencyManager.tryTreatAnvilResult(event, resultItem)) return true
|
||||||
|
|
||||||
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, anvilCost)
|
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, anvilCost)
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
if(result == null || first == result) {
|
if (result == null || first == result) {
|
||||||
CustomAnvil.log("lore edit, But input is same as output")
|
CustomAnvil.log("lore edit, But input is same as output")
|
||||||
event.result = null
|
event.result = null
|
||||||
return false
|
return false
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue