This commit is contained in:
themode 2020-11-11 08:16:42 +01:00
parent 93bae25085
commit 00656d96e0
3 changed files with 29 additions and 31 deletions

View File

@ -76,15 +76,12 @@ public class DynamicChunk extends Chunk {
final int index = getBlockIndex(x, y, z); final int index = getBlockIndex(x, y, z);
// True if the block is not complete air without any custom block capabilities // True if the block is not complete air without any custom block capabilities
final boolean hasBlock = blockStateId != 0 || customBlockId != 0; 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.blockPalette.setBlockAt(x, y, z, blockStateId);
//this.blocksStateId[index] = 0; // Set to air this.customBlockPalette.setBlockAt(x, y, z, customBlockId);
this.customBlockPalette.setBlockAt(x, y, z, (short) 0); // Remove custom block
if (!hasBlock) {
// Block has been deleted, clear cache and return
this.blocksData.remove(index); this.blocksData.remove(index);

View File

@ -744,7 +744,7 @@ public class InstanceContainer extends Instance {
* @param blockPosition the block position * @param blockPosition the block position
* @param blockStateId the new state of the block * @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 blockChangePacket = new BlockChangePacket();
blockChangePacket.blockPosition = blockPosition; blockChangePacket.blockPosition = blockPosition;
blockChangePacket.blockStateId = blockStateId; blockChangePacket.blockStateId = blockStateId;

View File

@ -1,5 +1,7 @@
package net.minestom.server.instance.palette; package net.minestom.server.instance.palette;
import net.minestom.server.utils.chunk.ChunkUtils;
import static net.minestom.server.instance.Chunk.*; import static net.minestom.server.instance.Chunk.*;
public class PaletteStorage { public class PaletteStorage {
@ -7,7 +9,7 @@ public class PaletteStorage {
private int bitsPerEntry; private int bitsPerEntry;
private int valuesPerLong; 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) { public PaletteStorage(int bitsPerEntry) {
this.bitsPerEntry = bitsPerEntry; this.bitsPerEntry = bitsPerEntry;
@ -24,13 +26,12 @@ public class PaletteStorage {
z = CHUNK_SIZE_Z + z; z = CHUNK_SIZE_Z + z;
} }
int sectionY = y % CHUNK_SECTION_SIZE; final int sectionIndex = getSectionIndex(x, y % CHUNK_SECTION_SIZE, z);
int sectionIndex = (((sectionY * 16) + z) * 16) + x;
final int index = sectionIndex / valuesPerLong; final int index = sectionIndex / valuesPerLong;
final int bitIndex = (sectionIndex % valuesPerLong) * bitsPerEntry; 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 (sectionBlocks[section].length == 0) {
if (blockId == 0) { if (blockId == 0) {
@ -44,10 +45,8 @@ public class PaletteStorage {
long block = sectionBlock[index]; long block = sectionBlock[index];
{ {
if (blockId != 0) { long cacheMask = (1L << bitIndex) - 1L;
long shiftCount = (long) bitIndex; long cache = block & cacheMask;
long cacheMask = (1L << shiftCount) - 1L;
long cache = block & cacheMask;
/*System.out.println("blockId "+blockId); /*System.out.println("blockId "+blockId);
System.out.println("bitIndex "+bitIndex); System.out.println("bitIndex "+bitIndex);
@ -55,17 +54,16 @@ public class PaletteStorage {
System.out.println("mask "+binary(cacheMask)); System.out.println("mask "+binary(cacheMask));
System.out.println("cache "+binary(cache));*/ System.out.println("cache "+binary(cache));*/
block = block >> shiftCount << shiftCount; block = block >> bitIndex << bitIndex;
//System.out.println("block "+binary(block)); //System.out.println("block "+binary(block));
block = block | (long) blockId; block = block | blockId;
//System.out.println("block2 "+binary(block)); //System.out.println("block2 "+binary(block));
block = (block << shiftCount); block = (block << bitIndex);
//System.out.println("block3 "+binary(block)); //System.out.println("block3 "+binary(block));
block = block | cache; block = block | cache;
//System.out.println("block4 "+binary(block)); //System.out.println("block4 "+binary(block));
sectionBlock[index] = block; sectionBlock[index] = block;
}
} }
} }
@ -80,13 +78,12 @@ public class PaletteStorage {
z = CHUNK_SIZE_Z + z; z = CHUNK_SIZE_Z + z;
} }
int sectionY = y % CHUNK_SECTION_SIZE; final int sectionIndex = getSectionIndex(x, y % CHUNK_SECTION_SIZE, z);
int sectionIndex = (((sectionY * 16) + z) * 16) + x;
final int index = sectionIndex / valuesPerLong; final int index = sectionIndex / valuesPerLong;
final int bitIndex = sectionIndex % valuesPerLong * bitsPerEntry; final int bitIndex = sectionIndex % valuesPerLong * bitsPerEntry;
final int section = y / CHUNK_SECTION_SIZE; final int section = ChunkUtils.getSectionAt(y);
long[] blocks = sectionBlocks[section]; long[] blocks = sectionBlocks[section];
@ -118,7 +115,7 @@ public class PaletteStorage {
System.out.println("mask " + binary(mask)); System.out.println("mask " + binary(mask));
System.out.println("bin " + binary(blocks[index])); System.out.println("bin " + binary(blocks[index]));
System.out.println("result " + ((blocks[index] >> bitIndex) & mask));*/ System.out.println("result " + ((blocks[index] >> bitIndex) & mask));*/
return (short) (finalValue); return (short) finalValue;
} }
private int getSize() { private int getSize() {
@ -147,4 +144,8 @@ public class PaletteStorage {
return "0b" + Long.toBinaryString(value); return "0b" + Long.toBinaryString(value);
} }
private int getSectionIndex(int x, int y, int z) {
return (((y * CHUNK_SECTION_SIZE) + z) * CHUNK_SECTION_SIZE) + x;
}
} }