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 c9ef0f38c..e0d67b117 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 @@ -5,13 +5,16 @@ import mekanism.api.energy.IStrictEnergyHandler import mekanism.api.math.FloatingLong import mekanism.common.config.MekanismConfig import net.minecraft.core.Direction +import net.minecraftforge.common.capabilities.ForgeCapabilities import net.minecraftforge.common.capabilities.ICapabilityProvider import net.minecraftforge.common.util.LazyOptional +import net.minecraftforge.energy.IEnergyStorage import org.apache.logging.log4j.LogManager import ru.dbotthepony.mc.otm.capability.IMatteryEnergyStorage import ru.dbotthepony.mc.otm.capability.MatteryCapability import ru.dbotthepony.mc.otm.capability.isMekanismLoaded import ru.dbotthepony.mc.otm.core.Decimal +import ru.dbotthepony.mc.otm.core.orNull import java.lang.ref.WeakReference import java.util.WeakHashMap @@ -67,7 +70,7 @@ private val mtj2Mekanism by DoubleLazy lazy@{ 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 { val action = when (simulate) { true -> Action.SIMULATE @@ -115,6 +118,20 @@ class Mekanism2MatteryEnergyWrapper(private val power: IStrictEnergyHandler) : I 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 { @@ -169,7 +186,7 @@ val ICapabilityProvider.mekanismEnergy: IMatteryEnergyStorage? get() { return null } - return Mekanism2MatteryEnergyWrapper(capability.orElseThrow(::IllegalStateException)) + return Mekanism2MatteryEnergyWrapper(capability.orElseThrow(::IllegalStateException), getCapability(ForgeCapabilities.ENERGY).orNull()) } private val lazyCache by lazy { WeakHashMap, WeakReference>>() } @@ -181,15 +198,9 @@ private val lazyCacheEast by lazy { WeakHashMap, WeakReference>>() } fun ICapabilityProvider.getMekanismEnergySided(side: Direction? = null): LazyOptional { - if (!isMekanismLoaded) { - return LazyOptional.empty() - } - + if (!isMekanismLoaded) return LazyOptional.empty() val lazyOptional = getCapability(MatteryCapability.MEKANISM_ENERGY, side) - - if (!lazyOptional.isPresent) { - return LazyOptional.empty() - } + if (!lazyOptional.isPresent) return LazyOptional.empty() val cache = when (side) { Direction.DOWN -> lazyCacheDown @@ -202,13 +213,12 @@ fun ICapabilityProvider.getMekanismEnergySided(side: Direction? = null): LazyOpt } val cached = cache[lazyOptional]?.get() + if (cached != null) return cached.cast() - if (cached != null) { - return cached.cast() - } + val forgeEnergy = getCapability(ForgeCapabilities.ENERGY, side) val resolver = LazyOptional.of { - Mekanism2MatteryEnergyWrapper(lazyOptional.orElseThrow(::IllegalStateException)) + Mekanism2MatteryEnergyWrapper(lazyOptional.orElseThrow(::IllegalStateException), forgeEnergy.orNull()) } val ref = WeakReference(resolver)