Update battery and matter implementations to reflect vars
This commit is contained in:
parent
adad99a6bf
commit
c439665871
@ -116,7 +116,7 @@ class BatteryBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte
|
|||||||
return BatteryBankDistribution(distribution, summ)
|
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)
|
if (!howMuch.isPositive)
|
||||||
return Decimal.ZERO
|
return Decimal.ZERO
|
||||||
|
|
||||||
@ -171,7 +171,11 @@ class BatteryBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte
|
|||||||
return distributeEnergy(isReceiving = true, howMuch, simulate)
|
return distributeEnergy(isReceiving = true, howMuch, simulate)
|
||||||
}
|
}
|
||||||
|
|
||||||
override val batteryLevel: Decimal get() {
|
override val canSetBatteryLevel: Boolean
|
||||||
|
get() = false
|
||||||
|
|
||||||
|
override var batteryLevel: Decimal
|
||||||
|
get() {
|
||||||
var result = Decimal.ZERO
|
var result = Decimal.ZERO
|
||||||
|
|
||||||
for (i in 0 until container.containerSize) {
|
for (i in 0 until container.containerSize) {
|
||||||
@ -186,6 +190,9 @@ class BatteryBankBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matte
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
set(value) {
|
||||||
|
throw UnsupportedOperationException("Can't set battery value for battery bank")
|
||||||
|
}
|
||||||
|
|
||||||
override val maxBatteryLevel: Decimal get() {
|
override val maxBatteryLevel: Decimal get() {
|
||||||
var result = Decimal.ZERO
|
var result = Decimal.ZERO
|
||||||
|
@ -190,7 +190,10 @@ class EnergyCounterBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Mat
|
|||||||
return Decimal.ZERO
|
return Decimal.ZERO
|
||||||
}
|
}
|
||||||
|
|
||||||
override val batteryLevel: Decimal
|
override val canSetBatteryLevel: Boolean
|
||||||
|
get() = false
|
||||||
|
|
||||||
|
override var batteryLevel: Decimal
|
||||||
get() {
|
get() {
|
||||||
if (isInput) {
|
if (isInput) {
|
||||||
if (outputCapability.isPresent) {
|
if (outputCapability.isPresent) {
|
||||||
@ -216,6 +219,9 @@ class EnergyCounterBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Mat
|
|||||||
|
|
||||||
return Decimal.ZERO
|
return Decimal.ZERO
|
||||||
}
|
}
|
||||||
|
set(value) {
|
||||||
|
throw UnsupportedOperationException("Can't set energy on energy counter")
|
||||||
|
}
|
||||||
|
|
||||||
override val maxBatteryLevel: Decimal
|
override val maxBatteryLevel: Decimal
|
||||||
get() {
|
get() {
|
||||||
|
@ -16,6 +16,7 @@ import net.minecraftforge.common.util.LazyOptional
|
|||||||
import ru.dbotthepony.mc.otm.block.IDroppableContainer
|
import ru.dbotthepony.mc.otm.block.IDroppableContainer
|
||||||
import ru.dbotthepony.mc.otm.capability.IMatteryEnergyStorage
|
import ru.dbotthepony.mc.otm.capability.IMatteryEnergyStorage
|
||||||
import ru.dbotthepony.mc.otm.capability.MatteryCapability
|
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.energy
|
||||||
import ru.dbotthepony.mc.otm.capability.energyStoredMattery
|
import ru.dbotthepony.mc.otm.capability.energyStoredMattery
|
||||||
import ru.dbotthepony.mc.otm.capability.extractEnergy
|
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
|
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
|
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
|
override val maxBatteryLevel: Decimal
|
||||||
get() = container[SLOT_CHARGE].energy?.maxEnergyStoredMattery ?: container[SLOT_DISCHARGE].energy?.maxEnergyStoredMattery ?: Decimal.ZERO
|
get() = container[SLOT_CHARGE].energy?.maxEnergyStoredMattery ?: container[SLOT_DISCHARGE].energy?.maxEnergyStoredMattery ?: Decimal.ZERO
|
||||||
|
@ -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.capability.matter.MatterDirection
|
||||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||||
import ru.dbotthepony.mc.otm.core.Decimal
|
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.Graph6Node
|
||||||
import ru.dbotthepony.mc.otm.graph.matter.IMatterGraphNode
|
import ru.dbotthepony.mc.otm.graph.matter.IMatterGraphNode
|
||||||
import ru.dbotthepony.mc.otm.graph.matter.MatterNetworkGraph
|
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)
|
override val matterNode = Graph6Node<IMatterGraphNode>(this)
|
||||||
private val resolverNode = LazyOptional.of { 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
|
var summ = Decimal.ZERO
|
||||||
|
|
||||||
for (stack in container)
|
for (stack in container)
|
||||||
if (!stack.isEmpty)
|
if (!stack.isEmpty)
|
||||||
stack.getCapability(MatteryCapability.MATTER).ifPresent {
|
stack.getCapability(MatteryCapability.MATTER).ifPresentK {
|
||||||
summ += it.storedMatter
|
summ += it.storedMatter
|
||||||
}
|
}
|
||||||
|
|
||||||
return summ
|
return summ
|
||||||
}
|
}
|
||||||
|
set(value) {
|
||||||
|
throw UnsupportedOperationException()
|
||||||
|
}
|
||||||
|
|
||||||
override val maxStoredMatter: Decimal get() {
|
override val maxStoredMatter: Decimal get() {
|
||||||
var summ = Decimal.ZERO
|
var summ = Decimal.ZERO
|
||||||
|
|
||||||
for (stack in container)
|
for (stack in container)
|
||||||
if (!stack.isEmpty)
|
if (!stack.isEmpty)
|
||||||
stack.getCapability(MatteryCapability.MATTER).ifPresent {
|
stack.getCapability(MatteryCapability.MATTER).ifPresentK {
|
||||||
summ += it.maxStoredMatter
|
summ += it.maxStoredMatter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,7 +131,7 @@ sealed class ItemEnergyStorageImpl(
|
|||||||
|
|
||||||
override var batteryLevel: Decimal
|
override var batteryLevel: Decimal
|
||||||
get() = itemStack.tag?.map(ENERGY_KEY, Decimal::deserializeNBT) ?: initialBatteryLevel
|
get() = itemStack.tag?.map(ENERGY_KEY, Decimal::deserializeNBT) ?: initialBatteryLevel
|
||||||
protected set(value) {
|
set(value) {
|
||||||
itemStack.tagNotNull[ENERGY_KEY] = value.serializeNBT()
|
itemStack.tagNotNull[ENERGY_KEY] = value.serializeNBT()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -301,7 +301,7 @@ sealed class BlockEnergyStorageImpl(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override var batteryLevel = Decimal.ZERO
|
override var batteryLevel = Decimal.ZERO
|
||||||
protected set(value) {
|
set(value) {
|
||||||
if (value != field) {
|
if (value != field) {
|
||||||
field = value
|
field = value
|
||||||
listener.invoke()
|
listener.invoke()
|
||||||
|
@ -63,13 +63,30 @@ val IEnergyStorage.maxEnergyStoredMattery: Decimal get() {
|
|||||||
return Decimal.valueOf(maxEnergyStored)
|
return Decimal.valueOf(maxEnergyStored)
|
||||||
}
|
}
|
||||||
|
|
||||||
val IEnergyStorage.energyStoredMattery: Decimal get() {
|
var IEnergyStorage.energyStoredMattery: Decimal
|
||||||
|
get() {
|
||||||
if (this is IMatteryEnergyStorage) {
|
if (this is IMatteryEnergyStorage) {
|
||||||
return batteryLevel
|
return batteryLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
return Decimal.valueOf(energyStored)
|
return Decimal.valueOf(energyStored)
|
||||||
}
|
}
|
||||||
|
set(value) {
|
||||||
|
if (this is IMatteryEnergyStorage) {
|
||||||
|
batteryLevel = value
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
throw UnsupportedOperationException("Can't set stored energy on ${this::class.qualifiedName}")
|
||||||
|
}
|
||||||
|
|
||||||
|
val IEnergyStorage.canSetBatteryMattery: Boolean get() {
|
||||||
|
if (this is IMatteryEnergyStorage) {
|
||||||
|
return canSetBatteryLevel
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
val IEnergyStorage.chargeRatio: Float get() {
|
val IEnergyStorage.chargeRatio: Float get() {
|
||||||
if (this is IMatteryEnergyStorage) {
|
if (this is IMatteryEnergyStorage) {
|
||||||
|
@ -42,6 +42,17 @@ interface IMatteryEnergyStorage : IEnergyStorage {
|
|||||||
*/
|
*/
|
||||||
fun receiveEnergyInner(howMuch: Decimal, simulate: Boolean): Decimal
|
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
|
var batteryLevel: Decimal
|
||||||
val maxBatteryLevel: Decimal
|
val maxBatteryLevel: Decimal
|
||||||
val missingPower: Decimal
|
val missingPower: Decimal
|
||||||
@ -49,6 +60,9 @@ interface IMatteryEnergyStorage : IEnergyStorage {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Empties power of this energy storage
|
* Empties power of this energy storage
|
||||||
|
*
|
||||||
|
* @throws [UnsupportedOperationException]
|
||||||
|
* @see batteryLevel
|
||||||
*/
|
*/
|
||||||
fun emptyBattery() {
|
fun emptyBattery() {
|
||||||
batteryLevel = Decimal.ZERO
|
batteryLevel = Decimal.ZERO
|
||||||
@ -56,6 +70,9 @@ interface IMatteryEnergyStorage : IEnergyStorage {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Fully fills power in this energy storage
|
* Fully fills power in this energy storage
|
||||||
|
*
|
||||||
|
* @throws [UnsupportedOperationException]
|
||||||
|
* @see batteryLevel
|
||||||
*/
|
*/
|
||||||
fun fillBattery() {
|
fun fillBattery() {
|
||||||
batteryLevel = maxBatteryLevel
|
batteryLevel = maxBatteryLevel
|
||||||
|
@ -8,11 +8,25 @@ import ru.dbotthepony.mc.otm.core.orNull
|
|||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
interface IMatterHandler {
|
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
|
var storedMatter: Decimal
|
||||||
val maxStoredMatter: Decimal
|
val maxStoredMatter: Decimal
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Empties matter stored of this matter storage
|
* Empties matter stored of this matter storage
|
||||||
|
*
|
||||||
|
* @throws [UnsupportedOperationException]
|
||||||
|
* @see storedMatter
|
||||||
*/
|
*/
|
||||||
fun emptyMatter() {
|
fun emptyMatter() {
|
||||||
storedMatter = Decimal.ZERO
|
storedMatter = Decimal.ZERO
|
||||||
@ -20,6 +34,9 @@ interface IMatterHandler {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Fully fills matter stored in this matter storage
|
* Fully fills matter stored in this matter storage
|
||||||
|
*
|
||||||
|
* @throws [UnsupportedOperationException]
|
||||||
|
* @see storedMatter
|
||||||
*/
|
*/
|
||||||
fun fillMatter() {
|
fun fillMatter() {
|
||||||
storedMatter = maxStoredMatter
|
storedMatter = maxStoredMatter
|
||||||
|
@ -42,7 +42,6 @@ open class MatterHandlerImpl @JvmOverloads constructor(
|
|||||||
get() = maxStoredMatterSupplier.invoke()
|
get() = maxStoredMatterSupplier.invoke()
|
||||||
|
|
||||||
override var storedMatter = Decimal.ZERO
|
override var storedMatter = Decimal.ZERO
|
||||||
protected set
|
|
||||||
|
|
||||||
private var handler = LazyOptional.of<IMatterHandler> { this }
|
private var handler = LazyOptional.of<IMatterHandler> { this }
|
||||||
|
|
||||||
|
@ -97,7 +97,10 @@ class Mekanism2MatteryEnergyWrapper(private val power: IStrictEnergyHandler, pri
|
|||||||
return receiveEnergyOuter(howMuch, simulate)
|
return receiveEnergyOuter(howMuch, simulate)
|
||||||
}
|
}
|
||||||
|
|
||||||
override val batteryLevel: Decimal
|
override val canSetBatteryLevel: Boolean
|
||||||
|
get() = power.energyContainerCount == 1
|
||||||
|
|
||||||
|
override var batteryLevel: Decimal
|
||||||
get() {
|
get() {
|
||||||
var sum = Decimal.ZERO
|
var sum = Decimal.ZERO
|
||||||
|
|
||||||
@ -107,6 +110,12 @@ class Mekanism2MatteryEnergyWrapper(private val power: IStrictEnergyHandler, pri
|
|||||||
|
|
||||||
return sum * mekanism2MtJ
|
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
|
override val maxBatteryLevel: Decimal
|
||||||
get() {
|
get() {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package ru.dbotthepony.mc.otm.item
|
package ru.dbotthepony.mc.otm.item
|
||||||
|
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectAVLTreeMap
|
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.Int2ObjectOpenHashMap
|
||||||
import it.unimi.dsi.fastutil.ints.IntAVLTreeSet
|
import it.unimi.dsi.fastutil.ints.IntAVLTreeSet
|
||||||
import it.unimi.dsi.fastutil.ints.IntOpenHashSet
|
import it.unimi.dsi.fastutil.ints.IntOpenHashSet
|
||||||
@ -169,7 +170,7 @@ class QuantumBatteryItem : Item {
|
|||||||
return diff
|
return diff
|
||||||
}
|
}
|
||||||
|
|
||||||
override val batteryLevel: Decimal
|
override var batteryLevel: Decimal
|
||||||
get() {
|
get() {
|
||||||
if (data.parent == null) {
|
if (data.parent == null) {
|
||||||
determineQuantumLink()
|
determineQuantumLink()
|
||||||
@ -181,6 +182,25 @@ class QuantumBatteryItem : Item {
|
|||||||
|
|
||||||
return data.energy
|
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() {
|
val passed: Decimal get() {
|
||||||
if (data.parent == null) {
|
if (data.parent == null) {
|
||||||
@ -282,9 +302,9 @@ class QuantumBatteryItem : Item {
|
|||||||
}
|
}
|
||||||
|
|
||||||
data class ClientData(
|
data class ClientData(
|
||||||
val energy: Decimal = Decimal.ZERO,
|
var energy: Decimal = Decimal.ZERO,
|
||||||
val passed: Decimal = Decimal.ZERO,
|
var passed: Decimal = Decimal.ZERO,
|
||||||
val received: Decimal = Decimal.ZERO,
|
var received: Decimal = Decimal.ZERO,
|
||||||
)
|
)
|
||||||
|
|
||||||
val clientPowerMap: Int2ObjectOpenHashMap<ClientData> by lazy {
|
val clientPowerMap: Int2ObjectOpenHashMap<ClientData> by lazy {
|
||||||
@ -447,7 +467,10 @@ class QuantumBatteryItem : Item {
|
|||||||
|
|
||||||
override fun play(context: Supplier<NetworkEvent.Context>) {
|
override fun play(context: Supplier<NetworkEvent.Context>) {
|
||||||
context.packetHandled = true
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -138,8 +138,19 @@ class PlasmaWeaponEnergy(val itemStack: ItemStack, private val innerCapacity: De
|
|||||||
return true
|
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
|
get() = (battery.energy?.energyStoredMattery ?: Decimal.ZERO) + innerBatteryLevel
|
||||||
|
set(value) {
|
||||||
|
innerBatteryLevel = value
|
||||||
|
}
|
||||||
|
|
||||||
override val maxBatteryLevel: Decimal
|
override val maxBatteryLevel: Decimal
|
||||||
get() = (battery.energy?.maxEnergyStoredMattery ?: Decimal.ZERO) + innerCapacity
|
get() = (battery.energy?.maxEnergyStoredMattery ?: Decimal.ZERO) + innerCapacity
|
||||||
|
Loading…
Reference in New Issue
Block a user