Fix item rarity not displaying in npc shops
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
14
src/main/kotlin/moe/nea/firmament/util/listutil.kt
Normal file
14
src/main/kotlin/moe/nea/firmament/util/listutil.kt
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user