Don't tick side if no neighbour is present
This commit is contained in:
parent
109de2b414
commit
98afdd1671
@ -101,6 +101,8 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
|
||||
private val _sides = EnumMap<RelativeSide, Side>(RelativeSide::class.java)
|
||||
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 val sidelessCaps = Reference2ObjectOpenHashMap<Capability<*>, 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<T> : Supplier<LazyOptional<T>> {
|
||||
fun addListener(listener: Consumer<LazyOptional<T>>)
|
||||
}
|
||||
|
||||
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<Capability<*>, SubRef<*>>()
|
||||
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
|
||||
set(value) {
|
||||
if (value !== field) {
|
||||
@ -226,7 +232,7 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
|
||||
|
||||
private val listeners = ArrayList<Consumer<LazyOptional<T>>>(0)
|
||||
|
||||
fun addListener(listener: Consumer<LazyOptional<T>>) {
|
||||
override fun addListener(listener: Consumer<LazyOptional<T>>) {
|
||||
listeners.add(listener)
|
||||
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>> {
|
||||
var subref = subscriptions[capability] as Supplier<LazyOptional<T>>?
|
||||
fun <T> track(capability: Capability<T>): SideListener<T> {
|
||||
var subref = subscriptions[capability] as SideListener<T>?
|
||||
|
||||
if (subref == null) {
|
||||
subref = SubRef(LazyOptional.empty<Any?>()) as SubRef<T>
|
||||
@ -252,13 +258,22 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
|
||||
return subref
|
||||
}
|
||||
|
||||
fun trackEnergy(): Supplier<LazyOptional<IEnergyStorage>> {
|
||||
return object : Supplier<LazyOptional<IEnergyStorage>> {
|
||||
fun trackEnergy(): SideListener<IEnergyStorage> {
|
||||
return object : SideListener<IEnergyStorage> {
|
||||
private val regular = track(ForgeCapabilities.ENERGY)
|
||||
private val mekanism: SubRef<IStrictEnergyHandler>?
|
||||
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 {
|
||||
regular.addListener { a -> listeners.forEach { it.accept(a) } }
|
||||
|
||||
if (isMekanismLoaded) {
|
||||
mekanism = track(MatteryCapability.MEKANISM_ENERGY) as SubRef<IStrictEnergyHandler>
|
||||
|
||||
@ -270,6 +285,8 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
|
||||
} else {
|
||||
actualMekanism = null
|
||||
}
|
||||
|
||||
listeners.forEach { it.accept(get()) }
|
||||
}
|
||||
} else {
|
||||
mekanism = null
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user