From 11f207fc542f7886b9c405a73d55e5740392deb7 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Wed, 31 May 2023 21:38:05 +0200 Subject: [PATCH] block changing: minimal performance improvements --- .../registries/blocks/state/BlockState.kt | 2 +- .../blocks/state/PropertyBlockState.kt | 2 +- .../minosoft/data/world/chunk/chunk/Chunk.kt | 25 ++++++++++++------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/data/registries/blocks/state/BlockState.kt b/src/main/java/de/bixilon/minosoft/data/registries/blocks/state/BlockState.kt index 5731f36c3..c50137083 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/blocks/state/BlockState.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/blocks/state/BlockState.kt @@ -34,7 +34,7 @@ open class BlockState( override fun equals(other: Any?): Boolean { if (other is ResourceLocation) return other == block.identifier - if (other is BlockState) return other.block == block + if (other is BlockState) return other.block == block && other.luminance == luminance return false } diff --git a/src/main/java/de/bixilon/minosoft/data/registries/blocks/state/PropertyBlockState.kt b/src/main/java/de/bixilon/minosoft/data/registries/blocks/state/PropertyBlockState.kt index bd6be3b13..00eaaf19a 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/blocks/state/PropertyBlockState.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/blocks/state/PropertyBlockState.kt @@ -37,7 +37,7 @@ open class PropertyBlockState( override fun equals(other: Any?): Boolean { if (other is ResourceLocation) return other == block.identifier - if (other is PropertyBlockState) return other.block == this.block && other.properties == this.properties + if (other is PropertyBlockState) return other.block == block && other.luminance == luminance && other.properties == this.properties return false } diff --git a/src/main/java/de/bixilon/minosoft/data/world/chunk/chunk/Chunk.kt b/src/main/java/de/bixilon/minosoft/data/world/chunk/chunk/Chunk.kt index ca9dfe343..6c6053ca7 100644 --- a/src/main/java/de/bixilon/minosoft/data/world/chunk/chunk/Chunk.kt +++ b/src/main/java/de/bixilon/minosoft/data/world/chunk/chunk/Chunk.kt @@ -142,27 +142,34 @@ class Chunk( if (updates.isEmpty()) return if (updates.size == 1) return apply(updates.first()) - lock.lock() val executed: MutableSet = hashSetOf() val sections: MutableSet = hashSetOf() + + lock.lock() for (update in updates) { - val section = getOrPut(update.position.y.sectionHeight, lock = false) ?: continue + val sectionHeight = update.position.y.sectionHeight + var section = this[sectionHeight] + if (update.state == null && section == null) continue + + section = getOrPut(sectionHeight, lock = false) ?: continue val previous = section.blocks.noOcclusionSet(update.position.x, update.position.y.inSectionHeight, update.position.z, update.state) if (previous == update.state) continue getOrPutBlockEntity(update.position) executed += update sections += section } - if (executed.isNotEmpty()) { - for (section in sections) { - section.blocks.occlusion.recalculate(true) - } - light.recalculateHeightmap() - light.recalculate() + + if (executed.isEmpty()) { + return lock.unlock() + } + light.recalculateHeightmap() + light.recalculate() + + for (section in sections) { + section.blocks.occlusion.recalculate(true) } lock.unlock() - if (executed.isEmpty()) return ChunkLocalBlockUpdate(chunkPosition, this, executed).fire(connection) }