From 67494255b5832a9696c668f1ae8637e738ed01a7 Mon Sep 17 00:00:00 2001 From: Anders Date: Tue, 18 May 2021 15:09:03 +0200 Subject: [PATCH] Count non air blocks in a section. --- .../server/instance/palette/Section.java | 24 +++++++++++++++++++ .../java/net/minestom/server/utils/Utils.java | 9 ++----- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/palette/Section.java b/src/main/java/net/minestom/server/instance/palette/Section.java index b95ae63c7..392a17879 100644 --- a/src/main/java/net/minestom/server/instance/palette/Section.java +++ b/src/main/java/net/minestom/server/instance/palette/Section.java @@ -53,6 +53,8 @@ public class Section implements PublicCloneable
{ private int valuesPerLong; private boolean hasPalette; + private short blockCount = 0; + protected Section(int bitsPerEntry, int bitsIncrement) { this.bitsPerEntry = bitsPerEntry; this.bitsIncrement = bitsIncrement; @@ -74,6 +76,9 @@ public class Section implements PublicCloneable
{ blocks = new long[getSize(valuesPerLong)]; } + // Check if the new block is air, used for counting none air blocks. + final boolean isAir = Block.fromStateId(blockId).isAir(); + // Change to palette value blockId = getPaletteIndex(blockId); @@ -91,6 +96,13 @@ public class Section implements PublicCloneable
{ block ^= clear << bitIndex; block |= (long) blockId << bitIndex; + boolean isCurrentAir = Block.fromStateId(getBlockAt(x, y, z)).isAir(); + if (!isCurrentAir && isAir) { // The old block isn't air & the new block is. + this.blockCount--; + } else if (isCurrentAir && !isAir) { // The old block is air & the new block isn't. + this.blockCount++; + } // If both block are air or not air then don't change the value. + blocks[index] = block; } } @@ -143,6 +155,7 @@ public class Section implements PublicCloneable
{ this.hasPalette = section.hasPalette; this.blocks = section.blocks; + this.blockCount = section.blockCount; } /** @@ -171,12 +184,22 @@ public class Section implements PublicCloneable
{ this.blocks = new long[0]; this.paletteBlockMap = createPaletteBlockMap(); this.blockPaletteMap = createBlockPaletteMap(); + this.blockCount = 0; } public long[] getBlocks() { return blocks; } + /** + * Get the amount of non air blocks in this section. + * + * @return The amount of blocks in this section. + */ + public short getBlockCount() { + return blockCount; + } + public Short2ShortLinkedOpenHashMap getPaletteBlockMap() { return paletteBlockMap; } @@ -288,6 +311,7 @@ public class Section implements PublicCloneable
{ section.blocks = blocks.clone(); section.paletteBlockMap = paletteBlockMap.clone(); section.blockPaletteMap = blockPaletteMap.clone(); + section.blockCount = blockCount; return section; } catch (CloneNotSupportedException e) { MinecraftServer.getExceptionManager().handleException(e); diff --git a/src/main/java/net/minestom/server/utils/Utils.java b/src/main/java/net/minestom/server/utils/Utils.java index a32c1470d..ac694e669 100644 --- a/src/main/java/net/minestom/server/utils/Utils.java +++ b/src/main/java/net/minestom/server/utils/Utils.java @@ -118,16 +118,11 @@ public final class Utils { } public static void writeSectionBlocks(ByteBuf buffer, Section section) { - /*short count = 0; - for (short id : blocksId) - if (id != 0) - count++;*/ + final short blockCount = section.getBlockCount(); final int bitsPerEntry = section.getBitsPerEntry(); - //buffer.writeShort(count); - // TODO count blocks - buffer.writeShort(200); + buffer.writeShort(blockCount); buffer.writeByte((byte) bitsPerEntry); // Palette