Java Code Examples for us.myles.ViaVersion.api.minecraft.chunks.Chunk#getSections()

The following examples show how to use us.myles.ViaVersion.api.minecraft.chunks.Chunk#getSections() . 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: Chunk1_8Type.java    From ViaRewind with MIT License 5 votes vote down vote up
@Override
public void write(ByteBuf output, ClientWorld world, Chunk chunk) throws Exception {
    ByteBuf buf = output.alloc().buffer();

    for (int i = 0; i < chunk.getSections().length; i++) {
        if ((chunk.getBitmask() & 1 << i) == 0) continue;
        CHUNK_SECTION_TYPE.write(buf, chunk.getSections()[i]);
    }

    for (int i = 0; i < chunk.getSections().length; i++) {
        if ((chunk.getBitmask() & 1 << i) == 0) continue;
        chunk.getSections()[i].writeBlockLight(buf);
    }

    boolean skyLight = world.getEnvironment() == Environment.NORMAL;
    if (skyLight) {
        for (int i = 0; i < chunk.getSections().length; i++) {
            if ((chunk.getBitmask() & 1 << i) == 0) continue;
            chunk.getSections()[i].writeSkyLight(buf);
        }
    }

    if (chunk.isGroundUp() && chunk.isBiomeData()) {
        for (int biome : chunk.getBiomeData()) {
            buf.writeByte((byte) biome);
        }
    }

    output.writeInt(chunk.getX());
    output.writeInt(chunk.getZ());
    output.writeBoolean(chunk.isGroundUp());
    output.writeShort(chunk.getBitmask());
    Type.VAR_INT.write(output, buf.readableBytes());
    output.writeBytes(buf, buf.readableBytes());
    buf.release();
}
 
Example 2
Source File: Chunk1_15Type.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public void write(ByteBuf output, ClientWorld world, Chunk chunk) throws Exception {
    output.writeInt(chunk.getX());
    output.writeInt(chunk.getZ());

    output.writeBoolean(chunk.isFullChunk());
    Type.VAR_INT.writePrimitive(output, chunk.getBitmask());
    Type.NBT.write(output, chunk.getHeightMap());

    // Write biome data
    if (chunk.isBiomeData()) {
        for (int value : chunk.getBiomeData()) {
            output.writeInt(value);
        }
    }

    ByteBuf buf = output.alloc().buffer();
    try {
        for (int i = 0; i < 16; i++) {
            ChunkSection section = chunk.getSections()[i];
            if (section == null) continue; // Section not set

            buf.writeShort(section.getNonAirBlocksCount());
            Types1_13.CHUNK_SECTION.write(buf, section);
        }
        buf.readerIndex(0);
        Type.VAR_INT.writePrimitive(output, buf.readableBytes());
        output.writeBytes(buf);
    } finally {
        buf.release(); // release buffer
    }

    // Write Block Entities
    Type.NBT_ARRAY.write(output, chunk.getBlockEntities().toArray(EMPTY_COMPOUNDS));
}
 
Example 3
Source File: Chunk1_9_3_4Type.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public void write(ByteBuf output, ClientWorld world, Chunk chunk) throws Exception {
    output.writeInt(chunk.getX());
    output.writeInt(chunk.getZ());

    output.writeBoolean(chunk.isFullChunk());
    Type.VAR_INT.writePrimitive(output, chunk.getBitmask());

    ByteBuf buf = output.alloc().buffer();
    try {
        for (int i = 0; i < 16; i++) {
            ChunkSection section = chunk.getSections()[i];
            if (section == null) continue; // Section not set
            Types1_9.CHUNK_SECTION.write(buf, section);
            section.writeBlockLight(buf);

            if (!section.hasSkyLight()) continue; // No sky light, we're done here.
            section.writeSkyLight(buf);
        }
        buf.readerIndex(0);
        Type.VAR_INT.writePrimitive(output, buf.readableBytes() + (chunk.isBiomeData() ? 256 : 0));
        output.writeBytes(buf);
    } finally {
        buf.release(); // release buffer
    }

    // Write biome data
    if (chunk.isBiomeData()) {
        for (int biome : chunk.getBiomeData()) {
            output.writeByte((byte) biome);
        }
    }

    // Write Block Entities
    Type.NBT_ARRAY.write(output, chunk.getBlockEntities().toArray(new CompoundTag[0]));
}
 
