Fix crash related to tracking mekanism energy

This commit is contained in:
DBotThePony 2023-07-22 11:58:04 +07:00
parent e8d7f0225c
commit d2c2fbd3d2
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -64,6 +64,7 @@ import ru.dbotthepony.mc.otm.once
import ru.dbotthepony.mc.otm.onceServer import ru.dbotthepony.mc.otm.onceServer
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import java.util.* import java.util.*
import java.util.function.Consumer
import java.util.function.Supplier import java.util.function.Supplier
import java.util.stream.Stream import java.util.stream.Stream
import kotlin.NoSuchElementException import kotlin.NoSuchElementException
@ -220,17 +221,17 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
if (value.isPresent) if (value.isPresent)
for (tracker in listeners) for (tracker in listeners)
tracker.run() tracker.accept(value)
} }
} }
private val listeners = ArrayList<Runnable>(0) private val listeners = ArrayList<Consumer<LazyOptional<T>>>(0)
fun addListener(listener: Runnable) { fun addListener(listener: Consumer<LazyOptional<T>>) {
listeners.add(listener) listeners.add(listener)
if (value.isPresent) if (value.isPresent)
listener.run() listener.accept(value)
} }
override fun get(): LazyOptional<T> { override fun get(): LazyOptional<T> {
@ -266,7 +267,12 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
mekanism.addListener { mekanism.addListener {
actualMekanism?.invalidate() actualMekanism?.invalidate()
actualMekanism = LazyOptional.of { Mekanism2MatteryEnergyWrapper(mekanism.value.orElseThrow { NoSuchElementException() }) }
if (it.isPresent) {
actualMekanism = LazyOptional.of { Mekanism2MatteryEnergyWrapper(it.orElseThrow { NoSuchElementException("No mekanism energy was present, and tracker did not call back in time") }) }
} else {
actualMekanism = null
}
} }
} else { } else {
mekanism = null mekanism = null