Merge branch 'feature/unknown-packets' into development
This commit is contained in:
10
src/main/java/mc/protocol/ProtocolConstant.java
Normal file
10
src/main/java/mc/protocol/ProtocolConstant.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package mc.protocol;
|
||||||
|
|
||||||
|
import lombok.experimental.UtilityClass;
|
||||||
|
|
||||||
|
@UtilityClass
|
||||||
|
public class ProtocolConstant {
|
||||||
|
|
||||||
|
public final int PROTOCOL_VERSION = 47;
|
||||||
|
public final String PROTOCOL_VERSION_VALUE = "1.8.8";
|
||||||
|
}
|
||||||
27
src/main/java/mc/protocol/UnknownPacket.java
Normal file
27
src/main/java/mc/protocol/UnknownPacket.java
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package mc.protocol;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.ToString;
|
||||||
|
import mc.protocol.io.NetInputStream;
|
||||||
|
import mc.protocol.io.NetOutputStream;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@ToString(exclude = "rawData")
|
||||||
|
public class UnknownPacket implements Packet {
|
||||||
|
|
||||||
|
private final State state;
|
||||||
|
private final int id;
|
||||||
|
private final int dataSize;
|
||||||
|
private byte[] rawData;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void readSelf(NetInputStream netInputStream) {
|
||||||
|
rawData = new byte[dataSize];
|
||||||
|
netInputStream.readBytes(rawData);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeSelf(NetOutputStream netOutputStream) {
|
||||||
|
netOutputStream.writeBytes(rawData);
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/main/java/mc/protocol/Utils.java
Normal file
17
src/main/java/mc/protocol/Utils.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package mc.protocol;
|
||||||
|
|
||||||
|
import lombok.experimental.UtilityClass;
|
||||||
|
|
||||||
|
@UtilityClass
|
||||||
|
public class Utils {
|
||||||
|
|
||||||
|
public int sizeOfVarInt(int value) {
|
||||||
|
for (int i = 1; i < 5; ++i) {
|
||||||
|
if ((value & -1 << i * 7) == 0) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 5;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,14 +30,14 @@ import mc.protocol.io.NetOutputStream;
|
|||||||
public class HandshakePacket implements Packet {
|
public class HandshakePacket implements Packet {
|
||||||
|
|
||||||
private int protocolVersion;
|
private int protocolVersion;
|
||||||
private String ip;
|
private String host;
|
||||||
private int port;
|
private int port;
|
||||||
private State nextState;
|
private State nextState;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void readSelf(NetInputStream netInputStream) {
|
public void readSelf(NetInputStream netInputStream) {
|
||||||
protocolVersion = netInputStream.readVarInt();
|
protocolVersion = netInputStream.readVarInt();
|
||||||
ip = netInputStream.readString(255);
|
host = netInputStream.readString(255);
|
||||||
port = netInputStream.readUnsignedShort();
|
port = netInputStream.readUnsignedShort();
|
||||||
nextState = State.getById(netInputStream.readVarInt());
|
nextState = State.getById(netInputStream.readVarInt());
|
||||||
}
|
}
|
||||||
@@ -45,7 +45,7 @@ public class HandshakePacket implements Packet {
|
|||||||
@Override
|
@Override
|
||||||
public void writeSelf(NetOutputStream netOutputStream) {
|
public void writeSelf(NetOutputStream netOutputStream) {
|
||||||
netOutputStream.writeVarInt(protocolVersion);
|
netOutputStream.writeVarInt(protocolVersion);
|
||||||
netOutputStream.writeString(ip);
|
netOutputStream.writeString(host);
|
||||||
netOutputStream.writeShort(port);
|
netOutputStream.writeShort(port);
|
||||||
netOutputStream.writeVarInt(nextState.getId());
|
netOutputStream.writeVarInt(nextState.getId());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
package mc.protocol.io.coder;
|
package mc.protocol.io.coder;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import mc.protocol.Packet;
|
import mc.protocol.*;
|
||||||
import mc.protocol.PacketDirection;
|
|
||||||
import mc.protocol.State;
|
|
||||||
import mc.protocol.io.NetInputStream;
|
import mc.protocol.io.NetInputStream;
|
||||||
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class ProtocolDecoder {
|
public class ProtocolDecoder {
|
||||||
|
|
||||||
@@ -19,15 +15,24 @@ public class ProtocolDecoder {
|
|||||||
|
|
||||||
int packetId = netInputStream.readVarInt();
|
int packetId = netInputStream.readVarInt();
|
||||||
Class<? extends Packet> packetClass = state.getPacketById(direction, packetId);
|
Class<? extends Packet> packetClass = state.getPacketById(direction, packetId);
|
||||||
Objects.requireNonNull(packetClass);
|
if (packetClass == null) {
|
||||||
|
//FIXME необходимо в лог пивать предупреждение
|
||||||
try {
|
UnknownPacket packet = new UnknownPacket(
|
||||||
Packet packet = packetClass.newInstance();
|
state,
|
||||||
|
packetId,
|
||||||
|
sizePacket - Utils.sizeOfVarInt(packetId)
|
||||||
|
);
|
||||||
packet.readSelf(netInputStream);
|
packet.readSelf(netInputStream);
|
||||||
return packet;
|
return packet;
|
||||||
} catch (InstantiationException | IllegalAccessException e) {
|
} else {
|
||||||
e.printStackTrace(); //FIXME нужно писать в лог
|
try {
|
||||||
return null;
|
Packet packet = packetClass.newInstance();
|
||||||
|
packet.readSelf(netInputStream);
|
||||||
|
return packet;
|
||||||
|
} catch (InstantiationException | IllegalAccessException e) {
|
||||||
|
e.printStackTrace(); //FIXME нужно писать в лог
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user