From c4396658715504c629941eccacb46da98dc6b731 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sat, 7 Jan 2023 16:50:20 +0700 Subject: [PATCH] Update battery and matter implementations to reflect vars --- .../block/entity/BatteryBankBlockEntity.kt | 29 +++++++++------- .../block/entity/EnergyCounterBlockEntity.kt | 8 ++++- .../block/entity/EnergyServoBlockEntity.kt | 10 +++++- .../matter/MatterCapacitorBankBlockEntity.kt | 13 ++++++-- .../mc/otm/capability/EnergyStorageImpl.kt | 4 +-- .../ru/dbotthepony/mc/otm/capability/Ext.kt | 25 +++++++++++--- .../otm/capability/IMatteryEnergyStorage.kt | 17 ++++++++++ .../otm/capability/matter/IMatterHandler.kt | 17 ++++++++++ .../capability/matter/MatterHandlerImpl.kt | 1 - .../mc/otm/compat/mekanism/Power.kt | 11 ++++++- .../mc/otm/item/QuantumBatteryItem.kt | 33 ++++++++++++++++--- .../mc/otm/item/weapon/PlasmaWeaponItem.kt | 13 +++++++- 12 files changed, 151 insertions(+), 30 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/BatteryBankBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/BatteryBankBlockEntity.kt index 0247fdf29..dab1c48a2 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/BatteryBankBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/BatteryBankBlockEntity.kt @@ -116,7 +116,7 @@ class BatteryBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte return BatteryBankDistribution(distribution, summ) } - private fun distributeEnergy(isReceiving: Boolean, howMuch: Decimal, simulate: Boolean): Decimal { + private fun distributeEnergy(isReceiving: Boolean, howMuch: Decimal, simulate: Boolean, set: Boolean = false): Decimal { if (!howMuch.isPositive) return Decimal.ZERO @@ -171,21 +171,28 @@ class BatteryBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte return distributeEnergy(isReceiving = true, howMuch, simulate) } - override val batteryLevel: Decimal get() { - var result = Decimal.ZERO + override val canSetBatteryLevel: Boolean + get() = false - for (i in 0 until container.containerSize) { - val stack = container.getItem(i) + override var batteryLevel: Decimal + get() { + var result = Decimal.ZERO - if (!stack.isEmpty) { - stack.energy?.let { - result += it.energyStoredMattery + for (i in 0 until container.containerSize) { + val stack = container.getItem(i) + + if (!stack.isEmpty) { + stack.energy?.let { + result += it.energyStoredMattery + } } } - } - return result - } + return result + } + set(value) { + throw UnsupportedOperationException("Can't set battery value for battery bank") + } override val maxBatteryLevel: Decimal get() { var result = Decimal.ZERO diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/EnergyCounterBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/EnergyCounterBlockEntity.kt index 5a4d9f90b..613ee4eb9 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/EnergyCounterBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/EnergyCounterBlockEntity.kt @@ -190,7 +190,10 @@ class EnergyCounterBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Mat return Decimal.ZERO } - override val batteryLevel: Decimal + override val canSetBatteryLevel: Boolean + get() = false + + override var batteryLevel: Decimal get() { if (isInput) { if (outputCapability.isPresent) { @@ -216,6 +219,9 @@ class EnergyCounterBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Mat return Decimal.ZERO } + set(value) { + throw UnsupportedOperationException("Can't set energy on energy counter") + } override val maxBatteryLevel: Decimal get() { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/EnergyServoBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/EnergyServoBlockEntity.kt index 6be52e29e..cdcff1a5b 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/EnergyServoBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/EnergyServoBlockEntity.kt @@ -16,6 +16,7 @@ import net.minecraftforge.common.util.LazyOptional import ru.dbotthepony.mc.otm.block.IDroppableContainer import ru.dbotthepony.mc.otm.capability.IMatteryEnergyStorage import ru.dbotthepony.mc.otm.capability.MatteryCapability +import ru.dbotthepony.mc.otm.capability.canSetBatteryMattery import ru.dbotthepony.mc.otm.capability.energy import ru.dbotthepony.mc.otm.capability.energyStoredMattery import ru.dbotthepony.mc.otm.capability.extractEnergy @@ -74,8 +75,15 @@ class EnergyServoBlockEntity(blockPos: BlockPos, blockState: BlockState) : Matte return container[SLOT_CHARGE].energy?.receiveEnergy(howMuch, simulate) ?: Decimal.ZERO } - override val batteryLevel: Decimal + override val canSetBatteryLevel: Boolean + get() = container[SLOT_CHARGE].energy?.canSetBatteryMattery ?: container[SLOT_DISCHARGE].energy?.canSetBatteryMattery ?: false + + override var batteryLevel: Decimal get() = container[SLOT_CHARGE].energy?.energyStoredMattery ?: container[SLOT_DISCHARGE].energy?.energyStoredMattery ?: Decimal.ZERO + set(value) { + val energy = container[SLOT_CHARGE].energy ?: container[SLOT_DISCHARGE].energy ?: throw UnsupportedOperationException("No item in slots") + energy.energyStoredMattery = value + } override val maxBatteryLevel: Decimal get() = container[SLOT_CHARGE].energy?.maxEnergyStoredMattery ?: container[SLOT_DISCHARGE].energy?.maxEnergyStoredMattery ?: Decimal.ZERO diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterCapacitorBankBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterCapacitorBankBlockEntity.kt index 73d01b49b..fe7bca537 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterCapacitorBankBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterCapacitorBankBlockEntity.kt @@ -24,6 +24,7 @@ import ru.dbotthepony.mc.otm.capability.matter.IMatterHandler import ru.dbotthepony.mc.otm.capability.matter.MatterDirection import ru.dbotthepony.mc.otm.container.MatteryContainer import ru.dbotthepony.mc.otm.core.Decimal +import ru.dbotthepony.mc.otm.core.ifPresentK import ru.dbotthepony.mc.otm.graph.Graph6Node import ru.dbotthepony.mc.otm.graph.matter.IMatterGraphNode import ru.dbotthepony.mc.otm.graph.matter.MatterNetworkGraph @@ -39,24 +40,30 @@ class MatterCapacitorBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) override val matterNode = Graph6Node(this) private val resolverNode = LazyOptional.of { this } - override val storedMatter: Decimal get() { + override val canSetMatterLevel: Boolean + get() = false + + override var storedMatter: Decimal get() { var summ = Decimal.ZERO for (stack in container) if (!stack.isEmpty) - stack.getCapability(MatteryCapability.MATTER).ifPresent { + stack.getCapability(MatteryCapability.MATTER).ifPresentK { summ += it.storedMatter } return summ } + set(value) { + throw UnsupportedOperationException() + } override val maxStoredMatter: Decimal get() { var summ = Decimal.ZERO for (stack in container) if (!stack.isEmpty) - stack.getCapability(MatteryCapability.MATTER).ifPresent { + stack.getCapability(MatteryCapability.MATTER).ifPresentK { summ += it.maxStoredMatter } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/EnergyStorageImpl.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/EnergyStorageImpl.kt index 5e96e9c81..3bf4d780d 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/EnergyStorageImpl.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/EnergyStorageImpl.kt @@ -131,7 +131,7 @@ sealed class ItemEnergyStorageImpl( override var batteryLevel: Decimal get() = itemStack.tag?.map(ENERGY_KEY, Decimal::deserializeNBT) ?: initialBatteryLevel - protected set(value) { + set(value) { itemStack.tagNotNull[ENERGY_KEY] = value.serializeNBT() } @@ -301,7 +301,7 @@ sealed class BlockEnergyStorageImpl( } override var batteryLevel = Decimal.ZERO - protected set(value) { + set(value) { if (value != field) { field = value listener.invoke() diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/Ext.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/Ext.kt index c49fad79e..a3e23da12 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/Ext.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/Ext.kt @@ -63,12 +63,29 @@ val IEnergyStorage.maxEnergyStoredMattery: Decimal get() { return Decimal.valueOf(maxEnergyStored) } -val IEnergyStorage.energyStoredMattery: Decimal get() { - if (this is IMatteryEnergyStorage) { - return batteryLevel +var IEnergyStorage.energyStoredMattery: Decimal + get() { + if (this is IMatteryEnergyStorage) { + return batteryLevel + } + + return Decimal.valueOf(energyStored) + } + set(value) { + if (this is IMatteryEnergyStorage) { + batteryLevel = value + return + } + + throw UnsupportedOperationException("Can't set stored energy on ${this::class.qualifiedName}") } - return Decimal.valueOf(energyStored) +val IEnergyStorage.canSetBatteryMattery: Boolean get() { + if (this is IMatteryEnergyStorage) { + return canSetBatteryLevel + } + + return false } val IEnergyStorage.chargeRatio: Float get() { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/IMatteryEnergyStorage.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/IMatteryEnergyStorage.kt index a38abf9ce..9903c83d9 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/IMatteryEnergyStorage.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/IMatteryEnergyStorage.kt @@ -42,6 +42,17 @@ interface IMatteryEnergyStorage : IEnergyStorage { */ fun receiveEnergyInner(howMuch: Decimal, simulate: Boolean): Decimal + /** + * If this is false, then [batteryLevel] will throw [UnsupportedOperationException] when trying to set it + */ + val canSetBatteryLevel: Boolean get() = true + + /** + * Implementations are free to throw [UnsupportedOperationException] if setting battery level is not supported + * due to technical complications (in this case, [canSetBatteryLevel] MUST be false) + * + * @throws [UnsupportedOperationException] + */ var batteryLevel: Decimal val maxBatteryLevel: Decimal val missingPower: Decimal @@ -49,6 +60,9 @@ interface IMatteryEnergyStorage : IEnergyStorage { /** * Empties power of this energy storage + * + * @throws [UnsupportedOperationException] + * @see batteryLevel */ fun emptyBattery() { batteryLevel = Decimal.ZERO @@ -56,6 +70,9 @@ interface IMatteryEnergyStorage : IEnergyStorage { /** * Fully fills power in this energy storage + * + * @throws [UnsupportedOperationException] + * @see batteryLevel */ fun fillBattery() { batteryLevel = maxBatteryLevel diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/matter/IMatterHandler.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/matter/IMatterHandler.kt index 38f5dd893..7863b2e9a 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/matter/IMatterHandler.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/matter/IMatterHandler.kt @@ -8,11 +8,25 @@ import ru.dbotthepony.mc.otm.core.orNull import kotlin.math.roundToInt interface IMatterHandler { + /** + * If this is false, then [storedMatter] will throw [UnsupportedOperationException] when trying to set it + */ + val canSetMatterLevel: Boolean get() = true + + /** + * Implementations are free to throw [UnsupportedOperationException] if setting battery level is not supported + * due to technical complications (in this case, [canSetMatterLevel] MUST be false) + * + * @throws [UnsupportedOperationException] + */ var storedMatter: Decimal val maxStoredMatter: Decimal /** * Empties matter stored of this matter storage + * + * @throws [UnsupportedOperationException] + * @see storedMatter */ fun emptyMatter() { storedMatter = Decimal.ZERO @@ -20,6 +34,9 @@ interface IMatterHandler { /** * Fully fills matter stored in this matter storage + * + * @throws [UnsupportedOperationException] + * @see storedMatter */ fun fillMatter() { storedMatter = maxStoredMatter diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/matter/MatterHandlerImpl.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/matter/MatterHandlerImpl.kt index b50bc0bb5..fb80c3b23 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/matter/MatterHandlerImpl.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/matter/MatterHandlerImpl.kt @@ -42,7 +42,6 @@ open class MatterHandlerImpl @JvmOverloads constructor( get() = maxStoredMatterSupplier.invoke() override var storedMatter = Decimal.ZERO - protected set private var handler = LazyOptional.of { this } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/compat/mekanism/Power.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/compat/mekanism/Power.kt index e0d67b117..233ca45e9 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/compat/mekanism/Power.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/compat/mekanism/Power.kt @@ -97,7 +97,10 @@ class Mekanism2MatteryEnergyWrapper(private val power: IStrictEnergyHandler, pri return receiveEnergyOuter(howMuch, simulate) } - override val batteryLevel: Decimal + override val canSetBatteryLevel: Boolean + get() = power.energyContainerCount == 1 + + override var batteryLevel: Decimal get() { var sum = Decimal.ZERO @@ -107,6 +110,12 @@ class Mekanism2MatteryEnergyWrapper(private val power: IStrictEnergyHandler, pri return sum * mekanism2MtJ } + set(value) { + if (power.energyContainerCount != 1) + throw UnsupportedOperationException("Can set power only when we have 1 energy container, ${power.energyContainerCount} present") + + power.setEnergy(0, (value * mtj2Mekanism).toFloatingLong()) + } override val maxBatteryLevel: Decimal get() { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/QuantumBatteryItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/QuantumBatteryItem.kt index fe7243c2f..d51b91b94 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/QuantumBatteryItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/QuantumBatteryItem.kt @@ -1,6 +1,7 @@ package ru.dbotthepony.mc.otm.item import it.unimi.dsi.fastutil.ints.Int2ObjectAVLTreeMap +import it.unimi.dsi.fastutil.ints.Int2ObjectFunction import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap import it.unimi.dsi.fastutil.ints.IntAVLTreeSet import it.unimi.dsi.fastutil.ints.IntOpenHashSet @@ -169,7 +170,7 @@ class QuantumBatteryItem : Item { return diff } - override val batteryLevel: Decimal + override var batteryLevel: Decimal get() { if (data.parent == null) { determineQuantumLink() @@ -181,6 +182,25 @@ class QuantumBatteryItem : Item { return data.energy } + set(value) { + if (data.parent == null) { + determineQuantumLink() + } + + if (isClientThread()) { + val energy1 = clientPowerMap[data.index] + + if (energy1 != null) { + energy1.energy = value + } else { + data.energy = value + } + + return + } + + data.energy = value + } val passed: Decimal get() { if (data.parent == null) { @@ -282,9 +302,9 @@ class QuantumBatteryItem : Item { } data class ClientData( - val energy: Decimal = Decimal.ZERO, - val passed: Decimal = Decimal.ZERO, - val received: Decimal = Decimal.ZERO, + var energy: Decimal = Decimal.ZERO, + var passed: Decimal = Decimal.ZERO, + var received: Decimal = Decimal.ZERO, ) val clientPowerMap: Int2ObjectOpenHashMap by lazy { @@ -447,7 +467,10 @@ class QuantumBatteryItem : Item { override fun play(context: Supplier) { context.packetHandled = true - type.clientPowerMap[channel] = ClientData(energy, passed, received) + val data = type.clientPowerMap.computeIfAbsent(channel, Int2ObjectFunction { ClientData() }) + data.energy = energy + data.passed = passed + data.received = received } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/PlasmaWeaponItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/PlasmaWeaponItem.kt index d16fe3a3f..a6c0824b3 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/PlasmaWeaponItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/PlasmaWeaponItem.kt @@ -138,8 +138,19 @@ class PlasmaWeaponEnergy(val itemStack: ItemStack, private val innerCapacity: De return true } - override val batteryLevel: Decimal + override fun emptyBattery() { + innerBatteryLevel = Decimal.ZERO + } + + override fun fillBattery() { + innerBatteryLevel = innerCapacity + } + + override var batteryLevel: Decimal get() = (battery.energy?.energyStoredMattery ?: Decimal.ZERO) + innerBatteryLevel + set(value) { + innerBatteryLevel = value + } override val maxBatteryLevel: Decimal get() = (battery.energy?.maxEnergyStoredMattery ?: Decimal.ZERO) + innerCapacity