Workaround rare race condition related to setLevel racing with other minecraft code

This commit is contained in:
DBotThePony 2025-02-08 00:16:30 +07:00
parent b9d85b7b17
commit a0f605c396
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -54,6 +54,7 @@ import ru.dbotthepony.mc.otm.core.util.TickList
import ru.dbotthepony.mc.otm.core.util.countingLazy
import ru.dbotthepony.mc.otm.network.BlockEntitySyncPacket
import ru.dbotthepony.mc.otm.network.syncher.SynchableGroup
import ru.dbotthepony.mc.otm.once
import ru.dbotthepony.mc.otm.onceServer
import ru.dbotthepony.mc.otm.registry.game.MBlocks
import ru.dbotthepony.mc.otm.sometimeServer
@ -240,7 +241,10 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
protected fun waitForServerLevel(lambda: () -> Unit) {
if (level is ServerLevel) {
lambda.invoke()
if (executeWaitForServerLevel)
lambda.invoke()
else
waitForServerLevel.add(lambda)
} else if (level == null) {
waitForServerLevel.add(lambda)
}
@ -371,6 +375,7 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
val syncher = SynchableGroup()
private val synchers = Object2ObjectArrayMap<ServerPlayer, SynchableGroup.Remote>()
private var executeWaitForServerLevel = false
override fun setLevel(level: Level) {
val old = this.level
@ -380,9 +385,15 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
if (!level.isClientSide) {
subscribe()
waitForServerLevel.forEach { it.invoke() }
waitForServerLevel.clear()
level.once {
if (isRemoved) return@once
executeWaitForServerLevel = true
waitForServerLevel.forEach { it.invoke() }
waitForServerLevel.clear()
}
} else {
executeWaitForServerLevel = true
waitForServerLevel.clear()
BlockEntitySyncPacket.applyBacklog(this)
}