diff --git a/pom.xml b/pom.xml
index 768b838..b5e43b8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -72,6 +72,11 @@
commons-io
2.6
+
+ com.google.guava
+ guava
+ 24.1-jre
+
diff --git a/src/main/java/mc/core/CSPacket.java b/src/main/java/mc/core/CSPacket.java
index 45394cb..319ba81 100644
--- a/src/main/java/mc/core/CSPacket.java
+++ b/src/main/java/mc/core/CSPacket.java
@@ -5,4 +5,6 @@
package mc.core;
public interface CSPacket {
+ default void readSelf(NetStream netStream) {
+ }
}
diff --git a/src/main/java/mc/core/NetStream.java b/src/main/java/mc/core/NetStream.java
new file mode 100644
index 0000000..898e5cb
--- /dev/null
+++ b/src/main/java/mc/core/NetStream.java
@@ -0,0 +1,57 @@
+/*
+ * DmitriyMX
+ * 2018-04-08
+ */
+package mc.core;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.nio.charset.StandardCharsets;
+
+@Slf4j
+public abstract class NetStream {
+ public static int sizeVarInt(final int value) {
+ byte size = 0;
+ int v = value;
+
+ do {
+ v >>>= 7;
+ size++;
+ } while (v != 0);
+
+ return size;
+ }
+
+ public int readVarInt() {
+ int result = 0;
+ byte read;
+ byte numRead = 0;
+
+ do {
+ read = readByte();
+ int value = (read & 0b01111111);
+ result |= (value << (7 * numRead));
+
+ numRead++;
+ if (numRead > 5) {
+ log.debug("VarInt is too big!");
+ break;
+ }
+ } while ((read & 0b10000000) != 0);
+
+ return result;
+ }
+
+ public String readString() {
+ int length = readVarInt();
+
+ byte[] buffer = new byte[length];
+ readBytes(buffer);
+
+ return new String(buffer, StandardCharsets.UTF_8);
+ }
+
+ public abstract byte readByte();
+ public abstract void readBytes(byte[] buffer);
+ public abstract int readUnsignedShort();
+}
diff --git a/src/main/java/mc/core/netty/PacketDecoder.java b/src/main/java/mc/core/netty/PacketDecoder.java
index 0da59da..6e83d75 100644
--- a/src/main/java/mc/core/netty/PacketDecoder.java
+++ b/src/main/java/mc/core/netty/PacketDecoder.java
@@ -8,62 +8,60 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import lombok.extern.slf4j.Slf4j;
+import mc.core.CSPacket;
+import mc.core.NetStream;
import mc.core.netty.packets.RawPacket;
import java.util.List;
@Slf4j
public class PacketDecoder extends ByteToMessageDecoder {
- private int readVarInt(ByteBuf byteBuf) throws Exception {
- int result = 0;
- byte read;
- byte numRead = 0;
-
- do {
- read = byteBuf.readByte();
- int value = (read & 0b01111111);
- result |= (value << (7 * numRead));
-
- numRead++;
- if (numRead > 5)
- throw new Exception("VarInt is too big");
- } while ((read & 0b10000000) != 0);
-
- return result;
+ @Override
+ public void channelActive(ChannelHandlerContext ctx) throws Exception {
+ ctx.channel().attr(State.ATTR_STATE).set(State.STATUS);
+ ctx.fireChannelActive();
}
- private int sizeVarInt(final int value) {
- byte size = 0;
- int v = value;
-
- do {
- v >>>= 7;
- size++;
- } while (v != 0);
-
- return size;
+ @Override
+ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
+ ctx.channel().attr(State.ATTR_STATE).set(null);
+ ctx.fireChannelInactive();
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List