From aebfbfb8c8d623ded8d409d29983a5e871a475bc Mon Sep 17 00:00:00 2001 From: Lane Kolbly Date: Sat, 2 Sep 2017 11:46:57 -0500 Subject: GetPacketID for protocol packet IDs (#3977) * Added GetPacketId method to protocol, implemented for all protocols. * Moved GetPacketID methods into a single file, alpha-sorted. * Fixed 1.12.1 HandlePacket switch statement. * Added SendLogin to the GetPacketId framework. * Added SpawnObject to GetPacketId framework. * Added missing sendEntityEquipment packet ID update for 1.12.1 * Added LeashEntity packet ID change to 1.12.1 * Alphabetized packet enum, added SpawnGlobalEntity to GetPacketId framework * Fixed clang errors * Indented cases, expanded comment for GetPacketId * Changed dyslexic comment. --- src/Protocol/Protocol_1_12.cpp | 1369 ++++------------------------------------ 1 file changed, 133 insertions(+), 1236 deletions(-) (limited to 'src/Protocol/Protocol_1_12.cpp') diff --git a/src/Protocol/Protocol_1_12.cpp b/src/Protocol/Protocol_1_12.cpp index a8e38a4e0..166078aef 100644 --- a/src/Protocol/Protocol_1_12.cpp +++ b/src/Protocol/Protocol_1_12.cpp @@ -985,1205 +985,31 @@ void cProtocol_1_12::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_Mo -void cProtocol_1_12::SendEntityRelMove(const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x26); // Entity Relative Move packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - // TODO: 1.9 changed these from chars to shorts, meaning that there can be more percision and data. Other code needs to be updated for that. - Pkt.WriteBEInt16(a_RelX * 128); - Pkt.WriteBEInt16(a_RelY * 128); - Pkt.WriteBEInt16(a_RelZ * 128); - Pkt.WriteBool(a_Entity.IsOnGround()); -} - - - - - -void cProtocol_1_12::SendEntityRelMoveLook(const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x27); // Entity Look And Relative Move packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - // TODO: 1.9 changed these from chars to shorts, meaning that there can be more percision and data. Other code needs to be updated for that. - Pkt.WriteBEInt16(a_RelX * 128); - Pkt.WriteBEInt16(a_RelY * 128); - Pkt.WriteBEInt16(a_RelZ * 128); - Pkt.WriteByteAngle(a_Entity.GetYaw()); - Pkt.WriteByteAngle(a_Entity.GetPitch()); - Pkt.WriteBool(a_Entity.IsOnGround()); -} - - - - - -void cProtocol_1_12::SendEntityLook(const cEntity & a_Entity) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x28); // Entity Look packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - Pkt.WriteByteAngle(a_Entity.GetYaw()); - Pkt.WriteByteAngle(a_Entity.GetPitch()); - Pkt.WriteBool(a_Entity.IsOnGround()); -} - - - - - -void cProtocol_1_12::SendDestroyEntity(const cEntity & a_Entity) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x31); // Destroy Entities packet - Pkt.WriteVarInt32(1); - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); -} - - - - - -void cProtocol_1_12::SendRemoveEntityEffect(const cEntity & a_Entity, int a_EffectID) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x32); // Remove entity effect packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - Pkt.WriteBEUInt8(static_cast(a_EffectID)); -} - - - - - -void cProtocol_1_12::SendRespawn(eDimension a_Dimension) -{ - cPacketizer Pkt(*this, 0x34); // Respawn packet - cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteBEInt32(static_cast(a_Dimension)); - Pkt.WriteBEUInt8(2); // TODO: Difficulty (set to Normal) - Pkt.WriteBEUInt8(static_cast(Player->GetEffectiveGameMode())); - Pkt.WriteString("default"); -} - - - - - -void cProtocol_1_12::SendEntityHeadLook(const cEntity & a_Entity) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x35); // Entity Head Look packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - Pkt.WriteByteAngle(a_Entity.GetHeadYaw()); -} - - - - - -void cProtocol_1_12::SendCameraSetTo(const cEntity & a_Entity) -{ - cPacketizer Pkt(*this, 0x38); // Camera Packet (Attach the camera of a player at another entity in spectator mode) - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); -} - - - - - -void cProtocol_1_12::SendDisplayObjective(const AString & a_Objective, cScoreboard::eDisplaySlot a_Display) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x3a); // Display scoreboard packet - Pkt.WriteBEUInt8(static_cast(a_Display)); - Pkt.WriteString(a_Objective); -} - - - - - -void cProtocol_1_12::SendEntityMetadata(const cEntity & a_Entity) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x3b); // Entity Metadata packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - WriteEntityMetadata(Pkt, a_Entity); - Pkt.WriteBEUInt8(0xff); // The termination byte -} - - - - - -void cProtocol_1_12::SendLeashEntity(const cEntity & a_Entity, const cEntity & a_EntityLeashedTo) -{ - ASSERT(m_State == 3); // In game mode? - cPacketizer Pkt(*this, 0x3c); // Set Attach Entity packet - Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); - Pkt.WriteBEUInt32(a_EntityLeashedTo.GetUniqueID()); -} - - - - - -void cProtocol_1_12::SendUnleashEntity(const cEntity & a_Entity) -{ - ASSERT(m_State == 3); // In game mode? - cPacketizer Pkt(*this, 0x3c); // Set Attach Entity packet - Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); - Pkt.WriteBEInt32(-1); // Unleash a_Entity -} - - - - - -void cProtocol_1_12::SendEntityVelocity(const cEntity & a_Entity) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x3d); // Entity Velocity packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - // 400 = 8000 / 20 ... Conversion from our speed in m / s to 8000 m / tick - Pkt.WriteBEInt16(static_cast(a_Entity.GetSpeedX() * 400)); - Pkt.WriteBEInt16(static_cast(a_Entity.GetSpeedY() * 400)); - Pkt.WriteBEInt16(static_cast(a_Entity.GetSpeedZ() * 400)); -} - - - - - -void cProtocol_1_12::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x3e); // Entity Equipment packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - // Needs to be adjusted due to the insertion of offhand at slot 1 - if (a_SlotNum > 0) - { - a_SlotNum++; - } - Pkt.WriteVarInt32(static_cast(a_SlotNum)); - WriteItem(Pkt, a_Item); -} - - - - - -void cProtocol_1_12::SendExperience(void) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x3f); // Set Experience Packet - cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteBEFloat(Player->GetXpPercentage()); - Pkt.WriteVarInt32(static_cast(Player->GetXpLevel())); - Pkt.WriteVarInt32(static_cast(Player->GetCurrentXp())); -} - - - - - -void cProtocol_1_12::SendHealth(void) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x40); // Update Health packet - cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteBEFloat(static_cast(Player->GetHealth())); - Pkt.WriteVarInt32(static_cast(Player->GetFoodLevel())); - Pkt.WriteBEFloat(static_cast(Player->GetFoodSaturationLevel())); -} - - - - - -void cProtocol_1_12::SendScoreboardObjective(const AString & a_Name, const AString & a_DisplayName, Byte a_Mode) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x41); // Scoreboard objective packet - Pkt.WriteString(a_Name); - Pkt.WriteBEUInt8(a_Mode); - if ((a_Mode == 0) || (a_Mode == 2)) - { - Pkt.WriteString(a_DisplayName); - Pkt.WriteString("integer"); - } -} - - - - - -void cProtocol_1_12::SendAttachEntity(const cEntity & a_Entity, const cEntity & a_Vehicle) -{ - ASSERT(m_State == 3); // In game mode? - cPacketizer Pkt(*this, 0x42); // Set Passengers packet - Pkt.WriteVarInt32(a_Vehicle.GetUniqueID()); - Pkt.WriteVarInt32(1); // 1 passenger - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); -} - - - - - -void cProtocol_1_12::SendDetachEntity(const cEntity & a_Entity, const cEntity & a_PreviousVehicle) -{ - ASSERT(m_State == 3); // In game mode? - cPacketizer Pkt(*this, 0x42); // Set Passengers packet - Pkt.WriteVarInt32(a_PreviousVehicle.GetUniqueID()); - Pkt.WriteVarInt32(0); // No passangers -} - - - - - -void cProtocol_1_12::SendScoreUpdate(const AString & a_Objective, const AString & a_Player, cObjective::Score a_Score, Byte a_Mode) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x44); // Update score packet - Pkt.WriteString(a_Player); - Pkt.WriteBEUInt8(a_Mode); - Pkt.WriteString(a_Objective); - - if (a_Mode != 1) - { - Pkt.WriteVarInt32(static_cast(a_Score)); - } -} - - - - - -void cProtocol_1_12::SendLogin(const cPlayer & a_Player, const cWorld & a_World) -{ - // Send the Join Game packet: - { - cServer * Server = cRoot::Get()->GetServer(); - cPacketizer Pkt(*this, 0x23); // Join Game packet - Pkt.WriteBEUInt32(a_Player.GetUniqueID()); - Pkt.WriteBEUInt8(static_cast(a_Player.GetEffectiveGameMode()) | (Server->IsHardcore() ? 0x08 : 0)); // Hardcore flag bit 4 - Pkt.WriteBEInt32(static_cast(a_World.GetDimension())); - Pkt.WriteBEUInt8(2); // TODO: Difficulty (set to Normal) - Pkt.WriteBEUInt8(static_cast(Clamp(Server->GetMaxPlayers(), 0, 255))); - Pkt.WriteString("default"); // Level type - wtf? - Pkt.WriteBool(false); // Reduced Debug Info - wtf? - } - - // Send the spawn position: - { - cPacketizer Pkt(*this, 0x45); // Spawn Position packet - Pkt.WritePosition64(FloorC(a_World.GetSpawnX()), FloorC(a_World.GetSpawnY()), FloorC(a_World.GetSpawnZ())); - } - - // Send the server difficulty: - { - cPacketizer Pkt(*this, 0x0d); // Server difficulty packet - Pkt.WriteBEInt8(1); - } - - // Send player abilities: - SendPlayerAbilities(); -} - - - - - -void cProtocol_1_12::SendUpdateBlockEntity(cBlockEntity & a_BlockEntity) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x09); // Update tile entity packet - Pkt.WritePosition64(a_BlockEntity.GetPosX(), a_BlockEntity.GetPosY(), a_BlockEntity.GetPosZ()); - - Byte Action = 0; - switch (a_BlockEntity.GetBlockType()) - { - case E_BLOCK_MOB_SPAWNER: Action = 1; break; // Update mob spawner spinny mob thing - case E_BLOCK_COMMAND_BLOCK: Action = 2; break; // Update command block text - case E_BLOCK_BEACON: Action = 3; break; // Update beacon entity - case E_BLOCK_HEAD: Action = 4; break; // Update Mobhead entity - case E_BLOCK_FLOWER_POT: Action = 5; break; // Update flower pot - case E_BLOCK_BED: Action = 11; break; // Update bed color - default: ASSERT(!"Unhandled or unimplemented BlockEntity update request!"); break; - } - Pkt.WriteBEUInt8(Action); - - WriteBlockEntity(Pkt, a_BlockEntity); -} - - - - - -void cProtocol_1_12::HandlePacketCraftingBookData(cByteBuffer & a_ByteBuffer) -{ - a_ByteBuffer.SkipRead(a_ByteBuffer.GetReadableSpace() - 1); - m_Client->GetPlayer()->SendMessageInfo("The green crafting book feature is not implemented yet."); -} - - - - - -void cProtocol_1_12::HandlePacketAdvancementTab(cByteBuffer & a_ByteBuffer) -{ - a_ByteBuffer.SkipRead(a_ByteBuffer.GetReadableSpace() - 1); - m_Client->GetPlayer()->SendMessageInfo("The new advancements are not implemented."); -} - - - - - -void cProtocol_1_12::SendTimeUpdate(Int64 a_WorldAge, Int64 a_TimeOfDay, bool a_DoDaylightCycle) -{ - ASSERT(m_State == 3); // In game mode? - if (!a_DoDaylightCycle) - { - // When writing a "-" before the number the client ignores it but it will stop the client-side time expiration. - a_TimeOfDay = std::min(-a_TimeOfDay, -1LL); - } - - cPacketizer Pkt(*this, 0x46); // Time update packet - Pkt.WriteBEInt64(a_WorldAge); - Pkt.WriteBEInt64(a_TimeOfDay); -} - - - - - -void cProtocol_1_12::SendSetRawTitle(const AString & a_Title) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x47); // Title packet - Pkt.WriteVarInt32(0); // Set title - - Pkt.WriteString(a_Title); -} - - - - - -void cProtocol_1_12::SendSetRawSubTitle(const AString & a_SubTitle) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x47); // Title packet - Pkt.WriteVarInt32(1); // Set subtitle - - Pkt.WriteString(a_SubTitle); -} - - - - - -void cProtocol_1_12::SendTitleTimes(int a_FadeInTicks, int a_DisplayTicks, int a_FadeOutTicks) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x47); // Title packet - Pkt.WriteVarInt32(3); // Set title display times - Pkt.WriteBEInt32(a_FadeInTicks); - Pkt.WriteBEInt32(a_DisplayTicks); - Pkt.WriteBEInt32(a_FadeOutTicks); -} - - - - - -void cProtocol_1_12::SendHideTitle(void) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x47); // Title packet - Pkt.WriteVarInt32(4); // Hide title -} - - - - - -void cProtocol_1_12::SendResetTitle(void) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x47); // Title packet - Pkt.WriteVarInt32(5); // Reset title -} - - - - - -void cProtocol_1_12::SendCollectEntity(const cEntity & a_Entity, const cPlayer & a_Player, int a_Count) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x4a); // Collect Item packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - Pkt.WriteVarInt32(a_Player.GetUniqueID()); - Pkt.WriteVarInt32(static_cast(a_Count)); -} - - - - - -void cProtocol_1_12::SendTeleportEntity(const cEntity & a_Entity) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x4b); // Entity teleport packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - Pkt.WriteBEDouble(a_Entity.GetPosX()); - Pkt.WriteBEDouble(a_Entity.GetPosY()); - Pkt.WriteBEDouble(a_Entity.GetPosZ()); - Pkt.WriteByteAngle(a_Entity.GetYaw()); - Pkt.WriteByteAngle(a_Entity.GetPitch()); - Pkt.WriteBool(a_Entity.IsOnGround()); -} - - - - - -void cProtocol_1_12::SendEntityProperties(const cEntity & a_Entity) -{ - ASSERT(m_State == 3); // In game mode? - - - cPacketizer Pkt(*this, 0x4d); // Entity Properties packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - WriteEntityProperties(Pkt, a_Entity); -} - - - - - -void cProtocol_1_12::SendPlayerMaxSpeed(void) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x4d); // Entity Properties - cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteVarInt32(Player->GetUniqueID()); - Pkt.WriteBEInt32(1); // Count - Pkt.WriteString("generic.movementSpeed"); - // The default game speed is 0.1, multiply that value by the relative speed: - Pkt.WriteBEDouble(0.1 * Player->GetNormalMaxSpeed()); - if (Player->IsSprinting()) - { - Pkt.WriteVarInt32(1); // Modifier count - Pkt.WriteBEUInt64(0x662a6b8dda3e4c1c); - Pkt.WriteBEUInt64(0x881396ea6097278d); // UUID of the modifier - Pkt.WriteBEDouble(Player->GetSprintingMaxSpeed() - Player->GetNormalMaxSpeed()); - Pkt.WriteBEUInt8(2); - } - else - { - Pkt.WriteVarInt32(0); // Modifier count - } -} - - - - - -void cProtocol_1_12::SendEntityEffect(const cEntity & a_Entity, int a_EffectID, int a_Amplifier, short a_Duration) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x4e); // Entity Effect packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - Pkt.WriteBEUInt8(static_cast(a_EffectID)); - Pkt.WriteBEUInt8(static_cast(a_Amplifier)); - Pkt.WriteVarInt32(static_cast(a_Duration)); - Pkt.WriteBool(false); // Hide particles -} - - - - - -bool cProtocol_1_12::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketType) -{ - switch (m_State) - { - case 1: - { - // Status - switch (a_PacketType) - { - case 0x00: HandlePacketStatusRequest(a_ByteBuffer); return true; - case 0x01: HandlePacketStatusPing(a_ByteBuffer); return true; - } - break; - } - - case 2: - { - // Login - switch (a_PacketType) - { - case 0x00: HandlePacketLoginStart(a_ByteBuffer); return true; - case 0x01: HandlePacketLoginEncryptionResponse(a_ByteBuffer); return true; - } - break; - } - - case 3: - { - // Game - switch (a_PacketType) - { - case 0x00: HandleConfirmTeleport(a_ByteBuffer); return true; - case 0x01: break; // Prepare Crafting Grid, not yet implemented - case 0x02: HandlePacketTabComplete(a_ByteBuffer); return true; - case 0x03: HandlePacketChatMessage(a_ByteBuffer); return true; - case 0x04: HandlePacketClientStatus(a_ByteBuffer); return true; - case 0x05: HandlePacketClientSettings(a_ByteBuffer); return true; - case 0x06: break; // Confirm transaction - not used in Cuberite - case 0x07: HandlePacketEnchantItem(a_ByteBuffer); return true; - case 0x08: HandlePacketWindowClick(a_ByteBuffer); return true; - case 0x09: HandlePacketWindowClose(a_ByteBuffer); return true; - case 0x0a: HandlePacketPluginMessage(a_ByteBuffer); return true; - case 0x0b: HandlePacketUseEntity(a_ByteBuffer); return true; - case 0x0c: HandlePacketKeepAlive(a_ByteBuffer); return true; - case 0x0d: HandlePacketPlayer(a_ByteBuffer); return true; - case 0x0e: HandlePacketPlayerPos(a_ByteBuffer); return true; - case 0x0f: HandlePacketPlayerPosLook(a_ByteBuffer); return true; - case 0x10: HandlePacketPlayerLook(a_ByteBuffer); return true; - case 0x11: HandlePacketVehicleMove(a_ByteBuffer); return true; - case 0x12: HandlePacketBoatSteer(a_ByteBuffer); return true; - case 0x13: HandlePacketPlayerAbilities(a_ByteBuffer); return true; - case 0x14: HandlePacketBlockDig(a_ByteBuffer); return true; - case 0x15: HandlePacketEntityAction(a_ByteBuffer); return true; - case 0x16: HandlePacketSteerVehicle(a_ByteBuffer); return true; - case 0x17: HandlePacketCraftingBookData(a_ByteBuffer); return true; - case 0x18: break; // Resource pack status - not yet implemented - case 0x19: HandlePacketAdvancementTab(a_ByteBuffer); return true; - case 0x1a: HandlePacketSlotSelect(a_ByteBuffer); return true; - case 0x1b: HandlePacketCreativeInventoryAction(a_ByteBuffer); return true; - case 0x1c: HandlePacketUpdateSign(a_ByteBuffer); return true; - case 0x1d: HandlePacketAnimation(a_ByteBuffer); return true; - case 0x1e: HandlePacketSpectate(a_ByteBuffer); return true; - case 0x1f: HandlePacketBlockPlace(a_ByteBuffer); return true; - case 0x20: HandlePacketUseItem(a_ByteBuffer); return true; - } - break; - } - default: - { - // Received a packet in an unknown state, report: - LOGWARNING("Received a packet in an unknown protocol state %d. Ignoring further packets.", m_State); - - // Cannot kick the client - we don't know this state and thus the packet number for the kick packet - - // Switch to a state when all further packets are silently ignored: - m_State = 255; - return false; - } - case 255: - { - // This is the state used for "not processing packets anymore" when we receive a bad packet from a client. - // Do not output anything (the caller will do that for us), just return failure - return false; - } - } // switch (m_State) - - // Unknown packet type, report to the ClientHandle: - m_Client->PacketUnknown(a_PacketType); - return false; -} - - - - - -cProtocol_1_12_1::cProtocol_1_12_1(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State) : - super(a_Client, a_ServerAddress, a_ServerPort, a_State) -{ -} - - - - - -void cProtocol_1_12_1::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) -{ - cServer * Server = cRoot::Get()->GetServer(); - AString ServerDescription = Server->GetDescription(); - auto NumPlayers = static_cast(Server->GetNumPlayers()); - auto MaxPlayers = static_cast(Server->GetMaxPlayers()); - AString Favicon = Server->GetFaviconData(); - cRoot::Get()->GetPluginManager()->CallHookServerPing(*m_Client, ServerDescription, NumPlayers, MaxPlayers, Favicon); - - // Version: - Json::Value Version; - Version["name"] = "Cuberite 1.12.1"; - Version["protocol"] = cProtocolRecognizer::PROTO_VERSION_1_12_1; - - // Players: - Json::Value Players; - Players["online"] = NumPlayers; - Players["max"] = MaxPlayers; - // TODO: Add "sample" - - // Description: - Json::Value Description; - Description["text"] = ServerDescription.c_str(); - - // Create the response: - Json::Value ResponseValue; - ResponseValue["version"] = Version; - ResponseValue["players"] = Players; - ResponseValue["description"] = Description; - m_Client->ForgeAugmentServerListPing(ResponseValue); - if (!Favicon.empty()) - { - ResponseValue["favicon"] = Printf("data:image/png;base64,%s", Favicon.c_str()); - } - - // Serialize the response into a packet: - Json::FastWriter Writer; - cPacketizer Pkt(*this, 0x00); // Response packet - Pkt.WriteString(Writer.write(ResponseValue)); -} - - - - - -void cProtocol_1_12_1::SendRespawn(eDimension a_Dimension) -{ - cPacketizer Pkt(*this, 0x35); // Respawn packet - cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteBEInt32(static_cast(a_Dimension)); - Pkt.WriteBEUInt8(2); // TODO: Difficulty (set to Normal) - Pkt.WriteBEUInt8(static_cast(Player->GetEffectiveGameMode())); - Pkt.WriteString("default"); -} - - - - - -void cProtocol_1_12_1::SendPlayerListAddPlayer(const cPlayer & a_Player) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x2e); // Player List Item packet - Pkt.WriteVarInt32(0); - Pkt.WriteVarInt32(1); - Pkt.WriteUUID(a_Player.GetUUID()); - Pkt.WriteString(a_Player.GetPlayerListName()); - - const Json::Value & Properties = a_Player.GetClientHandle()->GetProperties(); - Pkt.WriteVarInt32(Properties.size()); - for (auto & Node : Properties) - { - Pkt.WriteString(Node.get("name", "").asString()); - Pkt.WriteString(Node.get("value", "").asString()); - AString Signature = Node.get("signature", "").asString(); - if (Signature.empty()) - { - Pkt.WriteBool(false); - } - else - { - Pkt.WriteBool(true); - Pkt.WriteString(Signature); - } - } - - Pkt.WriteVarInt32(static_cast(a_Player.GetGameMode())); - Pkt.WriteVarInt32(static_cast(a_Player.GetClientHandle()->GetPing())); - Pkt.WriteBool(false); -} - - - - - -void cProtocol_1_12_1::SendPlayerListRemovePlayer(const cPlayer & a_Player) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x2e); // Player List Item packet - Pkt.WriteVarInt32(4); - Pkt.WriteVarInt32(1); - Pkt.WriteUUID(a_Player.GetUUID()); -} - - - - - -void cProtocol_1_12_1::SendPlayerListUpdateGameMode(const cPlayer & a_Player) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x2e); // Player List Item packet - Pkt.WriteVarInt32(1); - Pkt.WriteVarInt32(1); - Pkt.WriteUUID(a_Player.GetUUID()); - Pkt.WriteVarInt32(static_cast(a_Player.GetGameMode())); -} - - - - - -void cProtocol_1_12_1::SendPlayerListUpdatePing(const cPlayer & a_Player) -{ - ASSERT(m_State == 3); // In game mode? - - auto ClientHandle = a_Player.GetClientHandlePtr(); - if (ClientHandle != nullptr) - { - cPacketizer Pkt(*this, 0x2e); // Player List Item packet - Pkt.WriteVarInt32(2); - Pkt.WriteVarInt32(1); - Pkt.WriteUUID(a_Player.GetUUID()); - Pkt.WriteVarInt32(static_cast(ClientHandle->GetPing())); - } -} - - - - - -void cProtocol_1_12_1::SendPlayerListUpdateDisplayName(const cPlayer & a_Player, const AString & a_CustomName) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x2e); // Player List Item packet - Pkt.WriteVarInt32(3); - Pkt.WriteVarInt32(1); - Pkt.WriteUUID(a_Player.GetUUID()); - - if (a_CustomName.empty()) - { - Pkt.WriteBool(false); - } - else - { - Pkt.WriteBool(true); - Pkt.WriteString(Printf("{\"text\":\"%s\"}", a_CustomName.c_str())); - } -} - - - - - -void cProtocol_1_12_1::SendPlayerAbilities(void) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x2c); // Player Abilities packet - Byte Flags = 0; - cPlayer * Player = m_Client->GetPlayer(); - if (Player->IsGameModeCreative()) - { - Flags |= 0x01; - Flags |= 0x08; // Godmode, used for creative - } - if (Player->IsFlying()) - { - Flags |= 0x02; - } - if (Player->CanFly()) - { - Flags |= 0x04; - } - Pkt.WriteBEUInt8(Flags); - Pkt.WriteBEFloat(static_cast(0.05 * Player->GetFlyingMaxSpeed())); - Pkt.WriteBEFloat(static_cast(0.1 * Player->GetNormalMaxSpeed())); -} - - - - - -void cProtocol_1_12_1::SendPlayerMoveLook(void) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x2f); // Player Position And Look packet - cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteBEDouble(Player->GetPosX()); - Pkt.WriteBEDouble(Player->GetPosY()); - Pkt.WriteBEDouble(Player->GetPosZ()); - Pkt.WriteBEFloat(static_cast(Player->GetYaw())); - Pkt.WriteBEFloat(static_cast(Player->GetPitch())); - Pkt.WriteBEUInt8(0); - Pkt.WriteVarInt32(++m_OutstandingTeleportId); - - // This teleport ID hasn't been confirmed yet - m_IsTeleportIdConfirmed = false; -} - - - - - -void cProtocol_1_12_1::SendUseBed(const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x30); // Use bed - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - Pkt.WritePosition64(a_BlockX, a_BlockY, a_BlockZ); -} - - - - - -void cProtocol_1_12_1::SendDestroyEntity(const cEntity & a_Entity) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x32); // Destroy Entities packet - Pkt.WriteVarInt32(1); - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); -} - - - - - -void cProtocol_1_12_1::SendRemoveEntityEffect(const cEntity & a_Entity, int a_EffectID) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x33); // Remove entity effect packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - Pkt.WriteBEUInt8(static_cast(a_EffectID)); -} - - - - - -void cProtocol_1_12_1::SendEntityHeadLook(const cEntity & a_Entity) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x36); // Entity Head Look packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - Pkt.WriteByteAngle(a_Entity.GetHeadYaw()); -} - - - - - -void cProtocol_1_12_1::SendCameraSetTo(const cEntity & a_Entity) -{ - cPacketizer Pkt(*this, 0x39); // Camera packet (Attach the camera of a player at another entity in spectator mode) - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); -} - - - - - -void cProtocol_1_12_1::SendDisplayObjective(const AString & a_Objective, cScoreboard::eDisplaySlot a_Display) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x3b); // Display Scoreboard packet - Pkt.WriteBEUInt8(static_cast(a_Display)); - Pkt.WriteString(a_Objective); -} - - - - - -void cProtocol_1_12_1::SendEntityMetadata(const cEntity & a_Entity) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x3c); // Entity Metadata packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - WriteEntityMetadata(Pkt, a_Entity); - Pkt.WriteBEUInt8(0xff); // The termination byte -} - - - - - -void cProtocol_1_12_1::SendLeashEntity(const cEntity & a_Entity, const cEntity & a_EntityLeashedTo) -{ - ASSERT(m_State == 3); // In game mode? - cPacketizer Pkt(*this, 0x3d); // Set Attach Entity packet - Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); - Pkt.WriteBEUInt32(a_EntityLeashedTo.GetUniqueID()); -} - - - - - -void cProtocol_1_12_1::SendUnleashEntity(const cEntity & a_Entity) -{ - ASSERT(m_State == 3); // In game mode? - cPacketizer Pkt(*this, 0x3d); // Set Attach Entity packet - Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); - Pkt.WriteBEInt32(-1); // Unleash a_Entity -} - - - - - -void cProtocol_1_12_1::SendEntityVelocity(const cEntity & a_Entity) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x3e); // Entity Velocity packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - // 400 = 8000 / 20 ... Conversion from our speed in m / s to 8000 m / tick - Pkt.WriteBEInt16(static_cast(a_Entity.GetSpeedX() * 400)); - Pkt.WriteBEInt16(static_cast(a_Entity.GetSpeedY() * 400)); - Pkt.WriteBEInt16(static_cast(a_Entity.GetSpeedZ() * 400)); -} - - - - - -void cProtocol_1_12_1::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x3f); // Entity Equipment packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - // Needs to be adjusted due to the insertion of offhand at slot 1 - if (a_SlotNum > 0) - { - a_SlotNum++; - } - Pkt.WriteVarInt32(static_cast(a_SlotNum)); - WriteItem(Pkt, a_Item); -} - - - - - -void cProtocol_1_12_1::SendExperience(void) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x40); // Set Experience packet - cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteBEFloat(Player->GetXpPercentage()); - Pkt.WriteVarInt32(static_cast(Player->GetXpLevel())); - Pkt.WriteVarInt32(static_cast(Player->GetCurrentXp())); -} - - - - - -void cProtocol_1_12_1::SendHealth(void) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x41); // Update Health packet - cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteBEFloat(static_cast(Player->GetHealth())); - Pkt.WriteVarInt32(static_cast(Player->GetFoodLevel())); - Pkt.WriteBEFloat(static_cast(Player->GetFoodSaturationLevel())); -} - - - - - -void cProtocol_1_12_1::SendScoreboardObjective(const AString & a_Name, const AString & a_DisplayName, Byte a_Mode) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x42); // Scoreboard Objective packet - Pkt.WriteString(a_Name); - Pkt.WriteBEUInt8(a_Mode); - if ((a_Mode == 0) || (a_Mode == 2)) - { - Pkt.WriteString(a_DisplayName); - Pkt.WriteString("integer"); - } -} - - - - - -void cProtocol_1_12_1::SendAttachEntity(const cEntity & a_Entity, const cEntity & a_Vehicle) -{ - ASSERT(m_State == 3); // In game mode? - cPacketizer Pkt(*this, 0x43); // Set Passengers packet - Pkt.WriteVarInt32(a_Vehicle.GetUniqueID()); - Pkt.WriteVarInt32(1); // 1 passenger - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); -} - - - - - -void cProtocol_1_12_1::SendDetachEntity(const cEntity & a_Entity, const cEntity & a_PreviousVehicle) -{ - ASSERT(m_State == 3); // In game mode? - cPacketizer Pkt(*this, 0x43); // Set Passengers packet - Pkt.WriteVarInt32(a_PreviousVehicle.GetUniqueID()); - Pkt.WriteVarInt32(0); // No passangers -} - - - - - -void cProtocol_1_12_1::SendScoreUpdate(const AString & a_Objective, const AString & a_Player, cObjective::Score a_Score, Byte a_Mode) -{ - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x45); // Update Score packet - Pkt.WriteString(a_Player); - Pkt.WriteBEUInt8(a_Mode); - Pkt.WriteString(a_Objective); - - if (a_Mode != 1) - { - Pkt.WriteVarInt32(static_cast(a_Score)); - } -} - - - - - -void cProtocol_1_12_1::SendLogin(const cPlayer & a_Player, const cWorld & a_World) -{ - // Send the Join Game packet: - { - cServer * Server = cRoot::Get()->GetServer(); - cPacketizer Pkt(*this, 0x23); // Join Game packet - Pkt.WriteBEUInt32(a_Player.GetUniqueID()); - Pkt.WriteBEUInt8(static_cast(a_Player.GetEffectiveGameMode()) | (Server->IsHardcore() ? 0x08 : 0)); // Hardcore flag bit 4 - Pkt.WriteBEInt32(static_cast(a_World.GetDimension())); - Pkt.WriteBEUInt8(2); // TODO: Difficulty (set to Normal) - Pkt.WriteBEUInt8(static_cast(Clamp(Server->GetMaxPlayers(), 0, 255))); - Pkt.WriteString("default"); // Level type - wtf? - Pkt.WriteBool(false); // Reduced Debug Info - wtf? - } - - // Send the spawn position: - { - cPacketizer Pkt(*this, 0x46); // Spawn Position packet - Pkt.WritePosition64(FloorC(a_World.GetSpawnX()), FloorC(a_World.GetSpawnY()), FloorC(a_World.GetSpawnZ())); - } - - // Send the server difficulty: - { - cPacketizer Pkt(*this, 0x0d); // Server difficulty packet - Pkt.WriteBEInt8(1); - } - - // Send player abilities: - SendPlayerAbilities(); -} - - - - - -void cProtocol_1_12_1::SendTimeUpdate(Int64 a_WorldAge, Int64 a_TimeOfDay, bool a_DoDaylightCycle) -{ - ASSERT(m_State == 3); // In game mode? - if (!a_DoDaylightCycle) - { - // When writing a "-" before the number the client ignores it but it will stop the client-side time expiration. - a_TimeOfDay = std::min(-a_TimeOfDay, -1LL); - } - - cPacketizer Pkt(*this, 0x47); // Time update packet - Pkt.WriteBEInt64(a_WorldAge); - Pkt.WriteBEInt64(a_TimeOfDay); -} - - - - - -void cProtocol_1_12_1::SendSetRawTitle(const AString & a_Title) +void cProtocol_1_12::HandlePacketCraftingBookData(cByteBuffer & a_ByteBuffer) { - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x48); // Title packet - Pkt.WriteVarInt32(0); // Set title - - Pkt.WriteString(a_Title); + a_ByteBuffer.SkipRead(a_ByteBuffer.GetReadableSpace() - 1); + m_Client->GetPlayer()->SendMessageInfo("The green crafting book feature is not implemented yet."); } -void cProtocol_1_12_1::SendSetRawSubTitle(const AString & a_SubTitle) +void cProtocol_1_12::HandlePacketAdvancementTab(cByteBuffer & a_ByteBuffer) { - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x48); // Title packet - Pkt.WriteVarInt32(1); // Set subtitle - - Pkt.WriteString(a_SubTitle); + a_ByteBuffer.SkipRead(a_ByteBuffer.GetReadableSpace() - 1); + m_Client->GetPlayer()->SendMessageInfo("The new advancements are not implemented."); } -void cProtocol_1_12_1::SendTitleTimes(int a_FadeInTicks, int a_DisplayTicks, int a_FadeOutTicks) +void cProtocol_1_12::SendTitleTimes(int a_FadeInTicks, int a_DisplayTicks, int a_FadeOutTicks) { ASSERT(m_State == 3); // In game mode? - cPacketizer Pkt(*this, 0x48); // Title packet + cPacketizer Pkt(*this, GetPacketId(sendTitle)); // Title packet Pkt.WriteVarInt32(3); // Set title display times Pkt.WriteBEInt32(a_FadeInTicks); Pkt.WriteBEInt32(a_DisplayTicks); @@ -2194,11 +1020,11 @@ void cProtocol_1_12_1::SendTitleTimes(int a_FadeInTicks, int a_DisplayTicks, int -void cProtocol_1_12_1::SendHideTitle(void) +void cProtocol_1_12::SendHideTitle(void) { ASSERT(m_State == 3); // In game mode? - cPacketizer Pkt(*this, 0x48); // Title packet + cPacketizer Pkt(*this, GetPacketId(sendTitle)); // Title packet Pkt.WriteVarInt32(4); // Hide title } @@ -2206,11 +1032,11 @@ void cProtocol_1_12_1::SendHideTitle(void) -void cProtocol_1_12_1::SendResetTitle(void) +void cProtocol_1_12::SendResetTitle(void) { ASSERT(m_State == 3); // In game mode? - cPacketizer Pkt(*this, 0x48); // Title packet + cPacketizer Pkt(*this, GetPacketId(sendTitle)); // Title packet Pkt.WriteVarInt32(5); // Reset title } @@ -2218,11 +1044,11 @@ void cProtocol_1_12_1::SendResetTitle(void) -void cProtocol_1_12_1::SendCollectEntity(const cEntity & a_Entity, const cPlayer & a_Player, int a_Count) +void cProtocol_1_12::SendCollectEntity(const cEntity & a_Entity, const cPlayer & a_Player, int a_Count) { ASSERT(m_State == 3); // In game mode? - cPacketizer Pkt(*this, 0x4b); // Collect Item packet + cPacketizer Pkt(*this, GetPacketId(sendCollectEntity)); // Collect Item packet Pkt.WriteVarInt32(a_Entity.GetUniqueID()); Pkt.WriteVarInt32(a_Player.GetUniqueID()); Pkt.WriteVarInt32(static_cast(a_Count)); @@ -2232,77 +1058,148 @@ void cProtocol_1_12_1::SendCollectEntity(const cEntity & a_Entity, const cPlayer -void cProtocol_1_12_1::SendTeleportEntity(const cEntity & a_Entity) +bool cProtocol_1_12::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketType) { - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x4c); // Entity teleport packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - Pkt.WriteBEDouble(a_Entity.GetPosX()); - Pkt.WriteBEDouble(a_Entity.GetPosY()); - Pkt.WriteBEDouble(a_Entity.GetPosZ()); - Pkt.WriteByteAngle(a_Entity.GetYaw()); - Pkt.WriteByteAngle(a_Entity.GetPitch()); - Pkt.WriteBool(a_Entity.IsOnGround()); -} - - + switch (m_State) + { + case 1: + { + // Status + switch (a_PacketType) + { + case 0x00: HandlePacketStatusRequest(a_ByteBuffer); return true; + case 0x01: HandlePacketStatusPing(a_ByteBuffer); return true; + } + break; + } + case 2: + { + // Login + switch (a_PacketType) + { + case 0x00: HandlePacketLoginStart(a_ByteBuffer); return true; + case 0x01: HandlePacketLoginEncryptionResponse(a_ByteBuffer); return true; + } + break; + } + case 3: + { + // Game + switch (a_PacketType) + { + case 0x00: HandleConfirmTeleport(a_ByteBuffer); return true; + case 0x01: break; // Prepare Crafting Grid, not yet implemented + case 0x02: HandlePacketTabComplete(a_ByteBuffer); return true; + case 0x03: HandlePacketChatMessage(a_ByteBuffer); return true; + case 0x04: HandlePacketClientStatus(a_ByteBuffer); return true; + case 0x05: HandlePacketClientSettings(a_ByteBuffer); return true; + case 0x06: break; // Confirm transaction - not used in Cuberite + case 0x07: HandlePacketEnchantItem(a_ByteBuffer); return true; + case 0x08: HandlePacketWindowClick(a_ByteBuffer); return true; + case 0x09: HandlePacketWindowClose(a_ByteBuffer); return true; + case 0x0a: HandlePacketPluginMessage(a_ByteBuffer); return true; + case 0x0b: HandlePacketUseEntity(a_ByteBuffer); return true; + case 0x0c: HandlePacketKeepAlive(a_ByteBuffer); return true; + case 0x0d: HandlePacketPlayer(a_ByteBuffer); return true; + case 0x0e: HandlePacketPlayerPos(a_ByteBuffer); return true; + case 0x0f: HandlePacketPlayerPosLook(a_ByteBuffer); return true; + case 0x10: HandlePacketPlayerLook(a_ByteBuffer); return true; + case 0x11: HandlePacketVehicleMove(a_ByteBuffer); return true; + case 0x12: HandlePacketBoatSteer(a_ByteBuffer); return true; + case 0x13: HandlePacketPlayerAbilities(a_ByteBuffer); return true; + case 0x14: HandlePacketBlockDig(a_ByteBuffer); return true; + case 0x15: HandlePacketEntityAction(a_ByteBuffer); return true; + case 0x16: HandlePacketSteerVehicle(a_ByteBuffer); return true; + case 0x17: HandlePacketCraftingBookData(a_ByteBuffer); return true; + case 0x18: break; // Resource pack status - not yet implemented + case 0x19: HandlePacketAdvancementTab(a_ByteBuffer); return true; + case 0x1a: HandlePacketSlotSelect(a_ByteBuffer); return true; + case 0x1b: HandlePacketCreativeInventoryAction(a_ByteBuffer); return true; + case 0x1c: HandlePacketUpdateSign(a_ByteBuffer); return true; + case 0x1d: HandlePacketAnimation(a_ByteBuffer); return true; + case 0x1e: HandlePacketSpectate(a_ByteBuffer); return true; + case 0x1f: HandlePacketBlockPlace(a_ByteBuffer); return true; + case 0x20: HandlePacketUseItem(a_ByteBuffer); return true; + } + break; + } + default: + { + // Received a packet in an unknown state, report: + LOGWARNING("Received a packet in an unknown protocol state %d. Ignoring further packets.", m_State); -void cProtocol_1_12_1::SendEntityProperties(const cEntity & a_Entity) -{ - ASSERT(m_State == 3); // In game mode? + // Cannot kick the client - we don't know this state and thus the packet number for the kick packet + // Switch to a state when all further packets are silently ignored: + m_State = 255; + return false; + } + case 255: + { + // This is the state used for "not processing packets anymore" when we receive a bad packet from a client. + // Do not output anything (the caller will do that for us), just return failure + return false; + } + } // switch (m_State) - cPacketizer Pkt(*this, 0x4e); // Entity Properties packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - WriteEntityProperties(Pkt, a_Entity); + // Unknown packet type, report to the ClientHandle: + m_Client->PacketUnknown(a_PacketType); + return false; } -void cProtocol_1_12_1::SendEntityEffect(const cEntity & a_Entity, int a_EffectID, int a_Amplifier, short a_Duration) +cProtocol_1_12_1::cProtocol_1_12_1(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State) : + super(a_Client, a_ServerAddress, a_ServerPort, a_State) { - ASSERT(m_State == 3); // In game mode? - - cPacketizer Pkt(*this, 0x4f); // Entity Effect packet - Pkt.WriteVarInt32(a_Entity.GetUniqueID()); - Pkt.WriteBEUInt8(static_cast(a_EffectID)); - Pkt.WriteBEUInt8(static_cast(a_Amplifier)); - Pkt.WriteVarInt32(static_cast(a_Duration)); - Pkt.WriteBool(false); // Hide particles } -void cProtocol_1_12_1::SendPlayerMaxSpeed(void) +void cProtocol_1_12_1::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) { - ASSERT(m_State == 3); // In game mode? + cServer * Server = cRoot::Get()->GetServer(); + AString ServerDescription = Server->GetDescription(); + auto NumPlayers = static_cast(Server->GetNumPlayers()); + auto MaxPlayers = static_cast(Server->GetMaxPlayers()); + AString Favicon = Server->GetFaviconData(); + cRoot::Get()->GetPluginManager()->CallHookServerPing(*m_Client, ServerDescription, NumPlayers, MaxPlayers, Favicon); - cPacketizer Pkt(*this, 0x4e); // Entity Properties - cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteVarInt32(Player->GetUniqueID()); - Pkt.WriteBEInt32(1); // Count - Pkt.WriteString("generic.movementSpeed"); - // The default game speed is 0.1, multiply that value by the relative speed: - Pkt.WriteBEDouble(0.1 * Player->GetNormalMaxSpeed()); - if (Player->IsSprinting()) - { - Pkt.WriteVarInt32(1); // Modifier count - Pkt.WriteBEUInt64(0x662a6b8dda3e4c1c); - Pkt.WriteBEUInt64(0x881396ea6097278d); // UUID of the modifier - Pkt.WriteBEDouble(Player->GetSprintingMaxSpeed() - Player->GetNormalMaxSpeed()); - Pkt.WriteBEUInt8(2); - } - else + // Version: + Json::Value Version; + Version["name"] = "Cuberite 1.12.1"; + Version["protocol"] = cProtocolRecognizer::PROTO_VERSION_1_12_1; + + // Players: + Json::Value Players; + Players["online"] = NumPlayers; + Players["max"] = MaxPlayers; + // TODO: Add "sample" + + // Description: + Json::Value Description; + Description["text"] = ServerDescription.c_str(); + + // Create the response: + Json::Value ResponseValue; + ResponseValue["version"] = Version; + ResponseValue["players"] = Players; + ResponseValue["description"] = Description; + if (!Favicon.empty()) { - Pkt.WriteVarInt32(0); // Modifier count + ResponseValue["favicon"] = Printf("data:image/png;base64,%s", Favicon.c_str()); } + + // Serialize the response into a packet: + Json::FastWriter Writer; + cPacketizer Pkt(*this, 0x00); // Response packet + Pkt.WriteString(Writer.write(ResponseValue)); } -- cgit v1.2.3