From 3e56730191374843fda925e992d0de930aad4756 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 14 Feb 2025 21:27:47 +0700 Subject: [PATCH] Select random slots when distributing energy in battery bank --- .../entity/tech/BatteryBankBlockEntity.kt | 44 +++---------------- 1 file changed, 5 insertions(+), 39 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/BatteryBankBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/BatteryBankBlockEntity.kt index 3e2fb6c7a..d6acd5c05 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/BatteryBankBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/BatteryBankBlockEntity.kt @@ -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