From a5b96fa4c4925af43bc2ecffc920018d589dcda6 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 3 Mar 2025 18:33:24 +0700 Subject: [PATCH] Update bottler to use slotted container --- .../entity/matter/MatterBottlerBlockEntity.kt | 68 +++++++++---------- .../mc/otm/capability/FlowDirection.kt | 2 +- 2 files changed, 32 insertions(+), 38 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterBottlerBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterBottlerBlockEntity.kt index 17aae98fe..0a39c82f0 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterBottlerBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterBottlerBlockEntity.kt @@ -24,9 +24,12 @@ import ru.dbotthepony.mc.otm.config.MachinesConfig import ru.dbotthepony.mc.otm.container.MatteryContainer import ru.dbotthepony.mc.otm.container.HandlerFilter import ru.dbotthepony.mc.otm.container.UpgradeContainer +import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot +import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer import ru.dbotthepony.mc.otm.menu.matter.MatterBottlerMenu import ru.dbotthepony.mc.otm.registry.game.MBlockEntities import ru.dbotthepony.mc.otm.core.math.Decimal +import ru.dbotthepony.mc.otm.core.util.countingLazy import ru.dbotthepony.mc.otm.graph.matter.SimpleMatterNode import java.util.function.BooleanSupplier @@ -37,23 +40,20 @@ class MatterBottlerBlockEntity(blockPos: BlockPos, blockState: BlockState) : override val energy = ProfiledEnergyStorage(WorkerEnergyStorage(this::markDirtyFast, upgrades.transform(MachinesConfig.MatterBottler.VALUES))) val energyConfig = ConfigurableEnergy(energy) - private inner class Container : MatteryContainer(3) { - init { - addDroppableContainer(this) - } - - override fun getMaxStackSize(slot: Int, itemStack: ItemStack): Int { - return 1 - } - - override fun setChanged(slot: Int, new: ItemStack, old: ItemStack) { - markDirtyFast() - updateBlockState() + private inner class BottlingSlot(container: SlottedContainer, slot: Int) : ContainerSlot(container, slot) { + override fun canAutomationPlaceItem(itemStack: ItemStack): Boolean { + return super.canAutomationPlaceItem(itemStack) && isBottling && itemStack.getCapability(MatteryCapability.MATTER_ITEM)?.receiveMatterChecked(Decimal.ONE, true)?.isPositive == true } } - val unbottling: MatteryContainer = Container() - val bottling: MatteryContainer = Container() + private inner class UnBottlingSlot(container: SlottedContainer, slot: Int) : ContainerSlot(container, slot) { + override fun canAutomationPlaceItem(itemStack: ItemStack): Boolean { + return super.canAutomationPlaceItem(itemStack) && !isBottling && itemStack.getCapability(MatteryCapability.MATTER_ITEM)?.extractMatterChecked(Decimal.ONE, true)?.isPositive == true + } + } + + val unbottling = SlottedContainer.simple(3, ::UnBottlingSlot, ::updateBlockState) + val bottling = SlottedContainer.simple(3, ::BottlingSlot, ::updateBlockState) var isBottling: Boolean = true set(value) { @@ -63,11 +63,11 @@ class MatterBottlerBlockEntity(blockPos: BlockPos, blockState: BlockState) : this.markDirtyFast() if (value) { - inputHandler.parent = bottlingHandler - outputHandler.parent = unbottlingHandler + inputHandler.parent = bottling + outputHandler.parent = unbottling } else { - inputHandler.parent = unbottlingHandler - outputHandler.parent = bottlingHandler + inputHandler.parent = unbottling + outputHandler.parent = bottling } updateBlockState() @@ -79,20 +79,8 @@ class MatterBottlerBlockEntity(blockPos: BlockPos, blockState: BlockState) : this.markDirtyFast() } - val bottlingHandler = bottling.handler(object : HandlerFilter { - override fun canInsert(slot: Int, stack: ItemStack): Boolean { - return isBottling && stack.getCapability(MatteryCapability.MATTER_ITEM)?.let { it.matterFlow.input && it.missingMatter.isPositive } ?: false - } - }) - - val unbottlingHandler = unbottling.handler(object : HandlerFilter { - override fun canInsert(slot: Int, stack: ItemStack): Boolean { - return !isBottling && stack.getCapability(MatteryCapability.MATTER_ITEM)?.let { it.matterFlow.output && it.storedMatter.isPositive } ?: false - } - }) - - private val inputHandler = ProxiedItemHandler(bottlingHandler) - private val outputHandler = ProxiedItemHandler(unbottlingHandler) + private val inputHandler = ProxiedItemHandler(bottling) + private val outputHandler = ProxiedItemHandler(unbottling) val itemConfig = ConfigurableItemHandler( input = inputHandler, @@ -141,12 +129,12 @@ class MatterBottlerBlockEntity(blockPos: BlockPos, blockState: BlockState) : matterNode.isValid = false } - private fun updateBlockState(container: MatteryContainer) { + private fun updateBlockState(container: SlottedContainer) { val level = level as? ServerLevel ?: return var state = blockState - for (i in 0 .. 2) { - val desired = !container.getItem(i).isEmpty && container.getItem(i).getCapability(MatteryCapability.MATTER_ITEM) != null + for ((i, slot) in container.slotIterator().withIndex()) { + val desired = !slot.isEmpty && slot.item.getCapability(MatteryCapability.MATTER_ITEM) != null if (state.getValue(MatterBottlerBlock.SLOT_PROPERTIES[i]) != desired) { state = state.setValue(MatterBottlerBlock.SLOT_PROPERTIES[i], desired) @@ -159,20 +147,25 @@ class MatterBottlerBlockEntity(blockPos: BlockPos, blockState: BlockState) : } private fun updateBlockState() { + markDirtyFast() if (isBottling) updateBlockState(bottling) else updateBlockState(unbottling) } + private val workerState by countingLazy(blockStateChangesCounter) { + blockState.getValue(WorkerState.SEMI_WORKER_STATE) + } + private fun blockstateToWorking() { - if (blockState.getValue(WorkerState.SEMI_WORKER_STATE) !== WorkerState.WORKING) { + if (workerState !== WorkerState.WORKING) { level?.setBlock(blockPos, blockState.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.WORKING), Block.UPDATE_CLIENTS) } } private fun blockstateToIdle() { - if (blockState.getValue(WorkerState.SEMI_WORKER_STATE) !== WorkerState.IDLE) { + if (workerState !== WorkerState.IDLE) { level?.setBlock(blockPos, blockState.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.IDLE), Block.UPDATE_CLIENTS) } } @@ -191,6 +184,7 @@ class MatterBottlerBlockEntity(blockPos: BlockPos, blockState: BlockState) : for (slot in bottling.slotIterator()) { val item = slot.item + item.getCapability(MatteryCapability.MATTER_ITEM)?.let { if (!it.missingMatter.isPositive) { unbottling.consumeItem(item, false) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/FlowDirection.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/FlowDirection.kt index d00b2fb32..38e798d47 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/FlowDirection.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/FlowDirection.kt @@ -136,7 +136,7 @@ enum class FlowDirection(val input: Boolean, val output: Boolean, val translatio */ @JvmStatic fun input(flag: Boolean): FlowDirection { - return of(flag, !flag) + return if (flag) INPUT else OUTPUT } /**