Optimize matter capacitor bank

This commit is contained in:
DBotThePony 2025-03-26 22:38:02 +07:00
parent 7405989b97
commit 750f941525
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -39,7 +39,6 @@ class MatterCapacitorBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState)
var summ = Decimal.ZERO var summ = Decimal.ZERO
for (stack in container) for (stack in container)
if (!stack.isEmpty)
stack.getCapability(MatteryCapability.MATTER_ITEM)?.let { stack.getCapability(MatteryCapability.MATTER_ITEM)?.let {
summ += it.storedMatter summ += it.storedMatter
} }
@ -50,12 +49,10 @@ class MatterCapacitorBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState)
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
override val maxStoredMatter: Decimal override val maxStoredMatter: Decimal get() {
get() {
var summ = Decimal.ZERO var summ = Decimal.ZERO
for (stack in container) for (stack in container)
if (!stack.isEmpty)
stack.getCapability(MatteryCapability.MATTER_ITEM)?.let { stack.getCapability(MatteryCapability.MATTER_ITEM)?.let {
summ += it.maxStoredMatter summ += it.maxStoredMatter
} }
@ -63,6 +60,20 @@ class MatterCapacitorBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState)
return summ return summ
} }
override fun tick() {
super.tick()
var stored = Decimal.ZERO
var maxStored = Decimal.ZERO
for (stack in container) {
val cap = stack.getCapability(MatteryCapability.MATTER_ITEM) ?: continue
stored += cap.storedMatter
maxStored += cap.maxStoredMatter
}
gaugeLevel = stored.percentage(maxStored)
}
override fun receiveMatter(howMuch: Decimal, simulate: Boolean): Decimal { override fun receiveMatter(howMuch: Decimal, simulate: Boolean): Decimal {
if (!howMuch.isPositive) if (!howMuch.isPositive)
return Decimal.ZERO return Decimal.ZERO
@ -72,22 +83,15 @@ class MatterCapacitorBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState)
var summ = Decimal.ZERO var summ = Decimal.ZERO
for (stack in container) { for (stack in container) {
if (!stack.isEmpty) { val it = stack.getCapability(MatteryCapability.MATTER_ITEM) ?: continue
stack.getCapability(MatteryCapability.MATTER_ITEM)?.let {
val diff = it.receiveMatterChecked(howMuch, simulate) val diff = it.receiveMatterChecked(howMuch, simulate)
summ += diff summ += diff
howMuch -= diff howMuch -= diff
}
if (howMuch.isZero) { if (howMuch.isZero) {
break break
} }
} }
}
if (summ.isPositive && !simulate) {
gaugeLevel = storedMatter.percentage(maxStoredMatter)
}
return summ return summ
} }
@ -101,22 +105,15 @@ class MatterCapacitorBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState)
var summ = Decimal.ZERO var summ = Decimal.ZERO
for (stack in container) { for (stack in container) {
if (!stack.isEmpty) { val it = stack.getCapability(MatteryCapability.MATTER_ITEM) ?: continue
stack.getCapability(MatteryCapability.MATTER_ITEM)?.let {
val diff = it.extractMatterChecked(howMuch, simulate) val diff = it.extractMatterChecked(howMuch, simulate)
summ += diff summ += diff
howMuch -= diff howMuch -= diff
}
if (howMuch.isZero) { if (howMuch.isZero) {
break break
} }
} }
}
if (summ.isPositive && !simulate) {
gaugeLevel = storedMatter.percentage(maxStoredMatter)
}
return summ return summ
} }