From 6de2f14fcd45a0c52aa8e16b971e084954126fe8 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 29 Dec 2023 10:52:01 +0700 Subject: [PATCH] Update energy impls to make more use of infinity --- .../energy/BlockEnergyStorageImpl.kt | 20 ++----------------- .../energy/ItemEnergyStorageImpl.kt | 11 +--------- 2 files changed, 3 insertions(+), 28 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/BlockEnergyStorageImpl.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/BlockEnergyStorageImpl.kt index fe1dccd81..cc7a7ed7e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/BlockEnergyStorageImpl.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/BlockEnergyStorageImpl.kt @@ -72,18 +72,10 @@ sealed class BlockEnergyStorageImpl( if (!howMuch.isPositive) return Decimal.ZERO - @Suppress("NAME_SHADOWING") - var howMuch = howMuch - val maxOutput = maxOutput - - if (maxOutput != null) { - howMuch = howMuch.coerceAtMost(maxOutput) - } - if (!batteryLevel.isPositive) return Decimal.ZERO - val newLevel = (batteryLevel - howMuch).moreThanZero() + val newLevel = (batteryLevel - howMuch.coerceAtMost(maxOutput ?: Decimal.POSITIVE_INFINITY)).moreThanZero() val diff = (batteryLevel - newLevel) if (!simulate && batteryLevel != newLevel) { @@ -97,18 +89,10 @@ sealed class BlockEnergyStorageImpl( if (!howMuch.isPositive) return Decimal.ZERO - @Suppress("NAME_SHADOWING") - var howMuch = howMuch - val maxInput = maxInput - - if (maxInput != null) { - howMuch = howMuch.coerceAtMost(maxInput) - } - if (batteryLevel >= maxBatteryLevel && !maxBatteryLevel.isInfinite) return Decimal.ZERO - val newLevel = (batteryLevel + howMuch).coerceAtMost(maxBatteryLevel) + val newLevel = (batteryLevel + howMuch.coerceAtMost(maxInput ?: Decimal.POSITIVE_INFINITY)).coerceAtMost(maxBatteryLevel) val diff = (newLevel - batteryLevel) if (!simulate && batteryLevel != newLevel) { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/ItemEnergyStorageImpl.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/ItemEnergyStorageImpl.kt index b3f29cb4a..2867fabd4 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/ItemEnergyStorageImpl.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/ItemEnergyStorageImpl.kt @@ -65,20 +65,11 @@ abstract class ItemEnergyStorageImpl(val itemStack: ItemStack) : IMatteryEnergyS if (!howMuch.isPositive || itemStack.count != 1) return Decimal.ZERO - @Suppress("NAME_SHADOWING") - var howMuch = howMuch - val maxInput = maxInput - - if (maxInput != null) { - howMuch = howMuch.coerceAtMost(maxInput) - } - val batteryLevel = batteryLevel - if (batteryLevel >= maxBatteryLevel && !maxBatteryLevel.isInfinite) return Decimal.ZERO - val newLevel = (batteryLevel + howMuch).coerceAtMost(maxBatteryLevel) + val newLevel = (batteryLevel + howMuch.coerceAtMost(maxInput ?: Decimal.POSITIVE_INFINITY)).coerceAtMost(maxBatteryLevel) val diff = (newLevel - batteryLevel) if (!simulate && batteryLevel != newLevel) {