Division free energy distribution in battery bank
This commit is contained in:
parent
9b92221221
commit
9b270b6891
@ -7,16 +7,13 @@ import net.minecraft.world.inventory.AbstractContainerMenu
|
|||||||
import net.minecraft.world.item.ItemStack
|
import net.minecraft.world.item.ItemStack
|
||||||
import net.minecraft.world.level.block.state.BlockState
|
import net.minecraft.world.level.block.state.BlockState
|
||||||
import net.minecraftforge.common.capabilities.ForgeCapabilities
|
import net.minecraftforge.common.capabilities.ForgeCapabilities
|
||||||
import net.minecraftforge.energy.IEnergyStorage
|
|
||||||
import ru.dbotthepony.mc.otm.block.entity.MatteryDeviceBlockEntity
|
import ru.dbotthepony.mc.otm.block.entity.MatteryDeviceBlockEntity
|
||||||
import ru.dbotthepony.mc.otm.capability.FlowDirection
|
import ru.dbotthepony.mc.otm.capability.FlowDirection
|
||||||
import ru.dbotthepony.mc.otm.capability.energy
|
import ru.dbotthepony.mc.otm.capability.energy
|
||||||
import ru.dbotthepony.mc.otm.capability.energy.IMatteryEnergyStorage
|
import ru.dbotthepony.mc.otm.capability.energy.IMatteryEnergyStorage
|
||||||
import ru.dbotthepony.mc.otm.capability.energyStoredMattery
|
import ru.dbotthepony.mc.otm.capability.energyStoredMattery
|
||||||
import ru.dbotthepony.mc.otm.capability.extractEnergy
|
|
||||||
import ru.dbotthepony.mc.otm.capability.matteryEnergy
|
import ru.dbotthepony.mc.otm.capability.matteryEnergy
|
||||||
import ru.dbotthepony.mc.otm.capability.maxEnergyStoredMattery
|
import ru.dbotthepony.mc.otm.capability.maxEnergyStoredMattery
|
||||||
import ru.dbotthepony.mc.otm.capability.receiveEnergy
|
|
||||||
import ru.dbotthepony.mc.otm.capability.transcieveEnergy
|
import ru.dbotthepony.mc.otm.capability.transcieveEnergy
|
||||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||||
@ -53,94 +50,56 @@ class BatteryBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte
|
|||||||
bottomDefault = ItemHandlerMode.INPUT_OUTPUT,
|
bottomDefault = ItemHandlerMode.INPUT_OUTPUT,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
private var currentChangeSlot = 0
|
||||||
|
private var currentDischangeSlot = 0
|
||||||
|
|
||||||
init {
|
init {
|
||||||
savetable(::container, INVENTORY_KEY)
|
savetable(::container, INVENTORY_KEY)
|
||||||
|
savetables.int(::currentChangeSlot)
|
||||||
|
savetables.int(::currentDischangeSlot)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun distributeEnergy(isReceiving: Boolean, howMuch: Decimal, simulate: Boolean): Decimal {
|
private fun distributeEnergy(isReceiving: Boolean, howMuch: Decimal, simulate: Boolean): Decimal {
|
||||||
if (!howMuch.isPositive)
|
if (!howMuch.isPositive)
|
||||||
return Decimal.ZERO
|
return Decimal.ZERO
|
||||||
|
|
||||||
val energies = ArrayList<IEnergyStorage>()
|
var summ = Decimal.ZERO
|
||||||
|
var remaining = howMuch
|
||||||
|
var currentSlot = if (isReceiving) currentChangeSlot else currentDischangeSlot
|
||||||
|
|
||||||
|
for (i in 0 until container.containerSize - 1) {
|
||||||
|
val item = container[currentSlot]
|
||||||
|
|
||||||
for (item in container) {
|
|
||||||
if (!item.isEmpty) {
|
if (!item.isEmpty) {
|
||||||
val energy = item.matteryEnergy ?: item.energy
|
val energy = item.matteryEnergy ?: item.energy
|
||||||
|
|
||||||
if (energy != null) {
|
if (energy != null) {
|
||||||
energies.add(energy)
|
val diff = energy.transcieveEnergy(remaining, isReceiving, simulate)
|
||||||
|
summ += diff
|
||||||
|
remaining -= diff
|
||||||
|
|
||||||
|
if (!remaining.isPositive) {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentSlot = (currentSlot + 1) % container.containerSize
|
||||||
}
|
}
|
||||||
|
|
||||||
if (energies.isEmpty())
|
if (!simulate && !summ.isZero) {
|
||||||
return Decimal.ZERO
|
setChangedLight()
|
||||||
|
gaugeLevel = (batteryLevel / maxBatteryLevel).toFloat()
|
||||||
if (howMuch.toInt() < energies.size || !energies.any { it is IMatteryEnergyStorage && it.transcieveEnergy(Decimal.ONE, isReceiving, true) == Decimal.ONE }) {
|
|
||||||
// sequential
|
|
||||||
var summ = Decimal.ZERO
|
|
||||||
var remaining = howMuch
|
|
||||||
|
|
||||||
for (energy in energies) {
|
|
||||||
val diff = energy.transcieveEnergy(remaining, isReceiving, simulate)
|
|
||||||
summ += diff
|
|
||||||
remaining -= diff
|
|
||||||
|
|
||||||
if (!remaining.isPositive) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!simulate && !summ.isZero) {
|
|
||||||
setChangedLight()
|
|
||||||
gaugeLevel = (batteryLevel / maxBatteryLevel).toFloat()
|
|
||||||
}
|
|
||||||
|
|
||||||
return summ
|
|
||||||
} else {
|
|
||||||
val distribution = Array(energies.size) { Decimal.ZERO }
|
|
||||||
var summ2 = Decimal.ZERO
|
|
||||||
|
|
||||||
for ((i, energy) in energies.withIndex()) {
|
|
||||||
val diff = energy.transcieveEnergy(howMuch, isReceiving, true)
|
|
||||||
distribution[i] = diff
|
|
||||||
summ2 += distribution[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
if (summ2.isPositive) {
|
|
||||||
for (i in distribution.indices) {
|
|
||||||
distribution[i] = distribution[i] / summ2
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Decimal.ZERO
|
|
||||||
}
|
|
||||||
|
|
||||||
var summ = Decimal.ZERO
|
|
||||||
var remaining = howMuch
|
|
||||||
|
|
||||||
for ((i, energy) in energies.withIndex()) {
|
|
||||||
val diff = energy.transcieveEnergy((howMuch * distribution[i]).coerceAtMost(remaining), isReceiving, simulate)
|
|
||||||
summ += diff
|
|
||||||
remaining -= diff
|
|
||||||
|
|
||||||
if (!remaining.isPositive) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (remaining.isPositive) {
|
|
||||||
val diff = energies.first { it is IMatteryEnergyStorage }.transcieveEnergy(remaining, isReceiving, simulate)
|
|
||||||
summ += diff
|
|
||||||
remaining -= diff
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!simulate && !summ.isZero) {
|
|
||||||
setChangedLight()
|
|
||||||
gaugeLevel = (batteryLevel / maxBatteryLevel).toFloat()
|
|
||||||
}
|
|
||||||
|
|
||||||
return summ
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!simulate) {
|
||||||
|
if (isReceiving)
|
||||||
|
this.currentChangeSlot = currentSlot
|
||||||
|
else
|
||||||
|
this.currentDischangeSlot = currentSlot
|
||||||
|
}
|
||||||
|
|
||||||
|
return summ
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun extractEnergy(howMuch: Decimal, simulate: Boolean): Decimal {
|
override fun extractEnergy(howMuch: Decimal, simulate: Boolean): Decimal {
|
||||||
@ -151,6 +110,12 @@ class BatteryBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte
|
|||||||
return distributeEnergy(isReceiving = true, howMuch, simulate)
|
return distributeEnergy(isReceiving = true, howMuch, simulate)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun tick() {
|
||||||
|
super.tick()
|
||||||
|
currentChangeSlot = (currentChangeSlot + 1) % container.containerSize
|
||||||
|
currentDischangeSlot = (currentDischangeSlot + 1) % container.containerSize
|
||||||
|
}
|
||||||
|
|
||||||
override val canSetBatteryLevel: Boolean
|
override val canSetBatteryLevel: Boolean
|
||||||
get() = false
|
get() = false
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user