mirror of
https://github.com/Auxilor/EcoEnchants.git
synced 2025-02-09 03:21:21 +01:00
Fixed lookup and improved display
This commit is contained in:
parent
be8614e7b4
commit
e254007a34
@ -28,10 +28,6 @@ class EnchantDisplay(private val plugin: EcoEnchantsPlugin) : DisplayModule(plug
|
|||||||
props: DisplayProperties,
|
props: DisplayProperties,
|
||||||
vararg args: Any
|
vararg args: Any
|
||||||
) {
|
) {
|
||||||
if (!itemStack.isEnchantable) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
val fast = itemStack.fast()
|
val fast = itemStack.fast()
|
||||||
val pdc = fast.persistentDataContainer
|
val pdc = fast.persistentDataContainer
|
||||||
|
|
||||||
|
@ -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
|
@JvmStatic
|
||||||
@Suppress("UNCHECKED_CAST", "DEPRECATION")
|
@Suppress("UNCHECKED_CAST", "DEPRECATION")
|
||||||
fun removeEnchant(enchant: EcoEnchant) {
|
fun removeEnchant(enchant: Enchantment) {
|
||||||
BY_KEY.remove(enchant.id)
|
if (enchant is EcoEnchant) {
|
||||||
BY_NAME.remove(enchant.id)
|
BY_KEY.remove(enchant.id)
|
||||||
|
BY_NAME.remove(ChatColor.stripColor(enchant.displayName))
|
||||||
|
EnchantRegistrations.removeEnchant(enchant)
|
||||||
|
}
|
||||||
|
|
||||||
Enchantment::class.java.getDeclaredField("byKey")
|
Enchantment::class.java.getDeclaredField("byKey")
|
||||||
.apply {
|
.apply {
|
||||||
@ -121,8 +124,6 @@ object EcoEnchants {
|
|||||||
isAccessible = true
|
isAccessible = true
|
||||||
(get(null) as MutableMap<String, Enchantment>).apply { remove(enchant.name) }
|
(get(null) as MutableMap<String, Enchantment>).apply { remove(enchant.name) }
|
||||||
}
|
}
|
||||||
|
|
||||||
EnchantRegistrations.removeEnchant(enchant)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -134,10 +135,10 @@ object EcoEnchants {
|
|||||||
*/
|
*/
|
||||||
@Suppress("UNCHECKED_CAST", "DEPRECATION")
|
@Suppress("UNCHECKED_CAST", "DEPRECATION")
|
||||||
internal fun addNewEnchant(enchant: EcoEnchant) {
|
internal fun addNewEnchant(enchant: EcoEnchant) {
|
||||||
|
register(enchant)
|
||||||
|
|
||||||
BY_KEY[enchant.id] = enchant
|
BY_KEY[enchant.id] = enchant
|
||||||
BY_NAME[ChatColor.stripColor(enchant.displayName)] = enchant
|
BY_NAME[ChatColor.stripColor(enchant.displayName)] = enchant
|
||||||
|
|
||||||
register(enchant)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -153,9 +154,7 @@ object EcoEnchants {
|
|||||||
set(null, true)
|
set(null, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (enchantment is EcoEnchant) {
|
removeEnchant(enchantment)
|
||||||
removeEnchant(enchantment)
|
|
||||||
}
|
|
||||||
|
|
||||||
Enchantment.registerEnchantment(enchantment)
|
Enchantment.registerEnchantment(enchantment)
|
||||||
EnchantRegistrations.registerEnchantments()
|
EnchantRegistrations.registerEnchantments()
|
||||||
|
@ -11,10 +11,15 @@ import java.util.concurrent.TimeUnit
|
|||||||
// The Int is the inventory slot ID
|
// The Int is the inventory slot ID
|
||||||
typealias SlotProvider = (Player) -> Map<Int, ItemInSlot>
|
typealias SlotProvider = (Player) -> Map<Int, ItemInSlot>
|
||||||
|
|
||||||
data class ItemInSlot(
|
data class ItemInSlot internal constructor(
|
||||||
val item: ItemStack,
|
val item: ItemStack,
|
||||||
val slot: TargetSlot
|
val slot: Collection<TargetSlot>
|
||||||
)
|
) {
|
||||||
|
constructor(
|
||||||
|
item: ItemStack,
|
||||||
|
slot: TargetSlot
|
||||||
|
) : this(item, listOf(slot))
|
||||||
|
}
|
||||||
|
|
||||||
private data class HeldEnchant(
|
private data class HeldEnchant(
|
||||||
val enchant: EcoEnchant,
|
val enchant: EcoEnchant,
|
||||||
@ -52,7 +57,16 @@ object EnchantLookup {
|
|||||||
return itemCache.get(player) {
|
return itemCache.get(player) {
|
||||||
val found = mutableMapOf<Int, ItemInSlot>()
|
val found = mutableMapOf<Int, ItemInSlot>()
|
||||||
for (provider in slotProviders) {
|
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
|
found
|
||||||
@ -70,11 +84,6 @@ object EnchantLookup {
|
|||||||
for ((slotID, inSlot) in provide(this)) {
|
for ((slotID, inSlot) in provide(this)) {
|
||||||
val (item, slot) = inSlot
|
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
|
val enchants = item.fast().enchants
|
||||||
|
|
||||||
for ((enchant, level) in enchants) {
|
for ((enchant, level) in enchants) {
|
||||||
@ -82,7 +91,14 @@ object EnchantLookup {
|
|||||||
continue
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -216,6 +232,7 @@ object EnchantLookup {
|
|||||||
fun Player.clearEnchantCache() {
|
fun Player.clearEnchantCache() {
|
||||||
itemCache.invalidate(player)
|
itemCache.invalidate(player)
|
||||||
enchantCache.invalidate(player)
|
enchantCache.invalidate(player)
|
||||||
|
enchantLevelCache.invalidate(player)
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
Loading…
Reference in New Issue
Block a user