From 2b2f78e528ee61747397ffd9fe83c933600133ce Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sat, 1 Mar 2025 12:12:20 +0700 Subject: [PATCH] Fix Grill not properly saving cook progress when being deserialized from disk --- .../otm/block/entity/decorative/GrillBlockEntity.kt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/decorative/GrillBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/decorative/GrillBlockEntity.kt index bba8733a6..24d5f2f96 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/decorative/GrillBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/decorative/GrillBlockEntity.kt @@ -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>(SLOTS) { KOptional.empty() } private val outputs = arrayOfNulls(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) } }