Java Code Examples for us.myles.ViaVersion.api.minecraft.chunks.ChunkSection#SIZE

The following examples show how to use us.myles.ViaVersion.api.minecraft.chunks.ChunkSection#SIZE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChunkSectionType1_8.java    From ViaVersion with MIT License 6 votes vote down vote up
@Override
public ChunkSection read(ByteBuf buffer) throws Exception {
    ChunkSection chunkSection = new ChunkSection();
    // Don't clear palette because 0 index needs to be air in 1.9 version

    ByteBuf littleEndianView = buffer.order(ByteOrder.LITTLE_ENDIAN);

    for (int i = 0; i < ChunkSection.SIZE; i++) {
        int mask = littleEndianView.readShort();
        int type = mask >> 4;
        int data = mask & 0xF;
        chunkSection.setBlock(i, type, data);
    }

    return chunkSection;
}
 
Example 2
Source File: ChunkSectionType1_16.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public ChunkSection read(ByteBuf buffer) throws Exception {
    ChunkSection chunkSection = new ChunkSection();

    // Reaad bits per block
    int bitsPerBlock = buffer.readUnsignedByte();
    int originalBitsPerBlock = bitsPerBlock;

    if (bitsPerBlock == 0 || bitsPerBlock > 8) {
        bitsPerBlock = GLOBAL_PALETTE;
    }

    int paletteLength = bitsPerBlock == GLOBAL_PALETTE ? 0 : Type.VAR_INT.readPrimitive(buffer);
    // Read palette
    chunkSection.clearPalette();
    for (int i = 0; i < paletteLength; i++) {
        chunkSection.addPaletteEntry(Type.VAR_INT.readPrimitive(buffer));
    }

    // Read blocks
    long[] blockData = new long[Type.VAR_INT.readPrimitive(buffer)];
    if (blockData.length > 0) {
        char valuesPerLong = (char) (64 / bitsPerBlock);
        int expectedLength = (ChunkSection.SIZE + valuesPerLong - 1) / valuesPerLong;
        if (blockData.length != expectedLength) {
            throw new IllegalStateException("Block data length (" + blockData.length + ") does not match expected length (" + expectedLength + ")! bitsPerBlock=" + bitsPerBlock + ", originalBitsPerBlock=" + originalBitsPerBlock);
        }

        for (int i = 0; i < blockData.length; i++) {
            blockData[i] = buffer.readLong();
        }
        CompactArrayUtil.iterateCompactArrayWithPadding(bitsPerBlock, ChunkSection.SIZE, blockData,
                bitsPerBlock == GLOBAL_PALETTE ? chunkSection::setFlatBlock : chunkSection::setPaletteIndex);
    }

    return chunkSection;
}