Example 4
Source File: Chunk1_14Type.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public void write(ByteBuf output, ClientWorld world, Chunk chunk) throws Exception {
    output.writeInt(chunk.getX());
    output.writeInt(chunk.getZ());

    output.writeBoolean(chunk.isFullChunk());
    Type.VAR_INT.writePrimitive(output, chunk.getBitmask());
    Type.NBT.write(output, chunk.getHeightMap());

    ByteBuf buf = output.alloc().buffer();
    try {
        for (int i = 0; i < 16; i++) {
            ChunkSection section = chunk.getSections()[i];
            if (section == null) continue; // Section not set

            buf.writeShort(section.getNonAirBlocksCount());
            Types1_13.CHUNK_SECTION.write(buf, section);
        }
        buf.readerIndex(0);
        Type.VAR_INT.writePrimitive(output, buf.readableBytes() + (chunk.isBiomeData() ? 1024 : 0)); // 256 * 4
        output.writeBytes(buf);
    } finally {
        buf.release(); // release buffer
    }

    // Write biome data
    if (chunk.isBiomeData()) {
        for (int value : chunk.getBiomeData()) {
            output.writeInt(value & 0xFF); // This is a temporary workaround, we'll look into fixing this soon :)
        }
    }

    // Write Block Entities
    Type.NBT_ARRAY.write(output, chunk.getBlockEntities().toArray(new CompoundTag[0]));
}
 
Example 5
Source File: Chunk1_16Type.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public void write(ByteBuf output, ClientWorld world, Chunk chunk) throws Exception {
    output.writeInt(chunk.getX());
    output.writeInt(chunk.getZ());

    output.writeBoolean(chunk.isFullChunk());
    output.writeBoolean(chunk.isIgnoreOldLightData());
    Type.VAR_INT.writePrimitive(output, chunk.getBitmask());
    Type.NBT.write(output, chunk.getHeightMap());

    // Write biome data
    if (chunk.isBiomeData()) {
        for (int value : chunk.getBiomeData()) {
            output.writeInt(value);
        }
    }

    ByteBuf buf = output.alloc().buffer();
    try {
        for (int i = 0; i < 16; i++) {
            ChunkSection section = chunk.getSections()[i];
            if (section == null) continue; // Section not set

            buf.writeShort(section.getNonAirBlocksCount());
            Types1_16.CHUNK_SECTION.write(buf, section);
        }
        buf.readerIndex(0);
        Type.VAR_INT.writePrimitive(output, buf.readableBytes());
        output.writeBytes(buf);
    } finally {
        buf.release(); // release buffer
    }

    // Write Block Entities
    Type.NBT_ARRAY.write(output, chunk.getBlockEntities().toArray(EMPTY_COMPOUNDS));
}
 
Example 6
Source File: Chunk1_13Type.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public void write(ByteBuf output, ClientWorld world, Chunk chunk) throws Exception {
    output.writeInt(chunk.getX());
    output.writeInt(chunk.getZ());

    output.writeBoolean(chunk.isFullChunk());
    Type.VAR_INT.writePrimitive(output, chunk.getBitmask());

    ByteBuf buf = output.alloc().buffer();
    try {
        for (int i = 0; i < 16; i++) {
            ChunkSection section = chunk.getSections()[i];
            if (section == null) continue; // Section not set
            Types1_13.CHUNK_SECTION.write(buf, section);
            section.writeBlockLight(buf);

            if (!section.hasSkyLight()) continue; // No sky light, we're done here.
            section.writeSkyLight(buf);

        }
        buf.readerIndex(0);
        Type.VAR_INT.writePrimitive(output, buf.readableBytes() + (chunk.isBiomeData() ? 1024 : 0));
        output.writeBytes(buf);
    } finally {
        buf.release(); // release buffer
    }

    // Write biome data
    if (chunk.isBiomeData()) {
        for (int value : chunk.getBiomeData()) {
            output.writeInt(value);
        }
    }

    // Write Block Entities
    Type.NBT_ARRAY.write(output, chunk.getBlockEntities().toArray(new CompoundTag[0]));
}
 
Example 7
Source File: ConnectionData.java    From ViaVersion with MIT License 5 votes vote down vote up
public static void connectBlocks(UserConnection user, Chunk chunk) {
    long xOff = chunk.getX() << 4;
    long zOff = chunk.getZ() << 4;

    for (int i = 0; i < chunk.getSections().length; i++) {
        ChunkSection section = chunk.getSections()[i];
        if (section == null) continue;

        boolean willConnect = false;

        for (int p = 0; p < section.getPaletteSize(); p++) {
            int id = section.getPaletteEntry(p);
            if (ConnectionData.connects(id)) {
                willConnect = true;
                break;
            }
        }
        if (!willConnect) continue;

        long yOff = i << 4;

        for (int y = 0; y < 16; y++) {
            for (int z = 0; z < 16; z++) {
                for (int x = 0; x < 16; x++) {
                    int block = section.getFlatBlock(x, y, z);

                    ConnectionHandler handler = ConnectionData.getConnectionHandler(block);
                    if (handler != null) {
                        block = handler.connect(user, new Position(
                                (int) (xOff + x),
                                (short) (yOff + y),
                                (int) (zOff + z)
                        ), block);
                        section.setFlatBlock(x, y, z, block);
                    }
                }
            }
        }
    }
}
 
