Don't tick side if no neighbour is present

This commit is contained in:
DBotThePony 2023-08-03 20:01:10 +07:00
parent 109de2b414
commit 98afdd1671
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 46 additions and 17 deletions

View File

@ -101,6 +101,8 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
private val _sides = EnumMap<RelativeSide, Side>(RelativeSide::class.java) private val _sides = EnumMap<RelativeSide, Side>(RelativeSide::class.java)
val sides: Map<RelativeSide, Side> = Collections.unmodifiableMap(_sides) val sides: Map<RelativeSide, Side> = Collections.unmodifiableMap(_sides)
fun side(side: RelativeSide) = sides[side]!!
private data class SidelessCap<T : Any>(val cap: T, var optional: LazyOptional<T>) private data class SidelessCap<T : Any>(val cap: T, var optional: LazyOptional<T>)
private val sidelessCaps = Reference2ObjectOpenHashMap<Capability<*>, SidelessCap<*>>() private val sidelessCaps = Reference2ObjectOpenHashMap<Capability<*>, SidelessCap<*>>()
protected val tickList = TickList() 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 }) return SupplierList(_sides.values.map { it.track(capability)::get })
} }
interface SideListener<T> : Supplier<LazyOptional<T>> {
fun addListener(listener: Consumer<LazyOptional<T>>)
}
inner class Side(val side: RelativeSide) { inner class Side(val side: RelativeSide) {
init { init {
check(!_sides.containsKey(side)) { "dafuq are you trying to do" } 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<Capability<*>, SubRef<*>>() private val subscriptions = Reference2ObjectOpenHashMap<Capability<*>, SubRef<*>>()
private val knownLOs = WeakHashSet<LazyOptional<*>>() private val knownLOs = WeakHashSet<LazyOptional<*>>()
private inner class SubRef<T>(value: LazyOptional<T>) : Supplier<LazyOptional<T>> { private inner class SubRef<T>(value: LazyOptional<T>) : SideListener<T> {
var value: LazyOptional<T> = value var value: LazyOptional<T> = value
set(value) { set(value) {
if (value !== field) { if (value !== field) {
@ -226,7 +232,7 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
private val listeners = ArrayList<Consumer<LazyOptional<T>>>(0) private val listeners = ArrayList<Consumer<LazyOptional<T>>>(0)
fun addListener(listener: Consumer<LazyOptional<T>>) { override fun addListener(listener: Consumer<LazyOptional<T>>) {
listeners.add(listener) listeners.add(listener)
listener.accept(value) listener.accept(value)
} }
@ -240,8 +246,8 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
} }
} }
fun <T> track(capability: Capability<T>): Supplier<LazyOptional<T>> { fun <T> track(capability: Capability<T>): SideListener<T> {
var subref = subscriptions[capability] as Supplier<LazyOptional<T>>? var subref = subscriptions[capability] as SideListener<T>?
if (subref == null) { if (subref == null) {
subref = SubRef(LazyOptional.empty<Any?>()) as SubRef<T> subref = SubRef(LazyOptional.empty<Any?>()) as SubRef<T>
@ -252,13 +258,22 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
return subref return subref
} }
fun trackEnergy(): Supplier<LazyOptional<IEnergyStorage>> { fun trackEnergy(): SideListener<IEnergyStorage> {
return object : Supplier<LazyOptional<IEnergyStorage>> { return object : SideListener<IEnergyStorage> {
private val regular = track(ForgeCapabilities.ENERGY) private val regular = track(ForgeCapabilities.ENERGY)
private val mekanism: SubRef<IStrictEnergyHandler>? private val mekanism: SubRef<IStrictEnergyHandler>?
private var actualMekanism: LazyOptional<IEnergyStorage>? = null private var actualMekanism: LazyOptional<IEnergyStorage>? = null
private val listeners = ArrayList<Consumer<LazyOptional<IEnergyStorage>>>()
override fun addListener(listener: Consumer<LazyOptional<IEnergyStorage>>) {
listeners.add(listener)
listener.accept(get())
}
init { init {
regular.addListener { a -> listeners.forEach { it.accept(a) } }
if (isMekanismLoaded) { if (isMekanismLoaded) {
mekanism = track(MatteryCapability.MEKANISM_ENERGY) as SubRef<IStrictEnergyHandler> mekanism = track(MatteryCapability.MEKANISM_ENERGY) as SubRef<IStrictEnergyHandler>
@ -270,6 +285,8 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
} else { } else {
actualMekanism = null actualMekanism = null
} }
listeners.forEach { it.accept(get()) }
} }
} else { } else {
mekanism = null mekanism = null

View File

@ -130,11 +130,11 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo
inner class Piece(val side: RelativeSide) : IFluidHandler, ITickable { inner class Piece(val side: RelativeSide) : IFluidHandler, ITickable {
private val ticker = tickList.Ticker(this) private val ticker = tickList.Ticker(this)
private val controller = sides[side]!!.Cap(ForgeCapabilities.FLUID_HANDLER, this) private val controller = side(side).Cap(ForgeCapabilities.FLUID_HANDLER, this)
private val neighbour by sides[side]!!.track(ForgeCapabilities.FLUID_HANDLER) private val neighbour = side(side).track(ForgeCapabilities.FLUID_HANDLER)
private fun updateTickerState() { 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 { init {
@ -184,6 +184,10 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo
updateTickerState() updateTickerState()
} }
neighbour.addListener {
updateTickerState()
}
updateTickerState() updateTickerState()
} }
} }
@ -192,7 +196,7 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo
if (flow == FlowDirection.NONE || !automatePull && !automatePush || redstoneControl.isBlockedByRedstone) if (flow == FlowDirection.NONE || !automatePull && !automatePush || redstoneControl.isBlockedByRedstone)
return return
neighbour.ifPresentK { neighbour.get().ifPresentK {
if (flow.input && automatePull) { if (flow.input && automatePull) {
moveFluid(source = it, destination = capability) 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 { inner class Piece(val side: RelativeSide, val possibleModes: FlowDirection) : IMatteryEnergyStorage, ITickable {
private val capControllers = exposeEnergy(side, this@Piece) 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 var batteryLevel: Decimal by energy::batteryLevel
override val maxBatteryLevel: Decimal by energy::maxBatteryLevel override val maxBatteryLevel: Decimal by energy::maxBatteryLevel
@ -345,7 +349,7 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo
private val ticker = tickList.Ticker(this) private val ticker = tickList.Ticker(this)
private fun updateTickerState() { 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 // var automatePull by synchronizer.bool().property
@ -374,6 +378,10 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo
updateTickerState() updateTickerState()
} }
neighbour.addListener {
updateTickerState()
}
updateTickerState() updateTickerState()
} }
} }
@ -412,7 +420,7 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo
if (energyFlow == FlowDirection.NONE || !automatePull && !automatePush || redstoneControl.isBlockedByRedstone) if (energyFlow == FlowDirection.NONE || !automatePull && !automatePush || redstoneControl.isBlockedByRedstone)
return return
neighbour.ifPresentK { neighbour.get().ifPresentK {
if (energyFlow.input && automatePull) { if (energyFlow.input && automatePull) {
moveEnergy(source = it, destination = energy, simulate = false) moveEnergy(source = it, destination = energy, simulate = false)
} }
@ -573,8 +581,8 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo
updateTickerState() updateTickerState()
} }
private val capController = sides[side]!!.Cap(ForgeCapabilities.ITEM_HANDLER, this) private val capController = side(side).Cap(ForgeCapabilities.ITEM_HANDLER, this)
private val neighbour by sides[side]!!.track(ForgeCapabilities.ITEM_HANDLER) private val neighbour = side(side).track(ForgeCapabilities.ITEM_HANDLER)
private val ticker = tickList.Ticker(this) private val ticker = tickList.Ticker(this)
private var innerSlotPull = 0 private var innerSlotPull = 0
@ -584,7 +592,7 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo
private var outerSlotPush = 0 private var outerSlotPush = 0
private fun updateTickerState() { 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 { init {
@ -655,6 +663,10 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo
updateTickerState() updateTickerState()
} }
neighbour.addListener {
updateTickerState()
}
updateTickerState() updateTickerState()
} }
} }
@ -663,7 +675,7 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo
if (mode == ItemHandlerMode.DISABLED || !automatePull && !automatePush || redstoneControl.isBlockedByRedstone || currentHandler.slots == 0) if (mode == ItemHandlerMode.DISABLED || !automatePull && !automatePush || redstoneControl.isBlockedByRedstone || currentHandler.slots == 0)
return return
neighbour.ifPresentK { neighbour.get().ifPresentK {
if (it.slots == 0) if (it.slots == 0)
return return