#include "Protocol.h" #include "PacketReader.h" #include "PacketTypes.h" #include "MinecraftClient.h" #include // TODO Remove. const std::string json_template = R"({ "version": { "name": "1.16.1", "protocol": 736 }, "players": { "max": 10, "online": 10, "sample": [ { "name": "thinkofdeath", "id": "4566e69f-c907-48ee-8d71-d7ba5aa00d20" } ] }, "description": { "text": "Hello Nukem!" } })"; namespace Feather { void Protocol::HandlePacket(MinecraftClient& client, PacketReader& packet) { auto& context = client.GetContext(); switch (context.GetState()) { case ProtocolState::Handholding: { auto id = packet.ReadVarInt(); uint8_t legacyId = packet.Peek(); if (id != ServerboundHandholdingPacketId::Handshake && legacyId != LegacyServerListPing) { printf("[Protocol] Client sent packet with non-zero ID 0x%x while state was HANDSHAKING\n", id); break; } if (id != ServerboundHandholdingPacketId::Handshake) { // Legacy server ping. // TODO: Do server list ping here. break; } int clientProtocolVersion = packet.ReadVarInt(); string serverIp = packet.ReadString(); uint16_t port = packet.Read(); // next desired state ProtocolState intention = (ProtocolState)(packet.ReadVarInt()); printf("[Protocol] Client Intention Packet: version=%d, serverIp=%s, port=%u, intention=%d\n", clientProtocolVersion, serverIp.c_str(), port, intention ); context.SetState(intention); break; } case ProtocolState::Status: { auto id = packet.ReadVarInt(); switch (id) { case ServerboundStatusPacketId::Request: { printf("[Protocol] Client sent STATUS_PING_REQUEST\n"); DynamicNetworkMessage msg(VARINT_MAX_SIZE + json_template.length()); // Packet ID msg.WriteVarInt(0); // JSON Contents msg.WriteString(json_template.c_str(), static_cast(json_template.length())); msg.Finalize(); client.SendMessage(msg); break; } case ServerboundStatusPacketId::Ping: { int64_t timestamp = packet.Read(); printf("[Protocol] Client sent STATUS_PING: %lld\n", timestamp); DynamicNetworkMessage msg(VARINT_MAX_SIZE + sizeof(int64_t)); msg.WriteVarInt(1); msg.WriteLong(timestamp); msg.Finalize(); client.SendMessage(msg); break; } } break; } } } }