diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/StorageInterfaces.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/StorageInterfaces.kt index 9d52ab982..843f45d8f 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/StorageInterfaces.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/StorageInterfaces.kt @@ -9,6 +9,7 @@ import net.minecraft.world.entity.player.Inventory import net.minecraft.world.entity.player.Player import net.minecraft.world.inventory.AbstractContainerMenu import net.minecraft.world.level.Level +import net.minecraft.world.level.block.entity.BlockEntityType import net.minecraft.world.level.block.state.BlockState import net.minecraftforge.common.capabilities.Capability import net.minecraftforge.common.util.LazyOptional @@ -16,6 +17,7 @@ import net.minecraftforge.items.CapabilityItemHandler import net.minecraftforge.items.IItemHandler import ru.dbotthepony.mc.otm.OverdriveThatMatters import ru.dbotthepony.mc.otm.block.RotatableMatteryBlock +import ru.dbotthepony.mc.otm.capability.BlockEnergyStorageImpl import ru.dbotthepony.mc.otm.capability.MatteryCapability import ru.dbotthepony.mc.otm.capability.WorkerEnergyStorage import ru.dbotthepony.mc.otm.capability.extractStepInner @@ -30,17 +32,17 @@ import ru.dbotthepony.mc.otm.storage.ITEM_STORAGE import ru.dbotthepony.mc.otm.storage.ItemStackWrapper import ru.dbotthepony.mc.otm.unaryMinus -class StorageImporterBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryPoweredBlockEntity(MBlockEntities.STORAGE_IMPORTER, blockPos, blockState) { - override val defaultDisplayName: Component - get() = MACHINE_NAME - - override val energy = WorkerEnergyStorage(this, maxBatteryLevel = MAX_POWER) +abstract class AbstractStorageImportExport( + blockType: BlockEntityType<*>, + blockPos: BlockPos, + blockState: BlockState, + maxBatteryLevel: ImpreciseFraction = MAX_POWER, + maxInput: ImpreciseFraction? = BlockEnergyStorageImpl.DEFAULT_MAX_IO, + maxOutput: ImpreciseFraction? = maxInput +) : MatteryPoweredBlockEntity(blockType, blockPos, blockState) { + final override val energy = WorkerEnergyStorage(this, maxBatteryLevel, maxInput, maxOutput) val cell = BasicStorageGraphNode(energy) - override fun createMenu(containerID: Int, inventory: Inventory, ply: Player): AbstractContainerMenu? { - return null - } - private var valid = true override fun getCapability(cap: Capability, side: Direction?): LazyOptional { @@ -61,12 +63,6 @@ class StorageImporterBlockEntity(blockPos: BlockPos, blockState: BlockState) : M valid = true } - private var target: LazyOptional = LazyOptional.empty() - private var lastSlot = 0 - private var nextTick = INTERVAL - - private val enoughEnergy get() = energy.batteryLevel >= OverdriveThatMatters.INSTANCE.ITEM_STORAGE().energyPerOperation - override fun setRemoved() { super.setRemoved() cell.destroy(level) @@ -83,6 +79,40 @@ class StorageImporterBlockEntity(blockPos: BlockPos, blockState: BlockState) : M tickOnceServer(this::checkSurroundings) } + protected var target: LazyOptional = LazyOptional.empty() + protected abstract val targetCapability: Capability + + fun checkSurroundings() { + target = getAndBind( + target, + level?.getBlockEntity(blockPos + blockState.getValue(RotatableMatteryBlock.FACING_FULL).normal), + targetCapability, + -blockState.getValue(RotatableMatteryBlock.FACING_FULL), + ) { tickOnceServer(this::checkSurroundings) } + } + + companion object { + const val MAX_MOVE_PER_OPERATION = 4 + val MAX_POWER = ImpreciseFraction(10_000) + } +} + +class StorageImporterBlockEntity(blockPos: BlockPos, blockState: BlockState) : AbstractStorageImportExport(MBlockEntities.STORAGE_IMPORTER, blockPos, blockState) { + override val defaultDisplayName: Component + get() = MACHINE_NAME + + override fun createMenu(containerID: Int, inventory: Inventory, ply: Player): AbstractContainerMenu? { + return null + } + + private var lastSlot = 0 + private var nextTick = INTERVAL + + private val enoughEnergy get() = energy.batteryLevel >= OverdriveThatMatters.INSTANCE.ITEM_STORAGE().energyPerOperation + + override val targetCapability: Capability + get() = CapabilityItemHandler.ITEM_HANDLER_CAPABILITY + fun tick() { batteryChargeLoop() cell.tickEnergyDemanding() @@ -122,19 +152,8 @@ class StorageImporterBlockEntity(blockPos: BlockPos, blockState: BlockState) : M } } - fun checkSurroundings() { - target = getAndBind( - target, - level?.getBlockEntity(blockPos + blockState.getValue(RotatableMatteryBlock.FACING_FULL).normal), - CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, - -blockState.getValue(RotatableMatteryBlock.FACING_FULL), - ) { tickOnceServer(this::checkSurroundings) } - } - companion object { private val MACHINE_NAME = TranslatableComponent("block.${OverdriveThatMatters.MOD_ID}.${MNames.STORAGE_IMPORTER}") private const val INTERVAL = 5 - private const val MAX_MOVE_PER_OPERATION = 4 - private val MAX_POWER = ImpreciseFraction(10_000) } }