diff options
author | Linnea Gräf <nea@nea.moe> | 2024-05-18 14:57:07 +0200 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-05-18 14:57:07 +0200 |
commit | a39f6e9bec3318b2be251570ea7aea29d58b49cb (patch) | |
tree | e7d93e4243c172f80111e18768f5ee7ec19fb3f5 | |
parent | ad09808c25c769be760559e1a597e29e488827bf (diff) | |
download | Firmament-a39f6e9bec3318b2be251570ea7aea29d58b49cb.tar.gz Firmament-a39f6e9bec3318b2be251570ea7aea29d58b49cb.tar.bz2 Firmament-a39f6e9bec3318b2be251570ea7aea29d58b49cb.zip |
Fix item rarity not displaying in npc shops
5 files changed, 51 insertions, 8 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/features/inventory/ItemRarityCosmetics.kt b/src/main/kotlin/moe/nea/firmament/features/inventory/ItemRarityCosmetics.kt index dde0ddb..de37748 100644 --- a/src/main/kotlin/moe/nea/firmament/features/inventory/ItemRarityCosmetics.kt +++ b/src/main/kotlin/moe/nea/firmament/features/inventory/ItemRarityCosmetics.kt @@ -19,6 +19,9 @@ import moe.nea.firmament.features.FirmamentFeature import moe.nea.firmament.gui.config.ManagedConfig import moe.nea.firmament.util.MC import moe.nea.firmament.util.item.loreAccordingToNbt +import moe.nea.firmament.util.lastNotNullOfOrNull +import moe.nea.firmament.util.memoize +import moe.nea.firmament.util.memoizeIdentity import moe.nea.firmament.util.unformattedString object ItemRarityCosmetics : FirmamentFeature { @@ -48,15 +51,19 @@ object ItemRarityCosmetics : FirmamentFeature { val c = Color(it.value.colorValue!!) Triple(c.red / 255F, c.green / 255F, c.blue / 255F) } - private val ItemStack.skyblockLoreRarityColor: Triple<Float, Float, Float>? - get() { - val entry = loreAccordingToNbt.lastOrNull()?.unformattedString ?: "" - return rarityToColor.entries.find { (k, v) -> k in entry }?.value + + private fun getSkyblockRarity0(itemStack: ItemStack): Triple<Float, Float, Float>? { + return itemStack.loreAccordingToNbt.lastNotNullOfOrNull { + val entry = it.unformattedString + rarityToColor.entries.find { (k, v) -> k in entry }?.value } + } + + val getSkyblockRarity = ::getSkyblockRarity0.memoizeIdentity(100) fun drawItemStackRarity(drawContext: DrawContext, x: Int, y: Int, item: ItemStack) { - val (r, g, b) = item.skyblockLoreRarityColor ?: return + val (r, g, b) = getSkyblockRarity(item) ?: return drawContext.drawSprite( x, y, 0, diff --git a/src/main/kotlin/moe/nea/firmament/util/IdentityCharacteristics.kt b/src/main/kotlin/moe/nea/firmament/util/IdentityCharacteristics.kt index e6dad79..713242c 100644 --- a/src/main/kotlin/moe/nea/firmament/util/IdentityCharacteristics.kt +++ b/src/main/kotlin/moe/nea/firmament/util/IdentityCharacteristics.kt @@ -1,5 +1,6 @@ /* * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe> + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> * * SPDX-License-Identifier: GPL-3.0-or-later */ @@ -8,7 +9,9 @@ package moe.nea.firmament.util class IdentityCharacteristics<T>(val value: T) { override fun equals(other: Any?): Boolean { - return value === other + if (this === other) return true + if (other !is IdentityCharacteristics<*>) return false + return value === other.value } override fun hashCode(): Int { diff --git a/src/main/kotlin/moe/nea/firmament/util/MutableMapWithMaxSize.kt b/src/main/kotlin/moe/nea/firmament/util/MutableMapWithMaxSize.kt index 8032053..72e2dc1 100644 --- a/src/main/kotlin/moe/nea/firmament/util/MutableMapWithMaxSize.kt +++ b/src/main/kotlin/moe/nea/firmament/util/MutableMapWithMaxSize.kt @@ -11,3 +11,22 @@ fun <K, V> mutableMapWithMaxSize(maxSize: Int): MutableMap<K, V> = object : Link return size > maxSize } } + +fun <T, R> ((T) -> R).memoizeIdentity(maxCacheSize: Int): (T) -> R { + val memoized = { it: IdentityCharacteristics<T> -> + this(it.value) + }.memoize(maxCacheSize) + return { memoized(IdentityCharacteristics(it)) } +} + +private val SENTINEL_NULL = java.lang.Object() +fun <T, R> ((T) -> R).memoize(maxCacheSize: Int): (T) -> R { + val map = mutableMapWithMaxSize<T, Any>(maxCacheSize) + return { + val value = map.computeIfAbsent(it) { innerValue -> + this(innerValue) ?: SENTINEL_NULL + } + if (value == SENTINEL_NULL) null as R + else value as R + } +} diff --git a/src/main/kotlin/moe/nea/firmament/util/item/NbtItemData.kt b/src/main/kotlin/moe/nea/firmament/util/item/NbtItemData.kt index c366dc2..ef22325 100644 --- a/src/main/kotlin/moe/nea/firmament/util/item/NbtItemData.kt +++ b/src/main/kotlin/moe/nea/firmament/util/item/NbtItemData.kt @@ -21,6 +21,6 @@ var ItemStack.loreAccordingToNbt val ItemStack.displayNameAccordingToNbt get() = get(DataComponentTypes.CUSTOM_NAME) ?: get(DataComponentTypes.ITEM_NAME) ?: item.name -fun ItemStack.setCustomName(literal: Text) { - set(DataComponentTypes.CUSTOM_NAME, literal) +fun ItemStack.setCustomName(text: Text) { + set(DataComponentTypes.CUSTOM_NAME, text) } diff --git a/src/main/kotlin/moe/nea/firmament/util/listutil.kt b/src/main/kotlin/moe/nea/firmament/util/listutil.kt new file mode 100644 index 0000000..b72457f --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/util/listutil.kt @@ -0,0 +1,14 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.util + +fun <T, R> List<T>.lastNotNullOfOrNull(func: (T) -> R?): R? { + for (i in indices.reversed()) { + return func(this[i]) ?: continue + } + return null +} |