Optimize battery bank

This commit is contained in:
DBotThePony 2025-03-26 22:46:12 +07:00
parent 39a996f79f
commit 4d51ed5210
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -1,5 +1,6 @@
package ru.dbotthepony.mc.otm.block.entity.tech package ru.dbotthepony.mc.otm.block.entity.tech
import it.unimi.dsi.fastutil.ints.IntArrayList
import net.minecraft.core.BlockPos import net.minecraft.core.BlockPos
import net.minecraft.world.entity.player.Inventory import net.minecraft.world.entity.player.Inventory
import net.minecraft.world.entity.player.Player import net.minecraft.world.entity.player.Player
@ -12,6 +13,7 @@ import ru.dbotthepony.kommons.util.setValue
import ru.dbotthepony.kommons.util.value import ru.dbotthepony.kommons.util.value
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.MatteryCapability
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.energy.ProfiledEnergyStorage import ru.dbotthepony.mc.otm.capability.energy.ProfiledEnergyStorage
@ -19,10 +21,12 @@ import ru.dbotthepony.mc.otm.capability.energyStoredMattery
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.transcieveEnergy import ru.dbotthepony.mc.otm.capability.transcieveEnergy
import ru.dbotthepony.mc.otm.container.slotRange
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
import ru.dbotthepony.mc.otm.container.slotted.FilteredContainerSlot import ru.dbotthepony.mc.otm.container.slotted.FilteredContainerSlot
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
import ru.dbotthepony.mc.otm.core.immutableList import ru.dbotthepony.mc.otm.core.immutableList
import ru.dbotthepony.mc.otm.core.isNotEmpty
import ru.dbotthepony.mc.otm.core.math.Decimal import ru.dbotthepony.mc.otm.core.math.Decimal
import ru.dbotthepony.mc.otm.core.otmRandom import ru.dbotthepony.mc.otm.core.otmRandom
import ru.dbotthepony.mc.otm.core.shuffle import ru.dbotthepony.mc.otm.core.shuffle
@ -34,6 +38,8 @@ class BatteryBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte
var gaugeLevel by syncher.float() var gaugeLevel by syncher.float()
private set private set
private var containerSlotIndices = IntArray(0)
private inner class Slot(container: SlottedContainer, slot: Int) : FilteredContainerSlot(container, slot) { private inner class Slot(container: SlottedContainer, slot: Int) : FilteredContainerSlot(container, slot) {
override fun canAutomationPlaceItem(itemStack: ItemStack): Boolean { override fun canAutomationPlaceItem(itemStack: ItemStack): Boolean {
return super.canAutomationPlaceItem(itemStack) && itemStack.getCapability(Capabilities.EnergyStorage.ITEM)?.let { it.canExtract() && it.extractEnergy(Int.MAX_VALUE, true) > 0 } == true return super.canAutomationPlaceItem(itemStack) && itemStack.getCapability(Capabilities.EnergyStorage.ITEM)?.let { it.canExtract() && it.extractEnergy(Int.MAX_VALUE, true) > 0 } == true
@ -47,15 +53,30 @@ class BatteryBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte
super.notifyChanged(old) super.notifyChanged(old)
batteryStatus[slot].value = item.getCapability(Capabilities.EnergyStorage.ITEM) != null batteryStatus[slot].value = item.getCapability(Capabilities.EnergyStorage.ITEM) != null
gaugeLevel = batteryLevel.percentage(maxBatteryLevel) updateGaugeLevel()
} }
override val maxStackSize: Int override val maxStackSize: Int
get() = 1 get() = 1
} }
private fun containerUpdated() {
markDirtyFast()
val newSlots = IntArrayList()
for (i in 0 until container.containerSize) {
val stack = container[i]
if (stack.isNotEmpty && stack.energy != null)
newSlots.add(i)
}
containerSlotIndices = newSlots.toIntArray()
}
// 6 на 2 // 6 на 2
val container = SlottedContainer.simple(CAPACITY, ::Slot, ::markDirtyFast).also(::addDroppableContainer) val container = SlottedContainer.simple(CAPACITY, ::Slot, ::containerUpdated).also(::addDroppableContainer)
val batteryStatus = immutableList(CAPACITY) { syncher.boolean(false) } val batteryStatus = immutableList(CAPACITY) { syncher.boolean(false) }
val itemConfig = ConfigurableItemHandler(inputOutput = container) val itemConfig = ConfigurableItemHandler(inputOutput = container)
@ -64,10 +85,26 @@ class BatteryBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte
savetables.stateful(::container, INVENTORY_KEY) savetables.stateful(::container, INVENTORY_KEY)
} }
private val containerSlotIndices = IntArray(CAPACITY) { it } private fun updateGaugeLevel() {
var stored = Decimal.ZERO
var maxStored = Decimal.ZERO
for (stack in container) {
val cap = stack.energy ?: continue
stored += cap.energyStoredMattery
maxStored += cap.maxEnergyStoredMattery
}
gaugeLevel = stored.percentage(maxStored)
}
override fun tick() {
super.tick()
updateGaugeLevel()
}
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 || containerSlotIndices.isEmpty())
return Decimal.ZERO return Decimal.ZERO
containerSlotIndices.shuffle(level!!.otmRandom) containerSlotIndices.shuffle(level!!.otmRandom)
@ -94,7 +131,6 @@ class BatteryBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte
if (!simulate && !summ.isZero) { if (!simulate && !summ.isZero) {
markDirtyFast() markDirtyFast()
gaugeLevel = batteryLevel.percentage(maxBatteryLevel)
} }
return summ return summ
@ -118,15 +154,11 @@ class BatteryBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte
get() { get() {
var result = Decimal.ZERO var result = Decimal.ZERO
for (i in 0 until container.containerSize) { for (stack in container) {
val stack = container.getItem(i)
if (!stack.isEmpty) {
stack.energy?.let { stack.energy?.let {
result += it.energyStoredMattery result += it.energyStoredMattery
} }
} }
}
return result return result
} }
@ -138,15 +170,11 @@ class BatteryBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte
get() { get() {
var result = Decimal.ZERO var result = Decimal.ZERO
for (i in 0 until container.containerSize) { for (stack in container) {
val stack = container.getItem(i)
if (!stack.isEmpty) {
stack.energy?.let { stack.energy?.let {
result += it.maxEnergyStoredMattery result += it.maxEnergyStoredMattery
} }
} }
}
return result return result
} }