diff --git a/protocol/src/main/java/mc/protocol/packets/play/server/ChunkDataPacket.java b/protocol/src/main/java/mc/protocol/packets/play/server/ChunkDataPacket.java index ca75325..47a813e 100644 --- a/protocol/src/main/java/mc/protocol/packets/play/server/ChunkDataPacket.java +++ b/protocol/src/main/java/mc/protocol/packets/play/server/ChunkDataPacket.java @@ -7,12 +7,12 @@ import lombok.RequiredArgsConstructor; import mc.protocol.buffer.NetByteBuf; import mc.protocol.packets.ServerSidePacket; 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.Chunk; 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) { NetByteBuf data = ProtocolObjectPool.getNetByteBufPool().borrowObject().setByteBuf(Unpooled.buffer()); - NibbleArray blockLight = new HalfByteArray(2048); - NibbleArray skyLight = new HalfByteArray(2048); + BitArray blockLight = new BitByteArray(4, 2048 * 2); + BitArray skyLight = new BitByteArray(4, 2048 * 2); // data.writeUnsignedByte(BITS_PER_BLOCK); @@ -160,12 +160,11 @@ public class ChunkDataPacket implements ServerSidePacket { // // - int dataArraySize = _16_16_16 * BITS_PER_BLOCK / Long.SIZE; - data.writeVarInt(dataArraySize); + data.writeVarInt(_16_16_16); // // - NibbleArray dataArray = new Bit13LongArray(dataArraySize); + BitArray dataArray = new BitLongArray(BITS_PER_BLOCK, _16_16_16); boolean writeBiomes = biomes != null; for (int y = 0; y < 16; y++) { diff --git a/protocol/src/main/java/mc/protocol/utils/Bit13LongArray.java b/protocol/src/main/java/mc/protocol/utils/Bit13LongArray.java deleted file mode 100644 index b8add1d..0000000 --- a/protocol/src/main/java/mc/protocol/utils/Bit13LongArray.java +++ /dev/null @@ -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(); - } -} diff --git a/protocol/src/main/java/mc/protocol/utils/HalfByteArray.java b/protocol/src/main/java/mc/protocol/utils/HalfByteArray.java deleted file mode 100644 index d227757..0000000 --- a/protocol/src/main/java/mc/protocol/utils/HalfByteArray.java +++ /dev/null @@ -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(); - } -} diff --git a/protocol/src/main/java/mc/protocol/utils/NibbleArray.java b/protocol/src/main/java/mc/protocol/utils/NibbleArray.java deleted file mode 100644 index 4714ccb..0000000 --- a/protocol/src/main/java/mc/protocol/utils/NibbleArray.java +++ /dev/null @@ -1,10 +0,0 @@ -package mc.protocol.utils; - -import java.nio.ByteBuffer; - -public interface NibbleArray { - - void put(int value); - - ByteBuffer byteBuffer(); -} diff --git a/protocol/src/test/java/mc/protocol/utils/HalfByteArrayTest.java b/protocol/src/test/java/mc/protocol/utils/HalfByteArrayTest.java deleted file mode 100644 index c72f29e..0000000 --- a/protocol/src/test/java/mc/protocol/utils/HalfByteArrayTest.java +++ /dev/null @@ -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); - } -} \ No newline at end of file