package protocolsupport.protocol.types.chunk;

import io.netty.buffer.ByteBuf;
import protocolsupport.protocol.serializer.VarNumberSerializer;
import protocolsupport.protocol.utils.NumberBitsStoragePadded;

public class ChunkSectonBlockData extends NumberBitsStoragePadded {

	public static ChunkSectonBlockData readFromStream(ByteBuf stream) {
		short nonAirBlockCount = stream.readShort();
		byte bitsPerBlock = stream.readByte();
		short[] palette = ChunkConstants.GLOBAL_PALETTE;
		if (bitsPerBlock != ChunkConstants.GLOBAL_PALETTE_BITS_PER_BLOCK) {
			palette = new short[VarNumberSerializer.readVarInt(stream)];
			for (int i = 0; i < palette.length; i++) {
				palette[i] = (short) VarNumberSerializer.readVarInt(stream);
			}
		}
		long[] blockdata = new long[VarNumberSerializer.readVarInt(stream)];
		for (int i = 0; i < blockdata.length; i++) {
			blockdata[i] = stream.readLong();
		}
		return new ChunkSectonBlockData(nonAirBlockCount, palette, bitsPerBlock, blockdata);
	}

	protected final short nonAirBlockCount;
	protected final short[] palette;

	public ChunkSectonBlockData(short nonAirBlockCount, short[] palette, int bitsPerBlock, long[] blockdata) {
		super(bitsPerBlock, blockdata);
		this.nonAirBlockCount = nonAirBlockCount;
		this.palette = palette;
	}

	public int getNonAirBlockCount() {
		return nonAirBlockCount;
	}

	public short[] getPalette() {
		return palette;
	}

	public short getBlockData(int blockindex) {
		return palette[getNumber(blockindex)];
	}

}
@@ -407,16 +407,38 @@
 Block = 
+normalizeBitsPerBlock(
 stream.r
@@ -446,16 +446,17 @@
 adByte()
+)
 ;%0A%09%09shor
@@ -1004,16 +1004,255 @@
 a);%0A%09%7D%0A%0A
+%09protected static byte normalizeBitsPerBlock(byte bitsPerBlock) %7B%0A%09%09if (bitsPerBlock %3C= 4) %7B%0A%09%09%09return 4;%0A%09%09%7D else if (bitsPerBlock %3C= 8) %7B%0A%09%09%09return bitsPerBlock;%0A%09%09%7D else %7B%0A%09%09%09return ChunkConstants.GLOBAL_PALETTE_BITS_PER_BLOCK;%0A%09%09%7D%0A%09%7D%0A%0A
 %09protect

Normalize chunk section bits per block before read