Merge pull request #299 from Uraxys/chunk-block-count

Send the real amount of blocks in a chunk section.
This commit is contained in:
TheMode 2021-05-20 19:19:08 +02:00 committed by GitHub
commit d871cb2c9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 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);
@ -87,10 +92,20 @@ public class Section implements PublicCloneable<Section> {
{ {
final long clear = MAGIC_MASKS[bitsPerEntry]; final long clear = MAGIC_MASKS[bitsPerEntry];
final long value = block >> bitIndex & clear;
final boolean isCurrentAir = Block.fromStateId(
hasPalette ? paletteBlockMap.get((short) value) : (short) value).isAir();
block |= clear << bitIndex; block |= clear << bitIndex;
block ^= clear << bitIndex; block ^= clear << bitIndex;
block |= (long) blockId << bitIndex; block |= (long) blockId << bitIndex;
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 +158,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 +187,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 +314,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