From 750f94152586f868a59e02c87dcb6d2accf228a7 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 26 Mar 2025 22:38:02 +0700 Subject: [PATCH] Optimize matter capacitor bank --- .../matter/MatterCapacitorBankBlockEntity.kt | 69 +++++++++---------- 1 file changed, 33 insertions(+), 36 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterCapacitorBankBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterCapacitorBankBlockEntity.kt index 60f2383a9..96f2b418d 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterCapacitorBankBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterCapacitorBankBlockEntity.kt @@ -39,10 +39,9 @@ class MatterCapacitorBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) var summ = Decimal.ZERO for (stack in container) - if (!stack.isEmpty) - stack.getCapability(MatteryCapability.MATTER_ITEM)?.let { - summ += it.storedMatter - } + stack.getCapability(MatteryCapability.MATTER_ITEM)?.let { + summ += it.storedMatter + } return summ } @@ -50,19 +49,31 @@ class MatterCapacitorBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) throw UnsupportedOperationException() } - override val maxStoredMatter: Decimal - get() { + override val maxStoredMatter: Decimal get() { var summ = Decimal.ZERO for (stack in container) - if (!stack.isEmpty) - stack.getCapability(MatteryCapability.MATTER_ITEM)?.let { - summ += it.maxStoredMatter - } + stack.getCapability(MatteryCapability.MATTER_ITEM)?.let { + summ += it.maxStoredMatter + } 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 { if (!howMuch.isPositive) return Decimal.ZERO @@ -72,23 +83,16 @@ class MatterCapacitorBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) var summ = Decimal.ZERO for (stack in container) { - if (!stack.isEmpty) { - stack.getCapability(MatteryCapability.MATTER_ITEM)?.let { - val diff = it.receiveMatterChecked(howMuch, simulate) - summ += diff - howMuch -= diff - } + val it = stack.getCapability(MatteryCapability.MATTER_ITEM) ?: continue + val diff = it.receiveMatterChecked(howMuch, simulate) + summ += diff + howMuch -= diff - if (howMuch.isZero) { - break - } + if (howMuch.isZero) { + break } } - if (summ.isPositive && !simulate) { - gaugeLevel = storedMatter.percentage(maxStoredMatter) - } - return summ } @@ -101,23 +105,16 @@ class MatterCapacitorBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) var summ = Decimal.ZERO for (stack in container) { - if (!stack.isEmpty) { - stack.getCapability(MatteryCapability.MATTER_ITEM)?.let { - val diff = it.extractMatterChecked(howMuch, simulate) - summ += diff - howMuch -= diff - } + val it = stack.getCapability(MatteryCapability.MATTER_ITEM) ?: continue + val diff = it.extractMatterChecked(howMuch, simulate) + summ += diff + howMuch -= diff - if (howMuch.isZero) { - break - } + if (howMuch.isZero) { + break } } - if (summ.isPositive && !simulate) { - gaugeLevel = storedMatter.percentage(maxStoredMatter) - } - return summ }