From 6fb6687cd09a2fbbbb6574ae90fd5789c0af80b9 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 17 Nov 2023 13:19:17 +0700 Subject: [PATCH] Update block entity synchronization to match 1.20.2 chunk queue changes --- .../dbotthepony/mc/otm/GlobalEventHandler.kt | 13 ++++++++++ .../mc/otm/block/entity/MatteryBlockEntity.kt | 26 +++++++++++++------ .../dbotthepony/mc/otm/core/util/TickList.kt | 13 ++++++++++ 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/GlobalEventHandler.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/GlobalEventHandler.kt index 8f63cf0ec..7e8d4de0f 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/GlobalEventHandler.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/GlobalEventHandler.kt @@ -67,6 +67,19 @@ fun onceServer(inTicks: Int, callback: Runnable): TickList.Timer? { return postServerTick.Timer(inTicks, callback) } +/** + * schedules execution of Runnable somewhere in the future, + * at discretion of tick list + */ +fun sometimeServer(callback: Runnable): TickList.Timer? { + if (!SERVER_IS_LIVE) { + LOGGER.error("Refusing to add ticker $callback while server is dying", IllegalStateException("Server is stopping")) + return null + } + + return postServerTick.sometime(callback) +} + private val isPausedImpl: Boolean get() { val server = _server 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 cd9d6707b..c63c5b0cf 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 @@ -56,6 +56,7 @@ import ru.dbotthepony.mc.otm.network.GenericNetworkChannel import ru.dbotthepony.mc.otm.network.synchronizer.FieldSynchronizer import ru.dbotthepony.mc.otm.once import ru.dbotthepony.mc.otm.onceServer +import ru.dbotthepony.mc.otm.sometimeServer import java.lang.ref.WeakReference import java.util.* import java.util.function.Consumer @@ -612,23 +613,32 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc return blockEntities.any { it.synchronizer.hasObservers } } - fun subscribe(player: ServerPlayer) { - if (players.add(player)) { - veto.add(player) + private fun recheckPlayer(player: ServerPlayer) { + sometimeServer { + if (player in players && !player.hasDisconnected()) { + if (player.connection.chunkSender.isPending(chunkPos)) { + recheckPlayer(player) + } else { + veto.remove(player) - onceServer(10) { - veto.remove(player) - blockEntities.forEach { it.playerListUpdated = true } - - if (!player.hasDisconnected() && player in players) { blockEntities.forEach { + it.playerListUpdated = true it.synchronizeToPlayers(false) } } + } else if (player in players && player.hasDisconnected()) { + unsubscribe(player) } } } + fun subscribe(player: ServerPlayer) { + if (players.add(player)) { + veto.add(player) + recheckPlayer(player) + } + } + fun unsubscribe(player: ServerPlayer): Boolean { if (players.remove(player)) { veto.remove(player) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/TickList.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/TickList.kt index 62cfb08f1..a02a49ca8 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/TickList.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/TickList.kt @@ -24,6 +24,7 @@ class TickList : ITickable { private set var ticks = 0 private set + private var nextSometime = 0 inner class Timer(val timerTicks: Int, val runnable: Runnable) : Comparable { val ringAt = ticks + timerTicks @@ -147,6 +148,18 @@ class TickList : ITickable { return Timer(timerTicks, action) } + /** + * Schedules execution of Runnable somewhere in the future, + * at discretion of this tick list + */ + fun sometime(action: Runnable): Timer { + if (nextSometime < ticks || nextSometime - ticks >= 40) { + nextSometime = ticks + } + + return Timer((++nextSometime) - ticks, action) + } + override fun tick() { if (inTicker) { throw ConcurrentModificationException("Already ticking")