fix: Pets missing an item rarity

This commit is contained in:
Linnea Gräf
2024-11-17 16:11:21 +01:00
parent 762fb40215
commit ffebb438f8
7 changed files with 144 additions and 65 deletions

View File

@@ -101,7 +101,7 @@ data class HypixelPetInfo(
val uuid: UUID? = null,
val active: Boolean = false,
) {
val skyblockId get() = SkyblockId("${type.uppercase()};${tier.ordinal}")
val skyblockId get() = SkyblockId("${type.uppercase()};${tier.ordinal}") // TODO: is this ordinal set up correctly?
}
private val jsonparser = Json { ignoreUnknownKeys = true }
@@ -125,8 +125,8 @@ val ItemStack.skyblockUUID: UUID?
private val petDataCache = WeakCache.memoize<ItemStack, Optional<HypixelPetInfo>>("PetInfo") {
val jsonString = it.extraAttributes.getString("petInfo")
if (jsonString.isNullOrBlank()) return@memoize Optional.empty()
runCatching { jsonparser.decodeFromString<HypixelPetInfo>(jsonString) }
.getOrElse { null }.intoOptional()
ErrorUtil.catch<HypixelPetInfo?>("Could not decode hypixel pet info") { jsonparser.decodeFromString<HypixelPetInfo>(jsonString) }
.or { null }.intoOptional()
}
val ItemStack.petData: HypixelPetInfo?

View File

@@ -0,0 +1,9 @@
package moe.nea.firmament.util
object StringUtil {
fun String.words(): Sequence<String> {
return splitToSequence(" ") // TODO: better boundaries
}
fun Iterable<String>.unwords() = joinToString(" ")
}

View File

@@ -1,6 +1,6 @@
package moe.nea.firmament.util
package moe.nea.firmament.util.accessors
import me.shedaniel.math.Rectangle
import moe.nea.firmament.mixins.accessor.AccessorHandledScreen

View File

@@ -0,0 +1,21 @@
package moe.nea.firmament.util.skyblock
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
class ItemType(val name: String) {
companion object {
private val generated = object : ReadOnlyProperty<Any?, ItemType> {
override fun getValue(thisRef: Any?, property: KProperty<*>): ItemType {
return ItemType.ofName(property.name)
}
}
fun ofName(name: String): ItemType {
return ItemType(name)
}
val SWORD by generated
}
}

View File

@@ -0,0 +1,61 @@
package moe.nea.firmament.util.skyblock
import net.minecraft.item.ItemStack
import net.minecraft.text.Text
import moe.nea.firmament.util.StringUtil.words
import moe.nea.firmament.util.collections.lastNotNullOfOrNull
import moe.nea.firmament.util.mc.loreAccordingToNbt
import moe.nea.firmament.util.petData
import moe.nea.firmament.util.unformattedString
typealias RepoRarity = io.github.moulberry.repo.data.Rarity
enum class Rarity(vararg altNames: String) {
COMMON,
UNCOMMON,
RARE,
EPIC,
LEGENDARY("LEGENJERRY"),
MYTHIC,
DIVINE,
SUPREME,
SPECIAL,
VERY_SPECIAL,
UNKNOWN
;
val names = setOf(name) + altNames
val neuRepoRarity: RepoRarity? = RepoRarity.entries.find { it.name == name }
companion object {
val byName = entries.flatMap { en -> en.names.map { it to en } }.toMap()
val fromNeuRepo = entries.associateBy { it.neuRepoRarity }
fun fromNeuRepo(repo: RepoRarity): Rarity? {
return fromNeuRepo[repo]
}
fun fromString(name: String): Rarity? {
return byName[name]
}
fun fromTier(tier: Int): Rarity? {
return entries.getOrNull(tier)
}
fun fromItem(itemStack: ItemStack): Rarity? {
return fromLore(itemStack.loreAccordingToNbt) ?: fromPetItem(itemStack)
}
fun fromPetItem(itemStack: ItemStack): Rarity? =
itemStack.petData?.tier?.let(::fromNeuRepo)
fun fromLore(lore: List<Text>): Rarity? =
lore.lastNotNullOfOrNull {
it.unformattedString.words()
.firstNotNullOfOrNull(::fromString)
}
}
}