Make REI optional

This commit is contained in:
Linnea Gräf
2024-10-28 12:07:55 +01:00
parent 8ab4408854
commit c38dcee2c5
28 changed files with 451 additions and 374 deletions

View File

@@ -0,0 +1,24 @@
package moe.nea.firmament.repo
import io.github.moulberry.repo.data.Rarity
import moe.nea.firmament.util.HypixelPetInfo
// TODO: add in extra data like pet info, into this structure
data class PetData(
val rarity: Rarity,
val petId: String,
val exp: Double,
val isStub: Boolean = false,
) {
companion object {
fun fromHypixel(petInfo: HypixelPetInfo) = PetData(
petInfo.tier, petInfo.type, petInfo.exp,
)
fun forLevel(petId: String, rarity: Rarity, level: Int) = PetData(
rarity, petId, ExpLadders.getExpLadder(petId, rarity).getPetExpForLevel(level).toDouble()
)
}
val levelData by lazy { ExpLadders.getExpLadder(petId, rarity).getPetLevel(exp) }
}

View File

@@ -14,7 +14,6 @@ import moe.nea.firmament.Firmament
import moe.nea.firmament.Firmament.logger
import moe.nea.firmament.events.ReloadRegistrationEvent
import moe.nea.firmament.gui.config.ManagedConfig
import moe.nea.firmament.rei.PetData
import moe.nea.firmament.util.MinecraftDispatcher
import moe.nea.firmament.util.SkyblockId
@@ -139,9 +138,9 @@ object RepoManager {
return null
}
val intIndex = rarityIndex.toInt()
if (intIndex !in Rarity.values().indices) return null
if (intIndex !in Rarity.entries.indices) return null
if (petId !in neuRepo.constants.petNumbers) return null
return PetData(Rarity.values()[intIndex], petId, 0.0, true)
return PetData(Rarity.entries[intIndex], petId, 0.0, true)
}
}

View File

