Select random slots when distributing energy in battery bank
This commit is contained in:
parent
1363efecd8
commit
3e56730191
@ -23,6 +23,7 @@ import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.core.immutableList
|
||||
import ru.dbotthepony.mc.otm.core.math.Decimal
|
||||
import ru.dbotthepony.mc.otm.core.shuffle
|
||||
import ru.dbotthepony.mc.otm.menu.tech.BatteryBankMenu
|
||||
import ru.dbotthepony.mc.otm.registry.game.MBlockEntities
|
||||
import java.util.function.Supplier
|
||||
@ -48,29 +49,21 @@ class BatteryBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte
|
||||
|
||||
val itemConfig = ConfigurableItemHandler(inputOutput = container.handler(HandlerFilter.Dischargeable))
|
||||
|
||||
private var currentChangeSlot = 0
|
||||
private var currentDischangeSlot = 0
|
||||
|
||||
private var lastTickCharged = false
|
||||
private var lastTickDischarged = false
|
||||
|
||||
init {
|
||||
savetables.stateful(::container, INVENTORY_KEY)
|
||||
savetables.int(::currentChangeSlot)
|
||||
savetables.int(::currentDischangeSlot)
|
||||
savetables.bool(::lastTickCharged)
|
||||
savetables.bool(::lastTickDischarged)
|
||||
}
|
||||
|
||||
private val containerSlotIndices = IntArray(CAPACITY) { it }
|
||||
|
||||
private fun distributeEnergy(isReceiving: Boolean, howMuch: Decimal, simulate: Boolean): Decimal {
|
||||
if (!howMuch.isPositive)
|
||||
return Decimal.ZERO
|
||||
|
||||
containerSlotIndices.shuffle(level!!.random)
|
||||
var summ = Decimal.ZERO
|
||||
var remaining = howMuch
|
||||
var currentSlot = if (isReceiving) currentChangeSlot else currentDischangeSlot
|
||||
|
||||
for (i in 0 until container.containerSize) {
|
||||
for (currentSlot in containerSlotIndices) {
|
||||
val item = container[currentSlot]
|
||||
|
||||
if (!item.isEmpty) {
|
||||
@ -86,8 +79,6 @@ class BatteryBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
currentSlot = (currentSlot + 1) % container.containerSize
|
||||
}
|
||||
|
||||
if (!simulate && !summ.isZero) {
|
||||
@ -95,18 +86,6 @@ class BatteryBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte
|
||||
gaugeLevel = batteryLevel.percentage(maxBatteryLevel)
|
||||
}
|
||||
|
||||
if (!simulate) {
|
||||
if (isReceiving) {
|
||||
this.lastTickCharged = true
|
||||
if (currentSlot == this.currentChangeSlot) currentSlot = (currentSlot + 1) % container.containerSize
|
||||
this.currentChangeSlot = currentSlot
|
||||
} else {
|
||||
this.lastTickDischarged = true
|
||||
if (currentSlot == this.currentDischangeSlot) currentSlot = (currentSlot + 1) % container.containerSize
|
||||
this.currentDischangeSlot = currentSlot
|
||||
}
|
||||
}
|
||||
|
||||
return summ
|
||||
}
|
||||
|
||||
@ -118,19 +97,6 @@ class BatteryBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte
|
||||
return distributeEnergy(isReceiving = true, howMuch, simulate)
|
||||
}
|
||||
|
||||
override fun tick() {
|
||||
super.tick()
|
||||
|
||||
if (!lastTickCharged)
|
||||
currentChangeSlot = (currentChangeSlot + 1) % container.containerSize
|
||||
|
||||
if (!lastTickDischarged)
|
||||
currentDischangeSlot = (currentDischangeSlot + 1) % container.containerSize
|
||||
|
||||
lastTickCharged = false
|
||||
lastTickDischarged = false
|
||||
}
|
||||
|
||||
override val canSetBatteryLevel: Boolean
|
||||
get() = false
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user