From a5c1d7fbf87ad8c6841e13766ec3f7cff05da1f6 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Tue, 9 Jan 2024 12:01:38 +0700 Subject: [PATCH] Use networked field directly as backing fluid field --- .../mc/otm/block/decorative/FluidTankBlock.kt | 2 +- .../entity/decorative/FluidTankBlockEntity.kt | 14 ++++++-------- .../capability/fluid/BlockMatteryFluidHandler.kt | 10 +++------- .../client/render/blockentity/FluidTankRenderer.kt | 2 +- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/decorative/FluidTankBlock.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/decorative/FluidTankBlock.kt index ea0b5a975..edeb9f709 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/decorative/FluidTankBlock.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/decorative/FluidTankBlock.kt @@ -62,7 +62,7 @@ class FluidTankBlock : RotatableMatteryBlock(DEFAULT_MACHINE_PROPERTIES), Entity val tile = level?.getExistingBlockEntity(pos) ?: lightLevel if (tile is FluidTankBlockEntity) { - val fluid = tile.synchronizedFluid + val fluid = tile.fluid.fluid if (!fluid.isEmpty) { val newLevel = fluid.fluid.fluidType.getLightLevel(fluid) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/decorative/FluidTankBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/decorative/FluidTankBlockEntity.kt index c3e321746..b137b6ce1 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/decorative/FluidTankBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/decorative/FluidTankBlockEntity.kt @@ -27,12 +27,15 @@ import ru.dbotthepony.mc.otm.menu.decorative.FluidTankMenu import ru.dbotthepony.mc.otm.registry.MBlockEntities class FluidTankBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryDeviceBlockEntity(MBlockEntities.FLUID_TANK, blockPos, blockState) { - val fluid = BlockMatteryFluidHandler(::onChanged, ItemsConfig::FLUID_TANK_CAPACITY) - var synchronizedFluid by synchronizer.Field(FluidStack.EMPTY, FluidStackValueCodec, setter = { value, access, remote -> + val fluid = BlockMatteryFluidHandler(ItemsConfig::FLUID_TANK_CAPACITY, synchronizer.Field(FluidStack.EMPTY, FluidStackValueCodec, setter = { value, access, remote -> access.write(value) level?.lightEngine?.checkBlock(blockPos) - }) + + if (!remote) { + markDirtyFast() + } + })) val fillInput = MatteryContainer(::markDirtyFast, 1).also(::addDroppableContainer) val drainInput = MatteryContainer(::markDirtyFast, 1).also(::addDroppableContainer) @@ -73,11 +76,6 @@ class FluidTankBlockEntity(blockPos: BlockPos, blockState: BlockState) : Mattery savetables.stateful(::output) } - private fun onChanged(new: FluidStack, old: FluidStack) { - synchronizedFluid = new.copy() - markDirtyFast() - } - private fun drainItem() { val item = drainInput[0] diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/fluid/BlockMatteryFluidHandler.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/fluid/BlockMatteryFluidHandler.kt index ac675dac6..b93252b1a 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/fluid/BlockMatteryFluidHandler.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/fluid/BlockMatteryFluidHandler.kt @@ -7,18 +7,14 @@ import net.minecraftforge.common.util.INBTSerializable import net.minecraftforge.fluids.FluidStack import ru.dbotthepony.mc.otm.core.nbt.set import ru.dbotthepony.mc.otm.core.tagNotNull +import ru.dbotthepony.mc.otm.network.synchronizer.IMutableField import java.util.function.IntSupplier /** * Fluid handler for blocks */ -class BlockMatteryFluidHandler(val onChanged: (new: FluidStack, old: FluidStack) -> Unit, private val _capacity: IntSupplier) : AbstractMatteryFluidHandler(), INBTSerializable { - override var fluid: FluidStack = FluidStack.EMPTY - set(value) { - val old = field - field = value - onChanged(value, old) - } +open class BlockMatteryFluidHandler(private val _capacity: IntSupplier, field: IMutableField) : AbstractMatteryFluidHandler(), INBTSerializable { + override var fluid by field override val capacity: Int get() = _capacity.asInt diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/blockentity/FluidTankRenderer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/blockentity/FluidTankRenderer.kt index 4d9fd6fc4..7791ebc1e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/blockentity/FluidTankRenderer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/blockentity/FluidTankRenderer.kt @@ -32,7 +32,7 @@ class FluidTankRenderer(private val context: BlockEntityRendererProvider.Context packedLight: Int, packedOverlay: Int ) { - renderFluidInTank(tile.fluid, tile.synchronizedFluid, poseStack, bufferSource, packedLight, packedOverlay) + renderFluidInTank(tile.fluid, tile.fluid.fluid, poseStack, bufferSource, packedLight, packedOverlay) } object FluidTankItemRenderer : BlockEntityWithoutLevelRenderer(minecraft.blockEntityRenderDispatcher, minecraft.entityModels) {