expose more enchantment function to API & use them
Some checks are pending
Java CI with Gradle / build (push) Waiting to run

This commit is contained in:
alexcrea 2026-06-30 07:47:41 +02:00
parent 72a7860d93
commit 1e2acb6f48
Signed by: alexcrea
GPG key ID: E346CD16413450E3
6 changed files with 107 additions and 72 deletions

View file

@ -21,23 +21,6 @@ object ItemUtil {
*/
fun ItemStack.isEnchantedBook() = type == ENCHANTED_BOOK
/**
* Find the enchantment map for this [ItemStack] and return it as a [MutableMap]
*/
fun ItemStack.findEnchantments(): MutableMap<CAEnchantment, Int> = CAEnchantment.getEnchants(this)
/**
* Apply an [enchantments] map to this [ItemStack]
*/
fun ItemStack.setEnchantmentsUnsafe(enchantments: Map<CAEnchantment, Int>) {
CAEnchantment.clearEnchants(this)
enchantments.forEach { (enchantment, level) ->
enchantment.addEnchantmentUnsafe(this, level)
}
}
private fun maxDamage(damageable: Damageable): Int {
val ver = UpdateUtils.currentMinecraftVersion()
if(ver.major <= 1 && ver.minor <= 20 && ver.patch < 5) return Integer.MAX_VALUE

View file

@ -3,10 +3,8 @@ package xyz.alexcrea.cuanvil.anvil
import io.delilaheve.CustomAnvil
import io.delilaheve.util.ConfigOptions
import io.delilaheve.util.EnchantmentUtil.combineWith
import io.delilaheve.util.ItemUtil.findEnchantments
import io.delilaheve.util.ItemUtil.isEnchantedBook
import io.delilaheve.util.ItemUtil.repairFrom
import io.delilaheve.util.ItemUtil.setEnchantmentsUnsafe
import io.delilaheve.util.ItemUtil.unitRepair
import org.bukkit.ChatColor
import org.bukkit.Material
@ -17,6 +15,7 @@ import org.bukkit.inventory.InventoryView
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.ItemMeta
import org.bukkit.persistence.PersistentDataType
import xyz.alexcrea.cuanvil.api.EnchantmentApi
import xyz.alexcrea.cuanvil.dependency.DependencyManager
import xyz.alexcrea.cuanvil.dialog.AnvilRenameDialog
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
@ -189,14 +188,19 @@ object AnvilMergeLogic {
player: Player,
first: ItemStack, second: ItemStack
): AnvilResult {
val newEnchants = first.findEnchantments()
.combineWith(second.findEnchantments(), first, player)
var hasChanged = !isIdentical(first.findEnchantments(), newEnchants)
val firstEnchants = EnchantmentApi.getEnchantments(first)
val secondEnchants = EnchantmentApi.getEnchantments(second)
// newEnchants will be mutated by combineWith
val newEnchants = HashMap(firstEnchants)
newEnchants.combineWith(secondEnchants, first, player)
var hasChanged = !isIdentical(firstEnchants, newEnchants)
val resultItem = DependencyManager.cloneItem(player, first)
val cost = AnvilCost()
if (hasChanged) {
resultItem.setEnchantmentsUnsafe(newEnchants)
EnchantmentApi.setEnchantments(resultItem, newEnchants)
// Calculate enchantment cost
AnvilXpUtil.getRightValues(first, second, resultItem, cost)
}

View file

@ -5,6 +5,7 @@ import org.bukkit.NamespacedKey
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.enchantments.Enchantment
import org.bukkit.inventory.ItemStack
import xyz.alexcrea.cuanvil.api.EnchantmentApi
import xyz.alexcrea.cuanvil.enchant.AdditionalTestEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
@ -66,7 +67,7 @@ class EnchantConflictManager {
val keys = config.getKeys(false)
for (key in keys) {
val section = config.getConfigurationSection(key)
if(section == null) {
if (section == null) {
warnBadKey(key)
continue
}
@ -140,8 +141,12 @@ class EnchantConflictManager {
return conflict
}
private fun fetchConditionalRestriction(restrictions: MutableMap<CAEnchantment, Int>, section: ConfigurationSection?, conflictName: String) {
if(section == null) return
private fun fetchConditionalRestriction(
restrictions: MutableMap<CAEnchantment, Int>,
section: ConfigurationSection?,
conflictName: String
) {
if (section == null) return
for (enchantName in section.getKeys(false)) {
val enchants = getEnchantByIdentifier(enchantName)
if (enchants.isEmpty()) {
@ -150,7 +155,7 @@ class EnchantConflictManager {
}
val value = section.getInt(enchantName, -1)
if(value < 0) continue
if (value < 0) continue
for (enchant in enchants) {
val previous = restrictions.getOrDefault(enchant, value)
@ -259,7 +264,8 @@ class EnchantConflictManager {
}
if ((result != ConflictType.ITEM_CONFLICT) && (newEnchant is AdditionalTestEnchantment)) {
val partialItem = createPartialResult(item, immutableEnchants)
val partialItem = item.clone()
EnchantmentApi.setEnchantments(partialItem, immutableEnchants)
if (newEnchant.isItemConflict(immutableEnchants, type, partialItem)) {
return ConflictType.ITEM_CONFLICT
@ -270,17 +276,6 @@ class EnchantConflictManager {
return result
}
private fun createPartialResult(item: ItemStack, enchantments: Map<CAEnchantment, Int>): ItemStack {
val newItem = item.clone()
CAEnchantment.clearEnchants(newItem)
enchantments.forEach { enchantment ->
enchantment.key.addEnchantmentUnsafe(newItem, enchantment.value)
}
return newItem
}
}
/**

View file

@ -3,7 +3,6 @@ package xyz.alexcrea.cuanvil.util.anvil
import io.delilaheve.CustomAnvil
import io.delilaheve.util.ConfigOptions
import io.delilaheve.util.EnchantmentUtil.enchantmentName
import io.delilaheve.util.ItemUtil.findEnchantments
import io.delilaheve.util.ItemUtil.isEnchantedBook
import org.bukkit.GameMode
import org.bukkit.NamespacedKey
@ -17,6 +16,7 @@ import org.bukkit.persistence.PersistentDataType
import xyz.alexcrea.cuanvil.anvil.AnvilCost
import xyz.alexcrea.cuanvil.anvil.AnvilMergeLogic.AnvilResult
import xyz.alexcrea.cuanvil.anvil.AnvilUseType
import xyz.alexcrea.cuanvil.api.EnchantmentApi
import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.dependency.DependencyManager
import xyz.alexcrea.cuanvil.dependency.economy.EconomyManager
@ -247,8 +247,8 @@ object AnvilXpUtil {
// Calculate right value and illegal enchant penalty
val rightIsFormBook = right.isEnchantedBook()
val rightEnchs = right.findEnchantments()
val resultEnchs = result.findEnchantments()
val rightEnchs = EnchantmentApi.getEnchantments(right)
val resultEnchs = EnchantmentApi.getEnchantments(result)
val resultEnchsKeys = HashMap(resultEnchs)
var rightValue = 0
@ -282,7 +282,7 @@ object AnvilXpUtil {
}
if(ConfigOptions.includeLeftEnchantmentForCost) {
val leftIsFormBook = left.isEnchantedBook()
val leftEnchs = left.findEnchantments()
val leftEnchs = EnchantmentApi.getEnchantments(left)
for (enchantment in leftEnchs) {
// Do not process enchantment that are present on the sacrifice
if(rightEnchs.contains(enchantment.key)) continue