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 /src/main/kotlin/moe/nea/firmament/util | |
parent | ad09808c25c769be760559e1a597e29e488827bf (diff) | |
download | firmament-a39f6e9bec3318b2be251570ea7aea29d58b49cb.tar.gz firmament-a39f6e9bec3318b2be251570ea7aea29d58b49cb.tar.bz2 firmament-a39f6e9bec3318b2be251570ea7aea29d58b49cb.zip |
Fix item rarity not displaying in npc shops
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/util')
4 files changed, 39 insertions, 3 deletions
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 +} |