mirror of
https://github.com/Nan1t/NanoLimbo.git
synced 2025-07-15 21:50:14 +02:00
Changed packet signature to support different versions
This commit is contained in:
parent
ff5b605ee3
commit
62fbc0e8f2
@ -97,6 +97,7 @@ public class ClientConnection extends ChannelInboundHandlerAdapter {
|
||||
PacketHandshake handshake = (PacketHandshake) packet;
|
||||
clientVersion = handshake.getVersion();
|
||||
updateState(State.getById(handshake.getNextState()));
|
||||
|
||||
Logger.debug("Pinged from " + address);
|
||||
|
||||
if (server.getConfig().getInfoForwarding().isLegacy()) {
|
||||
@ -128,7 +129,7 @@ public class ClientConnection extends ChannelInboundHandlerAdapter {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!clientVersion.equals(Version.getCurrentSupported())) {
|
||||
if (clientVersion.equals(Version.UNDEFINED)) {
|
||||
disconnectLogin("Incompatible client version");
|
||||
return;
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ public class PacketDecoder extends MessageToMessageDecoder<ByteBuf> {
|
||||
|
||||
if (packet != null) {
|
||||
try {
|
||||
packet.decode(msg);
|
||||
packet.decode(msg, );
|
||||
} catch (Exception e) {
|
||||
Logger.warning("Cannot decode packet 0x%s: %s", Integer.toHexString(packetId), e.getMessage());
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ public class PacketEncoder extends MessageToByteEncoder<Packet> {
|
||||
msg.writeVarInt(packetId);
|
||||
|
||||
try {
|
||||
packet.encode(msg);
|
||||
packet.encode(msg, );
|
||||
} catch (Exception e) {
|
||||
Logger.warning("Cannot encode packet 0x%s: %s", Integer.toHexString(packetId), e.getMessage());
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
package ru.nanit.limbo.protocol;
|
||||
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
|
||||
public interface Packet {
|
||||
|
||||
void encode(ByteMessage msg);
|
||||
void encode(ByteMessage msg, Version version);
|
||||
|
||||
void decode(ByteMessage msg);
|
||||
void decode(ByteMessage msg, Version version);
|
||||
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
package ru.nanit.limbo.protocol;
|
||||
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
|
||||
public interface PacketIn extends Packet {
|
||||
|
||||
@Override
|
||||
default void encode(ByteMessage msg) {
|
||||
default void encode(ByteMessage msg, Version version) {
|
||||
// Can be ignored for incoming packets
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,11 @@
|
||||
package ru.nanit.limbo.protocol;
|
||||
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
|
||||
public interface PacketOut extends Packet {
|
||||
|
||||
@Override
|
||||
default void decode(ByteMessage msg) {
|
||||
default void decode(ByteMessage msg, Version version) {
|
||||
// Can be ignored for outgoing packets
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
package ru.nanit.limbo.protocol;
|
||||
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
|
||||
public class PreEncodedPacket implements PacketOut {
|
||||
|
||||
private final PacketOut packet;
|
||||
@ -15,14 +17,14 @@ public class PreEncodedPacket implements PacketOut {
|
||||
|
||||
public PreEncodedPacket encodePacket() {
|
||||
ByteMessage encodedMessage = ByteMessage.create();
|
||||
packet.encode(encodedMessage);
|
||||
packet.encode(encodedMessage, );
|
||||
this.message = encodedMessage.toByteArray();
|
||||
encodedMessage.release();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteMessage msg) {
|
||||
public void encode(ByteMessage msg, Version version) {
|
||||
msg.writeBytes(message);
|
||||
}
|
||||
|
||||
|
@ -15,36 +15,16 @@ public class PacketHandshake implements Packet {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Version version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public int getNextState() {
|
||||
return nextState;
|
||||
}
|
||||
|
||||
public void setNextState(int nextState) {
|
||||
this.nextState = nextState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteMessage msg) {
|
||||
public void encode(ByteMessage msg, Version version) {
|
||||
msg.writeVarInt(this.version.getProtocolNumber());
|
||||
msg.writeString(host);
|
||||
msg.writeShort(port);
|
||||
@ -52,7 +32,7 @@ public class PacketHandshake implements Packet {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decode(ByteMessage msg) {
|
||||
public void decode(ByteMessage msg, Version version) {
|
||||
this.version = Version.of(msg.readVarInt());
|
||||
this.host = msg.readString();
|
||||
this.port = msg.readUnsignedShort();
|
||||
|
@ -2,6 +2,7 @@ package ru.nanit.limbo.protocol.packets.login;
|
||||
|
||||
import ru.nanit.limbo.protocol.ByteMessage;
|
||||
import ru.nanit.limbo.protocol.PacketOut;
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
|
||||
public class PacketDisconnect implements PacketOut {
|
||||
|
||||
@ -12,7 +13,7 @@ public class PacketDisconnect implements PacketOut {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteMessage msg) {
|
||||
public void encode(ByteMessage msg, Version version) {
|
||||
msg.writeString(String.format("{\"text\": \"%s\"}", reason));
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@ package ru.nanit.limbo.protocol.packets.login;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import ru.nanit.limbo.protocol.ByteMessage;
|
||||
import ru.nanit.limbo.protocol.PacketOut;
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
|
||||
public class PacketLoginPluginRequest implements PacketOut {
|
||||
|
||||
@ -23,7 +24,7 @@ public class PacketLoginPluginRequest implements PacketOut {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteMessage msg) {
|
||||
public void encode(ByteMessage msg, Version version) {
|
||||
msg.writeVarInt(messageId);
|
||||
msg.writeString(channel);
|
||||
msg.writeBytes(data);
|
||||
|
@ -2,6 +2,7 @@ package ru.nanit.limbo.protocol.packets.login;
|
||||
|
||||
import ru.nanit.limbo.protocol.ByteMessage;
|
||||
import ru.nanit.limbo.protocol.PacketIn;
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
|
||||
public class PacketLoginPluginResponse implements PacketIn {
|
||||
|
||||
@ -22,7 +23,7 @@ public class PacketLoginPluginResponse implements PacketIn {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decode(ByteMessage msg) {
|
||||
public void decode(ByteMessage msg, Version version) {
|
||||
messageId = msg.readVarInt();
|
||||
successful = msg.readBoolean();
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package ru.nanit.limbo.protocol.packets.login;
|
||||
|
||||
import ru.nanit.limbo.protocol.*;
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
|
||||
public class PacketLoginStart implements PacketIn {
|
||||
|
||||
@ -11,7 +12,7 @@ public class PacketLoginStart implements PacketIn {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decode(ByteMessage msg) {
|
||||
public void decode(ByteMessage msg, Version version) {
|
||||
this.username = msg.readString();
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ package ru.nanit.limbo.protocol.packets.login;
|
||||
|
||||
import ru.nanit.limbo.protocol.ByteMessage;
|
||||
import ru.nanit.limbo.protocol.PacketOut;
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@ -19,7 +20,7 @@ public class PacketLoginSuccess implements PacketOut {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteMessage msg) {
|
||||
public void encode(ByteMessage msg, Version version) {
|
||||
msg.writeUuid(uuid);
|
||||
msg.writeString(username);
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package ru.nanit.limbo.protocol.packets.play;
|
||||
|
||||
import ru.nanit.limbo.protocol.ByteMessage;
|
||||
import ru.nanit.limbo.protocol.PacketOut;
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
import ru.nanit.limbo.server.data.BossBar;
|
||||
|
||||
import java.util.UUID;
|
||||
@ -25,7 +26,7 @@ public class PacketBossBar implements PacketOut {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteMessage msg) {
|
||||
public void encode(ByteMessage msg, Version version) {
|
||||
msg.writeUuid(uuid);
|
||||
msg.writeVarInt(0); // Create bossbar
|
||||
msg.writeString(bossBar.getText());
|
||||
|
@ -2,6 +2,7 @@ package ru.nanit.limbo.protocol.packets.play;
|
||||
|
||||
import ru.nanit.limbo.protocol.ByteMessage;
|
||||
import ru.nanit.limbo.protocol.PacketOut;
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@ -24,7 +25,7 @@ public class PacketChatMessage implements PacketOut {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteMessage msg) {
|
||||
public void encode(ByteMessage msg, Version version) {
|
||||
msg.writeString(jsonData);
|
||||
msg.writeByte(position.index);
|
||||
msg.writeUuid(sender);
|
||||
|
@ -2,6 +2,7 @@ package ru.nanit.limbo.protocol.packets.play;
|
||||
|
||||
import ru.nanit.limbo.protocol.ByteMessage;
|
||||
import ru.nanit.limbo.protocol.PacketOut;
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -14,7 +15,7 @@ public class PacketDeclareCommands implements PacketOut {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteMessage msg) {
|
||||
public void encode(ByteMessage msg, Version version) {
|
||||
msg.writeVarInt(commands.size() * 2 + 1); // +1 because declaring root node
|
||||
|
||||
// Declare root node
|
||||
|
@ -3,6 +3,7 @@ package ru.nanit.limbo.protocol.packets.play;
|
||||
import net.kyori.adventure.nbt.CompoundBinaryTag;
|
||||
import ru.nanit.limbo.protocol.ByteMessage;
|
||||
import ru.nanit.limbo.protocol.PacketOut;
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
|
||||
public class PacketJoinGame implements PacketOut {
|
||||
|
||||
@ -83,7 +84,7 @@ public class PacketJoinGame implements PacketOut {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteMessage msg) {
|
||||
public void encode(ByteMessage msg, Version version) {
|
||||
msg.writeInt(entityId);
|
||||
msg.writeBoolean(isHardcore);
|
||||
msg.writeByte(gameMode);
|
||||
|
@ -2,6 +2,7 @@ package ru.nanit.limbo.protocol.packets.play;
|
||||
|
||||
import ru.nanit.limbo.protocol.ByteMessage;
|
||||
import ru.nanit.limbo.protocol.Packet;
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
|
||||
public class PacketKeepAlive implements Packet {
|
||||
|
||||
@ -16,12 +17,12 @@ public class PacketKeepAlive implements Packet {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteMessage msg) {
|
||||
public void encode(ByteMessage msg, Version version) {
|
||||
msg.writeLong(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decode(ByteMessage msg) {
|
||||
public void decode(ByteMessage msg, Version version) {
|
||||
this.id = msg.readLong();
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ package ru.nanit.limbo.protocol.packets.play;
|
||||
|
||||
import ru.nanit.limbo.protocol.ByteMessage;
|
||||
import ru.nanit.limbo.protocol.PacketOut;
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
|
||||
public class PacketPlayerAbilities implements PacketOut {
|
||||
|
||||
@ -22,7 +23,7 @@ public class PacketPlayerAbilities implements PacketOut {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteMessage msg) {
|
||||
public void encode(ByteMessage msg, Version version) {
|
||||
msg.writeByte(flags);
|
||||
msg.writeFloat(flyingSpeed);
|
||||
msg.writeFloat(fieldOfView);
|
||||
|
@ -2,6 +2,7 @@ package ru.nanit.limbo.protocol.packets.play;
|
||||
|
||||
import ru.nanit.limbo.protocol.ByteMessage;
|
||||
import ru.nanit.limbo.protocol.PacketOut;
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@ -27,7 +28,7 @@ public class PacketPlayerInfo implements PacketOut {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteMessage msg) {
|
||||
public void encode(ByteMessage msg, Version version) {
|
||||
msg.writeVarInt(0);
|
||||
msg.writeVarInt(1);
|
||||
msg.writeUuid(uuid);
|
||||
|
@ -2,6 +2,7 @@ package ru.nanit.limbo.protocol.packets.play;
|
||||
|
||||
import ru.nanit.limbo.protocol.ByteMessage;
|
||||
import ru.nanit.limbo.protocol.PacketOut;
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
|
||||
public class PacketPlayerPositionAndLook implements PacketOut {
|
||||
|
||||
@ -42,7 +43,7 @@ public class PacketPlayerPositionAndLook implements PacketOut {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteMessage msg) {
|
||||
public void encode(ByteMessage msg, Version version) {
|
||||
msg.writeDouble(x);
|
||||
msg.writeDouble(y);
|
||||
msg.writeDouble(z);
|
||||
|
@ -2,18 +2,19 @@ package ru.nanit.limbo.protocol.packets.status;
|
||||
|
||||
import ru.nanit.limbo.protocol.ByteMessage;
|
||||
import ru.nanit.limbo.protocol.Packet;
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
|
||||
public class PacketStatusPing implements Packet {
|
||||
|
||||
private long randomId;
|
||||
|
||||
@Override
|
||||
public void encode(ByteMessage msg) {
|
||||
public void encode(ByteMessage msg, Version version) {
|
||||
msg.writeLong(randomId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decode(ByteMessage msg) {
|
||||
public void decode(ByteMessage msg, Version version) {
|
||||
this.randomId = msg.readLong();
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,12 @@
|
||||
package ru.nanit.limbo.protocol.packets.status;
|
||||
|
||||
import ru.nanit.limbo.protocol.*;
|
||||
import ru.nanit.limbo.protocol.registry.Version;
|
||||
|
||||
public class PacketStatusRequest implements PacketIn {
|
||||
|
||||
@Override
|
||||
public void decode(ByteMessage msg) {
|
||||
public void decode(ByteMessage msg, Version version) {
|
||||
|
||||
}
|
||||
|
||||
|
@ -17,10 +17,10 @@ public class PacketStatusResponse implements PacketOut {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteMessage msg) {
|
||||
public void encode(ByteMessage msg, Version version) {
|
||||
String ver = server.getConfig().getPingData().getVersion();
|
||||
String desc = server.getConfig().getPingData().getDescription();
|
||||
String json = getResponseJson(ver, Version.getCurrentSupported().getProtocolNumber(),
|
||||
String json = getResponseJson(ver, Version.getMinimal().getProtocolNumber(),
|
||||
server.getConfig().getMaxPlayers(), server.getConnections().getCount(), desc);
|
||||
|
||||
msg.writeString(json);
|
||||
|
@ -31,7 +31,10 @@ public enum Version {
|
||||
V1_16_1(736),
|
||||
V1_16_2(751),
|
||||
V1_16_3(753),
|
||||
V1_16_4(754);
|
||||
V1_16_4(754),
|
||||
// 1.16.5 has same protocol number
|
||||
V1_17(755),
|
||||
V1_17_1(756);
|
||||
|
||||
public static final Map<Integer, Version> VERSION_MAP;
|
||||
|
||||
@ -43,8 +46,8 @@ public enum Version {
|
||||
}
|
||||
}
|
||||
|
||||
public static Version getCurrentSupported() {
|
||||
return V1_16_4;
|
||||
public static Version getMinimal() {
|
||||
return V1_9;
|
||||
}
|
||||
|
||||
public static Version of(int protocolNumber) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user