diff options
author | Linnea Gräf <nea@nea.moe> | 2025-03-11 12:44:59 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2025-03-11 12:44:59 +0100 |
commit | 17a855a4bdfcca00b29f85981d8fb86968d9038c (patch) | |
tree | 4ecde192607e1b6fea54fc264a6b79a197ae8619 /src/main/kotlin/repo | |
parent | 0765c8aee181dda89b08f89c4a7deb301f00f656 (diff) | |
download | Firmament-17a855a4bdfcca00b29f85981d8fb86968d9038c.tar.gz Firmament-17a855a4bdfcca00b29f85981d8fb86968d9038c.tar.bz2 Firmament-17a855a4bdfcca00b29f85981d8fb86968d9038c.zip |
feat: add SBItemData implementation
Diffstat (limited to 'src/main/kotlin/repo')
-rw-r--r-- | src/main/kotlin/repo/item/SBBreakingPower.kt | 31 | ||||
-rw-r--r-- | src/main/kotlin/repo/item/SBItemData.kt | 122 | ||||
-rw-r--r-- | src/main/kotlin/repo/item/SBItemId.kt | 24 | ||||
-rw-r--r-- | src/main/kotlin/repo/item/SBItemProperty.kt | 59 | ||||
-rw-r--r-- | src/main/kotlin/repo/item/SBRarity.kt | 20 | ||||
-rw-r--r-- | src/main/kotlin/repo/item/SBStackSize.kt | 24 | ||||
-rw-r--r-- | src/main/kotlin/repo/item/SBStars.kt | 21 |
7 files changed, 301 insertions, 0 deletions
diff --git a/src/main/kotlin/repo/item/SBBreakingPower.kt b/src/main/kotlin/repo/item/SBBreakingPower.kt new file mode 100644 index 0000000..4c88a48 --- /dev/null +++ b/src/main/kotlin/repo/item/SBBreakingPower.kt @@ -0,0 +1,31 @@ +package moe.nea.firmament.repo.item + +import com.google.auto.service.AutoService +import io.github.moulberry.repo.data.NEUItem +import net.minecraft.item.ItemStack +import moe.nea.firmament.util.mc.loreAccordingToNbt +import moe.nea.firmament.util.removeColorCodes +import moe.nea.firmament.util.unformattedString +import moe.nea.firmament.util.useMatch + +@AutoService(SBItemProperty::class) +object SBBreakingPower : SBItemProperty<Int>() { + private val BREAKING_POWER_REGEX = "Breaking Power (?<power>[0-9]+)".toPattern() + + fun fromLore(string: String?): Int? { + return BREAKING_POWER_REGEX.useMatch(string) { + group("power").toInt() + } + } + + override fun fromNeuItem(neuItem: NEUItem, store: SBItemData): Int? { + return fromLore(neuItem.lore.firstOrNull()?.removeColorCodes()) + } + + override fun fromStack( + stack: ItemStack, + store: SBItemData + ): Int? { + return fromLore(stack.loreAccordingToNbt.firstOrNull()?.unformattedString) + } +} diff --git a/src/main/kotlin/repo/item/SBItemData.kt b/src/main/kotlin/repo/item/SBItemData.kt new file mode 100644 index 0000000..7a6b081 --- /dev/null +++ b/src/main/kotlin/repo/item/SBItemData.kt @@ -0,0 +1,122 @@ +package moe.nea.firmament.repo.item + +import kotlin.collections.runningFold +import net.minecraft.item.ItemStack +import moe.nea.firmament.repo.RepoManager + +class SBItemData { + private var itemCache: ItemStack? = null + + private val data: MutableMap<SBItemProperty<*>, Any> = mutableMapOf() + + fun <T> getData(prop: SBItemProperty<T>): T? { + return data[prop] as T? + } + + fun <Prop : SBItemProperty<T>, T> set(property: Prop, data: T) { + if (data != null) { + (this.data as MutableMap<Any, Any>)[property] = data + } else { + this.data.remove(property) + } + itemCache = null + } + + private fun <T> enhanceStack(stack: ItemStack, property: SBItemProperty.State<T>): ItemStack { + val data = getData(property) + return property.applyToStack(stack, this, data) + } + + private fun createStack(): ItemStack { + return SBItemProperty.allStates.fold(ItemStack.EMPTY) { stack, prop -> + enhanceStack(stack, prop) + } + } + + fun debugStackCreation(): List<PartialStack<*>> { + fun <T> combinedEnhanceStack(previous: PartialStack<*>, mod: SBItemProperty.State<T>): PartialStack<T> { + val nextStack = enhanceStack(previous.stack.copy(), mod) + return PartialStack(mod, getData(mod), nextStack) + } + return SBItemProperty.allStates + .runningFold( + PartialStack<Nothing>(null, null, ItemStack.EMPTY)) { stack: PartialStack<*>, prop -> + combinedEnhanceStack(stack, prop) + } + } + + /** + * Creates an [ItemStack] based on the current properties. The returned item stack must not be modified by the + * caller. + */ + fun toImmutableStack(): ItemStack { + var cached = itemCache + if (cached == null) { + cached = createStack() + itemCache = cached + } + return cached + } + + data class PartialStack<T>( + val lastAppliedModifier: SBItemProperty<T>?, + val data: T?, + val stack: ItemStack, + ) + + companion object { + /** + * Create an [SBItemData] from only the given characteristica. Any unspecified characteristica will be non-existent. + * If you want to compute all other properties based on the given properties, use [roundtrip]. + */ + fun fromCharacteristica( + vararg char: SBItemProperty.BoundState<*> + ): SBItemData { + val store = SBItemData() + char.forEach { + it.applyTo(store) + } + return store + } + + fun fromStack(itemStack: ItemStack): SBItemData { + val store = SBItemData() + store.loadFrom(itemStack) + return store + } + } + + /** + * Creates a new [SBItemData] from the item stack this [SBItemData] produces. This will initialize all properties. + */ + fun roundtrip(): SBItemData { + return fromStack(toImmutableStack()) + } + + /** + * Creates a new [SBItemData] with cheap inferences completed only by using data available in [io.github.moulberry.repo.data.NEUItem]. This is a cheaper version of [roundtrip], that does not create any [ItemStack]s, and preserves all properties already provided. Check if the property you need overrides [SBItemProperty.fromNeuItem]. + */ + fun cheapInfer(): SBItemData { + val neuItem = getData(SBItemId)?.let { RepoManager.getNEUItem(it) } ?: return this + val store = SBItemData() + SBItemProperty.allProperties.forEach { + it.fromNeuItem(neuItem, this) + } + store.data.putAll(this.data) + return store + } + + private fun loadFrom(stack: ItemStack) { + SBItemProperty.allProperties.forEach { + loadModifier(stack, it) + } + } + + private fun <T> loadModifier( + stack: ItemStack, + modifier: SBItemProperty<T> + ) { + val data = modifier.fromStack(stack, this) ?: return + set(modifier, data) + } +} diff --git a/src/main/kotlin/repo/item/SBItemId.kt b/src/main/kotlin/repo/item/SBItemId.kt new file mode 100644 index 0000000..fcafff0 --- /dev/null +++ b/src/main/kotlin/repo/item/SBItemId.kt @@ -0,0 +1,24 @@ +package moe.nea.firmament.repo.item + +import com.google.auto.service.AutoService +import net.minecraft.item.ItemStack +import moe.nea.firmament.repo.ItemCache.asItemStack +import moe.nea.firmament.repo.RepoManager +import moe.nea.firmament.util.SkyblockId +import moe.nea.firmament.util.skyBlockId + +@AutoService(SBItemProperty::class) +object SBItemId : SBItemProperty.State<SkyblockId>() { + + override fun fromStack(stack: ItemStack, store: SBItemData): SkyblockId? { + return stack.skyBlockId + } + + override fun applyToStack(stack: ItemStack, store: SBItemData, value: SkyblockId?): ItemStack { + val id = value ?: SkyblockId.NULL + return RepoManager.getNEUItem(id).asItemStack(idHint = id) + } + + override val order: Int + get() = -10000 +} diff --git a/src/main/kotlin/repo/item/SBItemProperty.kt b/src/main/kotlin/repo/item/SBItemProperty.kt new file mode 100644 index 0000000..55b8f01 --- /dev/null +++ b/src/main/kotlin/repo/item/SBItemProperty.kt @@ -0,0 +1,59 @@ +package moe.nea.firmament.repo.item + +import io.github.moulberry.repo.data.NEUItem +import net.minecraft.item.ItemStack +import moe.nea.firmament.repo.ItemCache.asItemStack +import moe.nea.firmament.util.compatloader.CompatLoader + +/** + * A property of a skyblock item. Not every skyblock item must have this property, but some should. + * + * Access to this class should be limited to [State.bindWith] and [SBItemData.getData]. + * @see State + */ +abstract class SBItemProperty<T> { + data class BoundState<T>(val property: State<T>, val data: T) { + fun applyTo(store: SBItemData) { + store.set(property, data) + } + } + + // TODO: Actually implement and make use of this method. + /** + * Extract this property's state from a [NEUItem]. If this method returns something, it may be equivalent to [fromStack] with the neu item resolved to an item stack according to [asItemStack], but *should not* instantiate an item stack. This method may return null to indicate that it needs a fully constructed item stack to extract a property. This method return one value and then later return another value from [fromStack], but behaviour is generally discouraged. + */ + open fun fromNeuItem(neuItem: NEUItem, store: SBItemData): T? { + return null + } + + /** + * Extract this property's state from an [ItemStack]. This should be fully reversible (i.e. all info used to in [fromStack] needs to be set by [State.applyToStack]. + */ + abstract fun fromStack(stack: ItemStack, store: SBItemData): T? + + /** + * A property of a skyblock item that carriers state. Unlike a plain [SBItemProperty] these modifiers can be used + * to change the state of an item, including its rendering as a vanilla [ItemStack]. + */ + abstract class State<T> : SBItemProperty<T>() { + abstract fun applyToStack(stack: ItemStack, store: SBItemData, value: T?): ItemStack + fun bindWith(data: T) = BoundState(this, data) + } + + /** + * The order of this property relative to other properties. Lower values get computed first, so higher values may + * rely on their data being stored already (if that item stack has any of that data), and they can overwrite the + * rendering of the lower states. + */ + open val order: Int get() = 0 + + companion object { + val loader = CompatLoader<SBItemProperty<*>>(SBItemProperty::class) + val allProperties by lazy { + loader.allValidInstances.sortedBy { it.order } + } + val allStates by lazy { + allProperties.filterIsInstance<State<*>>() + } + } +} diff --git a/src/main/kotlin/repo/item/SBRarity.kt b/src/main/kotlin/repo/item/SBRarity.kt new file mode 100644 index 0000000..d2ddd0b --- /dev/null +++ b/src/main/kotlin/repo/item/SBRarity.kt @@ -0,0 +1,20 @@ +package moe.nea.firmament.repo.item + +import com.google.auto.service.AutoService +import io.github.moulberry.repo.data.NEUItem +import net.minecraft.item.ItemStack +import moe.nea.firmament.util.skyblock.Rarity + +@AutoService(SBItemProperty::class) +object SBRarity : SBItemProperty<Rarity>() { + override fun fromStack( + stack: ItemStack, + store: SBItemData + ): Rarity? { + return Rarity.fromItem(stack) + } + + override fun fromNeuItem(neuItem: NEUItem, store: SBItemData): Rarity? { + return Rarity.fromStringLore(neuItem.lore) + } +} diff --git a/src/main/kotlin/repo/item/SBStackSize.kt b/src/main/kotlin/repo/item/SBStackSize.kt new file mode 100644 index 0000000..a31facd --- /dev/null +++ b/src/main/kotlin/repo/item/SBStackSize.kt @@ -0,0 +1,24 @@ +package moe.nea.firmament.repo.item + +import com.google.auto.service.AutoService +import net.minecraft.item.ItemStack + +@AutoService(SBItemProperty::class) +object SBStackSize : SBItemProperty.State<Int>() { + override fun fromStack( + stack: ItemStack, + store: SBItemData + ): Int? { + return stack.count + } + + override fun applyToStack( + stack: ItemStack, + store: SBItemData, + value: Int? + ): ItemStack { + if (value != null) + stack.count = value + return stack + } +} diff --git a/src/main/kotlin/repo/item/SBStars.kt b/src/main/kotlin/repo/item/SBStars.kt new file mode 100644 index 0000000..1fa930f --- /dev/null +++ b/src/main/kotlin/repo/item/SBStars.kt @@ -0,0 +1,21 @@ +package moe.nea.firmament.repo.item + +import net.minecraft.item.ItemStack +import moe.nea.firmament.util.getUpgradeStars + +object SBStars : SBItemProperty.State<Int>() { + override fun applyToStack( + stack: ItemStack, + store: SBItemData, + value: Int? + ): ItemStack { + TODO() + } + + override fun fromStack( + stack: ItemStack, + store: SBItemData + ): Int? { + return stack.getUpgradeStars() + } +} |