From bf450e2ff909a0eec71ca5ba00863e8df3930c92 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 8 Jan 2024 10:09:16 +0700 Subject: [PATCH] Add block entity sync backlog handling --- .../mc/otm/block/entity/MatteryBlockEntity.kt | 2 ++ .../mc/otm/network/GenericNetworkChannel.kt | 22 ++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) 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 bfbb8955c..ccfea941e 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 @@ -535,6 +535,8 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc waitForServerLevel.forEach { it.invoke() } } else { waitForServerLevel.clear() + + BlockEntitySyncPacket.applyBacklog(this) } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/network/GenericNetworkChannel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/network/GenericNetworkChannel.kt index ab054cf1d..931fad1fb 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/network/GenericNetworkChannel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/network/GenericNetworkChannel.kt @@ -69,9 +69,7 @@ class BlockEntitySyncPacket(val position: BlockPos, val buffer: ByteArray, val v val blockEntity = level.getBlockEntity(position) if (blockEntity == null) { - LOGGER.warn("Putting BlockEntitySyncPacket received for $position into backlog because there is literally no block entity there!") - LOGGER.warn("This is CERTAINLY a bug in one of optimizing mods you have or server has installed!") - LOGGER.warn("This can cause memory leak.") + LOGGER.warn("Putting BlockEntitySyncPacket received for $position into backlog because there is literally no block entity there! This is CERTAINLY a bug in one of optimizing mods you have or server has installed! This might cause memory leak.") backlog.computeIfAbsent(level) { Object2ObjectOpenHashMap() } .computeIfAbsent(position, Object2ObjectFunction { ArrayList() }) @@ -88,6 +86,8 @@ class BlockEntitySyncPacket(val position: BlockPos, val buffer: ByteArray, val v try { if (packets != null) { + LOGGER.info("Unfolding ${packets.size} backlog packets for $position (${level.getBlockState(position)}/$blockEntity)") + for (packet in packets) { blockEntity.synchronizer.read(FastByteArrayInputStream(packet.buffer, 0, packet.validBytes)) } @@ -115,6 +115,22 @@ class BlockEntitySyncPacket(val position: BlockPos, val buffer: ByteArray, val v } private val LOGGER = LogManager.getLogger() + + internal fun applyBacklog(blockEntity: MatteryBlockEntity) { + val packets = backlog[blockEntity.level]?.remove(blockEntity.blockPos) + + try { + if (packets != null) { + LOGGER.info("Unfolding ${packets.size} backlog packets for ${blockEntity.blockPos} (${blockEntity.level!!.getBlockState(blockEntity.blockPos)}/$blockEntity)") + + for (packet in packets) { + blockEntity.synchronizer.read(FastByteArrayInputStream(packet.buffer, 0, packet.validBytes)) + } + } + } catch(err: Throwable) { + LOGGER.error("Exception while reading synchronized BlockEntity data!\nPosition: ${blockEntity.blockPos}\nBlock: ${blockEntity.level!!.getBlockState(blockEntity.blockPos)}\nBlock entity: $blockEntity", err) + } + } } }