From b4977ea6ae34dcf0a6a43acd85c1d62423f98947 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 7 Mar 2025 10:26:09 +0700 Subject: [PATCH] Move Battery bank to slotted container --- .../entity/tech/BatteryBankBlockEntity.kt | 34 +++++++++++++------ .../mc/otm/menu/tech/BatteryBankMenu.kt | 5 +-- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/BatteryBankBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/BatteryBankBlockEntity.kt index cd5940b7b..1707800d2 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/BatteryBankBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/BatteryBankBlockEntity.kt @@ -21,6 +21,9 @@ import ru.dbotthepony.mc.otm.capability.maxEnergyStoredMattery import ru.dbotthepony.mc.otm.capability.transcieveEnergy import ru.dbotthepony.mc.otm.container.HandlerFilter import ru.dbotthepony.mc.otm.container.MatteryContainer +import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot +import ru.dbotthepony.mc.otm.container.slotted.FilteredContainerSlot +import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer import ru.dbotthepony.mc.otm.core.immutableList import ru.dbotthepony.mc.otm.core.math.Decimal import ru.dbotthepony.mc.otm.core.otmRandom @@ -33,22 +36,31 @@ class BatteryBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte var gaugeLevel by syncher.float() private set - // 6 на 2 - val container: MatteryContainer = object : MatteryContainer(::setChanged, CAPACITY) { - override fun setChanged(slot: Int, new: ItemStack, old: ItemStack) { - super.setChanged(slot, new, old) - batteryStatus[slot].value = new.getCapability(Capabilities.EnergyStorage.ITEM) != null + private inner class Slot(container: SlottedContainer, slot: Int) : FilteredContainerSlot(container, slot) { + override fun canAutomationPlaceItem(itemStack: ItemStack): Boolean { + return super.canAutomationPlaceItem(itemStack) && itemStack.getCapability(Capabilities.EnergyStorage.ITEM)?.let { it.canExtract() && it.extractEnergy(Int.MAX_VALUE, true) > 0 } == true + } + + override fun canAutomationTakeItem(desired: Int): Boolean { + return super.canAutomationTakeItem(desired) && item.getCapability(Capabilities.EnergyStorage.ITEM)?.let { !it.canExtract() || it.extractEnergy(Int.MAX_VALUE, true) <= 0 } != false + } + + override fun notifyChanged(old: ItemStack) { + super.notifyChanged(old) + + batteryStatus[slot].value = item.getCapability(Capabilities.EnergyStorage.ITEM) != null gaugeLevel = batteryLevel.percentage(maxBatteryLevel) } - override fun getMaxStackSize(): Int = 1 - }.also(::addDroppableContainer) - - val batteryStatus = immutableList(CAPACITY) { - syncher.boolean(false) + override val maxStackSize: Int + get() = 1 } - val itemConfig = ConfigurableItemHandler(inputOutput = container.handler(HandlerFilter.Dischargeable)) + // 6 на 2 + val container = SlottedContainer.simple(CAPACITY, ::Slot, ::markDirtyFast).also(::addDroppableContainer) + val batteryStatus = immutableList(CAPACITY) { syncher.boolean(false) } + + val itemConfig = ConfigurableItemHandler(inputOutput = container) init { savetables.stateful(::container, INVENTORY_KEY) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/BatteryBankMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/BatteryBankMenu.kt index 36a469139..0c35515f1 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/BatteryBankMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/BatteryBankMenu.kt @@ -5,6 +5,7 @@ import net.minecraft.world.entity.player.Inventory import ru.dbotthepony.mc.otm.block.entity.tech.BatteryBankBlockEntity import net.minecraft.world.SimpleContainer import ru.dbotthepony.mc.otm.block.entity.RedstoneSetting +import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer import ru.dbotthepony.mc.otm.core.immutableList import ru.dbotthepony.mc.otm.menu.BatteryMenuSlot import ru.dbotthepony.mc.otm.menu.MatteryMenu @@ -21,7 +22,7 @@ class BatteryBankMenu( tile: BatteryBankBlockEntity? = null, ) : MatteryMenu(MMenus.BATTERY_BANK, p_38852_, inventory, tile) { val powerLevel = ProfiledLevelGaugeWidget(this, tile?.energyConfig?.energy) - val storageSlots: List + val storageSlots: List val redstone = EnumInputWithFeedback(this, RedstoneSetting::class.java) val energyConfig = EnergyConfigPlayerInput(this, allowPull = false, allowPush = true) val itemConfig = ItemConfigPlayerInput(this, allowPull = false, allowPush = false) @@ -33,7 +34,7 @@ class BatteryBankMenu( itemConfig.with(tile.itemConfig) } - val container: Container = tile?.container ?: SimpleContainer(BatteryBankBlockEntity.CAPACITY) + val container = tile?.container ?: SlottedContainer.filtered(BatteryBankBlockEntity.CAPACITY) storageSlots = immutableList(BatteryBankBlockEntity.CAPACITY) { addStorageSlot(BatteryMenuSlot(container, it))