From a1515d54c4fdd1aa015596b788d1abe29f54c3ef Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 28 Dec 2023 20:37:18 +0700 Subject: [PATCH] Introduce infinity to energy impl, move creative battery to plain infinite values instead of special logic --- .../energy/BlockEnergyStorageImpl.kt | 2 +- .../energy/ItemEnergyStorageImpl.kt | 2 +- .../dbotthepony/mc/otm/core/math/Decimal.kt | 6 ++ .../ru/dbotthepony/mc/otm/item/BatteryItem.kt | 60 +++---------------- 4 files changed, 17 insertions(+), 53 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 150977628..fe1dccd81 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 @@ -105,7 +105,7 @@ sealed class BlockEnergyStorageImpl( howMuch = howMuch.coerceAtMost(maxInput) } - if (batteryLevel >= maxBatteryLevel) + if (batteryLevel >= maxBatteryLevel && !maxBatteryLevel.isInfinite) return Decimal.ZERO val newLevel = (batteryLevel + howMuch).coerceAtMost(maxBatteryLevel) 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 ac71aa330..b3f29cb4a 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 @@ -75,7 +75,7 @@ abstract class ItemEnergyStorageImpl(val itemStack: ItemStack) : IMatteryEnergyS val batteryLevel = batteryLevel - if (batteryLevel >= maxBatteryLevel) + if (batteryLevel >= maxBatteryLevel && !maxBatteryLevel.isInfinite) return Decimal.ZERO val newLevel = (batteryLevel + howMuch).coerceAtMost(maxBatteryLevel) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt index 1df81a457..2147a53ab 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt @@ -737,10 +737,16 @@ sealed class Decimal : Number(), Comparable { get() = throw UnsupportedOperationException("Attempt to get fractional part of positive infinity") override fun plus(other: Decimal): Decimal { + // not very mathematically correct, since + // case "infinity + infinity" with either sign on either side + // is undefined return this } override fun minus(other: Decimal): Decimal { + // not very mathematically correct, since + // case "infinity - infinity" with either sign on either side + // is undefined return this } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/BatteryItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/BatteryItem.kt index c557d3daa..4115d724e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/BatteryItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/BatteryItem.kt @@ -30,33 +30,6 @@ import ru.dbotthepony.mc.otm.runIfClient import kotlin.math.roundToInt open class BatteryItem : Item { - private inner class Power(stack: ItemStack) : EnergyCapacitorItem(stack, this@BatteryItem.capacity, this@BatteryItem.receive, this@BatteryItem.extract, initialBatteryLevel = this@BatteryItem.initialBatteryLevel) { - override fun extractEnergy(howMuch: Decimal, simulate: Boolean): Decimal { - if (isCreative) return howMuch - return super.extractEnergy(howMuch, simulate) - } - - override fun receiveEnergy(howMuch: Decimal, simulate: Boolean): Decimal { - if (isCreative) return howMuch - return super.receiveEnergy(howMuch, simulate) - } - - override val missingPower: Decimal - get() { - return if (isCreative) Decimal.LONG_MAX_VALUE else super.missingPower - } - - fun maxPower() { - batteryLevel = maxBatteryLevel - } - - override var batteryLevel: Decimal - get() { return if (isCreative) Decimal.LONG_MAX_VALUE else super.batteryLevel } - set(value) { super.batteryLevel = value } - } - - private val isCreative: Boolean - val _capacity: () -> Decimal val _receive: () -> Decimal val _extract: () -> Decimal @@ -77,7 +50,6 @@ open class BatteryItem : Item { extract: Decimal = receive, initialBatteryLevel: Decimal = Decimal.ZERO ) : super(Properties().stacksTo(1)) { - isCreative = false this._capacity = { storage } this._receive = { receive } this._extract = { extract } @@ -90,7 +62,6 @@ open class BatteryItem : Item { extract: () -> Decimal = receive, initialBatteryLevel: () -> Decimal = { Decimal.ZERO } ) : super(Properties().stacksTo(1)) { - isCreative = false this._capacity = storage this._receive = receive this._extract = extract @@ -98,7 +69,6 @@ open class BatteryItem : Item { } constructor(values: BatteryBalanceValues) : super(Properties().stacksTo(1)) { - isCreative = false this._capacity = values::energyCapacity this._receive = values::maxEnergyReceive this._extract = values::maxEnergyExtract @@ -106,29 +76,28 @@ open class BatteryItem : Item { } constructor() : super(Properties().stacksTo(1).rarity(Rarity.EPIC)) { - isCreative = true - _capacity = { Decimal.LONG_MAX_VALUE } - _receive = { Decimal.LONG_MAX_VALUE } - _extract = { Decimal.LONG_MAX_VALUE } - _initialBatteryLevel = { Decimal.LONG_MAX_VALUE } + _capacity = { Decimal.POSITIVE_INFINITY } + _receive = { Decimal.POSITIVE_INFINITY } + _extract = { Decimal.POSITIVE_INFINITY } + _initialBatteryLevel = { Decimal.POSITIVE_INFINITY } } override fun isBarVisible(p_150899_: ItemStack): Boolean { - if (isCreative) + if (_capacity.invoke().isInfinite) return false return p_150899_.matteryEnergy != null } override fun getBarWidth(p_150900_: ItemStack): Int { - if (isCreative) + if (_capacity.invoke().isInfinite) return 13 return p_150900_.matteryEnergy?.getBarWidth() ?: super.getBarWidth(p_150900_) } override fun getBarColor(p_150901_: ItemStack): Int { - if (isCreative) + if (_capacity.invoke().isInfinite) return 0 return p_150901_.matteryEnergy?.getBarColor() ?: super.getBarColor(p_150901_) @@ -141,22 +110,11 @@ open class BatteryItem : Item { p_41424_: TooltipFlag ) { super.appendHoverText(stack, p_41422_, p_41423_, p_41424_) - - if (isCreative) { - p_41423_.add(INFINITE_STORAGE) - p_41423_.add(TranslatableComponent("otm.item.power.infinite.throughput").withStyle(ChatFormatting.GRAY)) - } else { - ItemEnergyStorageImpl.appendHoverText(stack, p_41423_) - } + ItemEnergyStorageImpl.appendHoverText(stack, p_41423_) } override fun initCapabilities(stack: ItemStack, nbt: CompoundTag?): ICapabilityProvider { - return Power(stack) - } - - companion object { - private val INFINITE_STORAGE: Component = - TranslatableComponent("otm.item.power.infinite.storage").withStyle(ChatFormatting.GRAY) + return EnergyCapacitorItem(stack, this@BatteryItem.capacity, this@BatteryItem.receive, this@BatteryItem.extract, initialBatteryLevel = this@BatteryItem.initialBatteryLevel) } }