NibbleArray -> BitArray
This commit is contained in:
@@ -7,12 +7,12 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import mc.protocol.buffer.NetByteBuf;
|
import mc.protocol.buffer.NetByteBuf;
|
||||||
import mc.protocol.packets.ServerSidePacket;
|
import mc.protocol.packets.ServerSidePacket;
|
||||||
import mc.protocol.pool.ProtocolObjectPool;
|
import mc.protocol.pool.ProtocolObjectPool;
|
||||||
import mc.protocol.utils.Bit13LongArray;
|
|
||||||
import mc.protocol.utils.HalfByteArray;
|
|
||||||
import mc.protocol.utils.NibbleArray;
|
|
||||||
import mc.protocol.world.Block;
|
import mc.protocol.world.Block;
|
||||||
import mc.protocol.world.Chunk;
|
import mc.protocol.world.Chunk;
|
||||||
import mc.protocol.world.ChunkSection;
|
import mc.protocol.world.ChunkSection;
|
||||||
|
import mc.utils.array.BitArray;
|
||||||
|
import mc.utils.array.BitByteArray;
|
||||||
|
import mc.utils.array.BitLongArray;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Данные чанка.
|
* Данные чанка.
|
||||||
@@ -148,8 +148,8 @@ public class ChunkDataPacket implements ServerSidePacket {
|
|||||||
private NetByteBuf createData(ChunkSection section, NetByteBuf biomes) {
|
private NetByteBuf createData(ChunkSection section, NetByteBuf biomes) {
|
||||||
NetByteBuf data = ProtocolObjectPool.getNetByteBufPool().borrowObject().setByteBuf(Unpooled.buffer());
|
NetByteBuf data = ProtocolObjectPool.getNetByteBufPool().borrowObject().setByteBuf(Unpooled.buffer());
|
||||||
|
|
||||||
NibbleArray blockLight = new HalfByteArray(2048);
|
BitArray blockLight = new BitByteArray(4, 2048 * 2);
|
||||||
NibbleArray skyLight = new HalfByteArray(2048);
|
BitArray skyLight = new BitByteArray(4, 2048 * 2);
|
||||||
|
|
||||||
// <Bits Per Block>
|
// <Bits Per Block>
|
||||||
data.writeUnsignedByte(BITS_PER_BLOCK);
|
data.writeUnsignedByte(BITS_PER_BLOCK);
|
||||||
@@ -160,12 +160,11 @@ public class ChunkDataPacket implements ServerSidePacket {
|
|||||||
// </Palette>
|
// </Palette>
|
||||||
|
|
||||||
// <Data Array Length>
|
// <Data Array Length>
|
||||||
int dataArraySize = _16_16_16 * BITS_PER_BLOCK / Long.SIZE;
|
data.writeVarInt(_16_16_16);
|
||||||
data.writeVarInt(dataArraySize);
|
|
||||||
// </Data Array Length>
|
// </Data Array Length>
|
||||||
|
|
||||||
// <Data Array>
|
// <Data Array>
|
||||||
NibbleArray dataArray = new Bit13LongArray(dataArraySize);
|
BitArray dataArray = new BitLongArray(BITS_PER_BLOCK, _16_16_16);
|
||||||
boolean writeBiomes = biomes != null;
|
boolean writeBiomes = biomes != null;
|
||||||
|
|
||||||
for (int y = 0; y < 16; y++) {
|
for (int y = 0; y < 16; y++) {
|
||||||
|
|||||||
@@ -1,50 +0,0 @@
|
|||||||
package mc.protocol.utils;
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
|
|
||||||
public class Bit13LongArray implements NibbleArray {
|
|
||||||
|
|
||||||
private static final int BITS = 13;
|
|
||||||
private final ByteBuffer buffer;
|
|
||||||
|
|
||||||
private long longValue = 0L;
|
|
||||||
private int nibbleIndex = 0;
|
|
||||||
private int lastWriteIndex = 0;
|
|
||||||
|
|
||||||
public Bit13LongArray(int longCapacity) {
|
|
||||||
this.buffer = ByteBuffer.allocate(longCapacity * Long.SIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void put(int value) {
|
|
||||||
if (Integer.bitCount(value) > BITS) {
|
|
||||||
throw new IllegalArgumentException("Value is to big: " + value);
|
|
||||||
}
|
|
||||||
|
|
||||||
//@formetter:off
|
|
||||||
int headValueIndex = ((nibbleIndex + 1) * BITS - 1) / Long.SIZE;
|
|
||||||
int tailValueIndex = ((nibbleIndex ) * BITS ) / Long.SIZE;
|
|
||||||
int offsetValue = ((nibbleIndex ) * BITS ) % Long.SIZE;
|
|
||||||
//@formetter:on
|
|
||||||
|
|
||||||
if (tailValueIndex != lastWriteIndex) {
|
|
||||||
buffer.putLong(longValue);
|
|
||||||
lastWriteIndex++;
|
|
||||||
longValue = 0L;
|
|
||||||
}
|
|
||||||
|
|
||||||
longValue |= ((long) value << offsetValue);
|
|
||||||
nibbleIndex++;
|
|
||||||
|
|
||||||
if (headValueIndex != tailValueIndex) {
|
|
||||||
buffer.putLong(longValue);
|
|
||||||
lastWriteIndex++;
|
|
||||||
longValue = value >> (Long.SIZE - offsetValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ByteBuffer byteBuffer() {
|
|
||||||
return this.buffer.putLong(longValue).rewind();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
package mc.protocol.utils;
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
|
|
||||||
public class HalfByteArray implements NibbleArray {
|
|
||||||
|
|
||||||
private static final int BITS = 4;
|
|
||||||
private final ByteBuffer buffer;
|
|
||||||
private Byte halfValue = null;
|
|
||||||
|
|
||||||
public HalfByteArray(int capacity) {
|
|
||||||
this.buffer = ByteBuffer.allocate(capacity);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void put(int value) {
|
|
||||||
if (Integer.bitCount(value) > BITS) {
|
|
||||||
throw new IllegalArgumentException("Value is to big: " + value);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (halfValue == null) {
|
|
||||||
halfValue = (byte) (value);
|
|
||||||
} else {
|
|
||||||
buffer.put((byte) (halfValue | value << BITS));
|
|
||||||
halfValue = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ByteBuffer byteBuffer() {
|
|
||||||
if (halfValue != null) {
|
|
||||||
buffer.put(halfValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.buffer.rewind();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package mc.protocol.utils;
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
|
|
||||||
public interface NibbleArray {
|
|
||||||
|
|
||||||
void put(int value);
|
|
||||||
|
|
||||||
ByteBuffer byteBuffer();
|
|
||||||
}
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
package mc.protocol.utils;
|
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
import io.netty.buffer.Unpooled;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
|
|
||||||
class HalfByteArrayTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void addLast() {
|
|
||||||
byte expected = 0b0001_0010;
|
|
||||||
byte value1 = 0b0000_0001;
|
|
||||||
byte value2 = 0b0000_0010;
|
|
||||||
|
|
||||||
NibbleArray nibbleArray = new HalfByteArray(1);
|
|
||||||
nibbleArray.put(value1);
|
|
||||||
nibbleArray.put(value2);
|
|
||||||
|
|
||||||
byte[] bytes = nibbleArray.byteBuffer().array();
|
|
||||||
assertNotNull(bytes);
|
|
||||||
assertEquals(1, bytes.length);
|
|
||||||
assertEquals(expected, bytes[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void addLastHalf() {
|
|
||||||
byte[] expected = {0b0001_0010, 0b0011_0000};
|
|
||||||
byte value1 = 0b0000_0001;
|
|
||||||
byte value2 = 0b0000_0010;
|
|
||||||
byte value3 = 0b0000_0011;
|
|
||||||
|
|
||||||
NibbleArray nibbleArray = new HalfByteArray(2);
|
|
||||||
nibbleArray.put(value1);
|
|
||||||
nibbleArray.put(value2);
|
|
||||||
nibbleArray.put(value3);
|
|
||||||
|
|
||||||
byte[] bytes = nibbleArray.byteBuffer().array();
|
|
||||||
assertNotNull(bytes);
|
|
||||||
assertEquals(2, bytes.length);
|
|
||||||
assertArrayEquals(expected, bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void byteBufTest() {
|
|
||||||
byte[] expected = {0b0001_0010, 0b0011_0000};
|
|
||||||
byte value1 = 0b0000_0001;
|
|
||||||
byte value2 = 0b0000_0010;
|
|
||||||
byte value3 = 0b0000_0011;
|
|
||||||
|
|
||||||
NibbleArray nibbleArray = new HalfByteArray(2);
|
|
||||||
nibbleArray.put(value1);
|
|
||||||
nibbleArray.put(value2);
|
|
||||||
nibbleArray.put(value3);
|
|
||||||
|
|
||||||
ByteBuf byteBuf = Unpooled.buffer(2);
|
|
||||||
byteBuf.writeBytes(nibbleArray.byteBuffer());
|
|
||||||
|
|
||||||
byte[] bytes = byteBuf.array();
|
|
||||||
assertNotNull(bytes);
|
|
||||||
assertEquals(2, bytes.length);
|
|
||||||
assertArrayEquals(expected, bytes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user