use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutChunkData in project LanternServer by LanternPowered.
the class CodecPlayOutChunkData method encode.
@Override
public ByteBuffer encode(CodecContext context, MessagePlayOutChunkData message) throws CodecException {
final MessagePlayOutChunkData.Section[] sections = message.getSections();
final byte[] biomes = message.getBiomes();
final int x = message.getX();
final int z = message.getZ();
final ByteBuffer buf = context.byteBufAlloc().buffer();
buf.writeInteger(message.getX());
buf.writeInteger(message.getZ());
buf.writeBoolean(biomes != null);
int sectionBitmask = 0;
final ByteBuffer dataBuf = context.byteBufAlloc().buffer();
ByteBuffer tileEntitiesBuf = null;
int tileEntitiesCount = 0;
for (int i = 0; i < sections.length; i++) {
if (sections[i] == null) {
continue;
}
sectionBitmask |= 1 << i;
final MessagePlayOutChunkData.Section section = sections[i];
final VariableValueArray types = section.getTypes();
dataBuf.writeByte((byte) types.getBitsPerValue());
final int[] palette = section.getPalette();
if (palette != null) {
dataBuf.writeVarInt(palette.length);
for (int value : palette) {
dataBuf.writeVarInt(value);
}
} else {
// Using global palette
dataBuf.writeVarInt(0);
}
final long[] backing = types.getBacking();
dataBuf.writeVarInt(backing.length);
final byte[] blockLight = section.getBlockLight();
final byte[] skyLight = section.getSkyLight();
dataBuf.ensureWritable(backing.length * 8 + blockLight.length + (skyLight != null ? skyLight.length : 0));
for (long value : backing) {
dataBuf.writeLong(value);
}
dataBuf.writeBytes(blockLight);
if (skyLight != null) {
dataBuf.writeBytes(skyLight);
}
final Short2ObjectMap<DataView> tileEntities = section.getTileEntities();
if (!tileEntities.isEmpty() && tileEntitiesBuf == null) {
tileEntitiesBuf = context.byteBufAlloc().buffer();
}
for (Short2ObjectMap.Entry<DataView> tileEntityEntry : tileEntities.short2ObjectEntrySet()) {
tileEntitiesCount++;
final int index = tileEntityEntry.getShortKey() & 0xffff;
final DataView dataView = tileEntityEntry.getValue();
dataView.set(X, x * 16 + (index & 0xf));
dataView.set(Y, i << 4 | index >> 8);
dataView.set(Z, z * 16 + ((index >> 4) & 0xf));
// noinspection ConstantConditions
tileEntitiesBuf.writeDataView(dataView);
}
}
if (biomes != null) {
dataBuf.writeBytes(biomes);
}
buf.writeVarInt(sectionBitmask);
buf.writeVarInt(dataBuf.writerIndex());
try {
buf.writeBytes(dataBuf);
} finally {
dataBuf.release();
}
buf.writeVarInt(tileEntitiesCount);
if (tileEntitiesBuf != null) {
try {
buf.writeBytes(tileEntitiesBuf);
} finally {
tileEntitiesBuf.release();
}
}
return buf;
}
Aggregations