Search in sources :

Example 1 with VariableValueArray

use of org.lanternpowered.server.util.collect.array.VariableValueArray in project LanternServer by LanternPowered.

the class VariableValueArrayTest method test.

@Test
public void test() {
    int bits = 5;
    int maxValue = (1 << bits) - 1;
    int capacity = 4096;
    VariableValueArray array = new VariableValueArray(bits, capacity);
    for (int i = 0; i < capacity; i++) {
        int value = i % maxValue;
        array.set(i, value);
        assertEquals(value, array.get(i));
    }
    for (int i = 0; i < capacity; i++) {
        int value = maxValue - (i % maxValue);
        array.set(i, value);
        assertEquals(array.get(i), value);
    }
}
Also used : VariableValueArray(org.lanternpowered.server.util.collect.array.VariableValueArray) Test(org.junit.Test)

Example 2 with VariableValueArray

use of org.lanternpowered.server.util.collect.array.VariableValueArray 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;
}
Also used : VariableValueArray(org.lanternpowered.server.util.collect.array.VariableValueArray) MessagePlayOutChunkData(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutChunkData) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) Short2ObjectMap(it.unimi.dsi.fastutil.shorts.Short2ObjectMap) DataView(org.spongepowered.api.data.DataView)

Aggregations

VariableValueArray (org.lanternpowered.server.util.collect.array.VariableValueArray)2 Short2ObjectMap (it.unimi.dsi.fastutil.shorts.Short2ObjectMap)1 Test (org.junit.Test)1 ByteBuffer (org.lanternpowered.server.network.buffer.ByteBuffer)1 MessagePlayOutChunkData (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutChunkData)1 DataView (org.spongepowered.api.data.DataView)1