Split storage interface class

This commit is contained in:
DBotThePony 2022-05-14 18:55:34 +07:00
parent fffb44c9a0
commit c6b68ad270
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -9,6 +9,7 @@ import net.minecraft.world.entity.player.Inventory
import net.minecraft.world.entity.player.Player import net.minecraft.world.entity.player.Player
import net.minecraft.world.inventory.AbstractContainerMenu import net.minecraft.world.inventory.AbstractContainerMenu
import net.minecraft.world.level.Level import net.minecraft.world.level.Level
import net.minecraft.world.level.block.entity.BlockEntityType
import net.minecraft.world.level.block.state.BlockState import net.minecraft.world.level.block.state.BlockState
import net.minecraftforge.common.capabilities.Capability import net.minecraftforge.common.capabilities.Capability
import net.minecraftforge.common.util.LazyOptional import net.minecraftforge.common.util.LazyOptional
@ -16,6 +17,7 @@ import net.minecraftforge.items.CapabilityItemHandler
import net.minecraftforge.items.IItemHandler import net.minecraftforge.items.IItemHandler
import ru.dbotthepony.mc.otm.OverdriveThatMatters import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.block.RotatableMatteryBlock 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.MatteryCapability
import ru.dbotthepony.mc.otm.capability.WorkerEnergyStorage import ru.dbotthepony.mc.otm.capability.WorkerEnergyStorage
import ru.dbotthepony.mc.otm.capability.extractStepInner 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.storage.ItemStackWrapper
import ru.dbotthepony.mc.otm.unaryMinus import ru.dbotthepony.mc.otm.unaryMinus
class StorageImporterBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryPoweredBlockEntity(MBlockEntities.STORAGE_IMPORTER, blockPos, blockState) { abstract class AbstractStorageImportExport<T>(
override val defaultDisplayName: Component blockType: BlockEntityType<*>,
get() = MACHINE_NAME blockPos: BlockPos,
blockState: BlockState,
override val energy = WorkerEnergyStorage(this, maxBatteryLevel = MAX_POWER) 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) val cell = BasicStorageGraphNode(energy)
override fun createMenu(containerID: Int, inventory: Inventory, ply: Player): AbstractContainerMenu? {
return null
}
private var valid = true private var valid = true
override fun <T> getCapability(cap: Capability<T>, side: Direction?): LazyOptional<T> { override fun <T> getCapability(cap: Capability<T>, side: Direction?): LazyOptional<T> {
@ -61,12 +63,6 @@ class StorageImporterBlockEntity(blockPos: BlockPos, blockState: BlockState) : M
valid = true valid = true
} }
private var target: LazyOptional<IItemHandler> = LazyOptional.empty()
private var lastSlot = 0
private var nextTick = INTERVAL
private val enoughEnergy get() = energy.batteryLevel >= OverdriveThatMatters.INSTANCE.ITEM_STORAGE().energyPerOperation
override fun setRemoved() { override fun setRemoved() {
super.setRemoved() super.setRemoved()
cell.destroy(level) cell.destroy(level)
@ -83,6 +79,40 @@ class StorageImporterBlockEntity(blockPos: BlockPos, blockState: BlockState) : M
tickOnceServer(this::checkSurroundings) tickOnceServer(this::checkSurroundings)
} }
protected var target: LazyOptional<T> = LazyOptional.empty()
protected abstract val targetCapability: Capability<T>
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<IItemHandler>(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<IItemHandler>
get() = CapabilityItemHandler.ITEM_HANDLER_CAPABILITY
fun tick() { fun tick() {
batteryChargeLoop() batteryChargeLoop()
cell.tickEnergyDemanding() 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 { companion object {
private val MACHINE_NAME = TranslatableComponent("block.${OverdriveThatMatters.MOD_ID}.${MNames.STORAGE_IMPORTER}") private val MACHINE_NAME = TranslatableComponent("block.${OverdriveThatMatters.MOD_ID}.${MNames.STORAGE_IMPORTER}")
private const val INTERVAL = 5 private const val INTERVAL = 5
private const val MAX_MOVE_PER_OPERATION = 4
private val MAX_POWER = ImpreciseFraction(10_000)
} }
} }