This commit is contained in:
DBotThePony 2025-03-01 13:26:34 +07:00
parent 1b5476de40
commit 4292713874
Signed by: DBot
GPG Key ID: DCC23B5715498507
3 changed files with 8 additions and 30 deletions

View File

@ -38,7 +38,6 @@ import ru.dbotthepony.mc.otm.core.math.component2
import ru.dbotthepony.mc.otm.core.math.component3
import ru.dbotthepony.mc.otm.core.set
import ru.dbotthepony.mc.otm.core.util.countingLazy
import ru.dbotthepony.mc.otm.data.codec.KOptionalCodec
import ru.dbotthepony.mc.otm.data.codec.minRange
import ru.dbotthepony.mc.otm.menu.decorative.GrillMenu
import ru.dbotthepony.mc.otm.registry.game.MBlockEntities
@ -54,7 +53,7 @@ class GrillBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryBloc
private fun clearSlot() {
inputProgress[slot] = 0
outputs[slot] = null
outputsRecipeNames[slot] = KOptional.empty()
outputsRecipeNames[slot] = null
activeSlots.rem(slot)
}
@ -79,10 +78,10 @@ class GrillBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryBloc
if (result == null) {
clearSlot()
} else {
if (outputsRecipeNames[slot].orNull() != result.id)
if (outputsRecipeNames[slot] != result.id)
inputProgress[slot] = 0
outputsRecipeNames[slot] = KOptional.of(result.id)
outputsRecipeNames[slot] = result.id
outputs[slot] = result.value
activeSlots.add(slot)
}
@ -101,7 +100,7 @@ class GrillBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryBloc
return customDisplayName ?: level?.getBlockState(blockPos)?.block?.name ?: TextComponent("null at $blockPos")
}
private val outputsRecipeNames = Array<KOptional<ResourceLocation>>(SLOTS) { KOptional.empty() }
private val outputsRecipeNames = arrayOfNulls<ResourceLocation>(SLOTS)
private val outputs = arrayOfNulls<SmokingRecipe>(SLOTS)
private val activeSlots = IntArrayList()
private val inputProgress = IntArray(SLOTS)
@ -138,12 +137,12 @@ class GrillBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryBloc
savetables.stateful(::fuelSlot)
savetables.stateful(::experience)
outputsRecipeNames.fill(KOptional())
outputsRecipeNames.fill(null)
// TODO: could have been done as list (and get more space efficient storage), but we use an array, oh well
for (i in inputProgress.indices) {
savetables.codec(Delegate.Of({ inputProgress[i] }, { inputProgress[i] = it }), progressCodec, "inputProgress${i}")
savetables.codec(Delegate.Of({ outputsRecipeNames[i] }, { outputsRecipeNames[i] = it }), recipeNameCodec, "recipeName${i}")
savetables.codecNullable(Delegate.Of({ outputsRecipeNames[i] }, { outputsRecipeNames[i] = it }), ResourceLocation.CODEC, "recipeName${i}")
}
// addDroppableContainer(inputSlots)
@ -216,7 +215,6 @@ class GrillBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryBloc
const val SLOTS = 6
private val progressCodec = Codec.INT.minRange(0)
private val recipeNameCodec = KOptionalCodec(ResourceLocation.CODEC)
val FUEL_SLOT_PROVIDER = ContainerSlot.Simple(maxStackSize = 4, filter = AutomationFilters.CHEMICAL_FUEL)
}
}

View File

@ -7,6 +7,7 @@ import net.minecraft.core.HolderLookup.Provider
import net.minecraft.nbt.ByteTag
import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.DoubleTag
import net.minecraft.nbt.EndTag
import net.minecraft.nbt.FloatTag
import net.minecraft.nbt.IntTag
import net.minecraft.nbt.LongTag
@ -297,7 +298,7 @@ class Savetables : INBTSerializable<CompoundTag?> {
for (entry in entries) {
val value = entry.serializeNBT(registry)
if (value != null)
if (value != null && value !is EndTag)
nbt[entry.name] = value
}
}

View File

@ -1,21 +0,0 @@
package ru.dbotthepony.mc.otm.data.codec
import com.mojang.datafixers.util.Pair
import com.mojang.serialization.Codec
import com.mojang.serialization.DataResult
import com.mojang.serialization.DynamicOps
import ru.dbotthepony.kommons.util.KOptional
class KOptionalCodec<E : Any>(val parent: Codec<E>) : Codec<KOptional<E>> {
override fun <T> encode(input: KOptional<E>, ops: DynamicOps<T>, prefix: T): DataResult<T> {
return input.map { parent.encode(it, ops, prefix) }.orElse { DataResult.success(ops.empty()) }
}
override fun <T> decode(ops: DynamicOps<T>, input: T): DataResult<Pair<KOptional<E>, T>> {
if (input == ops.empty()) {
return DataResult.success(Pair(KOptional(), ops.empty()))
} else {
return parent.decode(ops, input).map { Pair(KOptional(it.first), it.second) }
}
}
}