Add block entity sync backlog handling

This commit is contained in:
DBotThePony 2024-01-08 10:09:16 +07:00
parent c04830a3bb
commit bf450e2ff9
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 21 additions and 3 deletions

View File

@ -535,6 +535,8 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
waitForServerLevel.forEach { it.invoke() }
} else {
waitForServerLevel.clear()
BlockEntitySyncPacket.applyBacklog(this)
}
}

View File

@ -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)
}
}
}
}