Introduce infinity to energy impl, move creative battery to plain infinite values instead of special logic
This commit is contained in:
parent
f13c725298
commit
a1515d54c4
@ -105,7 +105,7 @@ sealed class BlockEnergyStorageImpl(
|
|||||||
howMuch = howMuch.coerceAtMost(maxInput)
|
howMuch = howMuch.coerceAtMost(maxInput)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (batteryLevel >= maxBatteryLevel)
|
if (batteryLevel >= maxBatteryLevel && !maxBatteryLevel.isInfinite)
|
||||||
return Decimal.ZERO
|
return Decimal.ZERO
|
||||||
|
|
||||||
val newLevel = (batteryLevel + howMuch).coerceAtMost(maxBatteryLevel)
|
val newLevel = (batteryLevel + howMuch).coerceAtMost(maxBatteryLevel)
|
||||||
|
@ -75,7 +75,7 @@ abstract class ItemEnergyStorageImpl(val itemStack: ItemStack) : IMatteryEnergyS
|
|||||||
|
|
||||||
val batteryLevel = batteryLevel
|
val batteryLevel = batteryLevel
|
||||||
|
|
||||||
if (batteryLevel >= maxBatteryLevel)
|
if (batteryLevel >= maxBatteryLevel && !maxBatteryLevel.isInfinite)
|
||||||
return Decimal.ZERO
|
return Decimal.ZERO
|
||||||
|
|
||||||
val newLevel = (batteryLevel + howMuch).coerceAtMost(maxBatteryLevel)
|
val newLevel = (batteryLevel + howMuch).coerceAtMost(maxBatteryLevel)
|
||||||
|
@ -737,10 +737,16 @@ sealed class Decimal : Number(), Comparable<Decimal> {
|
|||||||
get() = throw UnsupportedOperationException("Attempt to get fractional part of positive infinity")
|
get() = throw UnsupportedOperationException("Attempt to get fractional part of positive infinity")
|
||||||
|
|
||||||
override fun plus(other: Decimal): Decimal {
|
override fun plus(other: Decimal): Decimal {
|
||||||
|
// not very mathematically correct, since
|
||||||
|
// case "infinity + infinity" with either sign on either side
|
||||||
|
// is undefined
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun minus(other: Decimal): Decimal {
|
override fun minus(other: Decimal): Decimal {
|
||||||
|
// not very mathematically correct, since
|
||||||
|
// case "infinity - infinity" with either sign on either side
|
||||||
|
// is undefined
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,33 +30,6 @@ import ru.dbotthepony.mc.otm.runIfClient
|
|||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
open class BatteryItem : Item {
|
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 _capacity: () -> Decimal
|
||||||
val _receive: () -> Decimal
|
val _receive: () -> Decimal
|
||||||
val _extract: () -> Decimal
|
val _extract: () -> Decimal
|
||||||
@ -77,7 +50,6 @@ open class BatteryItem : Item {
|
|||||||
extract: Decimal = receive,
|
extract: Decimal = receive,
|
||||||
initialBatteryLevel: Decimal = Decimal.ZERO
|
initialBatteryLevel: Decimal = Decimal.ZERO
|
||||||
) : super(Properties().stacksTo(1)) {
|
) : super(Properties().stacksTo(1)) {
|
||||||
isCreative = false
|
|
||||||
this._capacity = { storage }
|
this._capacity = { storage }
|
||||||
this._receive = { receive }
|
this._receive = { receive }
|
||||||
this._extract = { extract }
|
this._extract = { extract }
|
||||||
@ -90,7 +62,6 @@ open class BatteryItem : Item {
|
|||||||
extract: () -> Decimal = receive,
|
extract: () -> Decimal = receive,
|
||||||
initialBatteryLevel: () -> Decimal = { Decimal.ZERO }
|
initialBatteryLevel: () -> Decimal = { Decimal.ZERO }
|
||||||
) : super(Properties().stacksTo(1)) {
|
) : super(Properties().stacksTo(1)) {
|
||||||
isCreative = false
|
|
||||||
this._capacity = storage
|
this._capacity = storage
|
||||||
this._receive = receive
|
this._receive = receive
|
||||||
this._extract = extract
|
this._extract = extract
|
||||||
@ -98,7 +69,6 @@ open class BatteryItem : Item {
|
|||||||
}
|
}
|
||||||
|
|
||||||
constructor(values: BatteryBalanceValues) : super(Properties().stacksTo(1)) {
|
constructor(values: BatteryBalanceValues) : super(Properties().stacksTo(1)) {
|
||||||
isCreative = false
|
|
||||||
this._capacity = values::energyCapacity
|
this._capacity = values::energyCapacity
|
||||||
this._receive = values::maxEnergyReceive
|
this._receive = values::maxEnergyReceive
|
||||||
this._extract = values::maxEnergyExtract
|
this._extract = values::maxEnergyExtract
|
||||||
@ -106,29 +76,28 @@ open class BatteryItem : Item {
|
|||||||
}
|
}
|
||||||
|
|
||||||
constructor() : super(Properties().stacksTo(1).rarity(Rarity.EPIC)) {
|
constructor() : super(Properties().stacksTo(1).rarity(Rarity.EPIC)) {
|
||||||
isCreative = true
|
_capacity = { Decimal.POSITIVE_INFINITY }
|
||||||
_capacity = { Decimal.LONG_MAX_VALUE }
|
_receive = { Decimal.POSITIVE_INFINITY }
|
||||||
_receive = { Decimal.LONG_MAX_VALUE }
|
_extract = { Decimal.POSITIVE_INFINITY }
|
||||||
_extract = { Decimal.LONG_MAX_VALUE }
|
_initialBatteryLevel = { Decimal.POSITIVE_INFINITY }
|
||||||
_initialBatteryLevel = { Decimal.LONG_MAX_VALUE }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun isBarVisible(p_150899_: ItemStack): Boolean {
|
override fun isBarVisible(p_150899_: ItemStack): Boolean {
|
||||||
if (isCreative)
|
if (_capacity.invoke().isInfinite)
|
||||||
return false
|
return false
|
||||||
|
|
||||||
return p_150899_.matteryEnergy != null
|
return p_150899_.matteryEnergy != null
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getBarWidth(p_150900_: ItemStack): Int {
|
override fun getBarWidth(p_150900_: ItemStack): Int {
|
||||||
if (isCreative)
|
if (_capacity.invoke().isInfinite)
|
||||||
return 13
|
return 13
|
||||||
|
|
||||||
return p_150900_.matteryEnergy?.getBarWidth() ?: super.getBarWidth(p_150900_)
|
return p_150900_.matteryEnergy?.getBarWidth() ?: super.getBarWidth(p_150900_)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getBarColor(p_150901_: ItemStack): Int {
|
override fun getBarColor(p_150901_: ItemStack): Int {
|
||||||
if (isCreative)
|
if (_capacity.invoke().isInfinite)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
return p_150901_.matteryEnergy?.getBarColor() ?: super.getBarColor(p_150901_)
|
return p_150901_.matteryEnergy?.getBarColor() ?: super.getBarColor(p_150901_)
|
||||||
@ -141,22 +110,11 @@ open class BatteryItem : Item {
|
|||||||
p_41424_: TooltipFlag
|
p_41424_: TooltipFlag
|
||||||
) {
|
) {
|
||||||
super.appendHoverText(stack, p_41422_, p_41423_, p_41424_)
|
super.appendHoverText(stack, p_41422_, p_41423_, p_41424_)
|
||||||
|
ItemEnergyStorageImpl.appendHoverText(stack, p_41423_)
|
||||||
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_)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun initCapabilities(stack: ItemStack, nbt: CompoundTag?): ICapabilityProvider {
|
override fun initCapabilities(stack: ItemStack, nbt: CompoundTag?): ICapabilityProvider {
|
||||||
return Power(stack)
|
return EnergyCapacitorItem(stack, this@BatteryItem.capacity, this@BatteryItem.receive, this@BatteryItem.extract, initialBatteryLevel = this@BatteryItem.initialBatteryLevel)
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private val INFINITE_STORAGE: Component =
|
|
||||||
TranslatableComponent("otm.item.power.infinite.storage").withStyle(ChatFormatting.GRAY)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user