diff --git a/build.gradle.kts b/build.gradle.kts index 8c2f26d..c726534 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { } group = "io.delilaheve" -version = "1.1.3" +version = "1.1.4" repositories { mavenCentral() diff --git a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt index 96f4b44..4139067 100644 --- a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt +++ b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt @@ -18,6 +18,7 @@ import org.bukkit.inventory.AnvilInventory import org.bukkit.inventory.InventoryView.Property.REPAIR_COST import org.bukkit.inventory.ItemStack import org.bukkit.inventory.meta.Repairable +import xyz.alexcrea.group.ConflictType import kotlin.math.min /** @@ -49,7 +50,7 @@ class AnvilEventListener : Listener { val resultItem = first.clone() resultItem.setEnchantmentsUnsafe(newEnchants) - var anvilCost = calculateCost(first, second, resultItem, player.hasPermission(UnsafeEnchants.bypassFusePermission)) + var anvilCost = calculateCost(first, second, resultItem) if (!first.isBook() && !second.isBook()) { // we only need to be concerned with repair when neither item is a book val repaired = resultItem.repairFrom(first, second) @@ -114,7 +115,7 @@ class AnvilEventListener : Listener { * Function to calculate most of the xp requirement for the anvil fuse * Change result work penalty for future use */ - private fun calculateCost(left: ItemStack, right: ItemStack, result: ItemStack, bypassFuse: Boolean): Int{ + private fun calculateCost(left: ItemStack, right: ItemStack, result: ItemStack): Int{ // Extracted From https://minecraft.fandom.com/wiki/Anvil_mechanics#Enchantment_equation // Calculate work penality val leftPenality = (left.itemMeta as? Repairable)?.repairCost ?: 0 @@ -125,22 +126,26 @@ class AnvilEventListener : Listener { var illegalPenalty = 0 val rightIsFormBook = right.isBook() - val resultEnchs = result.findEnchantments().keys + val resultEnchs = result.findEnchantments() + val resultEnchsKeys = HashSet(resultEnchs.keys) for (enchantment in right.findEnchantments()) { // count enchant as illegal enchant if it conflicts with another enchant or not in result - if(!bypassFuse && ( - (enchantment.key !in resultEnchs) || - UnsafeEnchants.conflictManager.isConflicting(resultEnchs,result.type,enchantment.key) - )){ - // There may an issue when illegal enchant are trying to combine - // at least that what I think, but can't find why - illegalPenalty += ConfigOptions.sacrificeIllegalCost + if((enchantment.key !in resultEnchsKeys)){ + resultEnchsKeys.add(enchantment.key) + val conflictType = UnsafeEnchants.conflictManager.isConflicting(resultEnchsKeys,result.type,enchantment.key) + resultEnchsKeys.remove(enchantment.key) + + if(ConflictType.BIG_CONFLICT == conflictType){ + illegalPenalty += ConfigOptions.sacrificeIllegalCost + } continue } + // We know "enchantment.key in resultEnchs" true + val resultLevel = resultEnchs[enchantment.key]!! val enchantmentMultiplier = ConfigOptions.enchantmentValue(enchantment.key, rightIsFormBook) - val value = enchantment.value * enchantmentMultiplier + val value = resultLevel * enchantmentMultiplier UnsafeEnchants.log("Value for ${enchantment.key.enchantmentName} level ${enchantment.value} is $value") rightValue+=value diff --git a/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt b/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt index 5c5f1a1..b60adf2 100644 --- a/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt +++ b/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt @@ -4,6 +4,7 @@ import io.delilaheve.UnsafeEnchants import org.bukkit.Material import org.bukkit.enchantments.Enchantment import org.bukkit.entity.HumanEntity +import xyz.alexcrea.group.ConflictType import kotlin.math.max import kotlin.math.min @@ -34,7 +35,7 @@ object EnchantmentUtil { // Add the enchantment if it doesn't have conflicts, or, if player is allowed to bypass enchantment restrictions this[enchantment] = level if(!player.hasPermission(UnsafeEnchants.bypassFusePermission) && - UnsafeEnchants.conflictManager.isConflicting(this.keys,mat,enchantment)){ + (UnsafeEnchants.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT)){ this.remove(enchantment) } }else if(!keys.any { enchantment.conflictsWith(it) }){ @@ -45,7 +46,7 @@ object EnchantmentUtil { // Enchantment already in result list else{ // ... and they are conflicting - if(UnsafeEnchants.conflictManager.isConflicting(this.keys,mat,enchantment) + if((UnsafeEnchants.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT) && !player.hasPermission(UnsafeEnchants.bypassFusePermission)){ return@forEach } @@ -73,20 +74,4 @@ object EnchantmentUtil { } } - - /** - * Check if a set of enchantments has any conflicts - */ - fun Map.hasConflicts() : Boolean { - forEach { (enchantment1, _) -> - val hasConflict = any { (enchantment2, _) -> - enchantment2.conflictsWith(enchantment1) - } - if (hasConflict) { - return true - } - } - return false - } - } diff --git a/src/main/kotlin/xyz/alexcrea/group/EnchantConflictManager.kt b/src/main/kotlin/xyz/alexcrea/group/EnchantConflictManager.kt index 40016ce..b285989 100644 --- a/src/main/kotlin/xyz/alexcrea/group/EnchantConflictManager.kt +++ b/src/main/kotlin/xyz/alexcrea/group/EnchantConflictManager.kt @@ -122,15 +122,28 @@ class EnchantConflictManager { return group } - fun isConflicting(base: Set,mat: Material, newEnchant: Enchantment): Boolean{ - val conflictList = conflictMap[newEnchant] ?: return false + fun isConflicting(base: Set,mat: Material, newEnchant: Enchantment): ConflictType{ + val conflictList = conflictMap[newEnchant] ?: return ConflictType.NO_CONFLICT + var result = ConflictType.NO_CONFLICT for (conflict in conflictList) { if(!conflict.allowed(base,mat)) { - return true + if(conflict.getEnchants().size <= 1){ + result = ConflictType.SMALL_CONFLICT + }else{ + return ConflictType.BIG_CONFLICT + } } } - return false + return result } +} + +enum class ConflictType(){ + NO_CONFLICT, + SMALL_CONFLICT, + BIG_CONFLICT + + } \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 0c23ac6..b434ad8 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -18,7 +18,8 @@ remove_repair_limit: false # Valid range of 0 - 255 item_repair_cost: 2 -# Value added to the anvil when the sacrifice try to add illegal enchant to the item +# Value added to the anvil when a sacrifice enchantment conflict +# with one of the left item enchantment # # Valid range of 0 - 255 sacrifice_illegal_enchant_cost: 1 diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 9e3b21c..8c0e480 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,7 +1,7 @@ main: io.delilaheve.UnsafeEnchants name: UnsafeEnchantsPlus prefix: UnsafeEnchants+ -version: 1.1.3 +version: 1.1.4 description: Allow custom illegal enchantment api-version: 1.18 load: POSTWORLD