Redo matter bottler
This commit is contained in:
parent
b4c2f27c4c
commit
f18a3eaaaa
@ -555,12 +555,12 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo
|
|||||||
capController.expose()
|
capController.expose()
|
||||||
}
|
}
|
||||||
|
|
||||||
when (value) {
|
currentHandler = when (value) {
|
||||||
ItemHandlerMode.DISABLED -> currentHandler = EmptyItemHandler
|
ItemHandlerMode.DISABLED -> EmptyItemHandler
|
||||||
ItemHandlerMode.INPUT -> currentHandler = input!!
|
ItemHandlerMode.INPUT -> input!!
|
||||||
ItemHandlerMode.OUTPUT -> currentHandler = output!!
|
ItemHandlerMode.OUTPUT -> output!!
|
||||||
ItemHandlerMode.INPUT_OUTPUT -> currentHandler = inputOutput!!
|
ItemHandlerMode.INPUT_OUTPUT -> inputOutput!!
|
||||||
ItemHandlerMode.BATTERY -> currentHandler = battery!!
|
ItemHandlerMode.BATTERY -> battery!!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -39,16 +39,14 @@ abstract class MatteryPoweredBlockEntity(p_155228_: BlockEntityType<*>, p_155229
|
|||||||
if (demand.isZero) return
|
if (demand.isZero) return
|
||||||
|
|
||||||
for (stack in batteryContainer) {
|
for (stack in batteryContainer) {
|
||||||
if (!stack.isEmpty) {
|
stack.getCapability(ForgeCapabilities.ENERGY).ifPresentK {
|
||||||
stack.getCapability(ForgeCapabilities.ENERGY).ifPresentK {
|
val diff = it.extractEnergy(demand, false)
|
||||||
val diff = it.extractEnergy(demand, false)
|
energy.receiveEnergy(diff, false)
|
||||||
energy.receiveEnergy(diff, false)
|
demand -= diff
|
||||||
demand -= diff
|
|
||||||
}
|
|
||||||
|
|
||||||
if (demand <= Decimal.ZERO)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (demand <= Decimal.ZERO)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,11 +9,14 @@ import net.minecraft.world.item.ItemStack
|
|||||||
import net.minecraft.world.level.Level
|
import net.minecraft.world.level.Level
|
||||||
import net.minecraft.world.level.block.Block
|
import net.minecraft.world.level.block.Block
|
||||||
import net.minecraft.world.level.block.state.BlockState
|
import net.minecraft.world.level.block.state.BlockState
|
||||||
|
import net.minecraftforge.items.IItemHandler
|
||||||
import ru.dbotthepony.mc.otm.block.matter.MatterBottlerBlock
|
import ru.dbotthepony.mc.otm.block.matter.MatterBottlerBlock
|
||||||
import ru.dbotthepony.mc.otm.block.entity.MatteryPoweredBlockEntity
|
import ru.dbotthepony.mc.otm.block.entity.MatteryPoweredBlockEntity
|
||||||
import ru.dbotthepony.mc.otm.block.entity.WorkerState
|
import ru.dbotthepony.mc.otm.block.entity.WorkerState
|
||||||
|
import ru.dbotthepony.mc.otm.capability.CombinedItemHandler
|
||||||
import ru.dbotthepony.mc.otm.capability.FlowDirection
|
import ru.dbotthepony.mc.otm.capability.FlowDirection
|
||||||
import ru.dbotthepony.mc.otm.capability.MatteryCapability
|
import ru.dbotthepony.mc.otm.capability.MatteryCapability
|
||||||
|
import ru.dbotthepony.mc.otm.capability.ProxiedItemHandler
|
||||||
import ru.dbotthepony.mc.otm.capability.energy.ProfiledEnergyStorage
|
import ru.dbotthepony.mc.otm.capability.energy.ProfiledEnergyStorage
|
||||||
import ru.dbotthepony.mc.otm.capability.energy.WorkerEnergyStorage
|
import ru.dbotthepony.mc.otm.capability.energy.WorkerEnergyStorage
|
||||||
import ru.dbotthepony.mc.otm.capability.matter.IMatterStorage
|
import ru.dbotthepony.mc.otm.capability.matter.IMatterStorage
|
||||||
@ -32,66 +35,71 @@ class MatterBottlerBlockEntity(blockPos: BlockPos, blockState: BlockState) :
|
|||||||
MatteryPoweredBlockEntity(MBlockEntities.MATTER_BOTTLER, blockPos, blockState) {
|
MatteryPoweredBlockEntity(MBlockEntities.MATTER_BOTTLER, blockPos, blockState) {
|
||||||
|
|
||||||
val energy = ProfiledEnergyStorage(WorkerEnergyStorage(this, MachinesConfig.MatterBottler.VALUES))
|
val energy = ProfiledEnergyStorage(WorkerEnergyStorage(this, MachinesConfig.MatterBottler.VALUES))
|
||||||
|
val energyConfig = ConfigurableEnergy(energy)
|
||||||
|
|
||||||
var isBottling: Boolean = true
|
private inner class Container : MatteryContainer(3) {
|
||||||
set(value) {
|
init {
|
||||||
field = value
|
addDroppableContainer(this)
|
||||||
this.setChangedLight()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateBlockState() {
|
|
||||||
val level = level as? ServerLevel ?: return
|
|
||||||
|
|
||||||
var state = blockState
|
|
||||||
val initial = if (isBottling) 0 else 3
|
|
||||||
|
|
||||||
for (i in initial .. initial + 2) {
|
|
||||||
val desired = !container.getItem(i).isEmpty && container.getItem(i).getCapability(MatteryCapability.MATTER).isPresent
|
|
||||||
|
|
||||||
if (state.getValue(MatterBottlerBlock.SLOT_PROPERTIES[i - initial]) != desired) {
|
|
||||||
state = state.setValue(MatterBottlerBlock.SLOT_PROPERTIES[i - initial], desired)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (state !== blockState) {
|
|
||||||
level.setBlock(blockPos, state, Block.UPDATE_CLIENTS)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: оно должно что то делать
|
|
||||||
// true - continue even if empty when bottling / if full while unbottling
|
|
||||||
// false - spit into output slot
|
|
||||||
//private var work_behavior = true
|
|
||||||
|
|
||||||
val container: MatteryContainer = object : MatteryContainer(this::setChangedLight, 6) {
|
|
||||||
override fun getMaxStackSize(slot: Int, itemStack: ItemStack): Int {
|
override fun getMaxStackSize(slot: Int, itemStack: ItemStack): Int {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setChanged(slot: Int, new: ItemStack, old: ItemStack) {
|
override fun setChanged(slot: Int, new: ItemStack, old: ItemStack) {
|
||||||
super.setChanged(slot, new, old)
|
setChangedLight()
|
||||||
updateBlockState()
|
updateBlockState()
|
||||||
}
|
}
|
||||||
}.also(::addDroppableContainer)
|
}
|
||||||
|
|
||||||
val itemHandler = container.handler(object : HandlerFilter {
|
val unbottling: MatteryContainer = Container()
|
||||||
override fun canInsert(slot: Int, stack: ItemStack): Boolean {
|
val bottling: MatteryContainer = Container()
|
||||||
if (isBottling) {
|
|
||||||
return slot < 3 && stack.getCapability(MatteryCapability.MATTER).isPresent
|
var isBottling: Boolean = true
|
||||||
|
set(value) {
|
||||||
|
field = value
|
||||||
|
initialCapacity = null
|
||||||
|
workProgress = 0f
|
||||||
|
this.setChangedLight()
|
||||||
|
|
||||||
|
if (value) {
|
||||||
|
inputHandler.parent = bottlingHandler
|
||||||
|
outputHandler.parent = unbottlingHandler
|
||||||
|
} else {
|
||||||
|
inputHandler.parent = unbottlingHandler
|
||||||
|
outputHandler.parent = bottlingHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
return slot >= 3 && stack.getCapability(MatteryCapability.MATTER).isPresent
|
updateBlockState()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun canExtract(slot: Int, amount: Int, stack: ItemStack): Boolean {
|
var spitItemsWhenCantWork = false
|
||||||
if (isBottling) {
|
set(value) {
|
||||||
return slot >= 3
|
field = value
|
||||||
}
|
this.setChangedLight()
|
||||||
|
}
|
||||||
|
|
||||||
return slot < 3
|
val bottlingHandler = bottling.handler(object : HandlerFilter {
|
||||||
|
override fun canInsert(slot: Int, stack: ItemStack): Boolean {
|
||||||
|
return isBottling && stack.getCapability(MatteryCapability.MATTER).map { it.matterFlow.input && it.missingMatter.isPositive }.orElse(false)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
val unbottlingHandler = unbottling.handler(object : HandlerFilter {
|
||||||
|
override fun canInsert(slot: Int, stack: ItemStack): Boolean {
|
||||||
|
return !isBottling && stack.getCapability(MatteryCapability.MATTER).map { it.matterFlow.output && it.storedMatter.isPositive }.orElse(false)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
private val inputHandler = ProxiedItemHandler(bottlingHandler)
|
||||||
|
private val outputHandler = ProxiedItemHandler(unbottlingHandler)
|
||||||
|
|
||||||
|
val itemConfig = ConfigurableItemHandler(
|
||||||
|
input = inputHandler,
|
||||||
|
output = outputHandler,
|
||||||
|
battery = batteryItemHandler
|
||||||
|
)
|
||||||
|
|
||||||
val matter: ProfiledMatterStorage<MatterStorageImpl> = ProfiledMatterStorage(object : MatterStorageImpl(this::setChangedLight, FlowDirection.BI_DIRECTIONAL, MachinesConfig.MatterBottler.VALUES::matterCapacity) {
|
val matter: ProfiledMatterStorage<MatterStorageImpl> = ProfiledMatterStorage(object : MatterStorageImpl(this::setChangedLight, FlowDirection.BI_DIRECTIONAL, MachinesConfig.MatterBottler.VALUES::matterCapacity) {
|
||||||
override val matterFlow: FlowDirection get() {
|
override val matterFlow: FlowDirection get() {
|
||||||
return if (this@MatterBottlerBlockEntity.isBottling) FlowDirection.INPUT else FlowDirection.OUTPUT
|
return if (this@MatterBottlerBlockEntity.isBottling) FlowDirection.INPUT else FlowDirection.OUTPUT
|
||||||
@ -103,18 +111,19 @@ class MatterBottlerBlockEntity(blockPos: BlockPos, blockState: BlockState) :
|
|||||||
init {
|
init {
|
||||||
exposeGlobally(MatteryCapability.MATTER, matter)
|
exposeGlobally(MatteryCapability.MATTER, matter)
|
||||||
exposeGlobally(MatteryCapability.MATTER_NODE, matterNode)
|
exposeGlobally(MatteryCapability.MATTER_NODE, matterNode)
|
||||||
exposeItemsGlobally(itemHandler)
|
|
||||||
|
|
||||||
savetables.bool(::isBottling)
|
savetables.bool(::isBottling)
|
||||||
|
savetables.bool(::spitItemsWhenCantWork)
|
||||||
savetable(::energy, ENERGY_KEY)
|
savetable(::energy, ENERGY_KEY)
|
||||||
savetable(::matter, MATTER_STORAGE_KEY)
|
savetable(::matter, MATTER_STORAGE_KEY)
|
||||||
savetable(::container, INVENTORY_KEY)
|
savetable(::bottling)
|
||||||
|
savetable(::unbottling)
|
||||||
exposeEnergyGlobally(energy)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var workProgress: Float = 0f
|
||||||
|
private set
|
||||||
|
|
||||||
private var initialCapacity: Decimal? = null
|
private var initialCapacity: Decimal? = null
|
||||||
private var lastWorkStack: ItemStack? = null
|
|
||||||
|
|
||||||
override fun setLevel(level: Level) {
|
override fun setLevel(level: Level) {
|
||||||
super.setLevel(level)
|
super.setLevel(level)
|
||||||
@ -125,155 +134,156 @@ class MatterBottlerBlockEntity(blockPos: BlockPos, blockState: BlockState) :
|
|||||||
return MatterBottlerMenu(containerID, inventory, this)
|
return MatterBottlerMenu(containerID, inventory, this)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getWorkProgress(): Float {
|
|
||||||
val lastWorkStack = lastWorkStack ?: return 0f
|
|
||||||
val initialCapacity = initialCapacity ?: return 0f
|
|
||||||
val cap = lastWorkStack.getCapability(MatteryCapability.MATTER).orNull() ?: return 0f
|
|
||||||
|
|
||||||
if (this.isBottling) {
|
|
||||||
if (cap.maxStoredMatter - initialCapacity <= Decimal.ZERO) {
|
|
||||||
return 0f
|
|
||||||
}
|
|
||||||
|
|
||||||
return ((cap.storedMatter - initialCapacity) / (cap.maxStoredMatter - initialCapacity)).toFloat()
|
|
||||||
}
|
|
||||||
|
|
||||||
if (initialCapacity <= Decimal.ZERO) {
|
|
||||||
return 0f
|
|
||||||
}
|
|
||||||
|
|
||||||
return (Decimal.ONE - cap.storedMatter / initialCapacity).toFloat()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun setRemoved() {
|
override fun setRemoved() {
|
||||||
super.setRemoved()
|
super.setRemoved()
|
||||||
matterNode.isValid = false
|
matterNode.isValid = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun updateBlockState(container: MatteryContainer) {
|
||||||
|
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).isPresent
|
||||||
|
|
||||||
|
if (state.getValue(MatterBottlerBlock.SLOT_PROPERTIES[i]) != desired) {
|
||||||
|
state = state.setValue(MatterBottlerBlock.SLOT_PROPERTIES[i], desired)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state !== blockState) {
|
||||||
|
level.setBlock(blockPos, state, Block.UPDATE_CLIENTS)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateBlockState() {
|
||||||
|
if (isBottling)
|
||||||
|
updateBlockState(bottling)
|
||||||
|
else
|
||||||
|
updateBlockState(unbottling)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun blockstateToWorking() {
|
||||||
|
if (blockState.getValue(WorkerState.SEMI_WORKER_STATE) !== 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) {
|
||||||
|
level?.setBlock(blockPos, blockState.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.IDLE), Block.UPDATE_CLIENTS)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun tick() {
|
override fun tick() {
|
||||||
super.tick()
|
super.tick()
|
||||||
|
|
||||||
if (redstoneControl.isBlockedByRedstone) {
|
if (redstoneControl.isBlockedByRedstone) {
|
||||||
if (blockState.getValue(WorkerState.SEMI_WORKER_STATE) !== WorkerState.IDLE) {
|
blockstateToIdle()
|
||||||
level?.setBlock(
|
|
||||||
blockPos,
|
|
||||||
blockState.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.IDLE),
|
|
||||||
Block.UPDATE_CLIENTS
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var workStack: ItemStack? = null
|
if (isBottling) {
|
||||||
var capability: IMatterStorage? = null
|
var any = false
|
||||||
val align = if (isBottling) 0 else 3
|
var idle = false
|
||||||
var workSlot = -1
|
val iterator = bottling.iterator()
|
||||||
val unexpectedDirection = if (isBottling) FlowDirection.OUTPUT else FlowDirection.INPUT
|
|
||||||
|
|
||||||
for (i in align until align + 3) {
|
for (item in iterator) {
|
||||||
val itemStack = container.getItem(i)
|
item.getCapability(MatteryCapability.MATTER).ifPresentK {
|
||||||
|
if (!it.missingMatter.isPositive) {
|
||||||
|
unbottling.consumeItem(item, false)
|
||||||
|
iterator.setChanged()
|
||||||
|
} else {
|
||||||
|
any = true
|
||||||
|
initialCapacity = initialCapacity ?: it.storedMatter
|
||||||
|
val rate = MachinesConfig.MatterBottler.RATE
|
||||||
|
|
||||||
if (!itemStack.isEmpty) {
|
if (matter.storedMatter < rate) {
|
||||||
val cap = itemStack.getCapability(MatteryCapability.MATTER).orNull() ?: continue
|
matter.receiveMatter(matterNode.graph.extractMatter(matter.missingMatter
|
||||||
|
.coerceAtMost(rate * 200)
|
||||||
|
.coerceAtMost(it.missingMatter - matter.storedMatter), false), false)
|
||||||
|
}
|
||||||
|
|
||||||
if (cap.matterFlow != unexpectedDirection) {
|
if (matter.storedMatter.isPositive) {
|
||||||
if (this.isBottling && cap.missingMatter > Decimal.ZERO || !this.isBottling && cap.storedMatter > Decimal.ZERO) {
|
matter.extractMatter(it.receiveMatter(rate.coerceAtMost(matter.storedMatter), false), false)
|
||||||
workStack = itemStack
|
|
||||||
capability = cap
|
|
||||||
workSlot = i
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (workStack == null) {
|
if (!it.missingMatter.isPositive) {
|
||||||
lastWorkStack = null
|
initialCapacity = null
|
||||||
initialCapacity = null
|
workProgress = 0f
|
||||||
} else if (workStack != lastWorkStack) {
|
} else {
|
||||||
lastWorkStack = workStack
|
workProgress = ((it.storedMatter - initialCapacity!!) / it.maxStoredMatter).toFloat()
|
||||||
initialCapacity = capability!!.storedMatter
|
}
|
||||||
}
|
} else {
|
||||||
|
idle = true
|
||||||
|
|
||||||
if (capability != null) {
|
if (spitItemsWhenCantWork) {
|
||||||
if (blockState.getValue(WorkerState.SEMI_WORKER_STATE) !== WorkerState.WORKING) {
|
unbottling.consumeItem(item, false)
|
||||||
level?.setBlock(blockPos, blockState.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.WORKING), Block.UPDATE_CLIENTS)
|
iterator.setChanged()
|
||||||
}
|
|
||||||
|
|
||||||
val rate = MachinesConfig.MatterBottler.RATE
|
|
||||||
val consumption = MachinesConfig.MatterBottler.VALUES.energyConsumption
|
|
||||||
|
|
||||||
if (isBottling) {
|
|
||||||
if (matter.storedMatter < rate) {
|
|
||||||
val extracted = matterNode.graph.extractMatter(
|
|
||||||
matter.missingMatter
|
|
||||||
.coerceAtMost(rate * 200)
|
|
||||||
.coerceAtMost(capability.missingMatter - matter.storedMatter), true)
|
|
||||||
|
|
||||||
if (extracted > Decimal.ZERO) {
|
|
||||||
val received = matter.receiveMatter(extracted, false)
|
|
||||||
matterNode.graph.extractMatter(received, false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (matter.storedMatter > Decimal.ZERO) {
|
|
||||||
val energyExtracted = energy.extractEnergy(consumption, true)
|
|
||||||
|
|
||||||
if (!energyExtracted.isZero) {
|
|
||||||
val matter = capability.receiveMatter(rate.coerceAtMost(matter.storedMatter) * energyExtracted / consumption, true)
|
|
||||||
|
|
||||||
if (!matter.isZero) {
|
|
||||||
energy.extractEnergy(consumption * matter / rate, false)
|
|
||||||
|
|
||||||
capability.receiveMatter(matter, false)
|
|
||||||
this.matter.extractMatter(matter, false)
|
|
||||||
|
|
||||||
if (capability.missingMatter.isZero) {
|
|
||||||
for (i in 3..5) {
|
|
||||||
if (container.getItem(i).isEmpty) {
|
|
||||||
container.setItem(workSlot, ItemStack.EMPTY)
|
|
||||||
container.setItem(i, workStack!!)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (any) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (any && !idle) {
|
||||||
|
blockstateToWorking()
|
||||||
} else {
|
} else {
|
||||||
val energyExtracted = energy.extractEnergy(consumption, true)
|
matter.extractMatter(matterNode.graph.receiveMatter(matter.storedMatter, false), false)
|
||||||
|
blockstateToIdle()
|
||||||
if (!energyExtracted.isZero) {
|
|
||||||
val matter = capability.extractMatter(rate.coerceAtMost(matter.missingMatter) * energyExtracted / consumption, true)
|
|
||||||
|
|
||||||
if (!matter.isZero) {
|
|
||||||
this.energy.extractEnergy(consumption * matter / rate, false)
|
|
||||||
|
|
||||||
capability.extractMatter(matter, false)
|
|
||||||
this.matter.receiveMatter(matter, false)
|
|
||||||
|
|
||||||
if (capability.storedMatter.isZero) {
|
|
||||||
for (i in 2 downTo 0) {
|
|
||||||
if (container.getItem(i).isEmpty) {
|
|
||||||
container.setItem(workSlot, ItemStack.EMPTY)
|
|
||||||
container.setItem(i, workStack!!)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
level?.setBlock(blockPos, blockState.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.IDLE), Block.UPDATE_CLIENTS)
|
matter.extractMatter(matterNode.graph.receiveMatter(matter.storedMatter, false), false)
|
||||||
}
|
|
||||||
|
|
||||||
if (!isBottling && !matter.storedMatter.isZero) {
|
if (!matter.missingMatter.isPositive) {
|
||||||
val diff = matter.extractMatter(matter.storedMatter, true)
|
if (spitItemsWhenCantWork) {
|
||||||
val diff2 = matterNode.graph.receiveMatter(diff, true)
|
val iterator = unbottling.iterator()
|
||||||
matter.extractMatter(diff2, false)
|
|
||||||
matterNode.graph.receiveMatter(diff2, false)
|
for (item in iterator) {
|
||||||
|
bottling.consumeItem(item, false)
|
||||||
|
iterator.setChanged()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
blockstateToIdle()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var any = false
|
||||||
|
|
||||||
|
val iterator = unbottling.iterator()
|
||||||
|
for (item in iterator) {
|
||||||
|
item.getCapability(MatteryCapability.MATTER).ifPresentK {
|
||||||
|
if (!it.storedMatter.isPositive) {
|
||||||
|
bottling.consumeItem(item, false)
|
||||||
|
iterator.setChanged()
|
||||||
|
} else {
|
||||||
|
any = true
|
||||||
|
initialCapacity = initialCapacity ?: it.storedMatter
|
||||||
|
matter.receiveMatter(it.extractMatter(MachinesConfig.MatterBottler.RATE, false), false)
|
||||||
|
|
||||||
|
if (!it.storedMatter.isPositive) {
|
||||||
|
initialCapacity = null
|
||||||
|
workProgress = 0f
|
||||||
|
} else {
|
||||||
|
workProgress = 1f - (it.storedMatter / initialCapacity!!).toFloat()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (any) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (any) {
|
||||||
|
blockstateToWorking()
|
||||||
|
} else {
|
||||||
|
blockstateToIdle()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
package ru.dbotthepony.mc.otm.capability
|
||||||
|
|
||||||
|
import net.minecraft.world.item.ItemStack
|
||||||
|
import net.minecraftforge.items.IItemHandler
|
||||||
|
|
||||||
|
class ProxiedItemHandler<T : IItemHandler>(var parent: T? = null) : IItemHandler {
|
||||||
|
override fun getSlots(): Int {
|
||||||
|
return parent?.slots ?: 0
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getStackInSlot(slot: Int): ItemStack {
|
||||||
|
return parent?.getStackInSlot(slot) ?: ItemStack.EMPTY
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun insertItem(slot: Int, stack: ItemStack, simulate: Boolean): ItemStack {
|
||||||
|
return parent?.insertItem(slot, stack, simulate) ?: stack
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun extractItem(slot: Int, amount: Int, simulate: Boolean): ItemStack {
|
||||||
|
return parent?.extractItem(slot, amount, simulate) ?: ItemStack.EMPTY
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getSlotLimit(slot: Int): Int {
|
||||||
|
return parent?.getSlotLimit(slot) ?: 0
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isItemValid(slot: Int, stack: ItemStack): Boolean {
|
||||||
|
return parent?.isItemValid(slot, stack) ?: false
|
||||||
|
}
|
||||||
|
}
|
@ -12,6 +12,14 @@ class ProxiedEnergyStorage<T : IMatteryEnergyStorage>(var parent: T? = null) : I
|
|||||||
return parent?.receiveEnergy(howMuch, simulate) ?: Decimal.ZERO
|
return parent?.receiveEnergy(howMuch, simulate) ?: Decimal.ZERO
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun extractEnergyChecked(howMuch: Decimal, simulate: Boolean): Decimal {
|
||||||
|
return parent?.extractEnergyChecked(howMuch, simulate) ?: Decimal.ZERO
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun receiveEnergyChecked(howMuch: Decimal, simulate: Boolean): Decimal {
|
||||||
|
return parent?.receiveEnergyChecked(howMuch, simulate) ?: Decimal.ZERO
|
||||||
|
}
|
||||||
|
|
||||||
override var batteryLevel: Decimal
|
override var batteryLevel: Decimal
|
||||||
get() = parent?.batteryLevel ?: Decimal.ZERO
|
get() = parent?.batteryLevel ?: Decimal.ZERO
|
||||||
set(value) { parent?.batteryLevel = value }
|
set(value) { parent?.batteryLevel = value }
|
||||||
|
@ -25,7 +25,7 @@ object MatterBottlerProvider : IBlockComponentProvider, IServerDataProvider<Bloc
|
|||||||
val bottlerData = CompoundTag()
|
val bottlerData = CompoundTag()
|
||||||
bottlerData.putBoolean("isBottling", bottler.isBottling)
|
bottlerData.putBoolean("isBottling", bottler.isBottling)
|
||||||
bottlerData.putBoolean("isIdling", accessor.blockState.getValue(WorkerState.SEMI_WORKER_STATE) !== WorkerState.WORKING)
|
bottlerData.putBoolean("isIdling", accessor.blockState.getValue(WorkerState.SEMI_WORKER_STATE) !== WorkerState.WORKING)
|
||||||
bottlerData.putFloat("workProgress", bottler.getWorkProgress())
|
bottlerData.putFloat("workProgress", bottler.workProgress)
|
||||||
data.put(JadeTagKeys.MATTER_BOTTLER_DATA, bottlerData)
|
data.put(JadeTagKeys.MATTER_BOTTLER_DATA, bottlerData)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import net.minecraft.world.Container
|
|||||||
import net.minecraft.world.item.ItemStack
|
import net.minecraft.world.item.ItemStack
|
||||||
import ru.dbotthepony.mc.otm.core.isNotEmpty
|
import ru.dbotthepony.mc.otm.core.isNotEmpty
|
||||||
|
|
||||||
class ContainerIterator(private val container: Container) : MutableIterator<ItemStack> {
|
class ContainerIterator(private val container: Container) : IContainerIterator {
|
||||||
private var index = 0
|
private var index = 0
|
||||||
private var lastIndex = -1
|
private var lastIndex = -1
|
||||||
|
|
||||||
@ -37,6 +37,10 @@ class ContainerIterator(private val container: Container) : MutableIterator<Item
|
|||||||
container[lastIndex] = ItemStack.EMPTY
|
container[lastIndex] = ItemStack.EMPTY
|
||||||
lastIndex = -1
|
lastIndex = -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun setChanged() {
|
||||||
|
container.setChanged()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Container.iterator() = ContainerIterator(this)
|
fun Container.iterator() = ContainerIterator(this)
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
package ru.dbotthepony.mc.otm.container
|
||||||
|
|
||||||
|
import net.minecraft.world.item.ItemStack
|
||||||
|
|
||||||
|
interface IContainerIterator : MutableIterator<ItemStack> {
|
||||||
|
/**
|
||||||
|
* Notifies underlying container that last returned [ItemStack] was modified
|
||||||
|
*
|
||||||
|
* @throws NoSuchElementException
|
||||||
|
*/
|
||||||
|
fun setChanged()
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package ru.dbotthepony.mc.otm.menu.matter
|
package ru.dbotthepony.mc.otm.menu.matter
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableList
|
||||||
import net.minecraft.world.SimpleContainer
|
import net.minecraft.world.SimpleContainer
|
||||||
import net.minecraft.world.entity.player.Inventory
|
import net.minecraft.world.entity.player.Inventory
|
||||||
import net.minecraft.world.item.ItemStack
|
import net.minecraft.world.item.ItemStack
|
||||||
@ -9,12 +10,14 @@ import ru.dbotthepony.mc.otm.capability.MatteryCapability
|
|||||||
import ru.dbotthepony.mc.otm.capability.matter.ProfiledMatterStorage
|
import ru.dbotthepony.mc.otm.capability.matter.ProfiledMatterStorage
|
||||||
import ru.dbotthepony.mc.otm.capability.matter.canExtractMatter
|
import ru.dbotthepony.mc.otm.capability.matter.canExtractMatter
|
||||||
import ru.dbotthepony.mc.otm.capability.matter.canReceiveMatter
|
import ru.dbotthepony.mc.otm.capability.matter.canReceiveMatter
|
||||||
|
import ru.dbotthepony.mc.otm.container.CombinedContainer
|
||||||
import ru.dbotthepony.mc.otm.menu.input.BooleanInputWithFeedback
|
import ru.dbotthepony.mc.otm.menu.input.BooleanInputWithFeedback
|
||||||
import ru.dbotthepony.mc.otm.menu.widget.LevelGaugeWidget
|
import ru.dbotthepony.mc.otm.menu.widget.LevelGaugeWidget
|
||||||
import ru.dbotthepony.mc.otm.menu.widget.ProgressGaugeWidget
|
import ru.dbotthepony.mc.otm.menu.widget.ProgressGaugeWidget
|
||||||
import ru.dbotthepony.mc.otm.core.orNull
|
import ru.dbotthepony.mc.otm.core.orNull
|
||||||
import ru.dbotthepony.mc.otm.menu.MatteryPoweredMenu
|
import ru.dbotthepony.mc.otm.menu.MatteryPoweredMenu
|
||||||
import ru.dbotthepony.mc.otm.menu.MatterySlot
|
import ru.dbotthepony.mc.otm.menu.MatterySlot
|
||||||
|
import ru.dbotthepony.mc.otm.menu.makeSlots
|
||||||
import ru.dbotthepony.mc.otm.menu.widget.ProfiledLevelGaugeWidget
|
import ru.dbotthepony.mc.otm.menu.widget.ProfiledLevelGaugeWidget
|
||||||
import ru.dbotthepony.mc.otm.registry.MMenus
|
import ru.dbotthepony.mc.otm.registry.MMenus
|
||||||
|
|
||||||
@ -22,38 +25,33 @@ class MatterBottlerMenu @JvmOverloads constructor(
|
|||||||
p_38852_: Int,
|
p_38852_: Int,
|
||||||
inventory: Inventory,
|
inventory: Inventory,
|
||||||
tile: MatterBottlerBlockEntity? = null
|
tile: MatterBottlerBlockEntity? = null
|
||||||
) : MatteryPoweredMenu(
|
) : MatteryPoweredMenu(MMenus.MATTER_BOTTLER, p_38852_, inventory, tile) {
|
||||||
MMenus.MATTER_BOTTLER, p_38852_, inventory, tile
|
|
||||||
) {
|
|
||||||
val workFlow = BooleanInputWithFeedback(this)
|
val workFlow = BooleanInputWithFeedback(this)
|
||||||
val progressWidget = ProgressGaugeWidget(this)
|
val progressWidget = ProgressGaugeWidget(this)
|
||||||
val matterWidget = ProfiledLevelGaugeWidget(this, tile?.matter, LevelGaugeWidget(this, tile?.matter))
|
val matterWidget = ProfiledLevelGaugeWidget(this, tile?.matter, LevelGaugeWidget(this, tile?.matter))
|
||||||
val storageSlots: List<MatterySlot>
|
|
||||||
|
val storageSlots: ImmutableList<MatterySlot> = makeSlots(CombinedContainer(tile?.bottling ?: SimpleContainer(3), tile?.unbottling ?: SimpleContainer(3))) { it, index ->
|
||||||
|
object : MatterySlot(it, index) {
|
||||||
|
override fun mayPlace(itemStack: ItemStack): Boolean {
|
||||||
|
val cap = itemStack.getCapability(MatteryCapability.MATTER).orNull() ?: return false
|
||||||
|
|
||||||
|
if (workFlow.value) {
|
||||||
|
return index < 3 && cap.canReceiveMatter
|
||||||
|
} else {
|
||||||
|
return index >= 3 && cap.canExtractMatter
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.also(this::addStorageSlot)
|
||||||
|
}
|
||||||
|
|
||||||
val profiledEnergy = ProfiledLevelGaugeWidget(this, tile?.energy, energyWidget)
|
val profiledEnergy = ProfiledLevelGaugeWidget(this, tile?.energy, energyWidget)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
val container = tile?.container ?: SimpleContainer(6)
|
|
||||||
|
|
||||||
if (tile != null) {
|
if (tile != null) {
|
||||||
progressWidget.with(tile::getWorkProgress)
|
progressWidget.with(tile::workProgress)
|
||||||
workFlow.with(tile::isBottling)
|
workFlow.with(tile::isBottling)
|
||||||
}
|
}
|
||||||
|
|
||||||
storageSlots = immutableList(6) { index ->
|
|
||||||
object : MatterySlot(container, index) {
|
|
||||||
override fun mayPlace(itemStack: ItemStack): Boolean {
|
|
||||||
val cap = itemStack.getCapability(MatteryCapability.MATTER).orNull() ?: return false
|
|
||||||
|
|
||||||
if (workFlow.value) {
|
|
||||||
return index < 3 && cap.canReceiveMatter
|
|
||||||
} else {
|
|
||||||
return index >= 3 && cap.canExtractMatter
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
storageSlots.forEach(this::addStorageSlot)
|
|
||||||
addInventorySlots()
|
addInventorySlots()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user