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 {
|
||||
|
||||
private int protocolVersion;
|
||||
private String ip;
|
||||
private String host;
|
||||
private int port;
|
||||
private State nextState;
|
||||
|
||||
@Override
|
||||
public void readSelf(NetInputStream netInputStream) {
|
||||
protocolVersion = netInputStream.readVarInt();
|
||||
ip = netInputStream.readString(255);
|
||||
host = netInputStream.readString(255);
|
||||
port = netInputStream.readUnsignedShort();
|
||||
nextState = State.getById(netInputStream.readVarInt());
|
||||
}
|
||||
@@ -45,7 +45,7 @@ public class HandshakePacket implements Packet {
|
||||
@Override
|
||||
public void writeSelf(NetOutputStream netOutputStream) {
|
||||
netOutputStream.writeVarInt(protocolVersion);
|
||||
netOutputStream.writeString(ip);
|
||||
netOutputStream.writeString(host);
|
||||
netOutputStream.writeShort(port);
|
||||
netOutputStream.writeVarInt(nextState.getId());
|
||||
}
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
package mc.protocol.io.coder;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import mc.protocol.Packet;
|
||||
import mc.protocol.PacketDirection;
|
||||
import mc.protocol.State;
|
||||
import mc.protocol.*;
|
||||
import mc.protocol.io.NetInputStream;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class ProtocolDecoder {
|
||||
|
||||
@@ -19,8 +15,16 @@ public class ProtocolDecoder {
|
||||
|
||||
int packetId = netInputStream.readVarInt();
|
||||
Class<? extends Packet> packetClass = state.getPacketById(direction, packetId);
|
||||
Objects.requireNonNull(packetClass);
|
||||
|
||||
if (packetClass == null) {
|
||||
//FIXME необходимо в лог пивать предупреждение
|
||||
UnknownPacket packet = new UnknownPacket(
|
||||
state,
|
||||
packetId,
|
||||
sizePacket - Utils.sizeOfVarInt(packetId)
|
||||
);
|
||||
packet.readSelf(netInputStream);
|
||||
return packet;
|
||||
} else {
|
||||
try {
|
||||
Packet packet = packetClass.newInstance();
|
||||
packet.readSelf(netInputStream);
|
||||
@@ -30,4 +34,5 @@ public class ProtocolDecoder {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user