Fixed lookup and improved display

This commit is contained in:
Auxilor 2022-08-31 15:01:06 +01:00
parent be8614e7b4
commit e254007a34
3 changed files with 38 additions and 26 deletions

View File

@ -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

View File

@ -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<String, Enchantment>).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()

View File

@ -11,10 +11,15 @@ import java.util.concurrent.TimeUnit
// The Int is the inventory slot ID
typealias SlotProvider = (Player) -> Map<Int, ItemInSlot>
data class ItemInSlot(
data class ItemInSlot internal constructor(
val item: ItemStack,
val slot: TargetSlot
)
val slot: Collection<TargetSlot>
) {
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<Int, ItemInSlot>()
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 {