diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryBlockEntity.kt index 9c7264abe..aa24a6d06 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryBlockEntity.kt @@ -101,6 +101,8 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc private val _sides = EnumMap(RelativeSide::class.java) val sides: Map = Collections.unmodifiableMap(_sides) + fun side(side: RelativeSide) = sides[side]!! + private data class SidelessCap(val cap: T, var optional: LazyOptional) private val sidelessCaps = Reference2ObjectOpenHashMap, SidelessCap<*>>() protected val tickList = TickList() @@ -203,6 +205,10 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc return SupplierList(_sides.values.map { it.track(capability)::get }) } + interface SideListener : Supplier> { + fun addListener(listener: Consumer>) + } + inner class Side(val side: RelativeSide) { init { check(!_sides.containsKey(side)) { "dafuq are you trying to do" } @@ -213,7 +219,7 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc private val subscriptions = Reference2ObjectOpenHashMap, SubRef<*>>() private val knownLOs = WeakHashSet>() - private inner class SubRef(value: LazyOptional) : Supplier> { + private inner class SubRef(value: LazyOptional) : SideListener { var value: LazyOptional = value set(value) { if (value !== field) { @@ -226,7 +232,7 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc private val listeners = ArrayList>>(0) - fun addListener(listener: Consumer>) { + override fun addListener(listener: Consumer>) { listeners.add(listener) listener.accept(value) } @@ -240,8 +246,8 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc } } - fun track(capability: Capability): Supplier> { - var subref = subscriptions[capability] as Supplier>? + fun track(capability: Capability): SideListener { + var subref = subscriptions[capability] as SideListener? if (subref == null) { subref = SubRef(LazyOptional.empty()) as SubRef @@ -252,13 +258,22 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc return subref } - fun trackEnergy(): Supplier> { - return object : Supplier> { + fun trackEnergy(): SideListener { + return object : SideListener { private val regular = track(ForgeCapabilities.ENERGY) private val mekanism: SubRef? private var actualMekanism: LazyOptional? = null + private val listeners = ArrayList>>() + + override fun addListener(listener: Consumer>) { + listeners.add(listener) + listener.accept(get()) + } + init { + regular.addListener { a -> listeners.forEach { it.accept(a) } } + if (isMekanismLoaded) { mekanism = track(MatteryCapability.MEKANISM_ENERGY) as SubRef @@ -270,6 +285,8 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc } else { actualMekanism = null } + + listeners.forEach { it.accept(get()) } } } else { mekanism = null diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryDeviceBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryDeviceBlockEntity.kt index 3db9d7ab0..81d945102 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryDeviceBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryDeviceBlockEntity.kt @@ -130,11 +130,11 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo inner class Piece(val side: RelativeSide) : IFluidHandler, ITickable { private val ticker = tickList.Ticker(this) - private val controller = sides[side]!!.Cap(ForgeCapabilities.FLUID_HANDLER, this) - private val neighbour by sides[side]!!.track(ForgeCapabilities.FLUID_HANDLER) + private val controller = side(side).Cap(ForgeCapabilities.FLUID_HANDLER, this) + private val neighbour = side(side).track(ForgeCapabilities.FLUID_HANDLER) private fun updateTickerState() { - ticker.isEnabled = (automatePull || automatePush) && flow != FlowDirection.NONE && !redstoneControl.isBlockedByRedstone + ticker.isEnabled = (automatePull || automatePush) && flow != FlowDirection.NONE && !redstoneControl.isBlockedByRedstone && neighbour.get().isPresent } init { @@ -184,6 +184,10 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo updateTickerState() } + neighbour.addListener { + updateTickerState() + } + updateTickerState() } } @@ -192,7 +196,7 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo if (flow == FlowDirection.NONE || !automatePull && !automatePush || redstoneControl.isBlockedByRedstone) return - neighbour.ifPresentK { + neighbour.get().ifPresentK { if (flow.input && automatePull) { moveFluid(source = it, destination = capability) } @@ -334,7 +338,7 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo inner class Piece(val side: RelativeSide, val possibleModes: FlowDirection) : IMatteryEnergyStorage, ITickable { private val capControllers = exposeEnergy(side, this@Piece) - private val neighbour by sides[side]!!.trackEnergy() + private val neighbour = side(side).trackEnergy() override var batteryLevel: Decimal by energy::batteryLevel override val maxBatteryLevel: Decimal by energy::maxBatteryLevel @@ -345,7 +349,7 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo private val ticker = tickList.Ticker(this) private fun updateTickerState() { - ticker.isEnabled = (automatePull || automatePush) && energyFlow != FlowDirection.NONE && !redstoneControl.isBlockedByRedstone + ticker.isEnabled = (automatePull || automatePush) && energyFlow != FlowDirection.NONE && !redstoneControl.isBlockedByRedstone && neighbour.get().isPresent } // var automatePull by synchronizer.bool().property @@ -374,6 +378,10 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo updateTickerState() } + neighbour.addListener { + updateTickerState() + } + updateTickerState() } } @@ -412,7 +420,7 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo if (energyFlow == FlowDirection.NONE || !automatePull && !automatePush || redstoneControl.isBlockedByRedstone) return - neighbour.ifPresentK { + neighbour.get().ifPresentK { if (energyFlow.input && automatePull) { moveEnergy(source = it, destination = energy, simulate = false) } @@ -573,8 +581,8 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo updateTickerState() } - private val capController = sides[side]!!.Cap(ForgeCapabilities.ITEM_HANDLER, this) - private val neighbour by sides[side]!!.track(ForgeCapabilities.ITEM_HANDLER) + private val capController = side(side).Cap(ForgeCapabilities.ITEM_HANDLER, this) + private val neighbour = side(side).track(ForgeCapabilities.ITEM_HANDLER) private val ticker = tickList.Ticker(this) private var innerSlotPull = 0 @@ -584,7 +592,7 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo private var outerSlotPush = 0 private fun updateTickerState() { - ticker.isEnabled = (automatePull || automatePush) && mode != ItemHandlerMode.DISABLED && !redstoneControl.isBlockedByRedstone && currentHandler.slots != 0 + ticker.isEnabled = (automatePull || automatePush) && mode != ItemHandlerMode.DISABLED && !redstoneControl.isBlockedByRedstone && currentHandler.slots != 0 && neighbour.get().isPresent } init { @@ -655,6 +663,10 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo updateTickerState() } + neighbour.addListener { + updateTickerState() + } + updateTickerState() } } @@ -663,7 +675,7 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo if (mode == ItemHandlerMode.DISABLED || !automatePull && !automatePush || redstoneControl.isBlockedByRedstone || currentHandler.slots == 0) return - neighbour.ifPresentK { + neighbour.get().ifPresentK { if (it.slots == 0) return