Fix Grill not properly saving cook progress when being deserialized from disk

This commit is contained in:
DBotThePony 2025-03-01 12:12:20 +07:00
parent 454783a99a
commit 2b2f78e528
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -6,6 +6,7 @@ import net.minecraft.core.BlockPos
import net.minecraft.core.HolderLookup
import net.minecraft.nbt.CompoundTag
import net.minecraft.network.chat.Component
import net.minecraft.resources.ResourceLocation
import net.minecraft.server.level.ServerLevel
import net.minecraft.world.Containers
import net.minecraft.world.MenuProvider
@ -19,6 +20,7 @@ import net.minecraft.world.item.crafting.SmokingRecipe
import net.minecraft.world.level.block.Block
import net.minecraft.world.level.block.state.BlockState
import ru.dbotthepony.kommons.util.Delegate
import ru.dbotthepony.kommons.util.KOptional
import ru.dbotthepony.mc.otm.block.IBlockWithCustomName
import ru.dbotthepony.mc.otm.block.decorative.GrillBlock
import ru.dbotthepony.mc.otm.block.entity.ExperienceStorage
@ -36,6 +38,7 @@ 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
@ -51,6 +54,7 @@ class GrillBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryBloc
private fun clearSlot() {
inputProgress[slot] = 0
outputs[slot] = null
outputsRecipeNames[slot] = KOptional.empty()
activeSlots.rem(slot)
}
@ -75,9 +79,10 @@ class GrillBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryBloc
if (result == null) {
clearSlot()
} else {
if (outputs[slot] != result.value)
if (outputsRecipeNames[slot].orNull() != result.id)
inputProgress[slot] = 0
outputsRecipeNames[slot] = KOptional.of(result.id)
outputs[slot] = result.value
activeSlots.add(slot)
}
@ -96,6 +101,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 outputs = arrayOfNulls<SmokingRecipe>(SLOTS)
private val activeSlots = IntArrayList()
private val inputProgress = IntArray(SLOTS)
@ -132,9 +138,12 @@ class GrillBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryBloc
savetables.stateful(::fuelSlot)
savetables.stateful(::experience)
outputsRecipeNames.fill(KOptional())
// 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}")
}
// addDroppableContainer(inputSlots)
@ -205,6 +214,7 @@ 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)
}
}