Fixed warning about unsipported version

This commit is contained in:
Nanit 2021-10-30 20:19:28 +03:00
parent 73f1045a76
commit 1e4b4389d5
7 changed files with 17 additions and 9 deletions

View File

@ -101,6 +101,7 @@ public class ClientConnection extends ChannelInboundHandlerAdapter {
if (packet instanceof PacketHandshake) { if (packet instanceof PacketHandshake) {
PacketHandshake handshake = (PacketHandshake) packet; PacketHandshake handshake = (PacketHandshake) packet;
clientVersion = handshake.getVersion(); clientVersion = handshake.getVersion();
updateStateAndVersion(handshake.getNextState(), clientVersion); updateStateAndVersion(handshake.getNextState(), clientVersion);
Logger.debug("Pinged from %s [%s]", address, clientVersion.toString()); Logger.debug("Pinged from %s [%s]", address, clientVersion.toString());
@ -134,8 +135,8 @@ public class ClientConnection extends ChannelInboundHandlerAdapter {
return; return;
} }
if (clientVersion.equals(Version.UNDEFINED)) { if (!clientVersion.isSupported()) {
disconnectLogin("Incompatible client version"); disconnectLogin("Unsupported client version");
return; return;
} }

View File

@ -30,6 +30,7 @@ public class PacketDecoder extends MessageToMessageDecoder<ByteBuf> {
if (packet != null) { if (packet != null) {
try { try {
Logger.debug("Received packet %s", packet.toString());
packet.decode(msg, version); packet.decode(msg, version);
} catch (Exception e) { } catch (Exception e) {
Logger.warning("Cannot decode packet 0x%s: %s", Integer.toHexString(packetId), e.getMessage()); Logger.warning("Cannot decode packet 0x%s: %s", Integer.toHexString(packetId), e.getMessage());

View File

@ -47,6 +47,8 @@ public class ByteMessage extends ByteBuf {
} }
} }
buf.readBytes(maxRead);
throw new IllegalArgumentException("Cannot read VarInt"); throw new IllegalArgumentException("Cannot read VarInt");
} }

View File

@ -1,7 +1,6 @@
package ru.nanit.limbo.protocol.packets; package ru.nanit.limbo.protocol.packets;
import ru.nanit.limbo.protocol.ByteMessage; import ru.nanit.limbo.protocol.ByteMessage;
import ru.nanit.limbo.protocol.Packet;
import ru.nanit.limbo.protocol.PacketIn; import ru.nanit.limbo.protocol.PacketIn;
import ru.nanit.limbo.protocol.registry.State; import ru.nanit.limbo.protocol.registry.State;
import ru.nanit.limbo.protocol.registry.Version; import ru.nanit.limbo.protocol.registry.Version;
@ -31,7 +30,12 @@ public class PacketHandshake implements PacketIn {
@Override @Override
public void decode(ByteMessage msg, Version version) { public void decode(ByteMessage msg, Version version) {
try {
this.version = Version.of(msg.readVarInt()); this.version = Version.of(msg.readVarInt());
} catch (IllegalArgumentException e) {
this.version = Version.UNDEFINED;
}
this.host = msg.readString(); this.host = msg.readString();
this.port = msg.readUnsignedShort(); this.port = msg.readUnsignedShort();
this.nextState = State.getById(msg.readVarInt()); this.nextState = State.getById(msg.readVarInt());

View File

@ -172,7 +172,7 @@ public enum State {
private final Map<Version, PacketRegistry> registry = new HashMap<>(); private final Map<Version, PacketRegistry> registry = new HashMap<>();
public PacketRegistry getRegistry(Version version) { public PacketRegistry getRegistry(Version version) {
return registry.get(version); return registry.getOrDefault(version, registry.get(getMin()));
} }
public void register(Supplier<?> packet, Mapping... mappings) { public void register(Supplier<?> packet, Mapping... mappings) {

View File

@ -83,14 +83,14 @@ public enum Version {
return this.protocolNumber <= another.protocolNumber; return this.protocolNumber <= another.protocolNumber;
} }
public boolean between(Version min, Version max) {
return this.protocolNumber > min.protocolNumber && this.protocolNumber < max.protocolNumber;
}
public boolean fromTo(Version min, Version max) { public boolean fromTo(Version min, Version max) {
return this.protocolNumber >= min.protocolNumber && this.protocolNumber <= max.protocolNumber; return this.protocolNumber >= min.protocolNumber && this.protocolNumber <= max.protocolNumber;
} }
public boolean isSupported() {
return this != UNDEFINED;
}
public static Version getMin() { public static Version getMin() {
return V1_8; return V1_8;
} }