From a39f6e9bec3318b2be251570ea7aea29d58b49cb Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Sat, 18 May 2024 14:57:07 +0200 Subject: Fix item rarity not displaying in npc shops --- .../features/inventory/ItemRarityCosmetics.kt | 17 ++++++++++++----- .../moe/nea/firmament/util/IdentityCharacteristics.kt | 5 ++++- .../moe/nea/firmament/util/MutableMapWithMaxSize.kt | 19 +++++++++++++++++++ .../kotlin/moe/nea/firmament/util/item/NbtItemData.kt | 4 ++-- src/main/kotlin/moe/nea/firmament/util/listutil.kt | 14 ++++++++++++++ 5 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 src/main/kotlin/moe/nea/firmament/util/listutil.kt 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? - get() { - val entry = loreAccordingToNbt.lastOrNull()?.unformattedString ?: "" - return rarityToColor.entries.find { (k, v) -> k in entry }?.value + + private fun getSkyblockRarity0(itemStack: ItemStack): Triple? { + 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 + * SPDX-FileCopyrightText: 2024 Linnea Gräf * * SPDX-License-Identifier: GPL-3.0-or-later */ @@ -8,7 +9,9 @@ package moe.nea.firmament.util class IdentityCharacteristics(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 mutableMapWithMaxSize(maxSize: Int): MutableMap = object : Link return size > maxSize } } + +fun ((T) -> R).memoizeIdentity(maxCacheSize: Int): (T) -> R { + val memoized = { it: IdentityCharacteristics -> + this(it.value) + }.memoize(maxCacheSize) + return { memoized(IdentityCharacteristics(it)) } +} + +private val SENTINEL_NULL = java.lang.Object() +fun ((T) -> R).memoize(maxCacheSize: Int): (T) -> R { + val map = mutableMapWithMaxSize(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 + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.util + +fun List.lastNotNullOfOrNull(func: (T) -> R?): R? { + for (i in indices.reversed()) { + return func(this[i]) ?: continue + } + return null +} -- cgit