Search in sources :

Example 1 with ChunkDataMessage

use of net.glowstone.net.message.play.game.ChunkDataMessage in project Glowstone by GlowstoneMC.

the class GlowChunk method toMessage.

/**
     * Creates a new {@link ChunkDataMessage} which can be sent to a client to stream
     * parts of this chunk to them.
     *
     * @param skylight Whether to include skylight data.
     * @param entireChunk Whether to send all chunk sections.
     * @return The {@link ChunkDataMessage}.
     */
public ChunkDataMessage toMessage(boolean skylight, boolean entireChunk) {
    load();
    int sectionBitmask = 0;
    // filter sectionBitmask based on actual chunk contents
    if (sections != null) {
        int maxBitmask = (1 << sections.length) - 1;
        if (entireChunk) {
            sectionBitmask = maxBitmask;
        } else {
            sectionBitmask &= maxBitmask;
        }
        for (int i = 0; i < sections.length; ++i) {
            if (sections[i] == null || sections[i].isEmpty()) {
                // remove empty sections from bitmask
                sectionBitmask &= ~(1 << i);
            }
        }
    }
    ByteBuf buf = Unpooled.buffer();
    if (sections != null) {
        // get the list of sections
        for (int i = 0; i < sections.length; ++i) {
            if ((sectionBitmask & 1 << i) == 0) {
                continue;
            }
            sections[i].writeToBuf(buf, skylight);
        }
    }
    // biomes
    if (entireChunk && biomes != null) {
        buf.writeBytes(biomes);
    }
    ArrayList<CompoundTag> blockEntities = new ArrayList<>();
    for (BlockEntity blockEntity : getRawBlockEntities()) {
        CompoundTag tag = new CompoundTag();
        blockEntity.saveNbt(tag);
        blockEntities.add(tag);
    }
    return new ChunkDataMessage(x, z, entireChunk, sectionBitmask, buf, blockEntities.toArray(new CompoundTag[blockEntities.size()]));
}
Also used : ChunkDataMessage(net.glowstone.net.message.play.game.ChunkDataMessage) ByteBuf(io.netty.buffer.ByteBuf) CompoundTag(net.glowstone.util.nbt.CompoundTag) BlockEntity(net.glowstone.block.entity.BlockEntity)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1 BlockEntity (net.glowstone.block.entity.BlockEntity)1 ChunkDataMessage (net.glowstone.net.message.play.game.ChunkDataMessage)1 CompoundTag (net.glowstone.util.nbt.CompoundTag)1