From 00656d96e0133c1f6acc7d78003acb38a4e2f918 Mon Sep 17 00:00:00 2001 From: themode Date: Wed, 11 Nov 2020 08:16:42 +0100 Subject: [PATCH] Cleanup --- .../server/instance/DynamicChunk.java | 13 +++--- .../server/instance/InstanceContainer.java | 2 +- .../instance/palette/PaletteStorage.java | 45 ++++++++++--------- 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/DynamicChunk.java b/src/main/java/net/minestom/server/instance/DynamicChunk.java index 8bf1d474b..ad441612d 100644 --- a/src/main/java/net/minestom/server/instance/DynamicChunk.java +++ b/src/main/java/net/minestom/server/instance/DynamicChunk.java @@ -76,15 +76,12 @@ public class DynamicChunk extends Chunk { final int index = getBlockIndex(x, y, z); // True if the block is not complete air without any custom block capabilities final boolean hasBlock = blockStateId != 0 || customBlockId != 0; - if (hasBlock) { - this.blockPalette.setBlockAt(x, y, z, blockStateId); - this.customBlockPalette.setBlockAt(x, y, z, customBlockId); - } else { - // Block has been deleted, clear cache and return - this.blockPalette.setBlockAt(x, y, z, (short) 0); - //this.blocksStateId[index] = 0; // Set to air - this.customBlockPalette.setBlockAt(x, y, z, (short) 0); // Remove custom block + this.blockPalette.setBlockAt(x, y, z, blockStateId); + this.customBlockPalette.setBlockAt(x, y, z, customBlockId); + + if (!hasBlock) { + // Block has been deleted, clear cache and return this.blocksData.remove(index); diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index c6a7ff65b..b350cd5db 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -744,7 +744,7 @@ public class InstanceContainer extends Instance { * @param blockPosition the block position * @param blockStateId the new state of the block */ - private void sendBlockChange(Chunk chunk, BlockPosition blockPosition, short blockStateId) { + private void sendBlockChange(@NotNull Chunk chunk, @NotNull BlockPosition blockPosition, short blockStateId) { BlockChangePacket blockChangePacket = new BlockChangePacket(); blockChangePacket.blockPosition = blockPosition; blockChangePacket.blockStateId = blockStateId; diff --git a/src/main/java/net/minestom/server/instance/palette/PaletteStorage.java b/src/main/java/net/minestom/server/instance/palette/PaletteStorage.java index e829e2095..ae67993f2 100644 --- a/src/main/java/net/minestom/server/instance/palette/PaletteStorage.java +++ b/src/main/java/net/minestom/server/instance/palette/PaletteStorage.java @@ -1,5 +1,7 @@ package net.minestom.server.instance.palette; +import net.minestom.server.utils.chunk.ChunkUtils; + import static net.minestom.server.instance.Chunk.*; public class PaletteStorage { @@ -7,7 +9,7 @@ public class PaletteStorage { private int bitsPerEntry; private int valuesPerLong; - protected long[][] sectionBlocks = new long[CHUNK_SECTION_COUNT][0]; + private long[][] sectionBlocks = new long[CHUNK_SECTION_COUNT][0]; public PaletteStorage(int bitsPerEntry) { this.bitsPerEntry = bitsPerEntry; @@ -24,13 +26,12 @@ public class PaletteStorage { z = CHUNK_SIZE_Z + z; } - int sectionY = y % CHUNK_SECTION_SIZE; - int sectionIndex = (((sectionY * 16) + z) * 16) + x; + final int sectionIndex = getSectionIndex(x, y % CHUNK_SECTION_SIZE, z); final int index = sectionIndex / valuesPerLong; final int bitIndex = (sectionIndex % valuesPerLong) * bitsPerEntry; - final int section = y / CHUNK_SECTION_SIZE; + final int section = ChunkUtils.getSectionAt(y); if (sectionBlocks[section].length == 0) { if (blockId == 0) { @@ -44,10 +45,8 @@ public class PaletteStorage { long block = sectionBlock[index]; { - if (blockId != 0) { - long shiftCount = (long) bitIndex; - long cacheMask = (1L << shiftCount) - 1L; - long cache = block & cacheMask; + long cacheMask = (1L << bitIndex) - 1L; + long cache = block & cacheMask; /*System.out.println("blockId "+blockId); System.out.println("bitIndex "+bitIndex); @@ -55,17 +54,16 @@ public class PaletteStorage { System.out.println("mask "+binary(cacheMask)); System.out.println("cache "+binary(cache));*/ - block = block >> shiftCount << shiftCount; - //System.out.println("block "+binary(block)); - block = block | (long) blockId; - //System.out.println("block2 "+binary(block)); - block = (block << shiftCount); - //System.out.println("block3 "+binary(block)); - block = block | cache; - //System.out.println("block4 "+binary(block)); + block = block >> bitIndex << bitIndex; + //System.out.println("block "+binary(block)); + block = block | blockId; + //System.out.println("block2 "+binary(block)); + block = (block << bitIndex); + //System.out.println("block3 "+binary(block)); + block = block | cache; + //System.out.println("block4 "+binary(block)); - sectionBlock[index] = block; - } + sectionBlock[index] = block; } } @@ -80,13 +78,12 @@ public class PaletteStorage { z = CHUNK_SIZE_Z + z; } - int sectionY = y % CHUNK_SECTION_SIZE; - int sectionIndex = (((sectionY * 16) + z) * 16) + x; + final int sectionIndex = getSectionIndex(x, y % CHUNK_SECTION_SIZE, z); final int index = sectionIndex / valuesPerLong; final int bitIndex = sectionIndex % valuesPerLong * bitsPerEntry; - final int section = y / CHUNK_SECTION_SIZE; + final int section = ChunkUtils.getSectionAt(y); long[] blocks = sectionBlocks[section]; @@ -118,7 +115,7 @@ public class PaletteStorage { System.out.println("mask " + binary(mask)); System.out.println("bin " + binary(blocks[index])); System.out.println("result " + ((blocks[index] >> bitIndex) & mask));*/ - return (short) (finalValue); + return (short) finalValue; } private int getSize() { @@ -147,4 +144,8 @@ public class PaletteStorage { return "0b" + Long.toBinaryString(value); } + private int getSectionIndex(int x, int y, int z) { + return (((y * CHUNK_SECTION_SIZE) + z) * CHUNK_SECTION_SIZE) + x; + } + }