Update battery and matter implementations to reflect vars

This commit is contained in:
DBotThePony 2023-01-07 16:50:20 +07:00
parent adad99a6bf
commit c439665871
Signed by: DBot
GPG Key ID: DCC23B5715498507
12 changed files with 151 additions and 30 deletions

View File

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

View File

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

View File

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

View File

@ -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<IMatterGraphNode>(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
}

View File

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

View File

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

View File

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

View File

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

View File

@ -42,7 +42,6 @@ open class MatterHandlerImpl @JvmOverloads constructor(
get() = maxStoredMatterSupplier.invoke()
override var storedMatter = Decimal.ZERO
protected set
private var handler = LazyOptional.of<IMatterHandler> { this }

View File

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

View File

@ -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<ClientData> by lazy {
@ -447,7 +467,10 @@ class QuantumBatteryItem : Item {
override fun play(context: Supplier<NetworkEvent.Context>) {
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
}
}
}

View File

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