diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantDisplay.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantDisplay.kt index 2e8968c0..c7b06e21 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantDisplay.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/EnchantDisplay.kt @@ -28,10 +28,6 @@ class EnchantDisplay(private val plugin: EcoEnchantsPlugin) : DisplayModule(plug props: DisplayProperties, vararg args: Any ) { - if (!itemStack.isEnchantable) { - return - } - val fast = itemStack.fast() val pdc = fast.persistentDataContainer diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchants.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchants.kt index f41c03f8..0d6a983e 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchants.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchants.kt @@ -100,15 +100,18 @@ object EcoEnchants { } /** - * Remove [EcoEnchant] from EcoEnchants. + * Remove [Enchantment] from EcoEnchants. * - * @param enchant The [EcoEnchant] to remove. + * @param enchant The [Enchantment] to remove. */ @JvmStatic @Suppress("UNCHECKED_CAST", "DEPRECATION") - fun removeEnchant(enchant: EcoEnchant) { - BY_KEY.remove(enchant.id) - BY_NAME.remove(enchant.id) + fun removeEnchant(enchant: Enchantment) { + if (enchant is EcoEnchant) { + BY_KEY.remove(enchant.id) + BY_NAME.remove(ChatColor.stripColor(enchant.displayName)) + EnchantRegistrations.removeEnchant(enchant) + } Enchantment::class.java.getDeclaredField("byKey") .apply { @@ -121,8 +124,6 @@ object EcoEnchants { isAccessible = true (get(null) as MutableMap).apply { remove(enchant.name) } } - - EnchantRegistrations.removeEnchant(enchant) } /** @@ -134,10 +135,10 @@ object EcoEnchants { */ @Suppress("UNCHECKED_CAST", "DEPRECATION") internal fun addNewEnchant(enchant: EcoEnchant) { + register(enchant) + BY_KEY[enchant.id] = enchant BY_NAME[ChatColor.stripColor(enchant.displayName)] = enchant - - register(enchant) } /** @@ -153,9 +154,7 @@ object EcoEnchants { set(null, true) } - if (enchantment is EcoEnchant) { - removeEnchant(enchantment) - } + removeEnchant(enchantment) Enchantment.registerEnchantment(enchantment) EnchantRegistrations.registerEnchantments() diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/target/EnchantLookup.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/target/EnchantLookup.kt index dd2128eb..a0e9dea4 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/target/EnchantLookup.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/target/EnchantLookup.kt @@ -11,10 +11,15 @@ import java.util.concurrent.TimeUnit // The Int is the inventory slot ID typealias SlotProvider = (Player) -> Map -data class ItemInSlot( +data class ItemInSlot internal constructor( val item: ItemStack, - val slot: TargetSlot -) + val slot: Collection +) { + constructor( + item: ItemStack, + slot: TargetSlot + ) : this(item, listOf(slot)) +} private data class HeldEnchant( val enchant: EcoEnchant, @@ -52,7 +57,16 @@ object EnchantLookup { return itemCache.get(player) { val found = mutableMapOf() for (provider in slotProviders) { - found.putAll(provider(player)) + val fromProvider = provider(player) + for ((slot, item) in fromProvider) { + // Basically a multimap + val current = found[slot] + if (current == null) { + found[slot] = item + } else { + found[slot] = ItemInSlot(item.item, listOf(current.slot, item.slot).flatten()) + } + } } found @@ -70,11 +84,6 @@ object EnchantLookup { for ((slotID, inSlot) in provide(this)) { val (item, slot) = inSlot - // Prevent repeating slot IDs found in multiple TargetSlots (e.g. HANDS and ANY) - if (found.containsKey(slotID)) { - continue - } - val enchants = item.fast().enchants for ((enchant, level) in enchants) { @@ -82,7 +91,14 @@ object EnchantLookup { continue } - if (slot !in enchant.slots) { + if (slot.none { it in enchant.slots }) { + continue + } + + val held = HeldEnchant(enchant, level) + + // Prevent repeating slot IDs found in multiple TargetSlots (e.g. HANDS and ANY) + if (held in found.getOrDefault(slotID, mutableListOf())) { continue } @@ -216,6 +232,7 @@ object EnchantLookup { fun Player.clearEnchantCache() { itemCache.invalidate(player) enchantCache.invalidate(player) + enchantLevelCache.invalidate(player) } init {