aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/repo/item/SBItemData.kt
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2025-03-11 12:44:59 +0100
committerLinnea Gräf <nea@nea.moe>2025-03-11 12:44:59 +0100
commit17a855a4bdfcca00b29f85981d8fb86968d9038c (patch)
tree4ecde192607e1b6fea54fc264a6b79a197ae8619 /src/main/kotlin/repo/item/SBItemData.kt
parent0765c8aee181dda89b08f89c4a7deb301f00f656 (diff)
downloadFirmament-17a855a4bdfcca00b29f85981d8fb86968d9038c.tar.gz
Firmament-17a855a4bdfcca00b29f85981d8fb86968d9038c.tar.bz2
Firmament-17a855a4bdfcca00b29f85981d8fb86968d9038c.zip
feat: add SBItemData implementation
Diffstat (limited to 'src/main/kotlin/repo/item/SBItemData.kt')
-rw-r--r--src/main/kotlin/repo/item/SBItemData.kt122
1 files changed, 122 insertions, 0 deletions
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)
+ }
+}