From ffebb438f8d1582ee65936399b79dd714454f223 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Sun, 17 Nov 2024 16:11:21 +0100 Subject: fix: Pets missing an item rarity --- src/main/kotlin/util/GetRectangle.kt | 17 ------- src/main/kotlin/util/SkyblockId.kt | 6 +-- src/main/kotlin/util/StringUtil.kt | 9 ++++ src/main/kotlin/util/accessors/GetRectangle.kt | 17 +++++++ src/main/kotlin/util/skyblock/ItemType.kt | 21 +++++++++ src/main/kotlin/util/skyblock/Rarity.kt | 61 ++++++++++++++++++++++++++ 6 files changed, 111 insertions(+), 20 deletions(-) delete mode 100644 src/main/kotlin/util/GetRectangle.kt create mode 100644 src/main/kotlin/util/StringUtil.kt create mode 100644 src/main/kotlin/util/accessors/GetRectangle.kt create mode 100644 src/main/kotlin/util/skyblock/ItemType.kt create mode 100644 src/main/kotlin/util/skyblock/Rarity.kt (limited to 'src/main/kotlin/util') diff --git a/src/main/kotlin/util/GetRectangle.kt b/src/main/kotlin/util/GetRectangle.kt deleted file mode 100644 index ec64f31..0000000 --- a/src/main/kotlin/util/GetRectangle.kt +++ /dev/null @@ -1,17 +0,0 @@ - - -package moe.nea.firmament.util - -import me.shedaniel.math.Rectangle -import moe.nea.firmament.mixins.accessor.AccessorHandledScreen -import net.minecraft.client.gui.screen.ingame.HandledScreen - -fun HandledScreen<*>.getRectangle(): Rectangle { - this as AccessorHandledScreen - return Rectangle( - getX_Firmament(), - getY_Firmament(), - getBackgroundWidth_Firmament(), - getBackgroundHeight_Firmament() - ) -} diff --git a/src/main/kotlin/util/SkyblockId.kt b/src/main/kotlin/util/SkyblockId.kt index eeca70c..1c1aa77 100644 --- a/src/main/kotlin/util/SkyblockId.kt +++ b/src/main/kotlin/util/SkyblockId.kt @@ -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>("PetInfo") { val jsonString = it.extraAttributes.getString("petInfo") if (jsonString.isNullOrBlank()) return@memoize Optional.empty() - runCatching { jsonparser.decodeFromString(jsonString) } - .getOrElse { null }.intoOptional() + ErrorUtil.catch("Could not decode hypixel pet info") { jsonparser.decodeFromString(jsonString) } + .or { null }.intoOptional() } val ItemStack.petData: HypixelPetInfo? diff --git a/src/main/kotlin/util/StringUtil.kt b/src/main/kotlin/util/StringUtil.kt new file mode 100644 index 0000000..d9253cb --- /dev/null +++ b/src/main/kotlin/util/StringUtil.kt @@ -0,0 +1,9 @@ +package moe.nea.firmament.util + +object StringUtil { + fun String.words(): Sequence { + return splitToSequence(" ") // TODO: better boundaries + } + + fun Iterable.unwords() = joinToString(" ") +} diff --git a/src/main/kotlin/util/accessors/GetRectangle.kt b/src/main/kotlin/util/accessors/GetRectangle.kt new file mode 100644 index 0000000..37acfd9 --- /dev/null +++ b/src/main/kotlin/util/accessors/GetRectangle.kt @@ -0,0 +1,17 @@ + + +package moe.nea.firmament.util.accessors + +import me.shedaniel.math.Rectangle +import moe.nea.firmament.mixins.accessor.AccessorHandledScreen +import net.minecraft.client.gui.screen.ingame.HandledScreen + +fun HandledScreen<*>.getRectangle(): Rectangle { + this as AccessorHandledScreen + return Rectangle( + getX_Firmament(), + getY_Firmament(), + getBackgroundWidth_Firmament(), + getBackgroundHeight_Firmament() + ) +} diff --git a/src/main/kotlin/util/skyblock/ItemType.kt b/src/main/kotlin/util/skyblock/ItemType.kt new file mode 100644 index 0000000..7d53c0d --- /dev/null +++ b/src/main/kotlin/util/skyblock/ItemType.kt @@ -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 { + 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 + } +} diff --git a/src/main/kotlin/util/skyblock/Rarity.kt b/src/main/kotlin/util/skyblock/Rarity.kt new file mode 100644 index 0000000..f26cefe --- /dev/null +++ b/src/main/kotlin/util/skyblock/Rarity.kt @@ -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): Rarity? = + lore.lastNotNullOfOrNull { + it.unformattedString.words() + .firstNotNullOfOrNull(::fromString) + } + + } +} -- cgit