From 5e42bf3a90b295cc4f44fe070f5ef31e3a31ba11 Mon Sep 17 00:00:00 2001 From: alexcrea <42614139+alexcrea@users.noreply.github.com> Date: Sat, 22 Jun 2024 14:59:56 +0200 Subject: [PATCH] Fix enchantment being applied to the left item when trying to combine. Fix conflict not being applied on some situation. --- .../enchant/AdditionalTestEnchantment.java | 34 +++++++++++++++ .../cuanvil/enchant/CAEnchantment.java | 13 ------ .../cuanvil/enchant/CAEnchantmentBase.java | 8 ---- .../cuanvil/enchant/wrapped/CAEcoEnchant.java | 36 ++++++++-------- .../cuanvil/group/EnchantConflictManager.kt | 43 +++++++++++++------ 5 files changed, 83 insertions(+), 51 deletions(-) create mode 100644 src/main/java/xyz/alexcrea/cuanvil/enchant/AdditionalTestEnchantment.java diff --git a/src/main/java/xyz/alexcrea/cuanvil/enchant/AdditionalTestEnchantment.java b/src/main/java/xyz/alexcrea/cuanvil/enchant/AdditionalTestEnchantment.java new file mode 100644 index 0000000..832e5af --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/enchant/AdditionalTestEnchantment.java @@ -0,0 +1,34 @@ +package xyz.alexcrea.cuanvil.enchant; + +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public interface AdditionalTestEnchantment { + + /** + * Test if the provided enchantments can be compatible with this enchantment. only non-Custom Anvil conflict. + * @param enchantments Immutable map of validated enchantments for the item. + * @param itemMat Material of the tested item. + * @return If there is a conflict with the enchantments. + */ + boolean isEnchantConflict( + @NotNull Map enchantments, + @NotNull Material itemMat); + + + /** + * Test if the provided item can be compatible with this enchantment. only non-Custom Anvil conflict. + * @param enchantments Immutable map of validated enchantments for the item. + * @param itemMat Material of the tested item. + * @param item Provide a new instance of the used item stack with the partial enchantment applied. + * @return If there is a conflict with the enchantment and the item. + */ + boolean isItemConflict( + @NotNull Map enchantments, + @NotNull Material itemMat, + @NotNull ItemStack item); + +} diff --git a/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantment.java b/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantment.java index 70a7c84..4a9a766 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantment.java +++ b/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantment.java @@ -89,19 +89,6 @@ public interface CAEnchantment { */ @NotNull Collection getConflicts(); - /** - * Test if the provided item can be compatible with this enchantment. only non-Custom Anvil conflict. - * @param baseEnchantments Validated enchantments for the item. - * @param itemMat Material of the tested item. - * @param itemSupply Provide a new instance of used item stack but with baseEnchantments as enchantments. - * @return Type of conflict this enchantment has with the provided item. - */ - @NotNull - ConflictType testOtherConflicts( - @NotNull Map baseEnchantments, - @NotNull Material itemMat, - @NotNull Supplier itemSupply); - /** * Get current level of the enchantment. * @param item Item to search the level for. diff --git a/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantmentBase.java b/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantmentBase.java index 435914c..7ea4666 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantmentBase.java +++ b/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantmentBase.java @@ -112,12 +112,4 @@ public abstract class CAEnchantmentBase implements CAEnchantment { return conflicts; } - @Override - public @NotNull ConflictType testOtherConflicts( - @NotNull Map baseEnchantments, - @NotNull Material itemMat, - @NotNull Supplier itemSupply) { - return ConflictType.NO_CONFLICT; - } - } diff --git a/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/CAEcoEnchant.java b/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/CAEcoEnchant.java index c185918..31991f4 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/CAEcoEnchant.java +++ b/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/CAEcoEnchant.java @@ -7,12 +7,13 @@ import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; import xyz.alexcrea.cuanvil.enchant.CAEnchantment; import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity; +import xyz.alexcrea.cuanvil.enchant.AdditionalTestEnchantment; import xyz.alexcrea.cuanvil.group.ConflictType; import java.util.Map; import java.util.function.Supplier; -public class CAEcoEnchant extends CAVanillaEnchantment { +public class CAEcoEnchant extends CAVanillaEnchantment implements AdditionalTestEnchantment { private final @NotNull EcoEnchant ecoEnchant; @@ -22,36 +23,37 @@ public class CAEcoEnchant extends CAVanillaEnchantment { } @Override - public @NotNull ConflictType testOtherConflicts( - @NotNull Map baseEnchantments, - @NotNull Material itemMat, - @NotNull Supplier itemSupply) { - - if(!baseEnchantments.isEmpty()){ - if(this.ecoEnchant.getConflictsWithEverything()){ - return ConflictType.ENCHANTMENT_CONFLICT; + public boolean isEnchantConflict(@NotNull Map enchantments, @NotNull Material itemMat) { + if(!enchantments.isEmpty()) { + if (this.ecoEnchant.getConflictsWithEverything()) { + return true; } - for (CAEnchantment other : baseEnchantments.keySet()) { - if(other instanceof CAVanillaEnchantment otherVanilla //TODO ISSUE FOUND: if vanilla is applied first conflcit is ignored. maybe export only enchantment conflict but keep target from ecoEnchant. + for (CAEnchantment other : enchantments.keySet()) { + if(other instanceof CAVanillaEnchantment otherVanilla && this.ecoEnchant.conflictsWith(otherVanilla.getEnchant())){ - return ConflictType.ENCHANTMENT_CONFLICT; + return true; } } } + return false; + } - // Allow enchanted book + @Override + public boolean isItemConflict(@NotNull Map enchantments, + @NotNull Material itemMat, + @NotNull ItemStack item) { if(Material.ENCHANTED_BOOK.equals(itemMat)){ - return ConflictType.NO_CONFLICT; + return false; } - ItemStack item = itemSupply.get(); for (EnchantmentTarget target : this.ecoEnchant.getTargets()) { if(target.matches(item)){ - return ConflictType.NO_CONFLICT; + return false; } } - return ConflictType.ITEM_CONFLICT; + + return true; } } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt index 3b23455..b2d54d4 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt @@ -5,9 +5,11 @@ import org.bukkit.NamespacedKey import org.bukkit.configuration.ConfigurationSection import org.bukkit.enchantments.Enchantment import org.bukkit.inventory.ItemStack +import xyz.alexcrea.cuanvil.enchant.AdditionalTestEnchantment import xyz.alexcrea.cuanvil.enchant.CAEnchantment import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry -import java.util.function.Supplier +import java.util.* +import kotlin.collections.ArrayList class EnchantConflictManager { @@ -166,23 +168,38 @@ class EnchantConflictManager { } } - // Test conflict with other conflict system. - val otherConflict = newEnchant.testOtherConflicts(appliedEnchants, mat, reEnchantSupplier(item, appliedEnchants)) + val immutableEnchants = Collections.unmodifiableMap(appliedEnchants) + for (appliedEnchant in appliedEnchants) { + if(appliedEnchant is AdditionalTestEnchantment){ + val doConflict = appliedEnchant.isEnchantConflict(immutableEnchants, mat) + if(doConflict){ + return ConflictType.ENCHANTMENT_CONFLICT + } +; + } + } - return result.getWorstConflict(otherConflict) + if((result != ConflictType.ITEM_CONFLICT) && (newEnchant is AdditionalTestEnchantment)){ + val partialItem = createPartialResult(item, immutableEnchants) + + if(newEnchant.isItemConflict(immutableEnchants, mat, partialItem)){ + return ConflictType.ITEM_CONFLICT + } + + } + + return result; } - private fun reEnchantSupplier(item: ItemStack, enchantments: Map): Supplier { - return Supplier { - val newItem = item.clone() + private fun createPartialResult(item: ItemStack, enchantments: Map): ItemStack { + val newItem = item.clone() - CAEnchantment.clearEnchants(newItem) - enchantments.forEach{//TODO maybe bulk add if possible - enchantment -> enchantment.key.addEnchantmentUnsafe(item, enchantment.value) - } - - return@Supplier newItem + CAEnchantment.clearEnchants(newItem) + enchantments.forEach{//TODO maybe bulk add if possible + enchantment -> enchantment.key.addEnchantmentUnsafe(newItem, enchantment.value) } + + return newItem } }