Example 8
Source File: Chunk1_9_1_2Type.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public void write(ByteBuf output, ClientWorld world, Chunk chunk) throws Exception {
    output.writeInt(chunk.getX());
    output.writeInt(chunk.getZ());

    output.writeBoolean(chunk.isFullChunk());
    Type.VAR_INT.writePrimitive(output, chunk.getBitmask());

    ByteBuf buf = output.alloc().buffer();
    try {
        for (int i = 0; i < 16; i++) {
            ChunkSection section = chunk.getSections()[i];
            if (section == null) continue; // Section not set
            Types1_9.CHUNK_SECTION.write(buf, section);
            section.writeBlockLight(buf);

            if (!section.hasSkyLight()) continue; // No sky light, we're done here.
            section.writeSkyLight(buf);

        }
        buf.readerIndex(0);
        Type.VAR_INT.writePrimitive(output, buf.readableBytes() + (chunk.isBiomeData() ? 256 : 0));
        output.writeBytes(buf);
    } finally {
        buf.release(); // release buffer
    }

    // Write biome data
    if (chunk.isBiomeData()) {
        for (int biome : chunk.getBiomeData()) {
            output.writeByte((byte) biome);
        }
    }
}
 
Example 9
Source File: WorldPackets.java    From ViaVersion with MIT License 4 votes vote down vote up
private static void setNonFullLight(Chunk chunk, ChunkSection section, int ySection, int x, int y, int z) {
    int skyLight = 0;
    int blockLight = 0;
    for (BlockFace blockFace : BlockFace.values()) {
        NibbleArray skyLightArray = section.getSkyLightNibbleArray();
        NibbleArray blockLightArray = section.getBlockLightNibbleArray();
        int neighbourX = x + blockFace.getModX();
        int neighbourY = y + blockFace.getModY();
        int neighbourZ = z + blockFace.getModZ();

        if (blockFace.getModX() != 0) {
            // Another chunk, nothing we can do without an unnecessary amount of caching
            if (neighbourX == 16 || neighbourX == -1) continue;
        } else if (blockFace.getModY() != 0) {
            if (neighbourY == 16 || neighbourY == -1) {
                if (neighbourY == 16) {
                    ySection += 1;
                    neighbourY = 0;
                } else {
                    ySection -= 1;
                    neighbourY = 15;
                }

                if (ySection == 16 || ySection == -1) continue;

                ChunkSection newSection = chunk.getSections()[ySection];
                if (newSection == null) continue;

                skyLightArray = newSection.getSkyLightNibbleArray();
                blockLightArray = newSection.getBlockLightNibbleArray();
            }
        } else if (blockFace.getModZ() != 0) {
            // Another chunk, nothing we can do without an unnecessary amount of caching
            if (neighbourZ == 16 || neighbourZ == -1) continue;
        }

        if (blockLightArray != null && blockLight != 15) {
            int neighbourBlockLight = blockLightArray.get(neighbourX, neighbourY, neighbourZ);
            if (neighbourBlockLight == 15) {
                blockLight = 14;
            } else if (neighbourBlockLight > blockLight) {
                blockLight = neighbourBlockLight - 1; // lower light level by one
            }
        }
        if (skyLightArray != null && skyLight != 15) {
            int neighbourSkyLight = skyLightArray.get(neighbourX, neighbourY, neighbourZ);
            if (neighbourSkyLight == 15) {
                if (blockFace.getModY() == 1) {
                    // Keep 15 if block is exposed to sky
                    skyLight = 15;
                    continue;
                }

                skyLight = 14;
            } else if (neighbourSkyLight > skyLight) {
                skyLight = neighbourSkyLight - 1; // lower light level by one
            }
        }
    }

    if (skyLight != 0) {
        if (!section.hasSkyLight()) {
            byte[] newSkyLight = new byte[2028];
            section.setSkyLight(newSkyLight);
        }

        section.getSkyLightNibbleArray().set(x, y, z, skyLight);
    }
    if (blockLight != 0) {
        section.getBlockLightNibbleArray().set(x, y, z, blockLight);
    }
}