From 6e02402f068f918b36b4b85d4d9407b330150c5e Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sun, 30 Jan 2022 17:00:13 +0700 Subject: [PATCH] Add built in LazyOptional to EnergyStorageImpl --- .../otm/block/entity/BlockEntityChemicalGenerator.kt | 7 +++---- .../mc/otm/capability/MatteryMachineEnergyStorage.kt | 12 ++++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/BlockEntityChemicalGenerator.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/BlockEntityChemicalGenerator.kt index 6f1f59191..f12b689b3 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/BlockEntityChemicalGenerator.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/BlockEntityChemicalGenerator.kt @@ -41,7 +41,6 @@ class BlockEntityChemicalGenerator(pos: BlockPos, state: BlockState) : BlockEnti } private var valid = true - private var resolver = LazyOptional.of {energy} @JvmField val energy = GeneratorEnergyStorage(this::setChangedLight, MAX_ENERGY, THROUGHPUT) @@ -53,7 +52,7 @@ class BlockEntityChemicalGenerator(pos: BlockPos, state: BlockState) : BlockEnti override fun getCapability(cap: Capability, side: Direction?): LazyOptional { if (valid && (cap === MatteryCapability.ENERGY || cap === CapabilityEnergy.ENERGY) && side !== blockState.getValue(BlockMatteryRotatable.FACING)) - return resolver.cast() + return energy.resolver.cast() if (valid && cap === CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return itemHandler.get().cast() @@ -64,15 +63,15 @@ class BlockEntityChemicalGenerator(pos: BlockPos, state: BlockState) : BlockEnti override fun invalidateCaps() { super.invalidateCaps() itemHandler.invalidate() + energy.invalidate() valid = false - resolver.invalidate() } override fun reviveCaps() { super.reviveCaps() itemHandler.revive() + energy.revive() valid = true - resolver = LazyOptional.of {energy} } override fun setLevel(level: Level) { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryMachineEnergyStorage.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryMachineEnergyStorage.kt index d434e40b3..b21c2dbb2 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryMachineEnergyStorage.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryMachineEnergyStorage.kt @@ -3,6 +3,7 @@ package ru.dbotthepony.mc.otm.capability import net.minecraft.nbt.CompoundTag import net.minecraft.world.level.block.entity.BlockEntity import net.minecraftforge.common.util.INBTSerializable +import net.minecraftforge.common.util.LazyOptional import ru.dbotthepony.mc.otm.core.ImpreciseFraction import ru.dbotthepony.mc.otm.ifHas import ru.dbotthepony.mc.otm.set @@ -86,6 +87,17 @@ sealed class EnergyStorageImpl constructor( // nbt.ifHas("max_output") { maxOutput = deserializeNBT(it) } } + var resolver: LazyOptional = LazyOptional.of { this } + private set + + fun invalidate() { + resolver.invalidate() + } + + fun revive() { + resolver = LazyOptional.of { this } + } + companion object { val DEFAULT_MAX_IO = ImpreciseFraction(200) val DEFAULT_MAX_CAPACITY = ImpreciseFraction(60_000)