@@ -0,0 +1,165 @@
package moe.nea.firmament.repo
import io.github.moulberry.repo.constants.PetNumbers
import io.github.moulberry.repo.data.NEUIngredient
import io.github.moulberry.repo.data.NEUItem
import net.minecraft.item.ItemStack
import net.minecraft.text.Text
import net.minecraft.util.Formatting
import moe.nea.firmament.repo.ItemCache.asItemStack
import moe.nea.firmament.util.FirmFormatters
import moe.nea.firmament.util.LegacyFormattingCode
import moe.nea.firmament.util.SkyblockId
import moe.nea.firmament.util.mc.appendLore
import moe.nea.firmament.util.mc.displayNameAccordingToNbt
import moe.nea.firmament.util.petData
import moe.nea.firmament.util.skyBlockId
import moe.nea.firmament.util.skyblockId
import moe.nea.firmament.util.withColor
data class SBItemStack constructor(
val skyblockId: SkyblockId,
val neuItem: NEUItem?,
private var stackSize: Int,
private var petData: PetData?,
val extraLore: List<Text> = emptyList(),
// TODO: grab this star data from nbt if possible
val stars: Int = 0,
) {
fun getStackSize() = stackSize
fun setStackSize(newSize: Int) {
this.stackSize = newSize
this.itemStack_ = null
}
fun getPetData() = petData
fun setPetData(petData: PetData?) {
this.petData = petData
this.itemStack_ = null
}
companion object {
operator fun invoke(itemStack: ItemStack): SBItemStack {
val skyblockId = itemStack.skyBlockId ?: SkyblockId.NULL
return SBItemStack(
skyblockId,
RepoManager.getNEUItem(skyblockId),
itemStack.count,
petData = itemStack.petData?.let { PetData.fromHypixel(it) }
)
}
operator fun invoke(neuIngredient: NEUIngredient): SBItemStack? {
if (neuIngredient.skyblockId == SkyblockId.SENTINEL_EMPTY) return null // TODO: better fallback, maybe?
if (neuIngredient.skyblockId == SkyblockId.COINS) {
// TODO: specially handle coins to include the decimals
}
return SBItemStack(neuIngredient.skyblockId, neuIngredient.amount.toInt())
}
}
constructor(skyblockId: SkyblockId, petData: PetData) : this(
skyblockId,
RepoManager.getNEUItem(skyblockId),
1,
petData
)
constructor(skyblockId: SkyblockId, stackSize: Int = 1) : this(
skyblockId,
RepoManager.getNEUItem(skyblockId),
stackSize,
RepoManager.getPotentialStubPetData(skyblockId)
)
private fun injectReplacementDataForPetLevel(
petInfo: PetNumbers,
level: Int,
replacementData: MutableMap<String, String>
) {
val stats = petInfo.interpolatedStatsAtLevel(level) ?: return
stats.otherNumbers.forEachIndexed { index, it ->
replacementData[index.toString()] = FirmFormatters.formatCommas(it, 1)
}
stats.statNumbers.forEach { (t, u) ->
replacementData[t] = FirmFormatters.formatCommas(u, 1)
}
}
private fun injectReplacementDataForPets(replacementData: MutableMap<String, String>) {
val petData = this.petData ?: return
val petInfo = RepoManager.neuRepo.constants.petNumbers[petData.petId]?.get(petData.rarity) ?: return
if (petData.isStub) {
val mapLow = mutableMapOf<String, String>()
injectReplacementDataForPetLevel(petInfo, petInfo.lowLevel, mapLow)
val mapHigh = mutableMapOf<String, String>()
injectReplacementDataForPetLevel(petInfo, petInfo.highLevel, mapHigh)
mapHigh.forEach { (key, highValue) ->
mapLow.merge(key, highValue) { a, b -> "$a$b" }
}
replacementData.putAll(mapLow)
replacementData["LVL"] = "${petInfo.lowLevel}${petInfo.highLevel}"
} else {
injectReplacementDataForPetLevel(petInfo, petData.levelData.currentLevel, replacementData)
replacementData["LVL"] = petData.levelData.currentLevel.toString()
}
}
private var itemStack_: ItemStack? = null
private val itemStack: ItemStack
get() {
val itemStack = itemStack_ ?: run {
if (skyblockId == SkyblockId.COINS)
return@run ItemCache.coinItem(stackSize).also { it.appendLore(extraLore) }
val replacementData = mutableMapOf<String, String>()
injectReplacementDataForPets(replacementData)
return@run neuItem.asItemStack(idHint = skyblockId, replacementData)
.copyWithCount(stackSize)
.also { it.appendLore(extraLore) }
.also { enhanceStatsByStars(it, stars) }
}
if (itemStack_ == null)
itemStack_ = itemStack
return itemStack
}
private fun starString(stars: Int): Text {
if (stars <= 0) return Text.empty()
val tiers = listOf(
LegacyFormattingCode.GOLD,
LegacyFormattingCode.LIGHT_PURPLE,
LegacyFormattingCode.AQUA,
)
val maxStars = 5
if (stars > tiers.size * maxStars) return Text.literal(" ${stars}").withColor(Formatting.RED)
val starBaseTier = (stars - 1) / maxStars
val starBaseColor = tiers[starBaseTier]
val starsInCurrentTier = stars - starBaseTier * maxStars
val starString = Text.literal(" " + "".repeat(starsInCurrentTier)).withColor(starBaseColor.modern)
if (starBaseTier > 0) {
val starLastTier = tiers[starBaseTier - 1]
val starsInLastTier = 5 - starsInCurrentTier
starString.append(Text.literal("".repeat(starsInLastTier)).withColor(starLastTier.modern))
}
return starString
}
private fun enhanceStatsByStars(itemStack: ItemStack, stars: Int) {
if (stars == 0) return
// TODO: increase stats and add the star level into the nbt data so star displays work
itemStack.displayNameAccordingToNbt = itemStack.displayNameAccordingToNbt.copy()
.append(starString(stars))
}
fun asImmutableItemStack(): ItemStack {
return itemStack
}
fun asCopiedItemStack(): ItemStack {
return itemStack.copy()
}
}