Store IEnergyStorage in Mekanism2MatteryEnergyWrapper, providing canExtract and canReceive
Fixes #205
This commit is contained in:
parent
a6d020bdab
commit
017802e90f
@ -5,13 +5,16 @@ import mekanism.api.energy.IStrictEnergyHandler
|
|||||||
import mekanism.api.math.FloatingLong
|
import mekanism.api.math.FloatingLong
|
||||||
import mekanism.common.config.MekanismConfig
|
import mekanism.common.config.MekanismConfig
|
||||||
import net.minecraft.core.Direction
|
import net.minecraft.core.Direction
|
||||||
|
import net.minecraftforge.common.capabilities.ForgeCapabilities
|
||||||
import net.minecraftforge.common.capabilities.ICapabilityProvider
|
import net.minecraftforge.common.capabilities.ICapabilityProvider
|
||||||
import net.minecraftforge.common.util.LazyOptional
|
import net.minecraftforge.common.util.LazyOptional
|
||||||
|
import net.minecraftforge.energy.IEnergyStorage
|
||||||
import org.apache.logging.log4j.LogManager
|
import org.apache.logging.log4j.LogManager
|
||||||
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.isMekanismLoaded
|
import ru.dbotthepony.mc.otm.capability.isMekanismLoaded
|
||||||
import ru.dbotthepony.mc.otm.core.Decimal
|
import ru.dbotthepony.mc.otm.core.Decimal
|
||||||
|
import ru.dbotthepony.mc.otm.core.orNull
|
||||||
import java.lang.ref.WeakReference
|
import java.lang.ref.WeakReference
|
||||||
import java.util.WeakHashMap
|
import java.util.WeakHashMap
|
||||||
|
|
||||||
@ -67,7 +70,7 @@ private val mtj2Mekanism by DoubleLazy lazy@{
|
|||||||
return@lazy lazyOf(Decimal.ONE)
|
return@lazy lazyOf(Decimal.ONE)
|
||||||
}
|
}
|
||||||
|
|
||||||
class Mekanism2MatteryEnergyWrapper(private val power: IStrictEnergyHandler) : IMatteryEnergyStorage {
|
class Mekanism2MatteryEnergyWrapper(private val power: IStrictEnergyHandler, private val forgePower: IEnergyStorage? = null) : IMatteryEnergyStorage {
|
||||||
override fun extractEnergyOuter(howMuch: Decimal, simulate: Boolean): Decimal {
|
override fun extractEnergyOuter(howMuch: Decimal, simulate: Boolean): Decimal {
|
||||||
val action = when (simulate) {
|
val action = when (simulate) {
|
||||||
true -> Action.SIMULATE
|
true -> Action.SIMULATE
|
||||||
@ -115,6 +118,20 @@ class Mekanism2MatteryEnergyWrapper(private val power: IStrictEnergyHandler) : I
|
|||||||
|
|
||||||
return sum * mekanism2MtJ
|
return sum * mekanism2MtJ
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun canExtract(): Boolean {
|
||||||
|
if (forgePower != null)
|
||||||
|
return forgePower.canExtract()
|
||||||
|
|
||||||
|
return super.canExtract()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun canReceive(): Boolean {
|
||||||
|
if (forgePower != null)
|
||||||
|
return forgePower.canReceive()
|
||||||
|
|
||||||
|
return super.canReceive()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Mattery2MekanismEnergyWrapper(private val power: IMatteryEnergyStorage) : IStrictEnergyHandler {
|
class Mattery2MekanismEnergyWrapper(private val power: IMatteryEnergyStorage) : IStrictEnergyHandler {
|
||||||
@ -169,7 +186,7 @@ val ICapabilityProvider.mekanismEnergy: IMatteryEnergyStorage? get() {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
return Mekanism2MatteryEnergyWrapper(capability.orElseThrow(::IllegalStateException))
|
return Mekanism2MatteryEnergyWrapper(capability.orElseThrow(::IllegalStateException), getCapability(ForgeCapabilities.ENERGY).orNull())
|
||||||
}
|
}
|
||||||
|
|
||||||
private val lazyCache by lazy { WeakHashMap<LazyOptional<IStrictEnergyHandler>, WeakReference<LazyOptional<Mekanism2MatteryEnergyWrapper>>>() }
|
private val lazyCache by lazy { WeakHashMap<LazyOptional<IStrictEnergyHandler>, WeakReference<LazyOptional<Mekanism2MatteryEnergyWrapper>>>() }
|
||||||
@ -181,15 +198,9 @@ private val lazyCacheEast by lazy { WeakHashMap<LazyOptional<IStrictEnergyHandle
|
|||||||
private val lazyCacheWest by lazy { WeakHashMap<LazyOptional<IStrictEnergyHandler>, WeakReference<LazyOptional<Mekanism2MatteryEnergyWrapper>>>() }
|
private val lazyCacheWest by lazy { WeakHashMap<LazyOptional<IStrictEnergyHandler>, WeakReference<LazyOptional<Mekanism2MatteryEnergyWrapper>>>() }
|
||||||
|
|
||||||
fun ICapabilityProvider.getMekanismEnergySided(side: Direction? = null): LazyOptional<IMatteryEnergyStorage> {
|
fun ICapabilityProvider.getMekanismEnergySided(side: Direction? = null): LazyOptional<IMatteryEnergyStorage> {
|
||||||
if (!isMekanismLoaded) {
|
if (!isMekanismLoaded) return LazyOptional.empty()
|
||||||
return LazyOptional.empty()
|
|
||||||
}
|
|
||||||
|
|
||||||
val lazyOptional = getCapability(MatteryCapability.MEKANISM_ENERGY, side)
|
val lazyOptional = getCapability(MatteryCapability.MEKANISM_ENERGY, side)
|
||||||
|
if (!lazyOptional.isPresent) return LazyOptional.empty()
|
||||||
if (!lazyOptional.isPresent) {
|
|
||||||
return LazyOptional.empty()
|
|
||||||
}
|
|
||||||
|
|
||||||
val cache = when (side) {
|
val cache = when (side) {
|
||||||
Direction.DOWN -> lazyCacheDown
|
Direction.DOWN -> lazyCacheDown
|
||||||
@ -202,13 +213,12 @@ fun ICapabilityProvider.getMekanismEnergySided(side: Direction? = null): LazyOpt
|
|||||||
}
|
}
|
||||||
|
|
||||||
val cached = cache[lazyOptional]?.get()
|
val cached = cache[lazyOptional]?.get()
|
||||||
|
if (cached != null) return cached.cast()
|
||||||
|
|
||||||
if (cached != null) {
|
val forgeEnergy = getCapability(ForgeCapabilities.ENERGY, side)
|
||||||
return cached.cast()
|
|
||||||
}
|
|
||||||
|
|
||||||
val resolver = LazyOptional.of {
|
val resolver = LazyOptional.of {
|
||||||
Mekanism2MatteryEnergyWrapper(lazyOptional.orElseThrow(::IllegalStateException))
|
Mekanism2MatteryEnergyWrapper(lazyOptional.orElseThrow(::IllegalStateException), forgeEnergy.orNull())
|
||||||
}
|
}
|
||||||
|
|
||||||
val ref = WeakReference(resolver)
|
val ref = WeakReference(resolver)
|
||||||
|
Loading…
Reference in New Issue
Block a user