Count non air blocks in a section.

This commit is contained in:
Anders 2021-05-18 15:09:03 +02:00
parent 5ddda986a7
commit 67494255b5
No known key found for this signature in database
GPG Key ID: 51A1CC4FE32C205A
2 changed files with 26 additions and 7 deletions

View File

@ -53,6 +53,8 @@ public class Section implements PublicCloneable<Section> {
private int valuesPerLong; private int valuesPerLong;
private boolean hasPalette; private boolean hasPalette;
private short blockCount = 0;
protected Section(int bitsPerEntry, int bitsIncrement) { protected Section(int bitsPerEntry, int bitsIncrement) {
this.bitsPerEntry = bitsPerEntry; this.bitsPerEntry = bitsPerEntry;
this.bitsIncrement = bitsIncrement; this.bitsIncrement = bitsIncrement;
@ -74,6 +76,9 @@ public class Section implements PublicCloneable<Section> {
blocks = new long[getSize(valuesPerLong)]; 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 // Change to palette value
blockId = getPaletteIndex(blockId); blockId = getPaletteIndex(blockId);
@ -91,6 +96,13 @@ public class Section implements PublicCloneable<Section> {
block ^= clear << bitIndex; block ^= clear << bitIndex;
block |= (long) blockId << 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; blocks[index] = block;
} }
} }
@ -143,6 +155,7 @@ public class Section implements PublicCloneable<Section> {
this.hasPalette = section.hasPalette; this.hasPalette = section.hasPalette;
this.blocks = section.blocks; this.blocks = section.blocks;
this.blockCount = section.blockCount;
} }
/** /**
@ -171,12 +184,22 @@ public class Section implements PublicCloneable<Section> {
this.blocks = new long[0]; this.blocks = new long[0];
this.paletteBlockMap = createPaletteBlockMap(); this.paletteBlockMap = createPaletteBlockMap();
this.blockPaletteMap = createBlockPaletteMap(); this.blockPaletteMap = createBlockPaletteMap();
this.blockCount = 0;
} }
public long[] getBlocks() { public long[] getBlocks() {
return blocks; 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() { public Short2ShortLinkedOpenHashMap getPaletteBlockMap() {
return paletteBlockMap; return paletteBlockMap;
} }
@ -288,6 +311,7 @@ public class Section implements PublicCloneable<Section> {
section.blocks = blocks.clone(); section.blocks = blocks.clone();
section.paletteBlockMap = paletteBlockMap.clone(); section.paletteBlockMap = paletteBlockMap.clone();
section.blockPaletteMap = blockPaletteMap.clone(); section.blockPaletteMap = blockPaletteMap.clone();
section.blockCount = blockCount;
return section; return section;
} catch (CloneNotSupportedException e) { } catch (CloneNotSupportedException e) {
MinecraftServer.getExceptionManager().handleException(e); MinecraftServer.getExceptionManager().handleException(e);

View File

@ -118,16 +118,11 @@ public final class Utils {
} }
public static void writeSectionBlocks(ByteBuf buffer, Section section) { 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(); final int bitsPerEntry = section.getBitsPerEntry();
//buffer.writeShort(count); buffer.writeShort(blockCount);
// TODO count blocks
buffer.writeShort(200);
buffer.writeByte((byte) bitsPerEntry); buffer.writeByte((byte) bitsPerEntry);
// Palette // Palette