Fix item rarity not displaying in npc shops

This commit is contained in:
Linnea Gräf
2024-05-18 14:57:07 +02:00
parent ad09808c25
commit a39f6e9bec
5 changed files with 51 additions and 8 deletions

View File

@@ -19,6 +19,9 @@ import moe.nea.firmament.features.FirmamentFeature
import moe.nea.firmament.gui.config.ManagedConfig import moe.nea.firmament.gui.config.ManagedConfig
import moe.nea.firmament.util.MC import moe.nea.firmament.util.MC
import moe.nea.firmament.util.item.loreAccordingToNbt 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 import moe.nea.firmament.util.unformattedString
object ItemRarityCosmetics : FirmamentFeature { object ItemRarityCosmetics : FirmamentFeature {
@@ -48,15 +51,19 @@ object ItemRarityCosmetics : FirmamentFeature {
val c = Color(it.value.colorValue!!) val c = Color(it.value.colorValue!!)
Triple(c.red / 255F, c.green / 255F, c.blue / 255F) Triple(c.red / 255F, c.green / 255F, c.blue / 255F)
} }
private val ItemStack.skyblockLoreRarityColor: Triple<Float, Float, Float>?
get() { private fun getSkyblockRarity0(itemStack: ItemStack): Triple<Float, Float, Float>? {
val entry = loreAccordingToNbt.lastOrNull()?.unformattedString ?: "" return itemStack.loreAccordingToNbt.lastNotNullOfOrNull {
return rarityToColor.entries.find { (k, v) -> k in entry }?.value 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) { 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( drawContext.drawSprite(
x, y, x, y,
0, 0,

View File

@@ -1,5 +1,6 @@
/* /*
* SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe> * 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 * SPDX-License-Identifier: GPL-3.0-or-later
*/ */
@@ -8,7 +9,9 @@ package moe.nea.firmament.util
class IdentityCharacteristics<T>(val value: T) { class IdentityCharacteristics<T>(val value: T) {
override fun equals(other: Any?): Boolean { 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 { override fun hashCode(): Int {

View File

@@ -11,3 +11,22 @@ fun <K, V> mutableMapWithMaxSize(maxSize: Int): MutableMap<K, V> = object : Link
return size > maxSize 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
}
}

View File

@@ -21,6 +21,6 @@ var ItemStack.loreAccordingToNbt
val ItemStack.displayNameAccordingToNbt val ItemStack.displayNameAccordingToNbt
get() = get(DataComponentTypes.CUSTOM_NAME) ?: get(DataComponentTypes.ITEM_NAME) ?: item.name get() = get(DataComponentTypes.CUSTOM_NAME) ?: get(DataComponentTypes.ITEM_NAME) ?: item.name
fun ItemStack.setCustomName(literal: Text) { fun ItemStack.setCustomName(text: Text) {
set(DataComponentTypes.CUSTOM_NAME, literal) set(DataComponentTypes.CUSTOM_NAME, text)
} }

View File

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