Update energy impls to make more use of infinity

This commit is contained in:
DBotThePony 2023-12-29 10:52:01 +07:00
parent 2276f0ef45
commit 6de2f14fcd
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 3 additions and 28 deletions

View File

@ -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) {

View File

@ -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) {