Introduce infinity to energy impl, move creative battery to plain infinite values instead of special logic

This commit is contained in:
DBotThePony 2023-12-28 20:37:18 +07:00
parent f13c725298
commit a1515d54c4
Signed by: DBot
GPG Key ID: DCC23B5715498507
4 changed files with 17 additions and 53 deletions

View File

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

View File

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

View File

@ -737,10 +737,16 @@ sealed class Decimal : Number(), Comparable<Decimal> {
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
}

View File

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