From b1d4b3bb96629b3624e8328d7b1a0bce5333bb7d Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 21 Mar 2015 13:00:20 +0100 Subject: Unified cByteBuffer types. cByteBuffer now reads and writes any of the [U]Int types. --- src/Protocol/ChunkDataSerializer.cpp | 12 +- src/Protocol/Protocol17x.cpp | 220 ++++++++++++++++++++--------------- src/Protocol/Protocol17x.h | 15 ++- src/Protocol/Protocol18x.cpp | 138 +++++++++++++--------- src/Protocol/Protocol18x.h | 19 +-- src/Protocol/ProtocolRecognizer.cpp | 22 ++-- 6 files changed, 241 insertions(+), 185 deletions(-) (limited to 'src/Protocol') diff --git a/src/Protocol/ChunkDataSerializer.cpp b/src/Protocol/ChunkDataSerializer.cpp index 5d080656d..e850ceaec 100644 --- a/src/Protocol/ChunkDataSerializer.cpp +++ b/src/Protocol/ChunkDataSerializer.cpp @@ -188,10 +188,10 @@ void cChunkDataSerializer::Serialize47(AString & a_Data, int a_ChunkX, int a_Chu // Create the packet: cByteBuffer Packet(512 KiB); Packet.WriteVarInt(0x21); // Packet id (Chunk Data packet) - Packet.WriteBEInt(a_ChunkX); - Packet.WriteBEInt(a_ChunkZ); - Packet.WriteBool(true); // "Ground-up continuous", or rather, "biome data present" flag - Packet.WriteBEUShort(0xffff); // We're aways sending the full chunk with no additional data, so the bitmap is 0xffff + Packet.WriteBEInt32(a_ChunkX); + Packet.WriteBEInt32(a_ChunkZ); + Packet.WriteBool(true); // "Ground-up continuous", or rather, "biome data present" flag + Packet.WriteBEUInt16(0xffff); // We're aways sending the full chunk with no additional data, so the bitmap is 0xffff // Write the chunk size: const int BiomeDataSize = cChunkDef::Width * cChunkDef::Width; @@ -208,8 +208,8 @@ void cChunkDataSerializer::Serialize47(AString & a_Data, int a_ChunkX, int a_Chu { BLOCKTYPE BlockType = m_BlockTypes[Index] & 0xFF; NIBBLETYPE BlockMeta = m_BlockMetas[Index / 2] >> ((Index & 1) * 4) & 0x0f; - Packet.WriteByte((unsigned char)(BlockType << 4) | BlockMeta); - Packet.WriteByte((unsigned char)(BlockType >> 4)); + Packet.WriteBEUInt8(static_cast(BlockType << 4) | BlockMeta); + Packet.WriteBEUInt8(static_cast(BlockType >> 4)); } // Write the rest: diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index 9abe81238..797e32a75 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -50,6 +50,13 @@ Implements the 1.7.x protocol classes: +/** The slot number that the client uses to indicate "outside the window". */ +static const Int16 SLOT_NUM_OUTSIDE = -999; + + + + + #define HANDLE_READ(ByteBuf, Proc, Type, Var) \ Type Var; \ if (!ByteBuf.Proc(Var))\ @@ -1006,8 +1013,8 @@ void cProtocol172::SendExperience (void) cPacketizer Pkt(*this, 0x1f); // Experience Packet cPlayer * Player = m_Client->GetPlayer(); Pkt.WriteFloat(Player->GetXpPercentage()); - Pkt.WriteShort(Player->GetXpLevel()); - Pkt.WriteShort(Player->GetCurrentXp()); + Pkt.WriteShort(static_cast(std::max(Player->GetXpLevel(), std::numeric_limits::max()))); + Pkt.WriteShort(static_cast(std::max(Player->GetCurrentXp(), std::numeric_limits::max()))); } @@ -1198,9 +1205,9 @@ void cProtocol172::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleTyp Pkt.WriteInt(a_VehicleSubType); if (a_VehicleSubType != 0) { - Pkt.WriteShort(static_cast(a_Vehicle.GetSpeedX() * 400)); - Pkt.WriteShort(static_cast(a_Vehicle.GetSpeedY() * 400)); - Pkt.WriteShort(static_cast(a_Vehicle.GetSpeedZ() * 400)); + Pkt.WriteShort(static_cast(a_Vehicle.GetSpeedX() * 400)); + Pkt.WriteShort(static_cast(a_Vehicle.GetSpeedY() * 400)); + Pkt.WriteShort(static_cast(a_Vehicle.GetSpeedZ() * 400)); } } @@ -1756,34 +1763,34 @@ void cProtocol172::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) void cProtocol172::HandlePacketLoginEncryptionResponse(cByteBuffer & a_ByteBuffer) { - short EncKeyLength, EncNonceLength; - if (!a_ByteBuffer.ReadBEShort(EncKeyLength)) + UInt16 EncKeyLength, EncNonceLength; + if (!a_ByteBuffer.ReadBEUInt16(EncKeyLength)) { return; } - if ((EncKeyLength < 0) || (EncKeyLength > MAX_ENC_LEN)) + if (EncKeyLength > MAX_ENC_LEN) { - LOGD("Invalid Encryption Key length: %d. Kicking client.", EncKeyLength); + LOGD("Invalid Encryption Key length: %u (0x%04x). Kicking client.", EncKeyLength, EncKeyLength); m_Client->Kick("Invalid EncKeyLength"); return; } AString EncKey; - if (!a_ByteBuffer.ReadString(EncKey, static_cast(EncKeyLength))) + if (!a_ByteBuffer.ReadString(EncKey, EncKeyLength)) { return; } - if (!a_ByteBuffer.ReadBEShort(EncNonceLength)) + if (!a_ByteBuffer.ReadBEUInt16(EncNonceLength)) { return; } - if ((EncNonceLength < 0) || (EncNonceLength > MAX_ENC_LEN)) + if (EncNonceLength > MAX_ENC_LEN) { - LOGD("Invalid Encryption Nonce length: %d. Kicking client.", EncNonceLength); + LOGD("Invalid Encryption Nonce length: %u (0x%04x). Kicking client.", EncNonceLength, EncNonceLength); m_Client->Kick("Invalid EncNonceLength"); return; } AString EncNonce; - if (!a_ByteBuffer.ReadString(EncNonce, static_cast(EncNonceLength))) + if (!a_ByteBuffer.ReadString(EncNonce, EncNonceLength)) { return; } @@ -1791,7 +1798,10 @@ void cProtocol172::HandlePacketLoginEncryptionResponse(cByteBuffer & a_ByteBuffe // Decrypt EncNonce using privkey cRsaPrivateKey & rsaDecryptor = cRoot::Get()->GetServer()->GetPrivateKey(); Int32 DecryptedNonce[MAX_ENC_LEN / sizeof(Int32)]; - int res = rsaDecryptor.Decrypt((const Byte *)EncNonce.data(), EncNonce.size(), (Byte *)DecryptedNonce, sizeof(DecryptedNonce)); + int res = rsaDecryptor.Decrypt( + reinterpret_cast(EncNonce.data()), EncNonce.size(), + reinterpret_cast(DecryptedNonce), sizeof(DecryptedNonce) + ); if (res != 4) { LOGD("Bad nonce length: got %d, exp %d", res, 4); @@ -1862,8 +1872,8 @@ void cProtocol172::HandlePacketLoginStart(cByteBuffer & a_ByteBuffer) void cProtocol172::HandlePacketAnimation(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, EntityID); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, Animation); + HANDLE_READ(a_ByteBuffer, ReadBEUInt32, UInt32, EntityID); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Animation); m_Client->HandleAnimation(Animation); } @@ -1873,12 +1883,12 @@ void cProtocol172::HandlePacketAnimation(cByteBuffer & a_ByteBuffer) void cProtocol172::HandlePacketBlockDig(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadChar, char, Status); - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, BlockX); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, BlockY); - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, BlockZ); - HANDLE_READ(a_ByteBuffer, ReadChar, char, Face); - m_Client->HandleLeftClick(BlockX, BlockY, BlockZ, static_cast(Face), Status); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Status); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, BlockX); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, BlockY); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, BlockZ); + HANDLE_READ(a_ByteBuffer, ReadBEInt8, Int8, Face); + m_Client->HandleLeftClick(BlockX, BlockY, BlockZ, FaceIntToBlockFace(Face), Status); } @@ -1887,17 +1897,17 @@ void cProtocol172::HandlePacketBlockDig(cByteBuffer & a_ByteBuffer) void cProtocol172::HandlePacketBlockPlace(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, BlockX); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, BlockY); - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, BlockZ); - HANDLE_READ(a_ByteBuffer, ReadChar, char, Face); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, BlockX); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, BlockY); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, BlockZ); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Face); cItem Item; ReadItem(a_ByteBuffer, Item); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, CursorX); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, CursorY); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, CursorZ); - m_Client->HandleRightClick(BlockX, BlockY, BlockZ, static_cast(Face), CursorX, CursorY, CursorZ, m_Client->GetPlayer()->GetEquippedItem()); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, CursorX); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, CursorY); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, CursorZ); + m_Client->HandleRightClick(BlockX, BlockY, BlockZ, FaceIntToBlockFace(Face), CursorX, CursorY, CursorZ, m_Client->GetPlayer()->GetEquippedItem()); } @@ -1917,11 +1927,11 @@ void cProtocol172::HandlePacketChatMessage(cByteBuffer & a_ByteBuffer) void cProtocol172::HandlePacketClientSettings(cByteBuffer & a_ByteBuffer) { HANDLE_READ(a_ByteBuffer, ReadVarUTF8String, AString, Locale); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, ViewDistance); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, ChatFlags); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, ChatColors); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, Difficulty); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, ShowCape); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, ViewDistance); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, ChatFlags); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, ChatColors); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Difficulty); + HANDLE_READ(a_ByteBuffer, ReadBool, bool, ShowCape); m_Client->SetLocale(Locale); m_Client->SetViewDistance(ViewDistance); @@ -1934,7 +1944,7 @@ void cProtocol172::HandlePacketClientSettings(cByteBuffer & a_ByteBuffer) void cProtocol172::HandlePacketClientStatus(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, ActionID); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, ActionID); switch (ActionID) { case 0: @@ -1966,13 +1976,13 @@ void cProtocol172::HandlePacketClientStatus(cByteBuffer & a_ByteBuffer) void cProtocol172::HandlePacketCreativeInventoryAction(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadBEShort, short, SlotNum); + HANDLE_READ(a_ByteBuffer, ReadBEInt16, Int16, SlotNum); cItem Item; if (!ReadItem(a_ByteBuffer, Item)) { return; } - m_Client->HandleCreativeInventory(SlotNum, Item); + m_Client->HandleCreativeInventory(SlotNum, Item, (SlotNum < 0) ? caLeftClick : caLeftClickOutside); } @@ -1981,9 +1991,9 @@ void cProtocol172::HandlePacketCreativeInventoryAction(cByteBuffer & a_ByteBuffe void cProtocol172::HandlePacketEntityAction(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, PlayerID); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, Action); - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, JumpBoost); + HANDLE_READ(a_ByteBuffer, ReadBEUInt32, UInt32, PlayerID); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Action); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, JumpBoost); switch (Action) { @@ -2001,7 +2011,7 @@ void cProtocol172::HandlePacketEntityAction(cByteBuffer & a_ByteBuffer) void cProtocol172::HandlePacketKeepAlive(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, KeepAliveID); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, KeepAliveID); m_Client->HandleKeepAlive(KeepAliveID); } @@ -2021,10 +2031,11 @@ void cProtocol172::HandlePacketPlayer(cByteBuffer & a_ByteBuffer) void cProtocol172::HandlePacketPlayerAbilities(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, Flags); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Flags); HANDLE_READ(a_ByteBuffer, ReadBEFloat, float, FlyingSpeed); HANDLE_READ(a_ByteBuffer, ReadBEFloat, float, WalkingSpeed); + // Convert flags bitfield into individual bool flags: bool IsFlying = false, CanFly = false; if ((Flags & 2) != 0) { @@ -2087,11 +2098,11 @@ void cProtocol172::HandlePacketPlayerPosLook(cByteBuffer & a_ByteBuffer) void cProtocol172::HandlePacketPluginMessage(cByteBuffer & a_ByteBuffer) { HANDLE_READ(a_ByteBuffer, ReadVarUTF8String, AString, Channel); - HANDLE_READ(a_ByteBuffer, ReadBEShort, short, Length); - if (Length + 1 != (int)a_ByteBuffer.GetReadableSpace()) + HANDLE_READ(a_ByteBuffer, ReadBEUInt16, UInt16, Length); + if (Length != a_ByteBuffer.GetReadableSpace() - 1) { - LOGD("Invalid plugin message packet, payload length doesn't match packet length (exp %d, got %d)", - static_cast(a_ByteBuffer.GetReadableSpace()) - 1, Length + LOGD("Invalid plugin message packet, payload length doesn't match packet length (exp %u, got %u)", + a_ByteBuffer.GetReadableSpace() - 1, Length ); return; } @@ -2105,7 +2116,7 @@ void cProtocol172::HandlePacketPluginMessage(cByteBuffer & a_ByteBuffer) // Read the plugin message and relay to clienthandle: AString Data; - if (!a_ByteBuffer.ReadString(Data, static_cast(Length))) + if (!a_ByteBuffer.ReadString(Data, Length)) { return; } @@ -2118,7 +2129,7 @@ void cProtocol172::HandlePacketPluginMessage(cByteBuffer & a_ByteBuffer) void cProtocol172::HandlePacketSlotSelect(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadBEShort, short, SlotNum); + HANDLE_READ(a_ByteBuffer, ReadBEUInt16, UInt16, SlotNum); m_Client->HandleSlotSelected(SlotNum); } @@ -2158,9 +2169,9 @@ void cProtocol172::HandlePacketTabComplete(cByteBuffer & a_ByteBuffer) void cProtocol172::HandlePacketUpdateSign(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, BlockX); - HANDLE_READ(a_ByteBuffer, ReadBEShort, short, BlockY); - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, BlockZ); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, BlockX); + HANDLE_READ(a_ByteBuffer, ReadBEInt16, Int16, BlockY); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, BlockZ); HANDLE_READ(a_ByteBuffer, ReadVarUTF8String, AString, Line1); HANDLE_READ(a_ByteBuffer, ReadVarUTF8String, AString, Line2); HANDLE_READ(a_ByteBuffer, ReadVarUTF8String, AString, Line3); @@ -2174,8 +2185,8 @@ void cProtocol172::HandlePacketUpdateSign(cByteBuffer & a_ByteBuffer) void cProtocol172::HandlePacketUseEntity(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, EntityID); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, MouseButton); + HANDLE_READ(a_ByteBuffer, ReadBEUInt32, UInt32, EntityID); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, MouseButton); m_Client->HandleUseEntity(EntityID, (MouseButton == 1)); } @@ -2185,8 +2196,8 @@ void cProtocol172::HandlePacketUseEntity(cByteBuffer & a_ByteBuffer) void cProtocol172::HandlePacketEnchantItem(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, WindowID); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, Enchantment); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, WindowID); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Enchantment); m_Client->HandleEnchantItem(WindowID, Enchantment); } @@ -2197,11 +2208,11 @@ void cProtocol172::HandlePacketEnchantItem(cByteBuffer & a_ByteBuffer) void cProtocol172::HandlePacketWindowClick(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadChar, char, WindowID); - HANDLE_READ(a_ByteBuffer, ReadBEShort, short, SlotNum); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, Button); - HANDLE_READ(a_ByteBuffer, ReadBEShort, short, TransactionID); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, Mode); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, WindowID); + HANDLE_READ(a_ByteBuffer, ReadBEInt16, Int16, SlotNum); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Button); + HANDLE_READ(a_ByteBuffer, ReadBEUInt16, UInt16, TransactionID); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Mode); cItem Item; ReadItem(a_ByteBuffer, Item); @@ -2209,8 +2220,8 @@ void cProtocol172::HandlePacketWindowClick(cByteBuffer & a_ByteBuffer) eClickAction Action; switch ((Mode << 8) | Button) { - case 0x0000: Action = (SlotNum != -999) ? caLeftClick : caLeftClickOutside; break; - case 0x0001: Action = (SlotNum != -999) ? caRightClick : caRightClickOutside; break; + case 0x0000: Action = (SlotNum != SLOT_NUM_OUTSIDE) ? caLeftClick : caLeftClickOutside; break; + case 0x0001: Action = (SlotNum != SLOT_NUM_OUTSIDE) ? caRightClick : caRightClickOutside; break; case 0x0100: Action = caShiftLeftClick; break; case 0x0101: Action = caShiftRightClick; break; case 0x0200: Action = caNumber1; break; @@ -2223,14 +2234,14 @@ void cProtocol172::HandlePacketWindowClick(cByteBuffer & a_ByteBuffer) case 0x0207: Action = caNumber8; break; case 0x0208: Action = caNumber9; break; case 0x0300: Action = caMiddleClick; break; - case 0x0400: Action = (SlotNum == -999) ? caLeftClickOutsideHoldNothing : caDropKey; break; - case 0x0401: Action = (SlotNum == -999) ? caRightClickOutsideHoldNothing : caCtrlDropKey; break; - case 0x0500: Action = (SlotNum == -999) ? caLeftPaintBegin : caUnknown; break; - case 0x0501: Action = (SlotNum != -999) ? caLeftPaintProgress : caUnknown; break; - case 0x0502: Action = (SlotNum == -999) ? caLeftPaintEnd : caUnknown; break; - case 0x0504: Action = (SlotNum == -999) ? caRightPaintBegin : caUnknown; break; - case 0x0505: Action = (SlotNum != -999) ? caRightPaintProgress : caUnknown; break; - case 0x0506: Action = (SlotNum == -999) ? caRightPaintEnd : caUnknown; break; + case 0x0400: Action = (SlotNum == SLOT_NUM_OUTSIDE) ? caLeftClickOutsideHoldNothing : caDropKey; break; + case 0x0401: Action = (SlotNum == SLOT_NUM_OUTSIDE) ? caRightClickOutsideHoldNothing : caCtrlDropKey; break; + case 0x0500: Action = (SlotNum == SLOT_NUM_OUTSIDE) ? caLeftPaintBegin : caUnknown; break; + case 0x0501: Action = (SlotNum != SLOT_NUM_OUTSIDE) ? caLeftPaintProgress : caUnknown; break; + case 0x0502: Action = (SlotNum == SLOT_NUM_OUTSIDE) ? caLeftPaintEnd : caUnknown; break; + case 0x0504: Action = (SlotNum == SLOT_NUM_OUTSIDE) ? caRightPaintBegin : caUnknown; break; + case 0x0505: Action = (SlotNum != SLOT_NUM_OUTSIDE) ? caRightPaintProgress : caUnknown; break; + case 0x0506: Action = (SlotNum == SLOT_NUM_OUTSIDE) ? caRightPaintEnd : caUnknown; break; case 0x0600: Action = caDblClick; break; default: { @@ -2249,7 +2260,7 @@ void cProtocol172::HandlePacketWindowClick(cByteBuffer & a_ByteBuffer) void cProtocol172::HandlePacketWindowClose(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadChar, char, WindowID); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, WindowID); m_Client->HandleWindowClose(WindowID); } @@ -2257,20 +2268,20 @@ void cProtocol172::HandlePacketWindowClose(cByteBuffer & a_ByteBuffer) -void cProtocol172::HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const AString & a_Channel, short a_PayloadLength) +void cProtocol172::HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const AString & a_Channel, UInt16 a_PayloadLength) { if (a_Channel == "MC|AdvCdm") { size_t BeginningSpace = a_ByteBuffer.GetReadableSpace(); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, Mode); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Mode); switch (Mode) { case 0x00: { // Block-based commandblock update: - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, BlockX); - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, BlockY); - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, BlockZ); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, BlockX); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, BlockY); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, BlockZ); HANDLE_READ(a_ByteBuffer, ReadVarUTF8String, AString, Command); m_Client->HandleCommandBlockBlockChange(BlockX, BlockY, BlockZ, Command); break; @@ -2288,12 +2299,12 @@ void cProtocol172::HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const // Read the remainder of the packet (Vanilla sometimes sends bogus data at the end of the packet; #1692): size_t BytesRead = BeginningSpace - a_ByteBuffer.GetReadableSpace(); - if (BytesRead < static_cast(a_PayloadLength)) + if (BytesRead < a_PayloadLength) { LOGD("Protocol 1.7: Skipping garbage data at the end of a vanilla MC|AdvCdm packet, %u bytes", static_cast(a_PayloadLength - BytesRead) ); - a_ByteBuffer.SkipRead(static_cast(a_PayloadLength) - BytesRead); + a_ByteBuffer.SkipRead(a_PayloadLength - BytesRead); } return; } @@ -2301,7 +2312,7 @@ void cProtocol172::HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const { // Read the client's brand: AString Brand; - if (a_ByteBuffer.ReadString(Brand, static_cast(a_PayloadLength))) + if (a_ByteBuffer.ReadString(Brand, a_PayloadLength)) { m_Client->SetClientBrand(Brand); } @@ -2312,15 +2323,15 @@ void cProtocol172::HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const } else if (a_Channel == "MC|Beacon") { - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, Effect1); - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, Effect2); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, Effect1); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, Effect2); m_Client->HandleBeaconSelection(Effect1, Effect2); return; } else if (a_Channel == "MC|ItemName") { AString ItemName; - if (a_ByteBuffer.ReadString(ItemName, static_cast(a_PayloadLength))) + if (a_ByteBuffer.ReadString(ItemName, a_PayloadLength)) { m_Client->HandleAnvilItemName(ItemName); } @@ -2328,7 +2339,7 @@ void cProtocol172::HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const } else if (a_Channel == "MC|TrSel") { - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, SlotNum); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, SlotNum); m_Client->HandleNPCTrade(SlotNum); return; } @@ -2336,7 +2347,7 @@ void cProtocol172::HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const // Read the payload and send it through to the clienthandle: AString Message; - VERIFY(a_ByteBuffer.ReadString(Message, static_cast(a_PayloadLength))); + VERIFY(a_ByteBuffer.ReadString(Message, a_PayloadLength)); m_Client->HandlePluginMessage(a_Channel, Message); } @@ -2370,7 +2381,7 @@ void cProtocol172::SendData(const char * a_Data, size_t a_Size) bool cProtocol172::ReadItem(cByteBuffer & a_ByteBuffer, cItem & a_Item) { - HANDLE_PACKET_READ(a_ByteBuffer, ReadBEShort, short, ItemType); + HANDLE_PACKET_READ(a_ByteBuffer, ReadBEInt16, Int16, ItemType); if (ItemType == -1) { // The item is empty, no more data follows @@ -2379,8 +2390,8 @@ bool cProtocol172::ReadItem(cByteBuffer & a_ByteBuffer, cItem & a_Item) } a_Item.m_ItemType = ItemType; - HANDLE_PACKET_READ(a_ByteBuffer, ReadChar, char, ItemCount); - HANDLE_PACKET_READ(a_ByteBuffer, ReadBEShort, short, ItemDamage); + HANDLE_PACKET_READ(a_ByteBuffer, ReadBEInt8, Int8, ItemCount); + HANDLE_PACKET_READ(a_ByteBuffer, ReadBEInt16, Int16, ItemDamage); a_Item.m_ItemCount = ItemCount; a_Item.m_ItemDamage = ItemDamage; if (ItemCount <= 0) @@ -2388,15 +2399,10 @@ bool cProtocol172::ReadItem(cByteBuffer & a_ByteBuffer, cItem & a_Item) a_Item.Empty(); } - HANDLE_PACKET_READ(a_ByteBuffer, ReadBEShort, short, MetadataLength); - if (MetadataLength <= 0) - { - return true; - } - // Read the metadata + HANDLE_PACKET_READ(a_ByteBuffer, ReadBEUInt16, UInt16, MetadataLength); AString Metadata; - if (!a_ByteBuffer.ReadString(Metadata, static_cast(MetadataLength))) + if (!a_ByteBuffer.ReadString(Metadata, MetadataLength)) { return false; } @@ -2512,6 +2518,26 @@ void cProtocol172::StartEncryption(const Byte * a_Key) +eBlockFace cProtocol172::FaceIntToBlockFace(Int8 a_BlockFace) +{ + // Normalize the blockface values returned from the protocol + // Anything known gets mapped 1:1, everything else returns BLOCK_FACE_NONE + switch (a_BlockFace) + { + case BLOCK_FACE_XM: return BLOCK_FACE_XM; + case BLOCK_FACE_XP: return BLOCK_FACE_XP; + case BLOCK_FACE_YM: return BLOCK_FACE_YM; + case BLOCK_FACE_YP: return BLOCK_FACE_YP; + case BLOCK_FACE_ZM: return BLOCK_FACE_ZM; + case BLOCK_FACE_ZP: return BLOCK_FACE_ZP; + default: return BLOCK_FACE_NONE; + } +} + + + + + //////////////////////////////////////////////////////////////////////////////// // cProtocol172::cPacketizer: @@ -2520,7 +2546,7 @@ cProtocol172::cPacketizer::~cPacketizer() AString DataToSend; // Send the packet length - UInt32 PacketLen = (UInt32)m_Out.GetUsedSpace(); + UInt32 PacketLen = static_cast(m_Out.GetUsedSpace()); m_Protocol.m_OutPacketLenBuffer.WriteVarInt(PacketLen); m_Protocol.m_OutPacketLenBuffer.ReadAll(DataToSend); diff --git a/src/Protocol/Protocol17x.h b/src/Protocol/Protocol17x.h index f939bfb5e..2ee247330 100644 --- a/src/Protocol/Protocol17x.h +++ b/src/Protocol/Protocol17x.h @@ -161,22 +161,22 @@ protected: void WriteByte(Byte a_Value) { - m_Out.WriteByte(a_Value); + m_Out.WriteBEUInt8(a_Value); } void WriteChar(char a_Value) { - m_Out.WriteChar(a_Value); + m_Out.WriteBEInt8(a_Value); } void WriteShort(short a_Value) { - m_Out.WriteBEShort(a_Value); + m_Out.WriteBEInt16(a_Value); } - void WriteInt(int a_Value) + void WriteInt(Int32 a_Value) { - m_Out.WriteBEInt(a_Value); + m_Out.WriteBEInt32(a_Value); } void WriteInt64(Int64 a_Value) @@ -297,7 +297,7 @@ protected: /** Parses Vanilla plugin messages into specific ClientHandle calls. The message payload is still in the bytebuffer, to be read by this function. */ - void HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const AString & a_Channel, short a_PayloadLength); + void HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const AString & a_Channel, UInt16 a_PayloadLength); /** Sends the data to the client, encrypting them if needed. */ virtual void SendData(const char * a_Data, size_t a_Size) override; @@ -312,6 +312,9 @@ protected: void StartEncryption(const Byte * a_Key); + /** Converts the BlockFace received by the protocol into eBlockFace constants. + If the received value doesn't match any of our eBlockFace constants, BLOCK_FACE_NONE is returned. */ + eBlockFace FaceIntToBlockFace(Int8 a_FaceInt); } ; diff --git a/src/Protocol/Protocol18x.cpp b/src/Protocol/Protocol18x.cpp index 2d1a473d1..a80edbc22 100644 --- a/src/Protocol/Protocol18x.cpp +++ b/src/Protocol/Protocol18x.cpp @@ -49,6 +49,13 @@ Implements the 1.8.x protocol classes: +/** The slot number that the client uses to indicate "outside the window". */ +static const Int16 SLOT_NUM_OUTSIDE = -999; + + + + + #define HANDLE_READ(ByteBuf, Proc, Type, Var) \ Type Var; \ if (!ByteBuf.Proc(Var))\ @@ -2106,7 +2113,7 @@ void cProtocol180::HandlePacketAnimation(cByteBuffer & a_ByteBuffer) void cProtocol180::HandlePacketBlockDig(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, Status); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Status); int BlockX, BlockY, BlockZ; if (!a_ByteBuffer.ReadPosition(BlockX, BlockY, BlockZ)) @@ -2114,8 +2121,8 @@ void cProtocol180::HandlePacketBlockDig(cByteBuffer & a_ByteBuffer) return; } - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, Face); - m_Client->HandleLeftClick(BlockX, BlockY, BlockZ, static_cast(Face), Status); + HANDLE_READ(a_ByteBuffer, ReadBEInt8, Int8, Face); + m_Client->HandleLeftClick(BlockX, BlockY, BlockZ, FaceIntToBlockFace(Face), Status); } @@ -2130,19 +2137,15 @@ void cProtocol180::HandlePacketBlockPlace(cByteBuffer & a_ByteBuffer) return; } - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, Face); - if (Face == 255) - { - Face = 0; - } + HANDLE_READ(a_ByteBuffer, ReadBEInt8, Int8, Face); cItem Item; ReadItem(a_ByteBuffer, Item, 3); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, CursorX); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, CursorY); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, CursorZ); - m_Client->HandleRightClick(BlockX, BlockY, BlockZ, static_cast(Face), CursorX, CursorY, CursorZ, m_Client->GetPlayer()->GetEquippedItem()); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, CursorX); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, CursorY); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, CursorZ); + m_Client->HandleRightClick(BlockX, BlockY, BlockZ, FaceIntToBlockFace(Face), CursorX, CursorY, CursorZ, m_Client->GetPlayer()->GetEquippedItem()); } @@ -2162,10 +2165,10 @@ void cProtocol180::HandlePacketChatMessage(cByteBuffer & a_ByteBuffer) void cProtocol180::HandlePacketClientSettings(cByteBuffer & a_ByteBuffer) { HANDLE_READ(a_ByteBuffer, ReadVarUTF8String, AString, Locale); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, ViewDistance); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, ChatFlags); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, ViewDistance); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, ChatFlags); HANDLE_READ(a_ByteBuffer, ReadBool, bool, ChatColors); - HANDLE_READ(a_ByteBuffer, ReadChar, char, SkinFlags); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, SkinFlags); m_Client->SetLocale(Locale); m_Client->SetViewDistance(ViewDistance); @@ -2178,7 +2181,7 @@ void cProtocol180::HandlePacketClientSettings(cByteBuffer & a_ByteBuffer) void cProtocol180::HandlePacketClientStatus(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadChar, char, ActionID); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, ActionID); switch (ActionID) { case 0: @@ -2210,13 +2213,13 @@ void cProtocol180::HandlePacketClientStatus(cByteBuffer & a_ByteBuffer) void cProtocol180::HandlePacketCreativeInventoryAction(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadBEShort, short, SlotNum); + HANDLE_READ(a_ByteBuffer, ReadBEInt16, Int16, SlotNum); cItem Item; if (!ReadItem(a_ByteBuffer, Item)) { return; } - m_Client->HandleCreativeInventory(SlotNum, Item); + m_Client->HandleCreativeInventory(SlotNum, Item, (SlotNum == SLOT_NUM_OUTSIDE) ? caLeftClickOutside : caLeftClick); } @@ -2225,9 +2228,9 @@ void cProtocol180::HandlePacketCreativeInventoryAction(cByteBuffer & a_ByteBuffe void cProtocol180::HandlePacketEntityAction(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadVarInt, UInt32, PlayerID); - HANDLE_READ(a_ByteBuffer, ReadChar, char, Action); - HANDLE_READ(a_ByteBuffer, ReadVarInt, UInt32, JumpBoost); + HANDLE_READ(a_ByteBuffer, ReadVarInt, UInt32, PlayerID); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Action); + HANDLE_READ(a_ByteBuffer, ReadVarInt, UInt32, JumpBoost); switch (Action) { @@ -2246,7 +2249,7 @@ void cProtocol180::HandlePacketEntityAction(cByteBuffer & a_ByteBuffer) void cProtocol180::HandlePacketKeepAlive(cByteBuffer & a_ByteBuffer) { HANDLE_READ(a_ByteBuffer, ReadVarInt, UInt32, KeepAliveID); - m_Client->HandleKeepAlive((int)KeepAliveID); + m_Client->HandleKeepAlive(static_cast(KeepAliveID)); } @@ -2265,10 +2268,11 @@ void cProtocol180::HandlePacketPlayer(cByteBuffer & a_ByteBuffer) void cProtocol180::HandlePacketPlayerAbilities(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, Flags); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Flags); HANDLE_READ(a_ByteBuffer, ReadBEFloat, float, FlyingSpeed); HANDLE_READ(a_ByteBuffer, ReadBEFloat, float, WalkingSpeed); + // COnvert the bitfield into individual boolean flags: bool IsFlying = false, CanFly = false; if ((Flags & 2) != 0) { @@ -2359,7 +2363,7 @@ void cProtocol180::HandlePacketPluginMessage(cByteBuffer & a_ByteBuffer) void cProtocol180::HandlePacketSlotSelect(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadBEShort, short, SlotNum); + HANDLE_READ(a_ByteBuffer, ReadBEInt16, Int16, SlotNum); m_Client->HandleSlotSelected(SlotNum); } @@ -2371,7 +2375,7 @@ void cProtocol180::HandlePacketSteerVehicle(cByteBuffer & a_ByteBuffer) { HANDLE_READ(a_ByteBuffer, ReadBEFloat, float, Forward); HANDLE_READ(a_ByteBuffer, ReadBEFloat, float, Sideways); - HANDLE_READ(a_ByteBuffer, ReadChar, char, Flags); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Flags); if ((Flags & 0x2) != 0) { @@ -2390,7 +2394,7 @@ void cProtocol180::HandlePacketSteerVehicle(cByteBuffer & a_ByteBuffer) void cProtocol180::HandlePacketTabComplete(cByteBuffer & a_ByteBuffer) { HANDLE_READ(a_ByteBuffer, ReadVarUTF8String, AString, Text); - HANDLE_READ(a_ByteBuffer, ReadBool, bool, HasPosition); + HANDLE_READ(a_ByteBuffer, ReadBool, bool, HasPosition); if (HasPosition) { @@ -2435,12 +2439,12 @@ void cProtocol180::HandlePacketUseEntity(cByteBuffer & a_ByteBuffer) { case 0: { - m_Client->HandleUseEntity((int)EntityID, false); + m_Client->HandleUseEntity(EntityID, false); break; } case 1: { - m_Client->HandleUseEntity((int)EntityID, true); + m_Client->HandleUseEntity(EntityID, true); break; } case 2: @@ -2466,8 +2470,8 @@ void cProtocol180::HandlePacketUseEntity(cByteBuffer & a_ByteBuffer) void cProtocol180::HandlePacketEnchantItem(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, WindowID); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, Enchantment); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, WindowID); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Enchantment); m_Client->HandleEnchantItem(WindowID, Enchantment); } @@ -2478,11 +2482,11 @@ void cProtocol180::HandlePacketEnchantItem(cByteBuffer & a_ByteBuffer) void cProtocol180::HandlePacketWindowClick(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadChar, char, WindowID); - HANDLE_READ(a_ByteBuffer, ReadBEShort, short, SlotNum); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, Button); - HANDLE_READ(a_ByteBuffer, ReadBEShort, short, TransactionID); - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, Mode); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, WindowID); + HANDLE_READ(a_ByteBuffer, ReadBEInt16, Int16, SlotNum); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Button); + HANDLE_READ(a_ByteBuffer, ReadBEUInt16, UInt16, TransactionID); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Mode); cItem Item; ReadItem(a_ByteBuffer, Item); @@ -2490,8 +2494,8 @@ void cProtocol180::HandlePacketWindowClick(cByteBuffer & a_ByteBuffer) eClickAction Action; switch ((Mode << 8) | Button) { - case 0x0000: Action = (SlotNum != -999) ? caLeftClick : caLeftClickOutside; break; - case 0x0001: Action = (SlotNum != -999) ? caRightClick : caRightClickOutside; break; + case 0x0000: Action = (SlotNum != SLOT_NUM_OUTSIDE) ? caLeftClick : caLeftClickOutside; break; + case 0x0001: Action = (SlotNum != SLOT_NUM_OUTSIDE) ? caRightClick : caRightClickOutside; break; case 0x0100: Action = caShiftLeftClick; break; case 0x0101: Action = caShiftRightClick; break; case 0x0200: Action = caNumber1; break; @@ -2504,14 +2508,14 @@ void cProtocol180::HandlePacketWindowClick(cByteBuffer & a_ByteBuffer) case 0x0207: Action = caNumber8; break; case 0x0208: Action = caNumber9; break; case 0x0300: Action = caMiddleClick; break; - case 0x0400: Action = (SlotNum == -999) ? caLeftClickOutsideHoldNothing : caDropKey; break; - case 0x0401: Action = (SlotNum == -999) ? caRightClickOutsideHoldNothing : caCtrlDropKey; break; - case 0x0500: Action = (SlotNum == -999) ? caLeftPaintBegin : caUnknown; break; - case 0x0501: Action = (SlotNum != -999) ? caLeftPaintProgress : caUnknown; break; - case 0x0502: Action = (SlotNum == -999) ? caLeftPaintEnd : caUnknown; break; - case 0x0504: Action = (SlotNum == -999) ? caRightPaintBegin : caUnknown; break; - case 0x0505: Action = (SlotNum != -999) ? caRightPaintProgress : caUnknown; break; - case 0x0506: Action = (SlotNum == -999) ? caRightPaintEnd : caUnknown; break; + case 0x0400: Action = (SlotNum == SLOT_NUM_OUTSIDE) ? caLeftClickOutsideHoldNothing : caDropKey; break; + case 0x0401: Action = (SlotNum == SLOT_NUM_OUTSIDE) ? caRightClickOutsideHoldNothing : caCtrlDropKey; break; + case 0x0500: Action = (SlotNum == SLOT_NUM_OUTSIDE) ? caLeftPaintBegin : caUnknown; break; + case 0x0501: Action = (SlotNum != SLOT_NUM_OUTSIDE) ? caLeftPaintProgress : caUnknown; break; + case 0x0502: Action = (SlotNum == SLOT_NUM_OUTSIDE) ? caLeftPaintEnd : caUnknown; break; + case 0x0504: Action = (SlotNum == SLOT_NUM_OUTSIDE) ? caRightPaintBegin : caUnknown; break; + case 0x0505: Action = (SlotNum != SLOT_NUM_OUTSIDE) ? caRightPaintProgress : caUnknown; break; + case 0x0506: Action = (SlotNum == SLOT_NUM_OUTSIDE) ? caRightPaintEnd : caUnknown; break; case 0x0600: Action = caDblClick; break; default: { @@ -2530,7 +2534,7 @@ void cProtocol180::HandlePacketWindowClick(cByteBuffer & a_ByteBuffer) void cProtocol180::HandlePacketWindowClose(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadChar, char, WindowID); + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, WindowID); m_Client->HandleWindowClose(WindowID); } @@ -2542,14 +2546,14 @@ void cProtocol180::HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const { if (a_Channel == "MC|AdvCdm") { - HANDLE_READ(a_ByteBuffer, ReadByte, Byte, Mode) + HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Mode) switch (Mode) { case 0x00: { - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, BlockX); - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, BlockY); - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, BlockZ); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, BlockX); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, BlockY); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, BlockZ); HANDLE_READ(a_ByteBuffer, ReadVarUTF8String, AString, Command); m_Client->HandleCommandBlockBlockChange(BlockX, BlockY, BlockZ, Command); break; @@ -2557,7 +2561,7 @@ void cProtocol180::HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const default: { - m_Client->SendChat(Printf("Failure setting command block command; unhandled mode %d", Mode), mtFailure); + m_Client->SendChat(Printf("Failure setting command block command; unhandled mode %u (0x%02x)", Mode, Mode), mtFailure); LOG("Unhandled MC|AdvCdm packet mode."); return; } @@ -2574,8 +2578,8 @@ void cProtocol180::HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const } else if (a_Channel == "MC|Beacon") { - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, Effect1); - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, Effect2); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, Effect1); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, Effect2); m_Client->HandleBeaconSelection(Effect1, Effect2); return; } @@ -2587,7 +2591,7 @@ void cProtocol180::HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const } else if (a_Channel == "MC|TrSel") { - HANDLE_READ(a_ByteBuffer, ReadBEInt, int, SlotNum); + HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, SlotNum); m_Client->HandleNPCTrade(SlotNum); return; } @@ -2629,7 +2633,7 @@ void cProtocol180::SendData(const char * a_Data, size_t a_Size) bool cProtocol180::ReadItem(cByteBuffer & a_ByteBuffer, cItem & a_Item, size_t a_KeepRemainingBytes) { - HANDLE_PACKET_READ(a_ByteBuffer, ReadBEShort, short, ItemType); + HANDLE_PACKET_READ(a_ByteBuffer, ReadBEInt16, Int16, ItemType); if (ItemType == -1) { // The item is empty, no more data follows @@ -2638,8 +2642,8 @@ bool cProtocol180::ReadItem(cByteBuffer & a_ByteBuffer, cItem & a_Item, size_t a } a_Item.m_ItemType = ItemType; - HANDLE_PACKET_READ(a_ByteBuffer, ReadChar, char, ItemCount); - HANDLE_PACKET_READ(a_ByteBuffer, ReadBEShort, short, ItemDamage); + HANDLE_PACKET_READ(a_ByteBuffer, ReadBEInt8, Int8, ItemCount); + HANDLE_PACKET_READ(a_ByteBuffer, ReadBEInt16, Int16, ItemDamage); a_Item.m_ItemCount = ItemCount; a_Item.m_ItemDamage = ItemDamage; if (ItemCount <= 0) @@ -2755,6 +2759,26 @@ void cProtocol180::StartEncryption(const Byte * a_Key) +eBlockFace cProtocol180::FaceIntToBlockFace(Int8 a_BlockFace) +{ + // Normalize the blockface values returned from the protocol + // Anything known gets mapped 1:1, everything else returns BLOCK_FACE_NONE + switch (a_BlockFace) + { + case BLOCK_FACE_XM: return BLOCK_FACE_XM; + case BLOCK_FACE_XP: return BLOCK_FACE_XP; + case BLOCK_FACE_YM: return BLOCK_FACE_YM; + case BLOCK_FACE_YP: return BLOCK_FACE_YP; + case BLOCK_FACE_ZM: return BLOCK_FACE_ZM; + case BLOCK_FACE_ZP: return BLOCK_FACE_ZP; + default: return BLOCK_FACE_NONE; + } +} + + + + + //////////////////////////////////////////////////////////////////////////////// // cProtocol180::cPacketizer: diff --git a/src/Protocol/Protocol18x.h b/src/Protocol/Protocol18x.h index 92d9825ef..d7365c44f 100644 --- a/src/Protocol/Protocol18x.h +++ b/src/Protocol/Protocol18x.h @@ -169,24 +169,24 @@ protected: m_Out.WriteBool(a_Value); } - void WriteByte(Byte a_Value) + void WriteByte(UInt8 a_Value) { - m_Out.WriteByte(a_Value); + m_Out.WriteBEUInt8(a_Value); } - void WriteChar(char a_Value) + void WriteChar(Int8 a_Value) { - m_Out.WriteChar(a_Value); + m_Out.WriteBEInt8(a_Value); } - void WriteShort(short a_Value) + void WriteShort(Int16 a_Value) { - m_Out.WriteBEShort(a_Value); + m_Out.WriteBEInt16(a_Value); } - void WriteInt(int a_Value) + void WriteInt(Int32 a_Value) { - m_Out.WriteBEInt(a_Value); + m_Out.WriteBEInt32(a_Value); } void WriteInt64(Int64 a_Value) @@ -332,6 +332,9 @@ protected: void StartEncryption(const Byte * a_Key); + /** Converts the BlockFace received by the protocol into eBlockFace constants. + If the received value doesn't match any of our eBlockFace constants, BLOCK_FACE_NONE is returned. */ + eBlockFace FaceIntToBlockFace(Int8 a_FaceInt); } ; diff --git a/src/Protocol/ProtocolRecognizer.cpp b/src/Protocol/ProtocolRecognizer.cpp index af9e0d1bc..167a309a0 100644 --- a/src/Protocol/ProtocolRecognizer.cpp +++ b/src/Protocol/ProtocolRecognizer.cpp @@ -911,13 +911,13 @@ bool cProtocolRecognizer::TryRecognizeLengthedProtocol(UInt32 a_PacketLengthRema case PROTO_VERSION_1_7_2: { AString ServerAddress; - short ServerPort; + UInt16 ServerPort; UInt32 NextState; if (!m_Buffer.ReadVarUTF8String(ServerAddress)) { break; } - if (!m_Buffer.ReadBEShort(ServerPort)) + if (!m_Buffer.ReadBEUInt16(ServerPort)) { break; } @@ -926,19 +926,19 @@ bool cProtocolRecognizer::TryRecognizeLengthedProtocol(UInt32 a_PacketLengthRema break; } m_Buffer.CommitRead(); - m_Protocol = new cProtocol172(m_Client, ServerAddress, (UInt16)ServerPort, NextState); + m_Protocol = new cProtocol172(m_Client, ServerAddress, ServerPort, NextState); return true; } case PROTO_VERSION_1_7_6: { AString ServerAddress; - short ServerPort; + UInt16 ServerPort; UInt32 NextState; if (!m_Buffer.ReadVarUTF8String(ServerAddress)) { break; } - if (!m_Buffer.ReadBEShort(ServerPort)) + if (!m_Buffer.ReadBEUInt16(ServerPort)) { break; } @@ -947,19 +947,19 @@ bool cProtocolRecognizer::TryRecognizeLengthedProtocol(UInt32 a_PacketLengthRema break; } m_Buffer.CommitRead(); - m_Protocol = new cProtocol176(m_Client, ServerAddress, (UInt16)ServerPort, NextState); + m_Protocol = new cProtocol176(m_Client, ServerAddress, ServerPort, NextState); return true; } case PROTO_VERSION_1_8_0: { AString ServerAddress; - short ServerPort; + UInt16 ServerPort; UInt32 NextState; if (!m_Buffer.ReadVarUTF8String(ServerAddress)) { break; } - if (!m_Buffer.ReadBEShort(ServerPort)) + if (!m_Buffer.ReadBEUInt16(ServerPort)) { break; } @@ -968,12 +968,12 @@ bool cProtocolRecognizer::TryRecognizeLengthedProtocol(UInt32 a_PacketLengthRema break; } m_Buffer.CommitRead(); - m_Protocol = new cProtocol180(m_Client, ServerAddress, (UInt16)ServerPort, NextState); + m_Protocol = new cProtocol180(m_Client, ServerAddress, ServerPort, NextState); return true; } } - LOGINFO("Client \"%s\" uses an unsupported protocol (lengthed, version %u)", - m_Client->GetIPString().c_str(), ProtocolVersion + LOGINFO("Client \"%s\" uses an unsupported protocol (lengthed, version %u (0x%x))", + m_Client->GetIPString().c_str(), ProtocolVersion, ProtocolVersion ); m_Client->Kick("Unsupported protocol version"); return false; -- cgit v1.2.3 From 3923e3e37a60f7d51b9d527840cd1b93cde04578 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 21 Mar 2015 15:40:56 +0100 Subject: Fixed signedness issues in protocols. --- src/Protocol/Protocol17x.cpp | 100 ++++++++++++++++++++++--------------------- src/Protocol/Protocol17x.h | 5 +++ src/Protocol/Protocol18x.cpp | 24 +++++------ src/Protocol/Protocol18x.h | 5 +++ 4 files changed, 74 insertions(+), 60 deletions(-) (limited to 'src/Protocol') diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index 797e32a75..8d33f3918 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -166,8 +166,8 @@ void cProtocol172::SendAttachEntity(const cEntity & a_Entity, const cEntity * a_ ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x1b); // Attach Entity packet - Pkt.WriteInt(a_Entity.GetUniqueID()); - Pkt.WriteInt((a_Vehicle != nullptr) ? a_Vehicle->GetUniqueID() : 0); + Pkt.WriteUInt32(a_Entity.GetUniqueID()); + Pkt.WriteUInt32((a_Vehicle != nullptr) ? a_Vehicle->GetUniqueID() : 0); Pkt.WriteBool(false); } @@ -197,7 +197,7 @@ void cProtocol172::SendBlockBreakAnim(int a_EntityID, int a_BlockX, int a_BlockY ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x25); // Block Break Animation packet - Pkt.WriteVarInt(static_cast(a_EntityID)); + Pkt.WriteVarInt(a_EntityID); Pkt.WriteInt(a_BlockX); Pkt.WriteInt(a_BlockY); Pkt.WriteInt(a_BlockZ); @@ -297,8 +297,8 @@ void cProtocol172::SendCollectEntity(const cEntity & a_Entity, const cPlayer & a ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x0d); // Collect Item packet - Pkt.WriteInt(a_Entity.GetUniqueID()); - Pkt.WriteInt(a_Player.GetUniqueID()); + Pkt.WriteUInt32(a_Entity.GetUniqueID()); + Pkt.WriteUInt32(a_Player.GetUniqueID()); } @@ -311,7 +311,7 @@ void cProtocol172::SendDestroyEntity(const cEntity & a_Entity) cPacketizer Pkt(*this, 0x13); // Destroy Entities packet Pkt.WriteByte(1); - Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteUInt32(a_Entity.GetUniqueID()); } @@ -364,7 +364,7 @@ void cProtocol172::SendEntityEffect(const cEntity & a_Entity, int a_EffectID, in ASSERT((a_Amplifier >= 0) && (a_Amplifier < 256)); cPacketizer Pkt(*this, 0x1D); // Entity Effect packet - Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteUInt32(a_Entity.GetUniqueID()); Pkt.WriteByte(static_cast(a_EffectID)); Pkt.WriteByte(static_cast(a_Amplifier)); Pkt.WriteShort(a_Duration); @@ -379,7 +379,7 @@ void cProtocol172::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x04); // Entity Equipment packet - Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteUInt32(a_Entity.GetUniqueID()); Pkt.WriteShort(a_SlotNum); Pkt.WriteItem(a_Item); } @@ -393,7 +393,7 @@ void cProtocol172::SendEntityHeadLook(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x19); // Entity Head Look packet - Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteUInt32(a_Entity.GetUniqueID()); Pkt.WriteByteAngle(a_Entity.GetHeadYaw()); } @@ -406,7 +406,7 @@ void cProtocol172::SendEntityLook(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x16); // Entity Look packet - Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteUInt32(a_Entity.GetUniqueID()); Pkt.WriteByteAngle(a_Entity.GetYaw()); Pkt.WriteByteAngle(a_Entity.GetPitch()); } @@ -420,7 +420,7 @@ void cProtocol172::SendEntityMetadata(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x1c); // Entity Metadata packet - Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteUInt32(a_Entity.GetUniqueID()); Pkt.WriteEntityMetadata(a_Entity); Pkt.WriteByte(0x7f); // The termination byte } @@ -434,7 +434,7 @@ void cProtocol172::SendEntityProperties(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x20); // Entity Properties packet - Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteUInt32(a_Entity.GetUniqueID()); Pkt.WriteEntityProperties(a_Entity); } @@ -447,7 +447,7 @@ void cProtocol172::SendEntityRelMove(const cEntity & a_Entity, char a_RelX, char ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x15); // Entity Relative Move packet - Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteUInt32(a_Entity.GetUniqueID()); Pkt.WriteChar(a_RelX); Pkt.WriteChar(a_RelY); Pkt.WriteChar(a_RelZ); @@ -462,7 +462,7 @@ void cProtocol172::SendEntityRelMoveLook(const cEntity & a_Entity, char a_RelX, ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x17); // Entity Look And Relative Move packet - Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteUInt32(a_Entity.GetUniqueID()); Pkt.WriteChar(a_RelX); Pkt.WriteChar(a_RelY); Pkt.WriteChar(a_RelZ); @@ -479,7 +479,7 @@ void cProtocol172::SendEntityStatus(const cEntity & a_Entity, char a_Status) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x1a); // Entity Status packet - Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteUInt32(a_Entity.GetUniqueID()); Pkt.WriteChar(a_Status); } @@ -492,11 +492,11 @@ void cProtocol172::SendEntityVelocity(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x12); // Entity Velocity packet - Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteUInt32(a_Entity.GetUniqueID()); // 400 = 8000 / 20 ... Conversion from our speed in m/s to 8000 m/tick - Pkt.WriteShort((short)(a_Entity.GetSpeedX() * 400)); - Pkt.WriteShort((short)(a_Entity.GetSpeedY() * 400)); - Pkt.WriteShort((short)(a_Entity.GetSpeedZ() * 400)); + Pkt.WriteShort(static_cast(a_Entity.GetSpeedX() * 400)); + Pkt.WriteShort(static_cast(a_Entity.GetSpeedY() * 400)); + Pkt.WriteShort(static_cast(a_Entity.GetSpeedZ() * 400)); } @@ -593,7 +593,7 @@ void cProtocol172::SendLogin(const cPlayer & a_Player, const cWorld & a_World) { cServer * Server = cRoot::Get()->GetServer(); cPacketizer Pkt(*this, 0x01); // Join Game packet - Pkt.WriteInt(a_Player.GetUniqueID()); + Pkt.WriteUInt32(a_Player.GetUniqueID()); Pkt.WriteByte(static_cast(a_Player.GetEffectiveGameMode()) | (Server->IsHardcore() ? 0x08 : 0)); // Hardcore flag bit 4 Pkt.WriteChar(static_cast(a_World.GetDimension())); Pkt.WriteByte(2); // TODO: Difficulty (set to Normal) @@ -639,7 +639,7 @@ void cProtocol172::SendPaintingSpawn(const cPainting & a_Painting) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x10); // Spawn Painting packet - Pkt.WriteVarInt(static_cast(a_Painting.GetUniqueID())); + Pkt.WriteVarInt(a_Painting.GetUniqueID()); Pkt.WriteString(a_Painting.GetName().c_str()); Pkt.WriteInt(static_cast(a_Painting.GetPosX())); Pkt.WriteInt(static_cast(a_Painting.GetPosY())); @@ -651,7 +651,7 @@ void cProtocol172::SendPaintingSpawn(const cPainting & a_Painting) -void cProtocol172::SendMapColumn(int a_ID, int a_X, int a_Y, const Byte * a_Colors, unsigned int a_Length, unsigned int m_Scale) +void cProtocol172::SendMapColumn(int a_MapID, int a_X, int a_Y, const Byte * a_Colors, unsigned int a_Length, unsigned int m_Scale) { ASSERT(m_State == 3); // In game mode? ASSERT(a_Length + 3 <= USHRT_MAX); @@ -659,7 +659,7 @@ void cProtocol172::SendMapColumn(int a_ID, int a_X, int a_Y, const Byte * a_Colo ASSERT((a_Y >= 0) && (a_Y < 256)); cPacketizer Pkt(*this, 0x34); - Pkt.WriteVarInt(static_cast(a_ID)); + Pkt.WriteVarInt(static_cast(a_MapID)); Pkt.WriteShort (static_cast(3 + a_Length)); Pkt.WriteByte(0); @@ -673,13 +673,13 @@ void cProtocol172::SendMapColumn(int a_ID, int a_X, int a_Y, const Byte * a_Colo -void cProtocol172::SendMapDecorators(int a_ID, const cMapDecoratorList & a_Decorators, unsigned int m_Scale) +void cProtocol172::SendMapDecorators(int a_MapID, const cMapDecoratorList & a_Decorators, unsigned int m_Scale) { ASSERT(m_State == 3); // In game mode? ASSERT(1 + 3 * a_Decorators.size() < USHRT_MAX); cPacketizer Pkt(*this, 0x34); - Pkt.WriteVarInt(static_cast(a_ID)); + Pkt.WriteVarInt(static_cast(a_MapID)); Pkt.WriteShort (static_cast(1 + (3 * a_Decorators.size()))); Pkt.WriteByte(1); @@ -698,13 +698,13 @@ void cProtocol172::SendMapDecorators(int a_ID, const cMapDecoratorList & a_Decor -void cProtocol172::SendMapInfo(int a_ID, unsigned int a_Scale) +void cProtocol172::SendMapInfo(int a_MapID, unsigned int a_Scale) { ASSERT(m_State == 3); // In game mode? ASSERT(a_Scale < 256); cPacketizer Pkt(*this, 0x34); - Pkt.WriteVarInt(static_cast(a_ID)); + Pkt.WriteVarInt(static_cast(a_MapID)); Pkt.WriteShort (2); Pkt.WriteByte(2); @@ -721,7 +721,7 @@ void cProtocol172::SendPickupSpawn(const cPickup & a_Pickup) { cPacketizer Pkt(*this, 0x0e); // Spawn Object packet - Pkt.WriteVarInt(static_cast(a_Pickup.GetUniqueID())); + Pkt.WriteVarInt(a_Pickup.GetUniqueID()); Pkt.WriteByte(2); // Type = Pickup Pkt.WriteFPInt(a_Pickup.GetPosX()); Pkt.WriteFPInt(a_Pickup.GetPosY()); @@ -732,7 +732,7 @@ void cProtocol172::SendPickupSpawn(const cPickup & a_Pickup) } { cPacketizer Pkt(*this, 0x1c); // Entity Metadata packet - Pkt.WriteInt(a_Pickup.GetUniqueID()); + Pkt.WriteUInt32(a_Pickup.GetUniqueID()); Pkt.WriteByte((0x05 << 5) | 10); // Slot type + index 10 Pkt.WriteItem(a_Pickup.GetItem()); Pkt.WriteByte(0x7f); // End of metadata @@ -777,7 +777,7 @@ void cProtocol172::SendEntityAnimation(const cEntity & a_Entity, char a_Animatio ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x0b); // Animation packet - Pkt.WriteVarInt(static_cast(a_Entity.GetUniqueID())); + Pkt.WriteVarInt(a_Entity.GetUniqueID()); Pkt.WriteChar(a_Animation); } @@ -870,7 +870,7 @@ void cProtocol172::SendPlayerMaxSpeed(void) cPacketizer Pkt(*this, 0x20); // Entity Properties cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteInt(Player->GetUniqueID()); + Pkt.WriteUInt32(Player->GetUniqueID()); Pkt.WriteInt(1); // Count Pkt.WriteString("generic.movementSpeed"); // The default game speed is 0.1, multiply that value by the relative speed: @@ -931,7 +931,7 @@ void cProtocol172::SendPlayerSpawn(const cPlayer & a_Player) // Called to spawn another player for the client cPacketizer Pkt(*this, 0x0c); // Spawn Player packet - Pkt.WriteVarInt((UInt32) a_Player.GetUniqueID()); + Pkt.WriteVarInt(a_Player.GetUniqueID()); Pkt.WriteString(cMojangAPI::MakeUUIDDashed(a_Player.GetClientHandle()->GetUUID())); if (a_Player.HasCustomName()) { @@ -977,7 +977,7 @@ void cProtocol172::SendRemoveEntityEffect(const cEntity & a_Entity, int a_Effect ASSERT((a_EffectID >= 0) && (a_EffectID < 256)); cPacketizer Pkt(*this, 0x1e); - Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteUInt32(a_Entity.GetUniqueID()); Pkt.WriteByte(static_cast(a_EffectID)); } @@ -1027,7 +1027,7 @@ void cProtocol172::SendExperienceOrb(const cExpOrb & a_ExpOrb) ASSERT((a_ExpOrb.GetReward() >= 0) && (a_ExpOrb.GetReward() < SHRT_MAX)); cPacketizer Pkt(*this, 0x11); - Pkt.WriteVarInt(static_cast(a_ExpOrb.GetUniqueID())); + Pkt.WriteVarInt(a_ExpOrb.GetUniqueID()); Pkt.WriteFPInt(a_ExpOrb.GetPosX()); Pkt.WriteFPInt(a_ExpOrb.GetPosY()); Pkt.WriteFPInt(a_ExpOrb.GetPosZ()); @@ -1124,7 +1124,7 @@ void cProtocol172::SendSpawnFallingBlock(const cFallingBlock & a_FallingBlock) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x0e); // Spawn Object packet - Pkt.WriteVarInt(static_cast(a_FallingBlock.GetUniqueID())); + Pkt.WriteVarInt(a_FallingBlock.GetUniqueID()); Pkt.WriteByte(70); // Falling block Pkt.WriteFPInt(a_FallingBlock.GetPosX()); Pkt.WriteFPInt(a_FallingBlock.GetPosY()); @@ -1146,7 +1146,7 @@ void cProtocol172::SendSpawnMob(const cMonster & a_Mob) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x0f); // Spawn Mob packet - Pkt.WriteVarInt(static_cast(a_Mob.GetUniqueID())); + Pkt.WriteVarInt(a_Mob.GetUniqueID()); Pkt.WriteByte((Byte)a_Mob.GetMobType()); Pkt.WriteFPInt(a_Mob.GetPosX()); Pkt.WriteFPInt(a_Mob.GetPosY()); @@ -1170,7 +1170,7 @@ void cProtocol172::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0xe); // Spawn Object packet - Pkt.WriteVarInt(static_cast(a_Entity.GetUniqueID())); + Pkt.WriteVarInt(a_Entity.GetUniqueID()); Pkt.WriteChar(a_ObjectType); Pkt.WriteFPInt(a_Entity.GetPosX()); Pkt.WriteFPInt(a_Entity.GetPosY()); @@ -1195,7 +1195,7 @@ void cProtocol172::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleTyp ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0xe); // Spawn Object packet - Pkt.WriteVarInt(static_cast(a_Vehicle.GetUniqueID())); + Pkt.WriteVarInt(a_Vehicle.GetUniqueID()); Pkt.WriteChar(a_VehicleType); Pkt.WriteFPInt(a_Vehicle.GetPosX()); Pkt.WriteFPInt(a_Vehicle.GetPosY()); @@ -1258,7 +1258,7 @@ void cProtocol172::SendTeleportEntity(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x18); - Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteUInt32(a_Entity.GetUniqueID()); Pkt.WriteFPInt(a_Entity.GetPosX()); Pkt.WriteFPInt(a_Entity.GetPosY()); Pkt.WriteFPInt(a_Entity.GetPosZ()); @@ -1371,11 +1371,12 @@ void cProtocol172::SendUpdateSign(int a_BlockX, int a_BlockY, int a_BlockZ, cons void cProtocol172::SendUseBed(const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ) { ASSERT(m_State == 3); // In game mode? + ASSERT((a_BlockY >= 0) && (a_BlockY < cChunkDef::Height)); cPacketizer Pkt(*this, 0x0a); - Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteUInt32(a_Entity.GetUniqueID()); Pkt.WriteInt(a_BlockX); - Pkt.WriteByte((Byte)a_BlockY); + Pkt.WriteByte(static_cast(a_BlockY)); Pkt.WriteInt(a_BlockZ); } @@ -1563,7 +1564,7 @@ void cProtocol172::AddReceivedData(const char * a_Data, size_t a_Size) bb.ReadAll(Packet); Packet.resize(Packet.size() - 1); // Drop the final NUL pushed there for over-read detection AString Out; - CreateHexDump(Out, Packet.data(), (int)Packet.size(), 24); + CreateHexDump(Out, Packet.data(), Packet.size(), 24); LOGD("Packet contents:\n%s", Out.c_str()); #endif // _DEBUG @@ -1797,7 +1798,7 @@ void cProtocol172::HandlePacketLoginEncryptionResponse(cByteBuffer & a_ByteBuffe // Decrypt EncNonce using privkey cRsaPrivateKey & rsaDecryptor = cRoot::Get()->GetServer()->GetPrivateKey(); - Int32 DecryptedNonce[MAX_ENC_LEN / sizeof(Int32)]; + UInt32 DecryptedNonce[MAX_ENC_LEN / sizeof(UInt32)]; int res = rsaDecryptor.Decrypt( reinterpret_cast(EncNonce.data()), EncNonce.size(), reinterpret_cast(DecryptedNonce), sizeof(DecryptedNonce) @@ -1808,7 +1809,7 @@ void cProtocol172::HandlePacketLoginEncryptionResponse(cByteBuffer & a_ByteBuffe m_Client->Kick("Hacked client"); return; } - if (ntohl(DecryptedNonce[0]) != (unsigned)(uintptr_t)this) + if (ntohl(DecryptedNonce[0]) != static_cast(reinterpret_cast(this))) { LOGD("Bad nonce value"); m_Client->Kick("Hacked client"); @@ -1817,7 +1818,10 @@ void cProtocol172::HandlePacketLoginEncryptionResponse(cByteBuffer & a_ByteBuffe // Decrypt the symmetric encryption key using privkey: Byte DecryptedKey[MAX_ENC_LEN]; - res = rsaDecryptor.Decrypt((const Byte *)EncKey.data(), EncKey.size(), DecryptedKey, sizeof(DecryptedKey)); + res = rsaDecryptor.Decrypt( + reinterpret_cast(EncKey.data()), EncKey.size(), + DecryptedKey, sizeof(DecryptedKey) + ); if (res != 16) { LOGD("Bad key length"); @@ -1900,7 +1904,7 @@ void cProtocol172::HandlePacketBlockPlace(cByteBuffer & a_ByteBuffer) HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, BlockX); HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, BlockY); HANDLE_READ(a_ByteBuffer, ReadBEInt32, Int32, BlockZ); - HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Face); + HANDLE_READ(a_ByteBuffer, ReadBEInt8, Int8, Face); cItem Item; ReadItem(a_ByteBuffer, Item); @@ -2102,7 +2106,7 @@ void cProtocol172::HandlePacketPluginMessage(cByteBuffer & a_ByteBuffer) if (Length != a_ByteBuffer.GetReadableSpace() - 1) { LOGD("Invalid plugin message packet, payload length doesn't match packet length (exp %u, got %u)", - a_ByteBuffer.GetReadableSpace() - 1, Length + static_cast(a_ByteBuffer.GetReadableSpace() - 1), Length ); return; } @@ -3130,7 +3134,7 @@ void cProtocol176::SendPlayerSpawn(const cPlayer & a_Player) { // Called to spawn another player for the client cPacketizer Pkt(*this, 0x0c); // Spawn Player packet - Pkt.WriteVarInt(static_cast(a_Player.GetUniqueID())); + Pkt.WriteVarInt(a_Player.GetUniqueID()); Pkt.WriteString(cMojangAPI::MakeUUIDDashed(a_Player.GetClientHandle()->GetUUID())); if (a_Player.HasCustomName()) { diff --git a/src/Protocol/Protocol17x.h b/src/Protocol/Protocol17x.h index 2ee247330..8ff4bd158 100644 --- a/src/Protocol/Protocol17x.h +++ b/src/Protocol/Protocol17x.h @@ -179,6 +179,11 @@ protected: m_Out.WriteBEInt32(a_Value); } + void WriteUInt32(UInt32 a_Value) + { + m_Out.WriteBEUInt32(a_Value); + } + void WriteInt64(Int64 a_Value) { m_Out.WriteBEInt64(a_Value); diff --git a/src/Protocol/Protocol18x.cpp b/src/Protocol/Protocol18x.cpp index a80edbc22..5c6837577 100644 --- a/src/Protocol/Protocol18x.cpp +++ b/src/Protocol/Protocol18x.cpp @@ -162,8 +162,8 @@ void cProtocol180::SendAttachEntity(const cEntity & a_Entity, const cEntity * a_ ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x1b); // Attach Entity packet - Pkt.WriteInt(a_Entity.GetUniqueID()); - Pkt.WriteInt((a_Vehicle != nullptr) ? a_Vehicle->GetUniqueID() : 0); + Pkt.WriteUInt32(a_Entity.GetUniqueID()); + Pkt.WriteUInt32((a_Vehicle != nullptr) ? a_Vehicle->GetUniqueID() : 0); Pkt.WriteBool(false); } @@ -363,7 +363,7 @@ void cProtocol180::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x04); // Entity Equipment packet - Pkt.WriteVarInt((UInt32)a_Entity.GetUniqueID()); + Pkt.WriteVarInt(a_Entity.GetUniqueID()); Pkt.WriteShort(a_SlotNum); Pkt.WriteItem(a_Item); } @@ -377,7 +377,7 @@ void cProtocol180::SendEntityHeadLook(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x19); // Entity Head Look packet - Pkt.WriteVarInt((UInt32)a_Entity.GetUniqueID()); + Pkt.WriteVarInt(a_Entity.GetUniqueID()); Pkt.WriteByteAngle(a_Entity.GetHeadYaw()); } @@ -433,9 +433,9 @@ void cProtocol180::SendEntityRelMove(const cEntity & a_Entity, char a_RelX, char cPacketizer Pkt(*this, 0x15); // Entity Relative Move packet Pkt.WriteVarInt(a_Entity.GetUniqueID()); - Pkt.WriteByte(a_RelX); - Pkt.WriteByte(a_RelY); - Pkt.WriteByte(a_RelZ); + Pkt.WriteChar(a_RelX); + Pkt.WriteChar(a_RelY); + Pkt.WriteChar(a_RelZ); Pkt.WriteBool(a_Entity.IsOnGround()); } @@ -449,9 +449,9 @@ void cProtocol180::SendEntityRelMoveLook(const cEntity & a_Entity, char a_RelX, cPacketizer Pkt(*this, 0x17); // Entity Look And Relative Move packet Pkt.WriteVarInt(a_Entity.GetUniqueID()); - Pkt.WriteByte(a_RelX); - Pkt.WriteByte(a_RelY); - Pkt.WriteByte(a_RelZ); + Pkt.WriteChar(a_RelX); + Pkt.WriteChar(a_RelY); + Pkt.WriteChar(a_RelZ); Pkt.WriteByteAngle(a_Entity.GetYaw()); Pkt.WriteByteAngle(a_Entity.GetPitch()); Pkt.WriteBool(a_Entity.IsOnGround()); @@ -466,7 +466,7 @@ void cProtocol180::SendEntityStatus(const cEntity & a_Entity, char a_Status) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x1a); // Entity Status packet - Pkt.WriteInt(a_Entity.GetUniqueID()); + Pkt.WriteUInt32(a_Entity.GetUniqueID()); Pkt.WriteChar(a_Status); } @@ -580,7 +580,7 @@ void cProtocol180::SendLogin(const cPlayer & a_Player, const cWorld & a_World) { cServer * Server = cRoot::Get()->GetServer(); cPacketizer Pkt(*this, 0x01); // Join Game packet - Pkt.WriteInt(a_Player.GetUniqueID()); + Pkt.WriteUInt32(a_Player.GetUniqueID()); Pkt.WriteByte((Byte)a_Player.GetEffectiveGameMode() | (Server->IsHardcore() ? 0x08 : 0)); // Hardcore flag bit 4 Pkt.WriteChar((char)a_World.GetDimension()); Pkt.WriteByte(2); // TODO: Difficulty (set to Normal) diff --git a/src/Protocol/Protocol18x.h b/src/Protocol/Protocol18x.h index d7365c44f..73a0478e9 100644 --- a/src/Protocol/Protocol18x.h +++ b/src/Protocol/Protocol18x.h @@ -189,6 +189,11 @@ protected: m_Out.WriteBEInt32(a_Value); } + void WriteUInt32(UInt32 a_Value) + { + m_Out.WriteBEUInt32(a_Value); + } + void WriteInt64(Int64 a_Value) { m_Out.WriteBEInt64(a_Value); -- cgit v1.2.3 From c3c29577a52a2e820cd389b88d52119e8136a7d8 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 21 Mar 2015 16:11:57 +0100 Subject: Fixed signedness warnings in Protocol. --- src/Protocol/Protocol.h | 2 +- src/Protocol/Protocol17x.cpp | 4 ++-- src/Protocol/Protocol17x.h | 2 +- src/Protocol/Protocol18x.cpp | 2 +- src/Protocol/Protocol18x.h | 2 +- src/Protocol/ProtocolRecognizer.cpp | 4 ++-- src/Protocol/ProtocolRecognizer.h | 2 +- 7 files changed, 9 insertions(+), 9 deletions(-) (limited to 'src/Protocol') diff --git a/src/Protocol/Protocol.h b/src/Protocol/Protocol.h index 02a8a52f6..652b5aa7e 100644 --- a/src/Protocol/Protocol.h +++ b/src/Protocol/Protocol.h @@ -59,7 +59,7 @@ public: // Sending stuff to clients (alphabetically sorted): virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity * a_Vehicle) = 0; virtual void SendBlockAction (int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType) = 0; - virtual void SendBlockBreakAnim (int a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) = 0; + virtual void SendBlockBreakAnim (UInt32 a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) = 0; virtual void SendBlockChange (int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) = 0; virtual void SendBlockChanges (int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) = 0; virtual void SendChat (const AString & a_Message) = 0; diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index 8d33f3918..431072bdb 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -192,7 +192,7 @@ void cProtocol172::SendBlockAction(int a_BlockX, int a_BlockY, int a_BlockZ, cha -void cProtocol172::SendBlockBreakAnim(int a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) +void cProtocol172::SendBlockBreakAnim(UInt32 a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) { ASSERT(m_State == 3); // In game mode? @@ -2133,7 +2133,7 @@ void cProtocol172::HandlePacketPluginMessage(cByteBuffer & a_ByteBuffer) void cProtocol172::HandlePacketSlotSelect(cByteBuffer & a_ByteBuffer) { - HANDLE_READ(a_ByteBuffer, ReadBEUInt16, UInt16, SlotNum); + HANDLE_READ(a_ByteBuffer, ReadBEInt16, Int16, SlotNum); m_Client->HandleSlotSelected(SlotNum); } diff --git a/src/Protocol/Protocol17x.h b/src/Protocol/Protocol17x.h index 8ff4bd158..543ae6ee3 100644 --- a/src/Protocol/Protocol17x.h +++ b/src/Protocol/Protocol17x.h @@ -63,7 +63,7 @@ public: /** Sending stuff to clients (alphabetically sorted): */ virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity * a_Vehicle) override; virtual void SendBlockAction (int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType) override; - virtual void SendBlockBreakAnim (int a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) override; + virtual void SendBlockBreakAnim (UInt32 a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) override; virtual void SendBlockChange (int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; virtual void SendBlockChanges (int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) override; virtual void SendChat (const AString & a_Message) override; diff --git a/src/Protocol/Protocol18x.cpp b/src/Protocol/Protocol18x.cpp index 5c6837577..e35ec249b 100644 --- a/src/Protocol/Protocol18x.cpp +++ b/src/Protocol/Protocol18x.cpp @@ -186,7 +186,7 @@ void cProtocol180::SendBlockAction(int a_BlockX, int a_BlockY, int a_BlockZ, cha -void cProtocol180::SendBlockBreakAnim(int a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) +void cProtocol180::SendBlockBreakAnim(UInt32 a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) { ASSERT(m_State == 3); // In game mode? diff --git a/src/Protocol/Protocol18x.h b/src/Protocol/Protocol18x.h index 73a0478e9..7ba3a92fe 100644 --- a/src/Protocol/Protocol18x.h +++ b/src/Protocol/Protocol18x.h @@ -62,7 +62,7 @@ public: /** Sending stuff to clients (alphabetically sorted): */ virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity * a_Vehicle) override; virtual void SendBlockAction (int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType) override; - virtual void SendBlockBreakAnim (int a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) override; + virtual void SendBlockBreakAnim (UInt32 a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) override; virtual void SendBlockChange (int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; virtual void SendBlockChanges (int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) override; virtual void SendChat (const AString & a_Message) override; diff --git a/src/Protocol/ProtocolRecognizer.cpp b/src/Protocol/ProtocolRecognizer.cpp index 167a309a0..8d0f8148d 100644 --- a/src/Protocol/ProtocolRecognizer.cpp +++ b/src/Protocol/ProtocolRecognizer.cpp @@ -108,10 +108,10 @@ void cProtocolRecognizer::SendBlockAction(int a_BlockX, int a_BlockY, int a_Bloc -void cProtocolRecognizer::SendBlockBreakAnim(int a_entityID, int a_BlockX, int a_BlockY, int a_BlockZ, char stage) +void cProtocolRecognizer::SendBlockBreakAnim(UInt32 a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) { ASSERT(m_Protocol != nullptr); - m_Protocol->SendBlockBreakAnim(a_entityID, a_BlockX, a_BlockY, a_BlockZ, stage); + m_Protocol->SendBlockBreakAnim(a_EntityID, a_BlockX, a_BlockY, a_BlockZ, a_Stage); } diff --git a/src/Protocol/ProtocolRecognizer.h b/src/Protocol/ProtocolRecognizer.h index dcf653e75..165dc0c0c 100644 --- a/src/Protocol/ProtocolRecognizer.h +++ b/src/Protocol/ProtocolRecognizer.h @@ -50,7 +50,7 @@ public: /// Sending stuff to clients (alphabetically sorted): virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity * a_Vehicle) override; virtual void SendBlockAction (int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType) override; - virtual void SendBlockBreakAnim (int a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) override; + virtual void SendBlockBreakAnim (UInt32 a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) override; virtual void SendBlockChange (int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; virtual void SendBlockChanges (int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) override; virtual void SendChat (const AString & a_Message) override; -- cgit v1.2.3 From c6268483934eb2bfdcb76ae621a0be40f76209f5 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sun, 22 Mar 2015 19:46:08 +0100 Subject: Unified cPacketizer across all protocols. --- src/Protocol/CMakeLists.txt | 10 +- src/Protocol/Packetizer.cpp | 102 +++++++ src/Protocol/Packetizer.h | 149 ++++++++++ src/Protocol/Protocol.h | 26 +- src/Protocol/Protocol17x.cpp | 545 ++++++++++++++++++------------------ src/Protocol/Protocol17x.h | 112 ++------ src/Protocol/Protocol18x.cpp | 532 +++++++++++++++++------------------ src/Protocol/Protocol18x.h | 119 ++------ src/Protocol/ProtocolRecognizer.cpp | 12 + src/Protocol/ProtocolRecognizer.h | 7 +- 10 files changed, 857 insertions(+), 757 deletions(-) create mode 100644 src/Protocol/Packetizer.cpp create mode 100644 src/Protocol/Packetizer.h (limited to 'src/Protocol') diff --git a/src/Protocol/CMakeLists.txt b/src/Protocol/CMakeLists.txt index 7c97e67bc..c3a45ca47 100644 --- a/src/Protocol/CMakeLists.txt +++ b/src/Protocol/CMakeLists.txt @@ -8,19 +8,23 @@ SET (SRCS Authenticator.cpp ChunkDataSerializer.cpp MojangAPI.cpp + Packetizer.cpp Protocol17x.cpp Protocol18x.cpp - ProtocolRecognizer.cpp) + ProtocolRecognizer.cpp +) SET (HDRS Authenticator.h ChunkDataSerializer.h MojangAPI.h + Packetizer.h Protocol.h Protocol17x.h Protocol18x.h - ProtocolRecognizer.h) + ProtocolRecognizer.h +) -if(NOT MSVC) +if (NOT MSVC) add_library(Protocol ${SRCS} ${HDRS}) endif() diff --git a/src/Protocol/Packetizer.cpp b/src/Protocol/Packetizer.cpp new file mode 100644 index 000000000..2b27f7b2f --- /dev/null +++ b/src/Protocol/Packetizer.cpp @@ -0,0 +1,102 @@ + +// Packetizer.cpp + +// Implements the cPacketizer class representing a wrapper for sending a single packet over a protocol. + +#include "Globals.h" +#include "Packetizer.h" + + + + + +/** Converts the hex digit character to its value. */ +static UInt8 HexDigitValue(char a_Character) +{ + switch (a_Character) + { + case '0': return 0; + case '1': return 1; + case '2': return 2; + case '3': return 3; + case '4': return 4; + case '5': return 5; + case '6': return 6; + case '7': return 7; + case '8': return 8; + case '9': return 9; + case 'a': return 10; + case 'b': return 11; + case 'c': return 12; + case 'd': return 13; + case 'e': return 14; + case 'f': return 15; + case 'A': return 10; + case 'B': return 11; + case 'C': return 12; + case 'D': return 13; + case 'E': return 14; + case 'F': return 15; + default: + { + LOGWARNING("Bad hex digit: %c", a_Character); + ASSERT(!"Bad hex digit"); + return 0; + } + } +} + + + + + +//////////////////////////////////////////////////////////////////////////////// +// cPacketizer: + +cPacketizer::~cPacketizer() +{ + m_Protocol.SendPacket(*this); +} + + + + + +void cPacketizer::WriteByteAngle(double a_Angle) +{ + WriteChar(static_cast(255 * a_Angle / 360)); +} + + + + + +void cPacketizer::WriteFPInt(double a_Value) +{ + Int32 Value = static_cast(a_Value * 32); + WriteInt(Value); +} + + + + + +void cPacketizer::WriteUUID(const AString & a_UUID) +{ + if (a_UUID.length() != 32) + { + LOGWARNING("%s: Attempt to send a bad uuid (length isn't 32): %s", __FUNCTION__, a_UUID.c_str()); + ASSERT(!"Wrong uuid length!"); + return; + } + + for (size_t i = 0; i < 32; i += 2) + { + auto val = static_cast(HexDigitValue(a_UUID[i]) << 4 | HexDigitValue(a_UUID[i + 1])); + VERIFY(m_Out.WriteBEUInt8(val)); + } +} + + + + diff --git a/src/Protocol/Packetizer.h b/src/Protocol/Packetizer.h new file mode 100644 index 000000000..98b50c73f --- /dev/null +++ b/src/Protocol/Packetizer.h @@ -0,0 +1,149 @@ + +// Packetizer.h + +// Declares the cPacketizer class representing a wrapper for sending a single packet over a protocol. +// The class provides auto-locking, serialization and send-on-instance-destroy semantics + + + + + +#pragma once + +#include "Protocol.h" +#include "../ByteBuffer.h" + + + + + +/** Composes an individual packet in the protocol's m_OutPacketBuffer; sends it just before being destructed. */ +class cPacketizer +{ +public: + /** Starts serializing a new packet into the protocol's m_OutPacketBuffer. + Locks the protocol's m_CSPacket to avoid multithreading issues. */ + cPacketizer(cProtocol & a_Protocol, UInt32 a_PacketType) : + m_Protocol(a_Protocol), + m_Out(a_Protocol.m_OutPacketBuffer), + m_Lock(a_Protocol.m_CSPacket), + m_PacketType(a_PacketType) // Used for logging purposes + { + m_Out.WriteVarInt(a_PacketType); + } + + /** Sends the packet via the contained protocol's SendPacket() function. */ + ~cPacketizer(); + + inline void WriteBool(bool a_Value) + { + VERIFY(m_Out.WriteBool(a_Value)); + } + + inline void WriteByte(Byte a_Value) + { + VERIFY(m_Out.WriteBEUInt8(a_Value)); + } + + + inline void WriteChar(char a_Value) + { + VERIFY(m_Out.WriteBEInt8(a_Value)); + } + + + inline void WriteShort(short a_Value) + { + VERIFY(m_Out.WriteBEInt16(a_Value)); + } + + + inline void WriteBEUInt16(short a_Value) + { + VERIFY(m_Out.WriteBEUInt16(a_Value)); + } + + + inline void WriteInt(Int32 a_Value) + { + VERIFY(m_Out.WriteBEInt32(a_Value)); + } + + + inline void WriteUInt32(UInt32 a_Value) + { + VERIFY(m_Out.WriteBEUInt32(a_Value)); + } + + + inline void WriteInt64(Int64 a_Value) + { + VERIFY(m_Out.WriteBEInt64(a_Value)); + } + + + inline void WriteFloat(float a_Value) + { + VERIFY(m_Out.WriteBEFloat(a_Value)); + } + + + inline void WriteDouble(double a_Value) + { + VERIFY(m_Out.WriteBEDouble(a_Value)); + } + + + inline void WriteVarInt(UInt32 a_Value) + { + VERIFY(m_Out.WriteVarInt(a_Value)); + } + + + inline void WriteString(const AString & a_Value) + { + VERIFY(m_Out.WriteVarUTF8String(a_Value)); + } + + + inline void WriteBuf(const char * a_Data, size_t a_Size) + { + VERIFY(m_Out.Write(a_Data, a_Size)); + } + + + /** Writes the specified block position as a single encoded 64-bit BigEndian integer. */ + inline void WritePosition(int a_BlockX, int a_BlockY, int a_BlockZ) + { + VERIFY(m_Out.WritePosition64(a_BlockX, a_BlockY, a_BlockZ)); + } + + /** Writes the specified angle using a single byte. */ + void WriteByteAngle(double a_Angle); + + /** Writes the double value as a 27:5 fixed-point integer. */ + void WriteFPInt(double a_Value); + + /** Writes the specified UUID as a 128-bit BigEndian integer. */ + void WriteUUID(const AString & a_UUID); + + UInt32 GetPacketType(void) const { return m_PacketType; } + +protected: + /** The protocol instance in which the packet is being constructed. */ + cProtocol & m_Protocol; + + /** The protocol's buffer for the constructed packet data. */ + cByteBuffer & m_Out; + + /** The RAII lock preventing multithreaded access to the protocol buffer while constructing the packet. */ + cCSLock m_Lock; + + /** Type of the contained packet. + Used for logging purposes, the packet type is encoded into m_Out immediately in constructor. */ + UInt32 m_PacketType; +} ; + + + + diff --git a/src/Protocol/Protocol.h b/src/Protocol/Protocol.h index 652b5aa7e..d8399049e 100644 --- a/src/Protocol/Protocol.h +++ b/src/Protocol/Protocol.h @@ -14,6 +14,7 @@ #include "../Endianness.h" #include "../Scoreboard.h" #include "../Map.h" +#include "../ByteBuffer.h" @@ -32,6 +33,7 @@ class cChunkDataSerializer; class cFallingBlock; class cCompositeChat; class cStatManager; +class cPacketizer; @@ -47,7 +49,9 @@ class cProtocol { public: cProtocol(cClientHandle * a_Client) : - m_Client(a_Client) + m_Client(a_Client), + m_OutPacketBuffer(64 KiB), + m_OutPacketLenBuffer(20) // 20 bytes is more than enough for one VarInt { } @@ -136,11 +140,27 @@ public: virtual AString GetAuthServerID(void) = 0; protected: + friend class cPacketizer; + cClientHandle * m_Client; - cCriticalSection m_CSPacket; // Each SendXYZ() function must acquire this CS in order to send the whole packet at once - /// A generic data-sending routine, all outgoing packet data needs to be routed through this so that descendants may override it + /** Provides synchronization for sending the entire packet at once. + Each SendXYZ() function must acquire this CS in order to send the whole packet at once. + Automated via cPacketizer class. */ + cCriticalSection m_CSPacket; + + /** Buffer for composing the outgoing packets, through cPacketizer */ + cByteBuffer m_OutPacketBuffer; + + /** Buffer for composing packet length (so that each cPacketizer instance doesn't allocate a new cPacketBuffer) */ + cByteBuffer m_OutPacketLenBuffer; + + /** A generic data-sending routine, all outgoing packet data needs to be routed through this so that descendants may override it. */ virtual void SendData(const char * a_Data, size_t a_Size) = 0; + + /** Sends a single packet contained within the cPacketizer class. + The cPacketizer's destructor calls this to send the contained packet; protocol may transform the data (compression in 1.8 etc). */ + virtual void SendPacket(cPacketizer & a_Packet) = 0; } ; diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index 431072bdb..9632a54b7 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -14,6 +14,7 @@ Implements the 1.7.x protocol classes: #include "Protocol17x.h" #include "ChunkDataSerializer.h" #include "PolarSSL++/Sha1Checksum.h" +#include "Packetizer.h" #include "../ClientHandle.h" #include "../Root.h" @@ -105,8 +106,6 @@ cProtocol172::cProtocol172(cClientHandle * a_Client, const AString & a_ServerAdd m_ServerPort(a_ServerPort), m_State(a_State), m_ReceivedData(32 KiB), - m_OutPacketBuffer(64 KiB), - m_OutPacketLenBuffer(20), // 20 bytes is more than enough for one VarInt m_IsEncrypted(false), m_LastSentDimension(dimNotSet) { @@ -381,7 +380,7 @@ void cProtocol172::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum cPacketizer Pkt(*this, 0x04); // Entity Equipment packet Pkt.WriteUInt32(a_Entity.GetUniqueID()); Pkt.WriteShort(a_SlotNum); - Pkt.WriteItem(a_Item); + WriteItem(Pkt, a_Item); } @@ -421,7 +420,7 @@ void cProtocol172::SendEntityMetadata(const cEntity & a_Entity) cPacketizer Pkt(*this, 0x1c); // Entity Metadata packet Pkt.WriteUInt32(a_Entity.GetUniqueID()); - Pkt.WriteEntityMetadata(a_Entity); + WriteEntityMetadata(Pkt, a_Entity); Pkt.WriteByte(0x7f); // The termination byte } @@ -435,7 +434,7 @@ void cProtocol172::SendEntityProperties(const cEntity & a_Entity) cPacketizer Pkt(*this, 0x20); // Entity Properties packet Pkt.WriteUInt32(a_Entity.GetUniqueID()); - Pkt.WriteEntityProperties(a_Entity); + WriteEntityProperties(Pkt, a_Entity); } @@ -563,7 +562,7 @@ void cProtocol172::SendInventorySlot(char a_WindowID, short a_SlotNum, const cIt cPacketizer Pkt(*this, 0x2f); // Set Slot packet Pkt.WriteChar(a_WindowID); Pkt.WriteShort(a_SlotNum); - Pkt.WriteItem(a_Item); + WriteItem(Pkt, a_Item); } @@ -734,7 +733,7 @@ void cProtocol172::SendPickupSpawn(const cPickup & a_Pickup) cPacketizer Pkt(*this, 0x1c); // Entity Metadata packet Pkt.WriteUInt32(a_Pickup.GetUniqueID()); Pkt.WriteByte((0x05 << 5) | 10); // Slot type + index 10 - Pkt.WriteItem(a_Pickup.GetItem()); + WriteItem(Pkt, a_Pickup.GetItem()); Pkt.WriteByte(0x7f); // End of metadata } } @@ -1157,7 +1156,7 @@ void cProtocol172::SendSpawnMob(const cMonster & a_Mob) Pkt.WriteShort(static_cast(a_Mob.GetSpeedX() * 400)); Pkt.WriteShort(static_cast(a_Mob.GetSpeedY() * 400)); Pkt.WriteShort(static_cast(a_Mob.GetSpeedZ() * 400)); - Pkt.WriteEntityMetadata(a_Mob); + WriteEntityMetadata(Pkt, a_Mob); Pkt.WriteByte(0x7f); // Metadata terminator } @@ -1341,7 +1340,7 @@ void cProtocol172::SendUpdateBlockEntity(cBlockEntity & a_BlockEntity) } Pkt.WriteByte(Action); - Pkt.WriteBlockEntity(a_BlockEntity); + WriteBlockEntity(Pkt, a_BlockEntity); } @@ -1412,7 +1411,7 @@ void cProtocol172::SendWholeInventory(const cWindow & a_Window) a_Window.GetSlots(*(m_Client->GetPlayer()), Slots); for (cItems::const_iterator itr = Slots.begin(), end = Slots.end(); itr != end; ++itr) { - Pkt.WriteItem(*itr); + WriteItem(Pkt, *itr); } // for itr - Slots[] } @@ -2383,6 +2382,39 @@ void cProtocol172::SendData(const char * a_Data, size_t a_Size) +void cProtocol172::SendPacket(cPacketizer & a_Packet) +{ + AString DataToSend; + + // Send the packet length + UInt32 PacketLen = static_cast(m_OutPacketBuffer.GetUsedSpace()); + + m_OutPacketLenBuffer.WriteVarInt(PacketLen); + m_OutPacketLenBuffer.ReadAll(DataToSend); + SendData(DataToSend.data(), DataToSend.size()); + m_OutPacketLenBuffer.CommitRead(); + + // Send the packet data: + m_OutPacketBuffer.ReadAll(DataToSend); + SendData(DataToSend.data(), DataToSend.size()); + m_OutPacketBuffer.CommitRead(); + + // Log the comm into logfile: + if (g_ShouldLogCommOut) + { + AString Hex; + ASSERT(DataToSend.size() > 0); + CreateHexDump(Hex, DataToSend.data(), DataToSend.size(), 16); + m_CommLogFile.Printf("Outgoing packet: type %d (0x%x), length %u (0x%x), state %d. Payload (incl. type):\n%s\n", + a_Packet.GetPacketType(), a_Packet.GetPacketType(), PacketLen, PacketLen, m_State, Hex.c_str() + ); + } +} + + + + + bool cProtocol172::ReadItem(cByteBuffer & a_ByteBuffer, cItem & a_Item) { HANDLE_PACKET_READ(a_ByteBuffer, ReadBEInt16, Int16, ItemType); @@ -2542,43 +2574,7 @@ eBlockFace cProtocol172::FaceIntToBlockFace(Int8 a_BlockFace) -//////////////////////////////////////////////////////////////////////////////// -// cProtocol172::cPacketizer: - -cProtocol172::cPacketizer::~cPacketizer() -{ - AString DataToSend; - - // Send the packet length - UInt32 PacketLen = static_cast(m_Out.GetUsedSpace()); - - m_Protocol.m_OutPacketLenBuffer.WriteVarInt(PacketLen); - m_Protocol.m_OutPacketLenBuffer.ReadAll(DataToSend); - m_Protocol.SendData(DataToSend.data(), DataToSend.size()); - m_Protocol.m_OutPacketLenBuffer.CommitRead(); - - // Send the packet data: - m_Out.ReadAll(DataToSend); - m_Protocol.SendData(DataToSend.data(), DataToSend.size()); - m_Out.CommitRead(); - - // Log the comm into logfile: - if (g_ShouldLogCommOut) - { - AString Hex; - ASSERT(DataToSend.size() > 0); - CreateHexDump(Hex, DataToSend.data() + 1, DataToSend.size() - 1, 16); - m_Protocol.m_CommLogFile.Printf("Outgoing packet: type %d (0x%x), length %u (0x%x), state %d. Payload:\n%s\n", - DataToSend[0], DataToSend[0], PacketLen, PacketLen, m_Protocol.m_State, Hex.c_str() - ); - } -} - - - - - -void cProtocol172::cPacketizer::WriteItem(const cItem & a_Item) +void cProtocol172::WriteItem(cPacketizer & a_Pkt, const cItem & a_Item) { short ItemType = a_Item.m_ItemType; ASSERT(ItemType >= -1); // Check validity of packets in debug runtime @@ -2590,17 +2586,17 @@ void cProtocol172::cPacketizer::WriteItem(const cItem & a_Item) if (a_Item.IsEmpty()) { - WriteShort(-1); + a_Pkt.WriteShort(-1); return; } - WriteShort(ItemType); - WriteChar (a_Item.m_ItemCount); - WriteShort(a_Item.m_ItemDamage); + a_Pkt.WriteShort(ItemType); + a_Pkt.WriteChar (a_Item.m_ItemCount); + a_Pkt.WriteShort(a_Item.m_ItemDamage); if (a_Item.m_Enchantments.IsEmpty() && a_Item.IsBothNameAndLoreEmpty() && (a_Item.m_ItemType != E_ITEM_FIREWORK_ROCKET) && (a_Item.m_ItemType != E_ITEM_FIREWORK_STAR)) { - WriteShort(-1); + a_Pkt.WriteShort(-1); return; } @@ -2649,15 +2645,15 @@ void cProtocol172::cPacketizer::WriteItem(const cItem & a_Item) AString Compressed; CompressStringGZIP(Writer.GetResult().data(), Writer.GetResult().size(), Compressed); - WriteShort((short)Compressed.size()); - WriteBuf(Compressed.data(), Compressed.size()); + a_Pkt.WriteBEUInt16(static_cast(Compressed.size())); + a_Pkt.WriteBuf(Compressed.data(), Compressed.size()); } -void cProtocol172::cPacketizer::WriteBlockEntity(const cBlockEntity & a_BlockEntity) +void cProtocol172::WriteBlockEntity(cPacketizer & a_Pkt, const cBlockEntity & a_BlockEntity) { cFastNBTWriter Writer; @@ -2665,46 +2661,41 @@ void cProtocol172::cPacketizer::WriteBlockEntity(const cBlockEntity & a_BlockEnt { case E_BLOCK_BEACON: { - cBeaconEntity & BeaconEntity = (cBeaconEntity &)a_BlockEntity; - - Writer.AddInt("x", BeaconEntity.GetPosX()); - Writer.AddInt("y", BeaconEntity.GetPosY()); - Writer.AddInt("z", BeaconEntity.GetPosZ()); - Writer.AddInt("Primary", BeaconEntity.GetPrimaryEffect()); + auto & BeaconEntity = reinterpret_cast(a_BlockEntity); + Writer.AddInt("x", BeaconEntity.GetPosX()); + Writer.AddInt("y", BeaconEntity.GetPosY()); + Writer.AddInt("z", BeaconEntity.GetPosZ()); + Writer.AddInt("Primary", BeaconEntity.GetPrimaryEffect()); Writer.AddInt("Secondary", BeaconEntity.GetSecondaryEffect()); - Writer.AddInt("Levels", BeaconEntity.GetBeaconLevel()); + Writer.AddInt("Levels", BeaconEntity.GetBeaconLevel()); Writer.AddString("id", "Beacon"); // "Tile Entity ID" - MC wiki; vanilla server always seems to send this though break; } + case E_BLOCK_COMMAND_BLOCK: { - cCommandBlockEntity & CommandBlockEntity = (cCommandBlockEntity &)a_BlockEntity; - + auto & CommandBlockEntity = reinterpret_cast(a_BlockEntity); Writer.AddByte("TrackOutput", 1); // Neither I nor the MC wiki has any idea about this Writer.AddInt("SuccessCount", CommandBlockEntity.GetResult()); - Writer.AddInt("x", CommandBlockEntity.GetPosX()); - Writer.AddInt("y", CommandBlockEntity.GetPosY()); - Writer.AddInt("z", CommandBlockEntity.GetPosZ()); - Writer.AddString("Command", CommandBlockEntity.GetCommand().c_str()); + Writer.AddInt("x", CommandBlockEntity.GetPosX()); + Writer.AddInt("y", CommandBlockEntity.GetPosY()); + Writer.AddInt("z", CommandBlockEntity.GetPosZ()); + Writer.AddString("Command", CommandBlockEntity.GetCommand().c_str()); // You can set custom names for windows in Vanilla // For a command block, this would be the 'name' prepended to anything it outputs into global chat - // MCS doesn't have this, so just leave it @ '@'. (geddit?) + // MCS doesn't have this, so just leave it at '@'. Writer.AddString("CustomName", "@"); - Writer.AddString("id", "Control"); // "Tile Entity ID" - MC wiki; vanilla server always seems to send this though - + Writer.AddString("id", "Control"); // "Tile Entity ID" - MC wiki; vanilla server always seems to send this though if (!CommandBlockEntity.GetLastOutput().empty()) { - AString Output; - Printf(Output, "{\"text\":\"%s\"}", CommandBlockEntity.GetLastOutput().c_str()); - - Writer.AddString("LastOutput", Output.c_str()); + Writer.AddString("LastOutput", Printf("{\"text\":\"%s\"}", CommandBlockEntity.GetLastOutput().c_str())); } break; } + case E_BLOCK_HEAD: { - cMobHeadEntity & MobHeadEntity = (cMobHeadEntity &)a_BlockEntity; - + auto & MobHeadEntity = reinterpret_cast(a_BlockEntity); Writer.AddInt("x", MobHeadEntity.GetPosX()); Writer.AddInt("y", MobHeadEntity.GetPosY()); Writer.AddInt("z", MobHeadEntity.GetPosZ()); @@ -2714,10 +2705,10 @@ void cProtocol172::cPacketizer::WriteBlockEntity(const cBlockEntity & a_BlockEnt Writer.AddString("id", "Skull"); // "Tile Entity ID" - MC wiki; vanilla server always seems to send this though break; } + case E_BLOCK_FLOWER_POT: { - cFlowerPotEntity & FlowerPotEntity = (cFlowerPotEntity &)a_BlockEntity; - + auto & FlowerPotEntity = reinterpret_cast(a_BlockEntity); Writer.AddInt("x", FlowerPotEntity.GetPosX()); Writer.AddInt("y", FlowerPotEntity.GetPosY()); Writer.AddInt("z", FlowerPotEntity.GetPosZ()); @@ -2726,10 +2717,10 @@ void cProtocol172::cPacketizer::WriteBlockEntity(const cBlockEntity & a_BlockEnt Writer.AddString("id", "FlowerPot"); // "Tile Entity ID" - MC wiki; vanilla server always seems to send this though break; } + case E_BLOCK_MOB_SPAWNER: { - cMobSpawnerEntity & MobSpawnerEntity = (cMobSpawnerEntity &)a_BlockEntity; - + auto & MobSpawnerEntity = reinterpret_cast(a_BlockEntity); Writer.AddInt("x", MobSpawnerEntity.GetPosX()); Writer.AddInt("y", MobSpawnerEntity.GetPosY()); Writer.AddInt("z", MobSpawnerEntity.GetPosZ()); @@ -2738,41 +2729,26 @@ void cProtocol172::cPacketizer::WriteBlockEntity(const cBlockEntity & a_BlockEnt Writer.AddString("id", "MobSpawner"); break; } - default: break; + + default: + { + break; + } } Writer.Finish(); AString Compressed; CompressStringGZIP(Writer.GetResult().data(), Writer.GetResult().size(), Compressed); - WriteShort((short)Compressed.size()); - WriteBuf(Compressed.data(), Compressed.size()); -} - - - - - -void cProtocol172::cPacketizer::WriteByteAngle(double a_Angle) -{ - WriteChar(static_cast(255 * a_Angle / 360)); + a_Pkt.WriteBEUInt16(static_cast(Compressed.size())); + a_Pkt.WriteBuf(Compressed.data(), Compressed.size()); } -void cProtocol172::cPacketizer::WriteFPInt(double a_Value) -{ - int Value = (int)(a_Value * 32); - WriteInt(Value); -} - - - - - -void cProtocol172::cPacketizer::WriteEntityMetadata(const cEntity & a_Entity) +void cProtocol172::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_Entity) { // Common metadata: Byte Flags = 0; @@ -2796,21 +2772,21 @@ void cProtocol172::cPacketizer::WriteEntityMetadata(const cEntity & a_Entity) { Flags |= 0x20; } - WriteByte(0); // Byte(0) + index 0 - WriteByte(Flags); + a_Pkt.WriteByte(0); // Byte(0) + index 0 + a_Pkt.WriteByte(Flags); switch (a_Entity.GetEntityType()) { case cEntity::etPlayer: break; // TODO? case cEntity::etPickup: { - WriteByte((5 << 5) | 10); // Slot(5) + index 10 - WriteItem(((const cPickup &)a_Entity).GetItem()); + a_Pkt.WriteByte((5 << 5) | 10); // Slot(5) + index 10 + WriteItem(a_Pkt, reinterpret_cast(a_Entity).GetItem()); break; } case cEntity::etMinecart: { - WriteByte(0x51); + a_Pkt.WriteByte(0x51); // The following expression makes Minecarts shake more with less health or higher damage taken // It gets half the maximum health, and takes it away from the current health minus the half health: @@ -2819,263 +2795,274 @@ void cProtocol172::cPacketizer::WriteEntityMetadata(const cEntity & a_Entity) Health: 3 | 3 - (3 - 3) = 3 Health: 1 | 3 - (1 - 3) = 5 */ - WriteInt((((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * ((const cMinecart &)a_Entity).LastDamage()) * 4); - WriteByte(0x52); - WriteInt(1); // Shaking direction, doesn't seem to affect anything - WriteByte(0x73); - WriteFloat((float)(((const cMinecart &)a_Entity).LastDamage() + 10)); // Damage taken / shake effect multiplyer + auto & Minecart = reinterpret_cast(a_Entity); + a_Pkt.WriteInt((((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * Minecart.LastDamage()) * 4); + a_Pkt.WriteByte(0x52); + a_Pkt.WriteInt(1); // Shaking direction, doesn't seem to affect anything + a_Pkt.WriteByte(0x73); + a_Pkt.WriteFloat(static_cast(Minecart.LastDamage() + 10)); // Damage taken / shake effect multiplyer - if (((cMinecart &)a_Entity).GetPayload() == cMinecart::mpNone) + if (Minecart.GetPayload() == cMinecart::mpNone) { - cRideableMinecart & RideableMinecart = ((cRideableMinecart &)a_Entity); + auto & RideableMinecart = reinterpret_cast(Minecart); const cItem & MinecartContent = RideableMinecart.GetContent(); if (!MinecartContent.IsEmpty()) { - WriteByte(0x54); + a_Pkt.WriteByte(0x54); int Content = MinecartContent.m_ItemType; Content |= MinecartContent.m_ItemDamage << 8; - WriteInt(Content); - WriteByte(0x55); - WriteInt(RideableMinecart.GetBlockHeight()); - WriteByte(0x56); - WriteByte(1); + a_Pkt.WriteInt(Content); + a_Pkt.WriteByte(0x55); + a_Pkt.WriteInt(RideableMinecart.GetBlockHeight()); + a_Pkt.WriteByte(0x56); + a_Pkt.WriteByte(1); } } - else if (((cMinecart &)a_Entity).GetPayload() == cMinecart::mpFurnace) + else if (Minecart.GetPayload() == cMinecart::mpFurnace) { - WriteByte(0x10); - WriteByte(((const cMinecartWithFurnace &)a_Entity).IsFueled() ? 1 : 0); + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte(reinterpret_cast(Minecart).IsFueled() ? 1 : 0); } break; - } + } // case etMinecart + case cEntity::etProjectile: { - cProjectileEntity & Projectile = (cProjectileEntity &)a_Entity; + auto & Projectile = reinterpret_cast(a_Entity); switch (Projectile.GetProjectileKind()) { case cProjectileEntity::pkArrow: { - WriteByte(0x10); - WriteByte(((const cArrowEntity &)a_Entity).IsCritical() ? 1 : 0); + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte(reinterpret_cast(a_Entity).IsCritical() ? 1 : 0); break; } case cProjectileEntity::pkFirework: { - WriteByte(0xA8); - WriteItem(((const cFireworkEntity &)a_Entity).GetItem()); + a_Pkt.WriteByte(0xa8); + WriteItem(a_Pkt, reinterpret_cast(a_Entity).GetItem()); break; } default: break; } break; - } + } // case etProjectile + case cEntity::etMonster: { - WriteMobMetadata((const cMonster &)a_Entity); + WriteMobMetadata(a_Pkt, reinterpret_cast(a_Entity)); break; - } + } // case etMonster + case cEntity::etItemFrame: { - cItemFrame & Frame = (cItemFrame &)a_Entity; - WriteByte(0xA2); - WriteItem(Frame.GetItem()); - WriteByte(0x3); - WriteByte(Frame.GetItemRotation() / 2); + auto & Frame = reinterpret_cast(a_Entity); + a_Pkt.WriteByte(0xa2); + WriteItem(a_Pkt, Frame.GetItem()); + a_Pkt.WriteByte(0x03); + a_Pkt.WriteByte(Frame.GetItemRotation() / 2); + break; + } // case etItemFrame + + default: + { break; } - default: break; - } + } // switch (EntityType) } -void cProtocol172::cPacketizer::WriteMobMetadata(const cMonster & a_Mob) +void cProtocol172::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_Mob) { switch (a_Mob.GetMobType()) { - case mtCreeper: - { - WriteByte(0x10); - WriteByte(((const cCreeper &)a_Mob).IsBlowing() ? 1 : 0); - WriteByte(0x11); - WriteByte(((const cCreeper &)a_Mob).IsCharged() ? 1 : 0); - break; - } - case mtBat: { - WriteByte(0x10); - WriteByte(((const cBat &)a_Mob).IsHanging() ? 1 : 0); - break; - } - - case mtPig: - { - WriteByte(0x10); - WriteByte(((const cPig &)a_Mob).IsSaddled() ? 1 : 0); + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte(reinterpret_cast(a_Mob).IsHanging() ? 1 : 0); break; - } + } // case mtBat - case mtVillager: + case mtCreeper: { - WriteByte(0x50); - WriteInt(((const cVillager &)a_Mob).GetVilType()); + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte(reinterpret_cast(a_Mob).IsBlowing() ? 1 : 0); + a_Pkt.WriteByte(0x11); + a_Pkt.WriteByte(reinterpret_cast(a_Mob).IsCharged() ? 1 : 0); break; - } + } // case mtCreeper - case mtZombie: + case mtEnderman: { - WriteByte(0x0c); - WriteByte(((const cZombie &)a_Mob).IsBaby() ? 1 : 0); - WriteByte(0x0d); - WriteByte(((const cZombie &)a_Mob).IsVillagerZombie() ? 1 : 0); - WriteByte(0x0e); - WriteByte(((const cZombie &)a_Mob).IsConverting() ? 1 : 0); + auto & Enderman = reinterpret_cast(a_Mob); + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte((Byte)(Enderman.GetCarriedBlock())); + a_Pkt.WriteByte(0x11); + a_Pkt.WriteByte((Byte)(Enderman.GetCarriedMeta())); + a_Pkt.WriteByte(0x12); + a_Pkt.WriteByte(Enderman.IsScreaming() ? 1 : 0); break; - } + } // case mtEnderman case mtGhast: { - WriteByte(0x10); - WriteByte(((const cGhast &)a_Mob).IsCharging()); + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte(reinterpret_cast(a_Mob).IsCharging()); break; - } + } // case mtGhast - case mtWolf: + case mtHorse: { - const cWolf & Wolf = (const cWolf &)a_Mob; - Byte WolfStatus = 0; - if (Wolf.IsSitting()) + auto & Horse = reinterpret_cast(a_Mob); + int Flags = 0; + if (Horse.IsTame()) { - WolfStatus |= 0x1; + Flags |= 0x02; } - if (Wolf.IsAngry()) + if (Horse.IsSaddled()) { - WolfStatus |= 0x2; + Flags |= 0x04; } - if (Wolf.IsTame()) + if (Horse.IsChested()) { - WolfStatus |= 0x4; + Flags |= 0x08; } - WriteByte(0x10); - WriteByte(WolfStatus); - - WriteByte(0x72); - WriteFloat((float)(a_Mob.GetHealth())); - WriteByte(0x13); - WriteByte(Wolf.IsBegging() ? 1 : 0); - WriteByte(0x14); - WriteByte(static_cast(Wolf.GetCollarColor())); + if (Horse.IsBaby()) + { + Flags |= 0x10; + } + if (Horse.IsEating()) + { + Flags |= 0x20; + } + if (Horse.IsRearing()) + { + Flags |= 0x40; + } + if (Horse.IsMthOpen()) + { + Flags |= 0x80; + } + a_Pkt.WriteByte(0x50); // Int at index 16 + a_Pkt.WriteInt(Flags); + a_Pkt.WriteByte(0x13); // Byte at index 19 + a_Pkt.WriteByte(static_cast(Horse.GetHorseType())); + a_Pkt.WriteByte(0x54); // Int at index 20 + int Appearance = 0; + Appearance = Horse.GetHorseColor(); + Appearance |= Horse.GetHorseStyle() << 8; + a_Pkt.WriteInt(Appearance); + a_Pkt.WriteByte(0x56); // Int at index 22 + a_Pkt.WriteInt(Horse.GetHorseArmour()); break; - } + } // case mtHorse + + case mtMagmaCube: + { + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte(static_cast(reinterpret_cast(a_Mob).GetSize())); + break; + } // case mtMagmaCube + + case mtPig: + { + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte(reinterpret_cast(a_Mob).IsSaddled() ? 1 : 0); + break; + } // case mtPig case mtSheep: { - WriteByte(0x10); - Byte SheepMetadata = static_cast(((const cSheep &)a_Mob).GetFurColor() & 0x0f); - if (((const cSheep &)a_Mob).IsSheared()) + a_Pkt.WriteByte(0x10); + auto & Sheep = reinterpret_cast(a_Mob); + Byte SheepMetadata = static_cast(Sheep.GetFurColor() & 0x0f); + if (Sheep.IsSheared()) { SheepMetadata |= 0x10; } - WriteByte(SheepMetadata); + a_Pkt.WriteByte(SheepMetadata); break; - } - - case mtEnderman: - { - WriteByte(0x10); - WriteByte((Byte)(((const cEnderman &)a_Mob).GetCarriedBlock())); - WriteByte(0x11); - WriteByte((Byte)(((const cEnderman &)a_Mob).GetCarriedMeta())); - WriteByte(0x12); - WriteByte(((const cEnderman &)a_Mob).IsScreaming() ? 1 : 0); - break; - } + } // case mtSheep case mtSkeleton: { - WriteByte(0x0d); - WriteByte(((const cSkeleton &)a_Mob).IsWither() ? 1 : 0); + a_Pkt.WriteByte(0x0d); + a_Pkt.WriteByte(reinterpret_cast(a_Mob).IsWither() ? 1 : 0); break; - } + } // case mtSkeleton - case mtWitch: + case mtSlime: { - WriteByte(0x15); - WriteByte(((const cWitch &)a_Mob).IsAngry() ? 1 : 0); + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte(static_cast(reinterpret_cast(a_Mob).GetSize())); break; - } - - case mtWither: + } // case mtSlime + + case mtVillager: { - WriteByte(0x54); // Int at index 20 - WriteInt(static_cast(((const cWither &)a_Mob).GetWitherInvulnerableTicks())); - WriteByte(0x66); // Float at index 6 - WriteFloat((float)(a_Mob.GetHealth())); + a_Pkt.WriteByte(0x50); + a_Pkt.WriteInt(reinterpret_cast(a_Mob).GetVilType()); break; - } + } // case mtVillager - case mtSlime: + case mtWitch: { - WriteByte(0x10); - WriteByte(static_cast(((const cSlime &)a_Mob).GetSize())); + a_Pkt.WriteByte(0x15); + a_Pkt.WriteByte(reinterpret_cast(a_Mob).IsAngry() ? 1 : 0); break; - } - - case mtMagmaCube: + } // case mtWitch + + case mtWither: { - WriteByte(0x10); - WriteByte(static_cast(((const cMagmaCube &)a_Mob).GetSize())); + a_Pkt.WriteByte(0x54); // Int at index 20 + a_Pkt.WriteInt(static_cast(reinterpret_cast(a_Mob).GetWitherInvulnerableTicks())); + a_Pkt.WriteByte(0x66); // Float at index 6 + a_Pkt.WriteFloat(static_cast(a_Mob.GetHealth())); break; - } + } // case mtWither - case mtHorse: + case mtWolf: { - const cHorse & Horse = (const cHorse &)a_Mob; - int Flags = 0; - if (Horse.IsTame()) - { - Flags |= 0x02; - } - if (Horse.IsSaddled()) - { - Flags |= 0x04; - } - if (Horse.IsChested()) - { - Flags |= 0x08; - } - if (Horse.IsBaby()) - { - Flags |= 0x10; - } - if (Horse.IsEating()) + auto & Wolf = reinterpret_cast(a_Mob); + Byte WolfStatus = 0; + if (Wolf.IsSitting()) { - Flags |= 0x20; + WolfStatus |= 0x1; } - if (Horse.IsRearing()) + if (Wolf.IsAngry()) { - Flags |= 0x40; + WolfStatus |= 0x2; } - if (Horse.IsMthOpen()) + if (Wolf.IsTame()) { - Flags |= 0x80; + WolfStatus |= 0x4; } - WriteByte(0x50); // Int at index 16 - WriteInt(Flags); - WriteByte(0x13); // Byte at index 19 - WriteByte(static_cast(Horse.GetHorseType())); - WriteByte(0x54); // Int at index 20 - int Appearance = 0; - Appearance = Horse.GetHorseColor(); - Appearance |= Horse.GetHorseStyle() << 8; - WriteInt(Appearance); - WriteByte(0x56); // Int at index 22 - WriteInt(Horse.GetHorseArmour()); + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte(WolfStatus); + + a_Pkt.WriteByte(0x72); + a_Pkt.WriteFloat(static_cast(a_Mob.GetHealth())); + a_Pkt.WriteByte(0x13); + a_Pkt.WriteByte(Wolf.IsBegging() ? 1 : 0); + a_Pkt.WriteByte(0x14); + a_Pkt.WriteByte(static_cast(Wolf.GetCollarColor())); break; - } + } // case mtWolf + + case mtZombie: + { + auto & Zombie = reinterpret_cast(a_Mob); + a_Pkt.WriteByte(0x0c); + a_Pkt.WriteByte(Zombie.IsBaby() ? 1 : 0); + a_Pkt.WriteByte(0x0d); + a_Pkt.WriteByte(Zombie.IsVillagerZombie() ? 1 : 0); + a_Pkt.WriteByte(0x0e); + a_Pkt.WriteByte(Zombie.IsConverting() ? 1 : 0); + break; + } // case mtZombie default: { @@ -3087,10 +3074,10 @@ void cProtocol172::cPacketizer::WriteMobMetadata(const cMonster & a_Mob) // Custom name: if (a_Mob.HasCustomName()) { - WriteByte(0x8a); - WriteString(a_Mob.GetCustomName()); - WriteByte(0x0b); - WriteByte(a_Mob.IsCustomNameAlwaysVisible() ? 1 : 0); + a_Pkt.WriteByte(0x8a); + a_Pkt.WriteString(a_Mob.GetCustomName()); + a_Pkt.WriteByte(0x0b); + a_Pkt.WriteByte(a_Mob.IsCustomNameAlwaysVisible() ? 1 : 0); } } @@ -3098,20 +3085,20 @@ void cProtocol172::cPacketizer::WriteMobMetadata(const cMonster & a_Mob) -void cProtocol172::cPacketizer::WriteEntityProperties(const cEntity & a_Entity) +void cProtocol172::WriteEntityProperties(cPacketizer & a_Pkt, const cEntity & a_Entity) { if (!a_Entity.IsMob()) { // No properties for anything else than mobs - WriteInt(0); + a_Pkt.WriteInt(0); return; } - // const cMonster & Mob = (const cMonster &)a_Entity; + // auto & Mob = reinterpret_cast(a_Entity); // TODO: Send properties and modifiers based on the mob type - WriteInt(0); // NumProperties + a_Pkt.WriteInt(0); // NumProperties } diff --git a/src/Protocol/Protocol17x.h b/src/Protocol/Protocol17x.h index 543ae6ee3..1212cc325 100644 --- a/src/Protocol/Protocol17x.h +++ b/src/Protocol/Protocol17x.h @@ -140,94 +140,6 @@ public: protected: - /** Composes individual packets in the protocol's m_OutPacketBuffer; sends them upon being destructed */ - class cPacketizer - { - public: - cPacketizer(cProtocol172 & a_Protocol, UInt32 a_PacketType) : - m_Protocol(a_Protocol), - m_Out(a_Protocol.m_OutPacketBuffer), - m_Lock(a_Protocol.m_CSPacket) - { - m_Out.WriteVarInt(a_PacketType); - } - - ~cPacketizer(); - - void WriteBool(bool a_Value) - { - m_Out.WriteBool(a_Value); - } - - void WriteByte(Byte a_Value) - { - m_Out.WriteBEUInt8(a_Value); - } - - void WriteChar(char a_Value) - { - m_Out.WriteBEInt8(a_Value); - } - - void WriteShort(short a_Value) - { - m_Out.WriteBEInt16(a_Value); - } - - void WriteInt(Int32 a_Value) - { - m_Out.WriteBEInt32(a_Value); - } - - void WriteUInt32(UInt32 a_Value) - { - m_Out.WriteBEUInt32(a_Value); - } - - void WriteInt64(Int64 a_Value) - { - m_Out.WriteBEInt64(a_Value); - } - - void WriteFloat(float a_Value) - { - m_Out.WriteBEFloat(a_Value); - } - - void WriteDouble(double a_Value) - { - m_Out.WriteBEDouble(a_Value); - } - - void WriteVarInt(UInt32 a_Value) - { - m_Out.WriteVarInt(a_Value); - } - - void WriteString(const AString & a_Value) - { - m_Out.WriteVarUTF8String(a_Value); - } - - void WriteBuf(const char * a_Data, size_t a_Size) - { - m_Out.Write(a_Data, a_Size); - } - - void WriteItem(const cItem & a_Item); - void WriteByteAngle(double a_Angle); // Writes the specified angle using a single byte - void WriteFPInt(double a_Value); // Writes the double value as a 27:5 fixed-point integer - void WriteEntityMetadata(const cEntity & a_Entity); // Writes the metadata for the specified entity, not including the terminating 0x7f - void WriteMobMetadata(const cMonster & a_Mob); // Writes the mob-specific metadata for the specified mob - void WriteEntityProperties(const cEntity & a_Entity); // Writes the entity properties for the specified entity, including the Count field - void WriteBlockEntity(const cBlockEntity & a_BlockEntity); - - protected: - cProtocol172 & m_Protocol; - cByteBuffer & m_Out; - cCSLock m_Lock; - } ; - AString m_ServerAddress; UInt16 m_ServerPort; @@ -240,12 +152,6 @@ protected: /** Buffer for the received data */ cByteBuffer m_ReceivedData; - /** Buffer for composing the outgoing packets, through cPacketizer */ - cByteBuffer m_OutPacketBuffer; - - /** Buffer for composing packet length (so that each cPacketizer instance doesn't allocate a new cPacketBuffer) */ - cByteBuffer m_OutPacketLenBuffer; - bool m_IsEncrypted; cAesCfb128Decryptor m_Decryptor; @@ -307,6 +213,9 @@ protected: /** Sends the data to the client, encrypting them if needed. */ virtual void SendData(const char * a_Data, size_t a_Size) override; + /** Sends the packet to the client. Called by the cPacketizer's destructor. */ + virtual void SendPacket(cPacketizer & a_Packet) override; + void SendCompass(const cWorld & a_World); /** Reads an item out of the received data, sets a_Item to the values read. Returns false if not enough received data */ @@ -320,6 +229,21 @@ protected: /** Converts the BlockFace received by the protocol into eBlockFace constants. If the received value doesn't match any of our eBlockFace constants, BLOCK_FACE_NONE is returned. */ eBlockFace FaceIntToBlockFace(Int8 a_FaceInt); + + /** Writes the item data into a packet. */ + void WriteItem(cPacketizer & a_Pkt, const cItem & a_Item); + + /** Writes the metadata for the specified entity, not including the terminating 0x7f. */ + void WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_Entity); + + /** Writes the mob-specific metadata for the specified mob */ + void WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_Mob); + + /** Writes the entity properties for the specified entity, including the Count field. */ + void WriteEntityProperties(cPacketizer & a_Pkt, const cEntity & a_Entity); + + /** Writes the block entity data for the specified block entity into the packet. */ + void WriteBlockEntity(cPacketizer & a_Pkt, const cBlockEntity & a_BlockEntity); } ; diff --git a/src/Protocol/Protocol18x.cpp b/src/Protocol/Protocol18x.cpp index e35ec249b..c48ab14ad 100644 --- a/src/Protocol/Protocol18x.cpp +++ b/src/Protocol/Protocol18x.cpp @@ -13,6 +13,7 @@ Implements the 1.8.x protocol classes: #include "Protocol18x.h" #include "ChunkDataSerializer.h" #include "PolarSSL++/Sha1Checksum.h" +#include "Packetizer.h" #include "../ClientHandle.h" #include "../Root.h" @@ -105,8 +106,6 @@ cProtocol180::cProtocol180(cClientHandle * a_Client, const AString & a_ServerAdd m_ServerPort(a_ServerPort), m_State(a_State), m_ReceivedData(32 KiB), - m_OutPacketBuffer(64 KiB), - m_OutPacketLenBuffer(20), // 20 bytes is more than enough for one VarInt m_IsEncrypted(false), m_LastSentDimension(dimNotSet) { @@ -365,7 +364,7 @@ void cProtocol180::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum cPacketizer Pkt(*this, 0x04); // Entity Equipment packet Pkt.WriteVarInt(a_Entity.GetUniqueID()); Pkt.WriteShort(a_SlotNum); - Pkt.WriteItem(a_Item); + WriteItem(Pkt, a_Item); } @@ -406,7 +405,7 @@ void cProtocol180::SendEntityMetadata(const cEntity & a_Entity) cPacketizer Pkt(*this, 0x1c); // Entity Metadata packet Pkt.WriteVarInt(a_Entity.GetUniqueID()); - Pkt.WriteEntityMetadata(a_Entity); + WriteEntityMetadata(Pkt, a_Entity); Pkt.WriteByte(0x7f); // The termination byte } @@ -420,7 +419,7 @@ void cProtocol180::SendEntityProperties(const cEntity & a_Entity) cPacketizer Pkt(*this, 0x20); // Entity Properties packet Pkt.WriteVarInt(a_Entity.GetUniqueID()); - Pkt.WriteEntityProperties(a_Entity); + WriteEntityProperties(Pkt, a_Entity); } @@ -550,7 +549,7 @@ void cProtocol180::SendInventorySlot(char a_WindowID, short a_SlotNum, const cIt cPacketizer Pkt(*this, 0x2f); // Set Slot packet Pkt.WriteChar(a_WindowID); Pkt.WriteShort(a_SlotNum); - Pkt.WriteItem(a_Item); + WriteItem(Pkt, a_Item); } @@ -730,7 +729,7 @@ void cProtocol180::SendPickupSpawn(const cPickup & a_Pickup) cPacketizer Pkt(*this, 0x1c); // Entity Metadata packet Pkt.WriteVarInt(a_Pickup.GetUniqueID()); Pkt.WriteByte((0x05 << 5) | 10); // Slot type + index 10 - Pkt.WriteItem(a_Pickup.GetItem()); + WriteItem(Pkt, a_Pickup.GetItem()); Pkt.WriteByte(0x7f); // End of metadata } } @@ -1194,7 +1193,7 @@ void cProtocol180::SendSpawnMob(const cMonster & a_Mob) Pkt.WriteShort((short)(a_Mob.GetSpeedX() * 400)); Pkt.WriteShort((short)(a_Mob.GetSpeedY() * 400)); Pkt.WriteShort((short)(a_Mob.GetSpeedZ() * 400)); - Pkt.WriteEntityMetadata(a_Mob); + WriteEntityMetadata(Pkt, a_Mob); Pkt.WriteByte(0x7f); // Metadata terminator } @@ -1383,7 +1382,7 @@ void cProtocol180::SendUpdateBlockEntity(cBlockEntity & a_BlockEntity) } Pkt.WriteByte(Action); - Pkt.WriteBlockEntity(a_BlockEntity); + WriteBlockEntity(Pkt, a_BlockEntity); } @@ -1452,7 +1451,7 @@ void cProtocol180::SendWholeInventory(const cWindow & a_Window) a_Window.GetSlots(*(m_Client->GetPlayer()), Slots); for (cItems::const_iterator itr = Slots.begin(), end = Slots.end(); itr != end; ++itr) { - Pkt.WriteItem(*itr); + WriteItem(Pkt, *itr); } // for itr - Slots[] } @@ -2782,56 +2781,58 @@ eBlockFace cProtocol180::FaceIntToBlockFace(Int8 a_BlockFace) //////////////////////////////////////////////////////////////////////////////// // cProtocol180::cPacketizer: -cProtocol180::cPacketizer::~cPacketizer() +void cProtocol180::SendPacket(cPacketizer & a_Pkt) { - UInt32 PacketLen = (UInt32)m_Out.GetUsedSpace(); + UInt32 PacketLen = static_cast(m_OutPacketBuffer.GetUsedSpace()); AString PacketData, CompressedPacket; - m_Out.ReadAll(PacketData); - m_Out.CommitRead(); + m_OutPacketBuffer.ReadAll(PacketData); + m_OutPacketBuffer.CommitRead(); - if ((m_Protocol.m_State == 3) && (PacketLen >= 256)) + if ((m_State == 3) && (PacketLen >= 256)) { + // Compress the packet payload: if (!cProtocol180::CompressPacket(PacketData, CompressedPacket)) { return; } } - else if (m_Protocol.m_State == 3) + else if (m_State == 3) { - m_Protocol.m_OutPacketLenBuffer.WriteVarInt(PacketLen + 1); - m_Protocol.m_OutPacketLenBuffer.WriteVarInt(0); - + // The packet is not compressed, indicate this in the packet header: + m_OutPacketLenBuffer.WriteVarInt(PacketLen + 1); + m_OutPacketLenBuffer.WriteVarInt(0); AString LengthData; - m_Protocol.m_OutPacketLenBuffer.ReadAll(LengthData); - m_Protocol.SendData(LengthData.data(), LengthData.size()); + m_OutPacketLenBuffer.ReadAll(LengthData); + SendData(LengthData.data(), LengthData.size()); } else { - m_Protocol.m_OutPacketLenBuffer.WriteVarInt(PacketLen); - + // Compression doesn't apply to this state, send raw data: + m_OutPacketLenBuffer.WriteVarInt(PacketLen); AString LengthData; - m_Protocol.m_OutPacketLenBuffer.ReadAll(LengthData); - m_Protocol.SendData(LengthData.data(), LengthData.size()); + m_OutPacketLenBuffer.ReadAll(LengthData); + SendData(LengthData.data(), LengthData.size()); } + // Send the packet's payload, either direct or compressed: if (CompressedPacket.empty()) { - m_Protocol.m_OutPacketLenBuffer.CommitRead(); - m_Protocol.SendData(PacketData.data(), PacketData.size()); + m_OutPacketLenBuffer.CommitRead(); + SendData(PacketData.data(), PacketData.size()); } else { - m_Protocol.SendData(CompressedPacket.data(), CompressedPacket.size()); + SendData(CompressedPacket.data(), CompressedPacket.size()); } // Log the comm into logfile: - if (g_ShouldLogCommOut && m_Protocol.m_CommLogFile.IsOpen()) + if (g_ShouldLogCommOut && m_CommLogFile.IsOpen()) { AString Hex; ASSERT(PacketData.size() > 0); - CreateHexDump(Hex, PacketData.data() + 1, PacketData.size() - 1, 16); - m_Protocol.m_CommLogFile.Printf("Outgoing packet: type %d (0x%x), length %u (0x%x), state %d. Payload:\n%s\n", - PacketData[0], PacketData[0], PacketLen, PacketLen, m_Protocol.m_State, Hex.c_str() + CreateHexDump(Hex, PacketData.data(), PacketData.size(), 16); + m_CommLogFile.Printf("Outgoing packet: type %d (0x%x), length %u (0x%x), state %d. Payload (incl. type):\n%s\n", + a_Pkt.GetPacketType(), a_Pkt.GetPacketType(), PacketLen, PacketLen, m_State, Hex.c_str() ); } } @@ -2840,30 +2841,7 @@ cProtocol180::cPacketizer::~cPacketizer() -void cProtocol180::cPacketizer::WriteUUID(const AString & a_UUID) -{ - if (a_UUID.length() != 32) - { - LOGWARNING("Attempt to send a bad uuid (length isn't 32): %s", a_UUID.c_str()); - ASSERT(!"Wrong uuid length!"); - return; - } - AString UUID_1 = a_UUID.substr(0, 16); - AString UUID_2 = a_UUID.substr(16); - - Int64 Value_1, Value_2; - sscanf(UUID_1.c_str(), "%llx", &Value_1); - sscanf(UUID_2.c_str(), "%llx", &Value_2); - - WriteInt64(Value_1); - WriteInt64(Value_2); -} - - - - - -void cProtocol180::cPacketizer::WriteItem(const cItem & a_Item) +void cProtocol180::WriteItem(cPacketizer & a_Pkt, const cItem & a_Item) { short ItemType = a_Item.m_ItemType; ASSERT(ItemType >= -1); // Check validity of packets in debug runtime @@ -2875,17 +2853,17 @@ void cProtocol180::cPacketizer::WriteItem(const cItem & a_Item) if (a_Item.IsEmpty()) { - WriteShort(-1); + a_Pkt.WriteShort(-1); return; } - WriteShort(ItemType); - WriteByte (a_Item.m_ItemCount); - WriteShort(a_Item.m_ItemDamage); + a_Pkt.WriteShort(ItemType); + a_Pkt.WriteByte (a_Item.m_ItemCount); + a_Pkt.WriteShort(a_Item.m_ItemDamage); if (a_Item.m_Enchantments.IsEmpty() && a_Item.IsBothNameAndLoreEmpty() && (a_Item.m_ItemType != E_ITEM_FIREWORK_ROCKET) && (a_Item.m_ItemType != E_ITEM_FIREWORK_STAR)) { - WriteChar(0); + a_Pkt.WriteChar(0); return; } @@ -2928,24 +2906,24 @@ void cProtocol180::cPacketizer::WriteItem(const cItem & a_Item) } if ((a_Item.m_ItemType == E_ITEM_FIREWORK_ROCKET) || (a_Item.m_ItemType == E_ITEM_FIREWORK_STAR)) { - cFireworkItem::WriteToNBTCompound(a_Item.m_FireworkItem, Writer, (ENUM_ITEM_ID)a_Item.m_ItemType); + cFireworkItem::WriteToNBTCompound(a_Item.m_FireworkItem, Writer, static_cast(a_Item.m_ItemType)); } Writer.Finish(); AString Result = Writer.GetResult(); if (Result.size() == 0) { - WriteChar(0); + a_Pkt.WriteChar(0); return; } - WriteBuf(Result.data(), Result.size()); + a_Pkt.WriteBuf(Result.data(), Result.size()); } -void cProtocol180::cPacketizer::WriteBlockEntity(const cBlockEntity & a_BlockEntity) +void cProtocol180::WriteBlockEntity(cPacketizer & a_Pkt, const cBlockEntity & a_BlockEntity) { cFastNBTWriter Writer; @@ -2953,21 +2931,20 @@ void cProtocol180::cPacketizer::WriteBlockEntity(const cBlockEntity & a_BlockEnt { case E_BLOCK_BEACON: { - cBeaconEntity & BeaconEntity = (cBeaconEntity &)a_BlockEntity; - - Writer.AddInt("x", BeaconEntity.GetPosX()); - Writer.AddInt("y", BeaconEntity.GetPosY()); - Writer.AddInt("z", BeaconEntity.GetPosZ()); - Writer.AddInt("Primary", BeaconEntity.GetPrimaryEffect()); + auto & BeaconEntity = reinterpret_cast(a_BlockEntity); + Writer.AddInt("x", BeaconEntity.GetPosX()); + Writer.AddInt("y", BeaconEntity.GetPosY()); + Writer.AddInt("z", BeaconEntity.GetPosZ()); + Writer.AddInt("Primary", BeaconEntity.GetPrimaryEffect()); Writer.AddInt("Secondary", BeaconEntity.GetSecondaryEffect()); - Writer.AddInt("Levels", BeaconEntity.GetBeaconLevel()); + Writer.AddInt("Levels", BeaconEntity.GetBeaconLevel()); Writer.AddString("id", "Beacon"); // "Tile Entity ID" - MC wiki; vanilla server always seems to send this though break; } + case E_BLOCK_COMMAND_BLOCK: { - cCommandBlockEntity & CommandBlockEntity = (cCommandBlockEntity &)a_BlockEntity; - + auto & CommandBlockEntity = reinterpret_cast(a_BlockEntity); Writer.AddByte("TrackOutput", 1); // Neither I nor the MC wiki has any idea about this Writer.AddInt("SuccessCount", CommandBlockEntity.GetResult()); Writer.AddInt("x", CommandBlockEntity.GetPosX()); @@ -2979,20 +2956,16 @@ void cProtocol180::cPacketizer::WriteBlockEntity(const cBlockEntity & a_BlockEnt // MCS doesn't have this, so just leave it @ '@'. (geddit?) Writer.AddString("CustomName", "@"); Writer.AddString("id", "Control"); // "Tile Entity ID" - MC wiki; vanilla server always seems to send this though - if (!CommandBlockEntity.GetLastOutput().empty()) { - AString Output; - Printf(Output, "{\"text\":\"%s\"}", CommandBlockEntity.GetLastOutput().c_str()); - - Writer.AddString("LastOutput", Output.c_str()); + Writer.AddString("LastOutput", Printf("{\"text\":\"%s\"}", CommandBlockEntity.GetLastOutput().c_str())); } break; } + case E_BLOCK_HEAD: { - cMobHeadEntity & MobHeadEntity = (cMobHeadEntity &)a_BlockEntity; - + auto & MobHeadEntity = reinterpret_cast(a_BlockEntity); Writer.AddInt("x", MobHeadEntity.GetPosX()); Writer.AddInt("y", MobHeadEntity.GetPosY()); Writer.AddInt("z", MobHeadEntity.GetPosZ()); @@ -3002,10 +2975,10 @@ void cProtocol180::cPacketizer::WriteBlockEntity(const cBlockEntity & a_BlockEnt Writer.AddString("id", "Skull"); // "Tile Entity ID" - MC wiki; vanilla server always seems to send this though break; } + case E_BLOCK_FLOWER_POT: { - cFlowerPotEntity & FlowerPotEntity = (cFlowerPotEntity &)a_BlockEntity; - + auto & FlowerPotEntity = reinterpret_cast(a_BlockEntity); Writer.AddInt("x", FlowerPotEntity.GetPosX()); Writer.AddInt("y", FlowerPotEntity.GetPosY()); Writer.AddInt("z", FlowerPotEntity.GetPosZ()); @@ -3014,10 +2987,10 @@ void cProtocol180::cPacketizer::WriteBlockEntity(const cBlockEntity & a_BlockEnt Writer.AddString("id", "FlowerPot"); // "Tile Entity ID" - MC wiki; vanilla server always seems to send this though break; } + case E_BLOCK_MOB_SPAWNER: { - cMobSpawnerEntity & MobSpawnerEntity = (cMobSpawnerEntity &)a_BlockEntity; - + auto & MobSpawnerEntity = reinterpret_cast(a_BlockEntity); Writer.AddInt("x", MobSpawnerEntity.GetPosX()); Writer.AddInt("y", MobSpawnerEntity.GetPosY()); Writer.AddInt("z", MobSpawnerEntity.GetPosZ()); @@ -3026,37 +2999,22 @@ void cProtocol180::cPacketizer::WriteBlockEntity(const cBlockEntity & a_BlockEnt Writer.AddString("id", "MobSpawner"); break; } - default: break; + + default: + { + break; + } } Writer.Finish(); - WriteBuf(Writer.GetResult().data(), Writer.GetResult().size()); -} - - - - - -void cProtocol180::cPacketizer::WriteByteAngle(double a_Angle) -{ - WriteByte((char)(255 * a_Angle / 360)); + a_Pkt.WriteBuf(Writer.GetResult().data(), Writer.GetResult().size()); } -void cProtocol180::cPacketizer::WriteFPInt(double a_Value) -{ - int Value = (int)(a_Value * 32); - WriteInt(Value); -} - - - - - -void cProtocol180::cPacketizer::WriteEntityMetadata(const cEntity & a_Entity) +void cProtocol180::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_Entity) { // Common metadata: Byte Flags = 0; @@ -3080,21 +3038,21 @@ void cProtocol180::cPacketizer::WriteEntityMetadata(const cEntity & a_Entity) { Flags |= 0x20; } - WriteByte(0); // Byte(0) + index 0 - WriteByte(Flags); + a_Pkt.WriteByte(0); // Byte(0) + index 0 + a_Pkt.WriteByte(Flags); switch (a_Entity.GetEntityType()) { case cEntity::etPlayer: break; // TODO? case cEntity::etPickup: { - WriteByte((5 << 5) | 10); // Slot(5) + index 10 - WriteItem(((const cPickup &)a_Entity).GetItem()); + a_Pkt.WriteByte((5 << 5) | 10); // Slot(5) + index 10 + WriteItem(a_Pkt, reinterpret_cast(a_Entity).GetItem()); break; } case cEntity::etMinecart: { - WriteByte(0x51); + a_Pkt.WriteByte(0x51); // The following expression makes Minecarts shake more with less health or higher damage taken // It gets half the maximum health, and takes it away from the current health minus the half health: @@ -3103,71 +3061,82 @@ void cProtocol180::cPacketizer::WriteEntityMetadata(const cEntity & a_Entity) Health: 3 | 3 - (3 - 3) = 3 Health: 1 | 3 - (1 - 3) = 5 */ - WriteInt((((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * ((const cMinecart &)a_Entity).LastDamage()) * 4); - WriteByte(0x52); - WriteInt(1); // Shaking direction, doesn't seem to affect anything - WriteByte(0x73); - WriteFloat((float)(((const cMinecart &)a_Entity).LastDamage() + 10)); // Damage taken / shake effect multiplyer + auto & Minecart = reinterpret_cast(a_Entity); + a_Pkt.WriteInt((((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * Minecart.LastDamage()) * 4); + a_Pkt.WriteByte(0x52); + a_Pkt.WriteInt(1); // Shaking direction, doesn't seem to affect anything + a_Pkt.WriteByte(0x73); + a_Pkt.WriteFloat(static_cast(Minecart.LastDamage() + 10)); // Damage taken / shake effect multiplyer - if (((cMinecart &)a_Entity).GetPayload() == cMinecart::mpNone) + if (Minecart.GetPayload() == cMinecart::mpNone) { - cRideableMinecart & RideableMinecart = ((cRideableMinecart &)a_Entity); + auto & RideableMinecart = reinterpret_cast(Minecart); const cItem & MinecartContent = RideableMinecart.GetContent(); if (!MinecartContent.IsEmpty()) { - WriteByte(0x54); + a_Pkt.WriteByte(0x54); int Content = MinecartContent.m_ItemType; Content |= MinecartContent.m_ItemDamage << 8; - WriteInt(Content); - WriteByte(0x55); - WriteInt(RideableMinecart.GetBlockHeight()); - WriteByte(0x56); - WriteByte(1); + a_Pkt.WriteInt(Content); + a_Pkt.WriteByte(0x55); + a_Pkt.WriteInt(RideableMinecart.GetBlockHeight()); + a_Pkt.WriteByte(0x56); + a_Pkt.WriteByte(1); } } - else if (((cMinecart &)a_Entity).GetPayload() == cMinecart::mpFurnace) + else if (Minecart.GetPayload() == cMinecart::mpFurnace) { - WriteByte(0x10); - WriteByte(((const cMinecartWithFurnace &)a_Entity).IsFueled() ? 1 : 0); + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte(reinterpret_cast(Minecart).IsFueled() ? 1 : 0); } break; - } + } // case etMinecart + case cEntity::etProjectile: { - cProjectileEntity & Projectile = (cProjectileEntity &)a_Entity; + auto & Projectile = reinterpret_cast(a_Entity); switch (Projectile.GetProjectileKind()) { case cProjectileEntity::pkArrow: { - WriteByte(0x10); - WriteByte(((const cArrowEntity &)a_Entity).IsCritical() ? 1 : 0); + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte(reinterpret_cast(Projectile).IsCritical() ? 1 : 0); break; } case cProjectileEntity::pkFirework: { - WriteByte(0xA8); - WriteItem(((const cFireworkEntity &)a_Entity).GetItem()); + a_Pkt.WriteByte(0xa8); + WriteItem(a_Pkt, reinterpret_cast(Projectile).GetItem()); + break; + } + default: + { break; } - default: break; } break; - } + } // case etProjectile + case cEntity::etMonster: { - WriteMobMetadata((const cMonster &)a_Entity); + WriteMobMetadata(a_Pkt, reinterpret_cast(a_Entity)); break; } + case cEntity::etItemFrame: { - cItemFrame & Frame = (cItemFrame &)a_Entity; - WriteByte(0xA8); - WriteItem(Frame.GetItem()); - WriteByte(0x09); - WriteByte(Frame.GetItemRotation()); + auto & Frame = reinterpret_cast(a_Entity); + a_Pkt.WriteByte(0xa8); + WriteItem(a_Pkt, Frame.GetItem()); + a_Pkt.WriteByte(0x09); + a_Pkt.WriteByte(Frame.GetItemRotation()); + break; + } // case etItemFrame + + default: + { break; } - default: break; } } @@ -3175,192 +3144,205 @@ void cProtocol180::cPacketizer::WriteEntityMetadata(const cEntity & a_Entity) -void cProtocol180::cPacketizer::WriteMobMetadata(const cMonster & a_Mob) +void cProtocol180::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_Mob) { switch (a_Mob.GetMobType()) { - case mtCreeper: - { - WriteByte(0x10); - WriteByte(((const cCreeper &)a_Mob).IsBlowing() ? 1 : -1); - WriteByte(0x11); - WriteByte(((const cCreeper &)a_Mob).IsCharged() ? 1 : 0); - break; - } - case mtBat: { - WriteByte(0x10); - WriteByte(((const cBat &)a_Mob).IsHanging() ? 1 : 0); + auto & Bat = reinterpret_cast(a_Mob); + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte(Bat.IsHanging() ? 1 : 0); break; - } + } // case mtBat - case mtPig: - { - WriteByte(0x10); - WriteByte(((const cPig &)a_Mob).IsSaddled() ? 1 : 0); - break; - } - - case mtVillager: + case mtCreeper: { - WriteByte(0x50); - WriteInt(((const cVillager &)a_Mob).GetVilType()); + auto & Creeper = reinterpret_cast(a_Mob); + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte(Creeper.IsBlowing() ? 1 : -1); + a_Pkt.WriteByte(0x11); + a_Pkt.WriteByte(Creeper.IsCharged() ? 1 : 0); break; - } + } // case mtCreeper - case mtZombie: + case mtEnderman: { - WriteByte(0x0c); - WriteByte(((const cZombie &)a_Mob).IsBaby() ? 1 : 0); - WriteByte(0x0d); - WriteByte(((const cZombie &)a_Mob).IsVillagerZombie() ? 1 : 0); - WriteByte(0x0e); - WriteByte(((const cZombie &)a_Mob).IsConverting() ? 1 : 0); + auto & Enderman = reinterpret_cast(a_Mob); + a_Pkt.WriteByte(0x30); + a_Pkt.WriteShort(static_cast(Enderman.GetCarriedBlock())); + a_Pkt.WriteByte(0x11); + a_Pkt.WriteByte(static_cast(Enderman.GetCarriedMeta())); + a_Pkt.WriteByte(0x12); + a_Pkt.WriteByte(Enderman.IsScreaming() ? 1 : 0); break; - } + } // case mtEnderman case mtGhast: { - WriteByte(0x10); - WriteByte(((const cGhast &)a_Mob).IsCharging()); + auto & Ghast = reinterpret_cast(a_Mob); + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte(Ghast.IsCharging()); break; - } + } // case mtGhast - case mtWolf: + case mtHorse: { - const cWolf & Wolf = (const cWolf &)a_Mob; - Byte WolfStatus = 0; - if (Wolf.IsSitting()) + auto & Horse = reinterpret_cast(a_Mob); + int Flags = 0; + if (Horse.IsTame()) { - WolfStatus |= 0x1; + Flags |= 0x02; } - if (Wolf.IsAngry()) + if (Horse.IsSaddled()) { - WolfStatus |= 0x2; + Flags |= 0x04; } - if (Wolf.IsTame()) + if (Horse.IsChested()) { - WolfStatus |= 0x4; + Flags |= 0x08; } - WriteByte(0x10); - WriteByte(WolfStatus); - - WriteByte(0x72); - WriteFloat((float)(a_Mob.GetHealth())); - WriteByte(0x13); - WriteByte(Wolf.IsBegging() ? 1 : 0); - WriteByte(0x14); - WriteByte(Wolf.GetCollarColor()); + if (Horse.IsBaby()) + { + Flags |= 0x10; + } + if (Horse.IsEating()) + { + Flags |= 0x20; + } + if (Horse.IsRearing()) + { + Flags |= 0x40; + } + if (Horse.IsMthOpen()) + { + Flags |= 0x80; + } + a_Pkt.WriteByte(0x50); // Int at index 16 + a_Pkt.WriteInt(Flags); + a_Pkt.WriteByte(0x13); // Byte at index 19 + a_Pkt.WriteByte(Horse.GetHorseType()); + a_Pkt.WriteByte(0x54); // Int at index 20 + int Appearance = 0; + Appearance = Horse.GetHorseColor(); + Appearance |= Horse.GetHorseStyle() << 8; + a_Pkt.WriteInt(Appearance); + a_Pkt.WriteByte(0x56); // Int at index 22 + a_Pkt.WriteInt(Horse.GetHorseArmour()); break; - } + } // case mtHorse + + case mtMagmaCube: + { + auto & MagmaCube = reinterpret_cast(a_Mob); + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte(MagmaCube.GetSize()); + break; + } // case mtMagmaCube + + case mtPig: + { + auto & Pig = reinterpret_cast(a_Mob); + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte(Pig.IsSaddled() ? 1 : 0); + break; + } // case mtPig case mtSheep: { - WriteByte(0x10); + auto & Sheep = reinterpret_cast(a_Mob); + a_Pkt.WriteByte(0x10); Byte SheepMetadata = 0; - SheepMetadata = ((const cSheep &)a_Mob).GetFurColor(); - if (((const cSheep &)a_Mob).IsSheared()) + SheepMetadata = Sheep.GetFurColor(); + if (Sheep.IsSheared()) { SheepMetadata |= 0x10; } - WriteByte(SheepMetadata); + a_Pkt.WriteByte(SheepMetadata); break; - } - - case mtEnderman: - { - WriteByte(0x30); - WriteShort((Byte)(((const cEnderman &)a_Mob).GetCarriedBlock())); - WriteByte(0x11); - WriteByte((Byte)(((const cEnderman &)a_Mob).GetCarriedMeta())); - WriteByte(0x12); - WriteByte(((const cEnderman &)a_Mob).IsScreaming() ? 1 : 0); - break; - } + } // case mtSheep case mtSkeleton: { - WriteByte(0x0d); - WriteByte(((const cSkeleton &)a_Mob).IsWither() ? 1 : 0); + auto & Skeleton = reinterpret_cast(a_Mob); + a_Pkt.WriteByte(0x0d); + a_Pkt.WriteByte(Skeleton.IsWither() ? 1 : 0); break; - } + } // case mtSkeleton - case mtWitch: + case mtSlime: { - WriteByte(0x15); - WriteByte(((const cWitch &)a_Mob).IsAngry() ? 1 : 0); + auto & Slime = reinterpret_cast(a_Mob); + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte(Slime.GetSize()); break; - } + } // case mtSlime - case mtWither: + case mtVillager: { - WriteByte(0x54); // Int at index 20 - WriteInt(((const cWither &)a_Mob).GetWitherInvulnerableTicks()); - WriteByte(0x66); // Float at index 6 - WriteFloat((float)(a_Mob.GetHealth())); + auto & Villager = reinterpret_cast(a_Mob); + a_Pkt.WriteByte(0x50); + a_Pkt.WriteInt(Villager.GetVilType()); break; - } + } // case mtVillager - case mtSlime: + case mtWitch: { - WriteByte(0x10); - WriteByte(((const cSlime &)a_Mob).GetSize()); + auto & Witch = reinterpret_cast(a_Mob); + a_Pkt.WriteByte(0x15); + a_Pkt.WriteByte(Witch.IsAngry() ? 1 : 0); break; - } - - case mtMagmaCube: + } // case mtWitch + + case mtWither: { - WriteByte(0x10); - WriteByte(((const cMagmaCube &)a_Mob).GetSize()); + auto & Wither = reinterpret_cast(a_Mob); + a_Pkt.WriteByte(0x54); // Int at index 20 + a_Pkt.WriteInt(Wither.GetWitherInvulnerableTicks()); + a_Pkt.WriteByte(0x66); // Float at index 6 + a_Pkt.WriteFloat(static_cast(a_Mob.GetHealth())); break; - } + } // case mtWither - case mtHorse: + case mtWolf: { - const cHorse & Horse = (const cHorse &)a_Mob; - int Flags = 0; - if (Horse.IsTame()) - { - Flags |= 0x02; - } - if (Horse.IsSaddled()) - { - Flags |= 0x04; - } - if (Horse.IsChested()) - { - Flags |= 0x08; - } - if (Horse.IsBaby()) - { - Flags |= 0x10; - } - if (Horse.IsEating()) + auto & Wolf = reinterpret_cast(a_Mob); + Byte WolfStatus = 0; + if (Wolf.IsSitting()) { - Flags |= 0x20; + WolfStatus |= 0x1; } - if (Horse.IsRearing()) + if (Wolf.IsAngry()) { - Flags |= 0x40; + WolfStatus |= 0x2; } - if (Horse.IsMthOpen()) + if (Wolf.IsTame()) { - Flags |= 0x80; + WolfStatus |= 0x4; } - WriteByte(0x50); // Int at index 16 - WriteInt(Flags); - WriteByte(0x13); // Byte at index 19 - WriteByte(Horse.GetHorseType()); - WriteByte(0x54); // Int at index 20 - int Appearance = 0; - Appearance = Horse.GetHorseColor(); - Appearance |= Horse.GetHorseStyle() << 8; - WriteInt(Appearance); - WriteByte(0x56); // Int at index 22 - WriteInt(Horse.GetHorseArmour()); + a_Pkt.WriteByte(0x10); + a_Pkt.WriteByte(WolfStatus); + + a_Pkt.WriteByte(0x72); + a_Pkt.WriteFloat(static_cast(a_Mob.GetHealth())); + a_Pkt.WriteByte(0x13); + a_Pkt.WriteByte(Wolf.IsBegging() ? 1 : 0); + a_Pkt.WriteByte(0x14); + a_Pkt.WriteByte(Wolf.GetCollarColor()); break; - } + } // case mtWolf + + case mtZombie: + { + auto & Zombie = reinterpret_cast(a_Mob); + a_Pkt.WriteByte(0x0c); + a_Pkt.WriteByte(Zombie.IsBaby() ? 1 : 0); + a_Pkt.WriteByte(0x0d); + a_Pkt.WriteByte(Zombie.IsVillagerZombie() ? 1 : 0); + a_Pkt.WriteByte(0x0e); + a_Pkt.WriteByte(Zombie.IsConverting() ? 1 : 0); + break; + } // case mtZombie } // switch (a_Mob.GetType()) } @@ -3368,12 +3350,12 @@ void cProtocol180::cPacketizer::WriteMobMetadata(const cMonster & a_Mob) -void cProtocol180::cPacketizer::WriteEntityProperties(const cEntity & a_Entity) +void cProtocol180::WriteEntityProperties(cPacketizer & a_Pkt, const cEntity & a_Entity) { if (!a_Entity.IsMob()) { // No properties for anything else than mobs - WriteInt(0); + a_Pkt.WriteInt(0); return; } @@ -3381,7 +3363,7 @@ void cProtocol180::cPacketizer::WriteEntityProperties(const cEntity & a_Entity) // TODO: Send properties and modifiers based on the mob type - WriteInt(0); // NumProperties + a_Pkt.WriteInt(0); // NumProperties } diff --git a/src/Protocol/Protocol18x.h b/src/Protocol/Protocol18x.h index 7ba3a92fe..9aa5ed827 100644 --- a/src/Protocol/Protocol18x.h +++ b/src/Protocol/Protocol18x.h @@ -150,101 +150,6 @@ public: protected: - /** Composes individual packets in the protocol's m_OutPacketBuffer; sends them upon being destructed */ - class cPacketizer - { - public: - cPacketizer(cProtocol180 & a_Protocol, UInt32 a_PacketType) : - m_Protocol(a_Protocol), - m_Out(a_Protocol.m_OutPacketBuffer), - m_Lock(a_Protocol.m_CSPacket) - { - m_Out.WriteVarInt(a_PacketType); - } - - ~cPacketizer(); - - void WriteBool(bool a_Value) - { - m_Out.WriteBool(a_Value); - } - - void WriteByte(UInt8 a_Value) - { - m_Out.WriteBEUInt8(a_Value); - } - - void WriteChar(Int8 a_Value) - { - m_Out.WriteBEInt8(a_Value); - } - - void WriteShort(Int16 a_Value) - { - m_Out.WriteBEInt16(a_Value); - } - - void WriteInt(Int32 a_Value) - { - m_Out.WriteBEInt32(a_Value); - } - - void WriteUInt32(UInt32 a_Value) - { - m_Out.WriteBEUInt32(a_Value); - } - - void WriteInt64(Int64 a_Value) - { - m_Out.WriteBEInt64(a_Value); - } - - void WriteFloat(float a_Value) - { - m_Out.WriteBEFloat(a_Value); - } - - void WriteDouble(double a_Value) - { - m_Out.WriteBEDouble(a_Value); - } - - void WriteVarInt(UInt32 a_Value) - { - m_Out.WriteVarInt(a_Value); - } - - void WriteString(const AString & a_Value) - { - m_Out.WriteVarUTF8String(a_Value); - } - - void WritePosition(int a_BlockX, int a_BlockY, int a_BlockZ) - { - m_Out.WritePosition(a_BlockX, a_BlockY, a_BlockZ); - } - - void WriteUUID(const AString & a_UUID); - - void WriteBuf(const char * a_Data, size_t a_Size) - { - m_Out.Write(a_Data, a_Size); - } - - void WriteItem(const cItem & a_Item); - void WriteByteAngle(double a_Angle); // Writes the specified angle using a single byte - void WriteFPInt(double a_Value); // Writes the double value as a 27:5 fixed-point integer - void WriteEntityMetadata(const cEntity & a_Entity); // Writes the metadata for the specified entity, not including the terminating 0x7f - void WriteMobMetadata(const cMonster & a_Mob); // Writes the mob-specific metadata for the specified mob - void WriteEntityProperties(const cEntity & a_Entity); // Writes the entity properties for the specified entity, including the Count field - void WriteBlockEntity(const cBlockEntity & a_BlockEntity); - - protected: - cProtocol180 & m_Protocol; - cByteBuffer & m_Out; - cCSLock m_Lock; - } ; - AString m_ServerAddress; UInt16 m_ServerPort; @@ -257,12 +162,6 @@ protected: /** Buffer for the received data */ cByteBuffer m_ReceivedData; - /** Buffer for composing the outgoing packets, through cPacketizer */ - cByteBuffer m_OutPacketBuffer; - - /** Buffer for composing packet length (so that each cPacketizer instance doesn't allocate a new cPacketBuffer) */ - cByteBuffer m_OutPacketLenBuffer; - bool m_IsEncrypted; cAesCfb128Decryptor m_Decryptor; @@ -325,6 +224,9 @@ protected: /** Sends the data to the client, encrypting them if needed. */ virtual void SendData(const char * a_Data, size_t a_Size) override; + /** Sends the packet to the client. Called by the cPacketizer's destructor. */ + virtual void SendPacket(cPacketizer & a_Packet) override; + void SendCompass(const cWorld & a_World); /** Reads an item out of the received data, sets a_Item to the values read. @@ -340,6 +242,21 @@ protected: /** Converts the BlockFace received by the protocol into eBlockFace constants. If the received value doesn't match any of our eBlockFace constants, BLOCK_FACE_NONE is returned. */ eBlockFace FaceIntToBlockFace(Int8 a_FaceInt); + + /** Writes the item data into a packet. */ + void WriteItem(cPacketizer & a_Pkt, const cItem & a_Item); + + /** Writes the metadata for the specified entity, not including the terminating 0x7f. */ + void WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_Entity); + + /** Writes the mob-specific metadata for the specified mob */ + void WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_Mob); + + /** Writes the entity properties for the specified entity, including the Count field. */ + void WriteEntityProperties(cPacketizer & a_Pkt, const cEntity & a_Entity); + + /** Writes the block entity data for the specified block entity into the packet. */ + void WriteBlockEntity(cPacketizer & a_Pkt, const cBlockEntity & a_BlockEntity); } ; diff --git a/src/Protocol/ProtocolRecognizer.cpp b/src/Protocol/ProtocolRecognizer.cpp index 8d0f8148d..36f8bc791 100644 --- a/src/Protocol/ProtocolRecognizer.cpp +++ b/src/Protocol/ProtocolRecognizer.cpp @@ -982,3 +982,15 @@ bool cProtocolRecognizer::TryRecognizeLengthedProtocol(UInt32 a_PacketLengthRema +void cProtocolRecognizer::SendPacket(cPacketizer & a_Pkt) +{ + // This function should never be called - it needs to exists so that cProtocolRecognizer can be instantiated, + // but the actual sending is done by the internal m_Protocol itself. + LOGWARNING("%s: This function shouldn't ever be called.", __FUNCTION__); + ASSERT(!"Function not to be called"); +} + + + + + diff --git a/src/Protocol/ProtocolRecognizer.h b/src/Protocol/ProtocolRecognizer.h index 165dc0c0c..d46d31cb1 100644 --- a/src/Protocol/ProtocolRecognizer.h +++ b/src/Protocol/ProtocolRecognizer.h @@ -136,9 +136,12 @@ protected: /** Tries to recognize a protocol in the lengthed family (1.7+), based on m_Buffer; returns true if recognized. The packet length and type have already been read, type is 0 - The number of bytes remaining in the packet is passed as a_PacketLengthRemaining - **/ + The number of bytes remaining in the packet is passed as a_PacketLengthRemaining. **/ bool TryRecognizeLengthedProtocol(UInt32 a_PacketLengthRemaining); + + /** Sends a single packet contained within the cPacketizer class. + The cPacketizer's destructor calls this to send the contained packet; protocol may transform the data (compression in 1.8 etc). */ + virtual void SendPacket(cPacketizer & a_Pkt) override; } ; -- cgit v1.2.3 From b913c5da69f362abbb70de6e11baca4cbce2b919 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sun, 22 Mar 2015 23:09:23 +0100 Subject: Added VarInt64, normalized cPacketizer datatype names. --- src/Protocol/ChunkDataSerializer.cpp | 8 +- src/Protocol/Packetizer.cpp | 5 +- src/Protocol/Packetizer.h | 30 +- src/Protocol/Protocol17x.cpp | 657 ++++++++++++++++++----------------- src/Protocol/Protocol18x.cpp | 655 +++++++++++++++++----------------- 5 files changed, 682 insertions(+), 673 deletions(-) (limited to 'src/Protocol') diff --git a/src/Protocol/ChunkDataSerializer.cpp b/src/Protocol/ChunkDataSerializer.cpp index e850ceaec..60fd5f935 100644 --- a/src/Protocol/ChunkDataSerializer.cpp +++ b/src/Protocol/ChunkDataSerializer.cpp @@ -187,7 +187,7 @@ void cChunkDataSerializer::Serialize47(AString & a_Data, int a_ChunkX, int a_Chu // Create the packet: cByteBuffer Packet(512 KiB); - Packet.WriteVarInt(0x21); // Packet id (Chunk Data packet) + Packet.WriteVarInt32(0x21); // Packet id (Chunk Data packet) Packet.WriteBEInt32(a_ChunkX); Packet.WriteBEInt32(a_ChunkZ); Packet.WriteBool(true); // "Ground-up continuous", or rather, "biome data present" flag @@ -201,7 +201,7 @@ void cChunkDataSerializer::Serialize47(AString & a_Data, int a_ChunkX, int a_Chu sizeof(m_BlockSkyLight) + // Block sky light BiomeDataSize // Biome data ); - Packet.WriteVarInt(ChunkSize); + Packet.WriteVarInt32(ChunkSize); // Write the block types to the packet: for (size_t Index = 0; Index < cChunkDef::NumBlocks; Index++) @@ -234,8 +234,8 @@ void cChunkDataSerializer::Serialize47(AString & a_Data, int a_ChunkX, int a_Chu else { AString PostData; - Buffer.WriteVarInt((UInt32)Packet.GetUsedSpace() + 1); - Buffer.WriteVarInt(0); + Buffer.WriteVarInt32(static_cast(Packet.GetUsedSpace() + 1)); + Buffer.WriteVarInt32(0); Buffer.ReadAll(PostData); Buffer.CommitRead(); diff --git a/src/Protocol/Packetizer.cpp b/src/Protocol/Packetizer.cpp index 2b27f7b2f..0a84d4678 100644 --- a/src/Protocol/Packetizer.cpp +++ b/src/Protocol/Packetizer.cpp @@ -64,7 +64,7 @@ cPacketizer::~cPacketizer() void cPacketizer::WriteByteAngle(double a_Angle) { - WriteChar(static_cast(255 * a_Angle / 360)); + WriteBEInt8(static_cast(255 * a_Angle / 360)); } @@ -73,8 +73,7 @@ void cPacketizer::WriteByteAngle(double a_Angle) void cPacketizer::WriteFPInt(double a_Value) { - Int32 Value = static_cast(a_Value * 32); - WriteInt(Value); + WriteBEInt32(static_cast(a_Value * 32)); } diff --git a/src/Protocol/Packetizer.h b/src/Protocol/Packetizer.h index 98b50c73f..7f5e9c2c3 100644 --- a/src/Protocol/Packetizer.h +++ b/src/Protocol/Packetizer.h @@ -29,7 +29,7 @@ public: m_Lock(a_Protocol.m_CSPacket), m_PacketType(a_PacketType) // Used for logging purposes { - m_Out.WriteVarInt(a_PacketType); + m_Out.WriteVarInt32(a_PacketType); } /** Sends the packet via the contained protocol's SendPacket() function. */ @@ -40,19 +40,19 @@ public: VERIFY(m_Out.WriteBool(a_Value)); } - inline void WriteByte(Byte a_Value) + inline void WriteBEUInt8(UInt8 a_Value) { VERIFY(m_Out.WriteBEUInt8(a_Value)); } - inline void WriteChar(char a_Value) + inline void WriteBEInt8(Int8 a_Value) { VERIFY(m_Out.WriteBEInt8(a_Value)); } - inline void WriteShort(short a_Value) + inline void WriteBEInt16(Int16 a_Value) { VERIFY(m_Out.WriteBEInt16(a_Value)); } @@ -64,39 +64,45 @@ public: } - inline void WriteInt(Int32 a_Value) + inline void WriteBEInt32(Int32 a_Value) { VERIFY(m_Out.WriteBEInt32(a_Value)); } - inline void WriteUInt32(UInt32 a_Value) + inline void WriteBEUInt32(UInt32 a_Value) { VERIFY(m_Out.WriteBEUInt32(a_Value)); } - inline void WriteInt64(Int64 a_Value) + inline void WriteBEInt64(Int64 a_Value) { VERIFY(m_Out.WriteBEInt64(a_Value)); } - inline void WriteFloat(float a_Value) + inline void WriteBEUInt64(UInt64 a_Value) + { + VERIFY(m_Out.WriteBEUInt64(a_Value)); + } + + + inline void WriteBEFloat(float a_Value) { VERIFY(m_Out.WriteBEFloat(a_Value)); } - inline void WriteDouble(double a_Value) + inline void WriteBEDouble(double a_Value) { VERIFY(m_Out.WriteBEDouble(a_Value)); } - inline void WriteVarInt(UInt32 a_Value) + inline void WriteVarInt32(UInt32 a_Value) { - VERIFY(m_Out.WriteVarInt(a_Value)); + VERIFY(m_Out.WriteVarInt32(a_Value)); } @@ -113,7 +119,7 @@ public: /** Writes the specified block position as a single encoded 64-bit BigEndian integer. */ - inline void WritePosition(int a_BlockX, int a_BlockY, int a_BlockZ) + inline void WritePosition64(int a_BlockX, int a_BlockY, int a_BlockZ) { VERIFY(m_Out.WritePosition64(a_BlockX, a_BlockY, a_BlockZ)); } diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index 9632a54b7..2bc58e7e5 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -165,8 +165,8 @@ void cProtocol172::SendAttachEntity(const cEntity & a_Entity, const cEntity * a_ ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x1b); // Attach Entity packet - Pkt.WriteUInt32(a_Entity.GetUniqueID()); - Pkt.WriteUInt32((a_Vehicle != nullptr) ? a_Vehicle->GetUniqueID() : 0); + Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEUInt32((a_Vehicle != nullptr) ? a_Vehicle->GetUniqueID() : 0); Pkt.WriteBool(false); } @@ -179,12 +179,12 @@ void cProtocol172::SendBlockAction(int a_BlockX, int a_BlockY, int a_BlockZ, cha ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x24); // Block Action packet - Pkt.WriteInt(a_BlockX); - Pkt.WriteShort(static_cast(a_BlockY)); - Pkt.WriteInt(a_BlockZ); - Pkt.WriteByte(static_cast(a_Byte1)); - Pkt.WriteByte(static_cast(a_Byte2)); - Pkt.WriteVarInt(a_BlockType); + Pkt.WriteBEInt32(a_BlockX); + Pkt.WriteBEInt16(static_cast(a_BlockY)); + Pkt.WriteBEInt32(a_BlockZ); + Pkt.WriteBEInt8(a_Byte1); + Pkt.WriteBEInt8(a_Byte2); + Pkt.WriteVarInt32(a_BlockType); } @@ -196,11 +196,11 @@ void cProtocol172::SendBlockBreakAnim(UInt32 a_EntityID, int a_BlockX, int a_Blo ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x25); // Block Break Animation packet - Pkt.WriteVarInt(a_EntityID); - Pkt.WriteInt(a_BlockX); - Pkt.WriteInt(a_BlockY); - Pkt.WriteInt(a_BlockZ); - Pkt.WriteChar(a_Stage); + Pkt.WriteVarInt32(a_EntityID); + Pkt.WriteBEInt32(a_BlockX); + Pkt.WriteBEInt32(a_BlockY); + Pkt.WriteBEInt32(a_BlockZ); + Pkt.WriteBEInt8(a_Stage); } @@ -213,11 +213,11 @@ void cProtocol172::SendBlockChange(int a_BlockX, int a_BlockY, int a_BlockZ, BLO ASSERT((a_BlockY >= 0) && (a_BlockY < 256)); cPacketizer Pkt(*this, 0x23); // Block Change packet - Pkt.WriteInt(a_BlockX); - Pkt.WriteByte(static_cast(a_BlockY)); - Pkt.WriteInt(a_BlockZ); - Pkt.WriteVarInt(a_BlockType); - Pkt.WriteByte(a_BlockMeta); + Pkt.WriteBEInt32(a_BlockX); + Pkt.WriteBEUInt8(static_cast(a_BlockY)); + Pkt.WriteBEInt32(a_BlockZ); + Pkt.WriteVarInt32(a_BlockType); + Pkt.WriteBEUInt8(a_BlockMeta); } @@ -229,15 +229,15 @@ void cProtocol172::SendBlockChanges(int a_ChunkX, int a_ChunkZ, const sSetBlockV ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x22); // Multi Block Change packet - Pkt.WriteInt(a_ChunkX); - Pkt.WriteInt(a_ChunkZ); - Pkt.WriteShort((short)a_Changes.size()); - Pkt.WriteInt((int)a_Changes.size() * 4); + Pkt.WriteBEInt32(a_ChunkX); + Pkt.WriteBEInt32(a_ChunkZ); + Pkt.WriteBEUInt16(static_cast(a_Changes.size())); + Pkt.WriteBEUInt32(static_cast(a_Changes.size() * 4)); for (sSetBlockVector::const_iterator itr = a_Changes.begin(), end = a_Changes.end(); itr != end; ++itr) { int Coords = itr->m_RelY | (itr->m_RelZ << 8) | (itr->m_RelX << 12); int Blocks = static_cast(itr->m_BlockMeta | (itr->m_BlockType << 4)); - Pkt.WriteInt((Coords << 16) | Blocks); + Pkt.WriteBEInt32((Coords << 16) | Blocks); } // for itr - a_Changes[] } @@ -282,8 +282,8 @@ void cProtocol172::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerialize const AString & ChunkData = a_Serializer.Serialize(cChunkDataSerializer::RELEASE_1_3_2, a_ChunkX, a_ChunkZ); cPacketizer Pkt(*this, 0x21); // Chunk Data packet - Pkt.WriteInt(a_ChunkX); - Pkt.WriteInt(a_ChunkZ); + Pkt.WriteBEInt32(a_ChunkX); + Pkt.WriteBEInt32(a_ChunkZ); Pkt.WriteBuf(ChunkData.data(), ChunkData.size()); } @@ -296,8 +296,8 @@ void cProtocol172::SendCollectEntity(const cEntity & a_Entity, const cPlayer & a ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x0d); // Collect Item packet - Pkt.WriteUInt32(a_Entity.GetUniqueID()); - Pkt.WriteUInt32(a_Player.GetUniqueID()); + Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEUInt32(a_Player.GetUniqueID()); } @@ -309,8 +309,8 @@ void cProtocol172::SendDestroyEntity(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x13); // Destroy Entities packet - Pkt.WriteByte(1); - Pkt.WriteUInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEUInt8(1); + Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); } @@ -347,9 +347,9 @@ void cProtocol172::SendEditSign(int a_BlockX, int a_BlockY, int a_BlockZ) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x36); // Sign Editor Open packet - Pkt.WriteInt(a_BlockX); - Pkt.WriteInt(a_BlockY); - Pkt.WriteInt(a_BlockZ); + Pkt.WriteBEInt32(a_BlockX); + Pkt.WriteBEInt32(a_BlockY); + Pkt.WriteBEInt32(a_BlockZ); } @@ -363,10 +363,10 @@ void cProtocol172::SendEntityEffect(const cEntity & a_Entity, int a_EffectID, in ASSERT((a_Amplifier >= 0) && (a_Amplifier < 256)); cPacketizer Pkt(*this, 0x1D); // Entity Effect packet - Pkt.WriteUInt32(a_Entity.GetUniqueID()); - Pkt.WriteByte(static_cast(a_EffectID)); - Pkt.WriteByte(static_cast(a_Amplifier)); - Pkt.WriteShort(a_Duration); + Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEUInt8(static_cast(a_EffectID)); + Pkt.WriteBEUInt8(static_cast(a_Amplifier)); + Pkt.WriteBEInt16(a_Duration); } @@ -378,8 +378,8 @@ void cProtocol172::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x04); // Entity Equipment packet - Pkt.WriteUInt32(a_Entity.GetUniqueID()); - Pkt.WriteShort(a_SlotNum); + Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEInt16(a_SlotNum); WriteItem(Pkt, a_Item); } @@ -392,7 +392,7 @@ void cProtocol172::SendEntityHeadLook(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x19); // Entity Head Look packet - Pkt.WriteUInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); Pkt.WriteByteAngle(a_Entity.GetHeadYaw()); } @@ -405,7 +405,7 @@ void cProtocol172::SendEntityLook(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x16); // Entity Look packet - Pkt.WriteUInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); Pkt.WriteByteAngle(a_Entity.GetYaw()); Pkt.WriteByteAngle(a_Entity.GetPitch()); } @@ -419,9 +419,9 @@ void cProtocol172::SendEntityMetadata(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x1c); // Entity Metadata packet - Pkt.WriteUInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); WriteEntityMetadata(Pkt, a_Entity); - Pkt.WriteByte(0x7f); // The termination byte + Pkt.WriteBEUInt8(0x7f); // The termination byte } @@ -433,7 +433,7 @@ void cProtocol172::SendEntityProperties(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x20); // Entity Properties packet - Pkt.WriteUInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); WriteEntityProperties(Pkt, a_Entity); } @@ -446,10 +446,10 @@ void cProtocol172::SendEntityRelMove(const cEntity & a_Entity, char a_RelX, char ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x15); // Entity Relative Move packet - Pkt.WriteUInt32(a_Entity.GetUniqueID()); - Pkt.WriteChar(a_RelX); - Pkt.WriteChar(a_RelY); - Pkt.WriteChar(a_RelZ); + Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEInt8(a_RelX); + Pkt.WriteBEInt8(a_RelY); + Pkt.WriteBEInt8(a_RelZ); } @@ -461,10 +461,10 @@ void cProtocol172::SendEntityRelMoveLook(const cEntity & a_Entity, char a_RelX, ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x17); // Entity Look And Relative Move packet - Pkt.WriteUInt32(a_Entity.GetUniqueID()); - Pkt.WriteChar(a_RelX); - Pkt.WriteChar(a_RelY); - Pkt.WriteChar(a_RelZ); + Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEInt8(a_RelX); + Pkt.WriteBEInt8(a_RelY); + Pkt.WriteBEInt8(a_RelZ); Pkt.WriteByteAngle(a_Entity.GetYaw()); Pkt.WriteByteAngle(a_Entity.GetPitch()); } @@ -478,8 +478,8 @@ void cProtocol172::SendEntityStatus(const cEntity & a_Entity, char a_Status) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x1a); // Entity Status packet - Pkt.WriteUInt32(a_Entity.GetUniqueID()); - Pkt.WriteChar(a_Status); + Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEInt8(a_Status); } @@ -491,11 +491,11 @@ void cProtocol172::SendEntityVelocity(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x12); // Entity Velocity packet - Pkt.WriteUInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); // 400 = 8000 / 20 ... Conversion from our speed in m/s to 8000 m/tick - Pkt.WriteShort(static_cast(a_Entity.GetSpeedX() * 400)); - Pkt.WriteShort(static_cast(a_Entity.GetSpeedY() * 400)); - Pkt.WriteShort(static_cast(a_Entity.GetSpeedZ() * 400)); + Pkt.WriteBEInt16(static_cast(a_Entity.GetSpeedX() * 400)); + Pkt.WriteBEInt16(static_cast(a_Entity.GetSpeedY() * 400)); + Pkt.WriteBEInt16(static_cast(a_Entity.GetSpeedZ() * 400)); } @@ -507,20 +507,20 @@ void cProtocol172::SendExplosion(double a_BlockX, double a_BlockY, double a_Bloc ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x27); // Explosion packet - Pkt.WriteFloat((float)a_BlockX); - Pkt.WriteFloat((float)a_BlockY); - Pkt.WriteFloat((float)a_BlockZ); - Pkt.WriteFloat((float)a_Radius); - Pkt.WriteInt((int)a_BlocksAffected.size()); + Pkt.WriteBEFloat(static_cast(a_BlockX)); + Pkt.WriteBEFloat(static_cast(a_BlockY)); + Pkt.WriteBEFloat(static_cast(a_BlockZ)); + Pkt.WriteBEFloat(static_cast(a_Radius)); + Pkt.WriteBEUInt32(static_cast(a_BlocksAffected.size())); for (cVector3iArray::const_iterator itr = a_BlocksAffected.begin(), end = a_BlocksAffected.end(); itr != end; ++itr) { - Pkt.WriteChar((char)itr->x); - Pkt.WriteChar((char)itr->y); - Pkt.WriteChar((char)itr->z); + Pkt.WriteBEInt8(static_cast(itr->x)); + Pkt.WriteBEInt8(static_cast(itr->y)); + Pkt.WriteBEInt8(static_cast(itr->z)); } // for itr - a_BlockAffected[] - Pkt.WriteFloat((float)a_PlayerMotion.x); - Pkt.WriteFloat((float)a_PlayerMotion.y); - Pkt.WriteFloat((float)a_PlayerMotion.z); + Pkt.WriteBEFloat(static_cast(a_PlayerMotion.x)); + Pkt.WriteBEFloat(static_cast(a_PlayerMotion.y)); + Pkt.WriteBEFloat(static_cast(a_PlayerMotion.z)); } @@ -532,8 +532,8 @@ void cProtocol172::SendGameMode(eGameMode a_GameMode) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x2b); // Change Game State packet - Pkt.WriteByte(3); // Reason: Change game mode - Pkt.WriteFloat((float)a_GameMode); + Pkt.WriteBEUInt8(3); // Reason: Change game mode + Pkt.WriteBEFloat(static_cast(a_GameMode)); // The protocol really represents the value with a float! } @@ -546,9 +546,9 @@ void cProtocol172::SendHealth(void) cPacketizer Pkt(*this, 0x06); // Update Health packet cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteFloat(static_cast(Player->GetHealth())); - Pkt.WriteShort(static_cast(Player->GetFoodLevel())); - Pkt.WriteFloat(static_cast(Player->GetFoodSaturationLevel())); + Pkt.WriteBEFloat(static_cast(Player->GetHealth())); + Pkt.WriteBEInt16(static_cast(Player->GetFoodLevel())); + Pkt.WriteBEFloat(static_cast(Player->GetFoodSaturationLevel())); } @@ -560,8 +560,8 @@ void cProtocol172::SendInventorySlot(char a_WindowID, short a_SlotNum, const cIt ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x2f); // Set Slot packet - Pkt.WriteChar(a_WindowID); - Pkt.WriteShort(a_SlotNum); + Pkt.WriteBEInt8(a_WindowID); + Pkt.WriteBEInt16(a_SlotNum); WriteItem(Pkt, a_Item); } @@ -579,7 +579,7 @@ void cProtocol172::SendKeepAlive(int a_PingID) } cPacketizer Pkt(*this, 0x00); // Keep Alive packet - Pkt.WriteInt(a_PingID); + Pkt.WriteBEInt32(a_PingID); } @@ -592,11 +592,11 @@ void cProtocol172::SendLogin(const cPlayer & a_Player, const cWorld & a_World) { cServer * Server = cRoot::Get()->GetServer(); cPacketizer Pkt(*this, 0x01); // Join Game packet - Pkt.WriteUInt32(a_Player.GetUniqueID()); - Pkt.WriteByte(static_cast(a_Player.GetEffectiveGameMode()) | (Server->IsHardcore() ? 0x08 : 0)); // Hardcore flag bit 4 - Pkt.WriteChar(static_cast(a_World.GetDimension())); - Pkt.WriteByte(2); // TODO: Difficulty (set to Normal) - Pkt.WriteByte(static_cast(std::min(Server->GetMaxPlayers(), 60))); + Pkt.WriteBEUInt32(a_Player.GetUniqueID()); + Pkt.WriteBEUInt8(static_cast(a_Player.GetEffectiveGameMode()) | (Server->IsHardcore() ? 0x08 : 0)); // Hardcore flag bit 4 + Pkt.WriteBEInt8(static_cast(a_World.GetDimension())); + Pkt.WriteBEUInt8(2); // TODO: Difficulty (set to Normal) + Pkt.WriteBEUInt8(static_cast(std::min(Server->GetMaxPlayers(), 60))); Pkt.WriteString("default"); // Level type - wtf? } m_LastSentDimension = a_World.GetDimension(); @@ -604,9 +604,9 @@ void cProtocol172::SendLogin(const cPlayer & a_Player, const cWorld & a_World) // Send the spawn position: { cPacketizer Pkt(*this, 0x05); // Spawn Position packet - Pkt.WriteInt(static_cast(a_World.GetSpawnX())); - Pkt.WriteInt(static_cast(a_World.GetSpawnY())); - Pkt.WriteInt(static_cast(a_World.GetSpawnZ())); + Pkt.WriteBEInt32(static_cast(a_World.GetSpawnX())); + Pkt.WriteBEInt32(static_cast(a_World.GetSpawnY())); + Pkt.WriteBEInt32(static_cast(a_World.GetSpawnZ())); } // Send player abilities: @@ -638,12 +638,12 @@ void cProtocol172::SendPaintingSpawn(const cPainting & a_Painting) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x10); // Spawn Painting packet - Pkt.WriteVarInt(a_Painting.GetUniqueID()); + Pkt.WriteVarInt32(a_Painting.GetUniqueID()); Pkt.WriteString(a_Painting.GetName().c_str()); - Pkt.WriteInt(static_cast(a_Painting.GetPosX())); - Pkt.WriteInt(static_cast(a_Painting.GetPosY())); - Pkt.WriteInt(static_cast(a_Painting.GetPosZ())); - Pkt.WriteInt(a_Painting.GetProtocolFacing()); + Pkt.WriteBEInt32(static_cast(a_Painting.GetPosX())); + Pkt.WriteBEInt32(static_cast(a_Painting.GetPosY())); + Pkt.WriteBEInt32(static_cast(a_Painting.GetPosZ())); + Pkt.WriteBEInt32(a_Painting.GetProtocolFacing()); } @@ -658,12 +658,12 @@ void cProtocol172::SendMapColumn(int a_MapID, int a_X, int a_Y, const Byte * a_C ASSERT((a_Y >= 0) && (a_Y < 256)); cPacketizer Pkt(*this, 0x34); - Pkt.WriteVarInt(static_cast(a_MapID)); - Pkt.WriteShort (static_cast(3 + a_Length)); + Pkt.WriteVarInt32(static_cast(a_MapID)); + Pkt.WriteBEUInt16(static_cast(3 + a_Length)); - Pkt.WriteByte(0); - Pkt.WriteByte(static_cast(a_X)); - Pkt.WriteByte(static_cast(a_Y)); + Pkt.WriteBEUInt8(0); + Pkt.WriteBEUInt8(static_cast(a_X)); + Pkt.WriteBEUInt8(static_cast(a_Y)); Pkt.WriteBuf(reinterpret_cast(a_Colors), a_Length); } @@ -678,18 +678,18 @@ void cProtocol172::SendMapDecorators(int a_MapID, const cMapDecoratorList & a_De ASSERT(1 + 3 * a_Decorators.size() < USHRT_MAX); cPacketizer Pkt(*this, 0x34); - Pkt.WriteVarInt(static_cast(a_MapID)); - Pkt.WriteShort (static_cast(1 + (3 * a_Decorators.size()))); + Pkt.WriteVarInt32(static_cast(a_MapID)); + Pkt.WriteBEUInt16(static_cast(1 + (3 * a_Decorators.size()))); - Pkt.WriteByte(1); + Pkt.WriteBEUInt8(1); for (cMapDecoratorList::const_iterator it = a_Decorators.begin(); it != a_Decorators.end(); ++it) { ASSERT(it->GetPixelX() < 256); ASSERT(it->GetPixelZ() < 256); - Pkt.WriteByte(static_cast((it->GetType() << 4) | static_cast(it->GetRot() & 0xf))); - Pkt.WriteByte(static_cast(it->GetPixelX())); - Pkt.WriteByte(static_cast(it->GetPixelZ())); + Pkt.WriteBEUInt8(static_cast((it->GetType() << 4) | static_cast(it->GetRot() & 0xf))); + Pkt.WriteBEUInt8(static_cast(it->GetPixelX())); + Pkt.WriteBEUInt8(static_cast(it->GetPixelZ())); } } @@ -703,11 +703,11 @@ void cProtocol172::SendMapInfo(int a_MapID, unsigned int a_Scale) ASSERT(a_Scale < 256); cPacketizer Pkt(*this, 0x34); - Pkt.WriteVarInt(static_cast(a_MapID)); - Pkt.WriteShort (2); + Pkt.WriteVarInt32(static_cast(a_MapID)); + Pkt.WriteBEUInt16(2); - Pkt.WriteByte(2); - Pkt.WriteByte(static_cast(a_Scale)); + Pkt.WriteBEUInt8(2); + Pkt.WriteBEUInt8(static_cast(a_Scale)); } @@ -720,21 +720,21 @@ void cProtocol172::SendPickupSpawn(const cPickup & a_Pickup) { cPacketizer Pkt(*this, 0x0e); // Spawn Object packet - Pkt.WriteVarInt(a_Pickup.GetUniqueID()); - Pkt.WriteByte(2); // Type = Pickup + Pkt.WriteVarInt32(a_Pickup.GetUniqueID()); + Pkt.WriteBEUInt8(2); // Type = Pickup Pkt.WriteFPInt(a_Pickup.GetPosX()); Pkt.WriteFPInt(a_Pickup.GetPosY()); Pkt.WriteFPInt(a_Pickup.GetPosZ()); Pkt.WriteByteAngle(a_Pickup.GetYaw()); Pkt.WriteByteAngle(a_Pickup.GetPitch()); - Pkt.WriteInt(0); // No object data + Pkt.WriteBEInt32(0); // No object data } { cPacketizer Pkt(*this, 0x1c); // Entity Metadata packet - Pkt.WriteUInt32(a_Pickup.GetUniqueID()); - Pkt.WriteByte((0x05 << 5) | 10); // Slot type + index 10 + Pkt.WriteBEUInt32(a_Pickup.GetUniqueID()); + Pkt.WriteBEUInt8((0x05 << 5) | 10); // Slot type + index 10 WriteItem(Pkt, a_Pickup.GetItem()); - Pkt.WriteByte(0x7f); // End of metadata + Pkt.WriteBEUInt8(0x7f); // End of metadata } } @@ -762,9 +762,9 @@ void cProtocol172::SendPlayerAbilities(void) { Flags |= 0x04; } - Pkt.WriteByte(Flags); - Pkt.WriteFloat((float)(0.05 * Player->GetFlyingMaxSpeed())); - Pkt.WriteFloat((float)(0.1 * Player->GetNormalMaxSpeed())); + Pkt.WriteBEUInt8(Flags); + Pkt.WriteBEFloat(static_cast(0.05 * Player->GetFlyingMaxSpeed())); + Pkt.WriteBEFloat(static_cast(0.1 * Player->GetNormalMaxSpeed())); } @@ -776,8 +776,8 @@ void cProtocol172::SendEntityAnimation(const cEntity & a_Entity, char a_Animatio ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x0b); // Animation packet - Pkt.WriteVarInt(a_Entity.GetUniqueID()); - Pkt.WriteChar(a_Animation); + Pkt.WriteVarInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEInt8(a_Animation); } @@ -790,14 +790,14 @@ void cProtocol172::SendParticleEffect(const AString & a_ParticleName, float a_Sr cPacketizer Pkt(*this, 0x2A); Pkt.WriteString(a_ParticleName); - Pkt.WriteFloat(a_SrcX); - Pkt.WriteFloat(a_SrcY); - Pkt.WriteFloat(a_SrcZ); - Pkt.WriteFloat(a_OffsetX); - Pkt.WriteFloat(a_OffsetY); - Pkt.WriteFloat(a_OffsetZ); - Pkt.WriteFloat(a_ParticleData); - Pkt.WriteInt(a_ParticleAmount); + Pkt.WriteBEFloat(a_SrcX); + Pkt.WriteBEFloat(a_SrcY); + Pkt.WriteBEFloat(a_SrcZ); + Pkt.WriteBEFloat(a_OffsetX); + Pkt.WriteBEFloat(a_OffsetY); + Pkt.WriteBEFloat(a_OffsetZ); + Pkt.WriteBEFloat(a_ParticleData); + Pkt.WriteBEInt32(a_ParticleAmount); } @@ -811,7 +811,7 @@ void cProtocol172::SendPlayerListAddPlayer(const cPlayer & a_Player) cPacketizer Pkt(*this, 0x38); // Playerlist Item packet Pkt.WriteString(a_Player.GetPlayerListName()); Pkt.WriteBool(true); - Pkt.WriteShort(a_Player.GetClientHandle()->GetPing()); + Pkt.WriteBEInt16(a_Player.GetClientHandle()->GetPing()); } @@ -825,7 +825,7 @@ void cProtocol172::SendPlayerListRemovePlayer(const cPlayer & a_Player) cPacketizer Pkt(*this, 0x38); Pkt.WriteString(a_Player.GetPlayerListName()); Pkt.WriteBool(false); - Pkt.WriteShort(0); + Pkt.WriteBEInt16(0); } @@ -869,22 +869,22 @@ void cProtocol172::SendPlayerMaxSpeed(void) cPacketizer Pkt(*this, 0x20); // Entity Properties cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteUInt32(Player->GetUniqueID()); - Pkt.WriteInt(1); // Count + Pkt.WriteBEUInt32(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.WriteDouble(0.1 * Player->GetNormalMaxSpeed()); + Pkt.WriteBEDouble(0.1 * Player->GetNormalMaxSpeed()); if (Player->IsSprinting()) { - Pkt.WriteShort(1); // Modifier count - Pkt.WriteInt64(0x662a6b8dda3e4c1c); - Pkt.WriteInt64(static_cast(0x881396ea6097278d)); // UUID of the modifier - Pkt.WriteDouble(Player->GetSprintingMaxSpeed() - Player->GetNormalMaxSpeed()); - Pkt.WriteByte(2); + Pkt.WriteBEInt16(1); // Modifier count + Pkt.WriteBEUInt64(0x662a6b8dda3e4c1c); + Pkt.WriteBEUInt64(0x881396ea6097278d); // UUID of the modifier + Pkt.WriteBEDouble(Player->GetSprintingMaxSpeed() - Player->GetNormalMaxSpeed()); + Pkt.WriteBEUInt8(2); } else { - Pkt.WriteShort(0); // Modifier count + Pkt.WriteBEInt16(0); // Modifier count } } @@ -898,15 +898,15 @@ void cProtocol172::SendPlayerMoveLook(void) cPacketizer Pkt(*this, 0x08); // Player Position And Look packet cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteDouble(Player->GetPosX()); + Pkt.WriteBEDouble(Player->GetPosX()); // Protocol docs say this is PosY, but #323 says this is eye-pos // Moreover, the "+ 0.001" is there because otherwise the player falls through the block they were standing on. - Pkt.WriteDouble(Player->GetStance() + 0.001); + Pkt.WriteBEDouble(Player->GetStance() + 0.001); - Pkt.WriteDouble(Player->GetPosZ()); - Pkt.WriteFloat((float)Player->GetYaw()); - Pkt.WriteFloat((float)Player->GetPitch()); + Pkt.WriteBEDouble(Player->GetPosZ()); + Pkt.WriteBEFloat(static_cast(Player->GetYaw())); + Pkt.WriteBEFloat(static_cast(Player->GetPitch())); Pkt.WriteBool(Player->IsOnGround()); } @@ -930,7 +930,7 @@ void cProtocol172::SendPlayerSpawn(const cPlayer & a_Player) // Called to spawn another player for the client cPacketizer Pkt(*this, 0x0c); // Spawn Player packet - Pkt.WriteVarInt(a_Player.GetUniqueID()); + Pkt.WriteVarInt32(a_Player.GetUniqueID()); Pkt.WriteString(cMojangAPI::MakeUUIDDashed(a_Player.GetClientHandle()->GetUUID())); if (a_Player.HasCustomName()) { @@ -946,10 +946,10 @@ void cProtocol172::SendPlayerSpawn(const cPlayer & a_Player) Pkt.WriteByteAngle(a_Player.GetYaw()); Pkt.WriteByteAngle(a_Player.GetPitch()); short ItemType = a_Player.GetEquippedItem().IsEmpty() ? 0 : a_Player.GetEquippedItem().m_ItemType; - Pkt.WriteShort(ItemType); - Pkt.WriteByte((3 << 5) | 6); // Metadata: float + index 6 - Pkt.WriteFloat((float)a_Player.GetHealth()); - Pkt.WriteByte(0x7f); // Metadata: end + Pkt.WriteBEInt16(ItemType); + Pkt.WriteBEUInt8((3 << 5) | 6); // Metadata: float + index 6 + Pkt.WriteBEFloat(static_cast(a_Player.GetHealth())); + Pkt.WriteBEUInt8(0x7f); // Metadata: end } @@ -959,10 +959,11 @@ void cProtocol172::SendPlayerSpawn(const cPlayer & a_Player) void cProtocol172::SendPluginMessage(const AString & a_Channel, const AString & a_Message) { ASSERT(m_State == 3); // In game mode? + ASSERT(a_Message.size() <= std::numeric_limits::max()); cPacketizer Pkt(*this, 0x3f); Pkt.WriteString(a_Channel); - Pkt.WriteShort((short)a_Message.size()); + Pkt.WriteBEUInt16(static_cast(a_Message.size())); Pkt.WriteBuf(a_Message.data(), a_Message.size()); } @@ -976,8 +977,8 @@ void cProtocol172::SendRemoveEntityEffect(const cEntity & a_Entity, int a_Effect ASSERT((a_EffectID >= 0) && (a_EffectID < 256)); cPacketizer Pkt(*this, 0x1e); - Pkt.WriteUInt32(a_Entity.GetUniqueID()); - Pkt.WriteByte(static_cast(a_EffectID)); + Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEUInt8(static_cast(a_EffectID)); } @@ -994,9 +995,9 @@ void cProtocol172::SendRespawn(eDimension a_Dimension, bool a_ShouldIgnoreDimens cPacketizer Pkt(*this, 0x07); // Respawn packet cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteInt((int)a_Dimension); - Pkt.WriteByte(2); // TODO: Difficulty (set to Normal) - Pkt.WriteByte((Byte)Player->GetEffectiveGameMode()); + Pkt.WriteBEInt32((int)a_Dimension); + Pkt.WriteBEUInt8(2); // TODO: Difficulty (set to Normal) + Pkt.WriteBEUInt8((Byte)Player->GetEffectiveGameMode()); Pkt.WriteString("default"); m_LastSentDimension = a_Dimension; } @@ -1011,9 +1012,9 @@ void cProtocol172::SendExperience (void) cPacketizer Pkt(*this, 0x1f); // Experience Packet cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteFloat(Player->GetXpPercentage()); - Pkt.WriteShort(static_cast(std::max(Player->GetXpLevel(), std::numeric_limits::max()))); - Pkt.WriteShort(static_cast(std::max(Player->GetCurrentXp(), std::numeric_limits::max()))); + Pkt.WriteBEFloat(Player->GetXpPercentage()); + Pkt.WriteBEInt16(static_cast(std::max(Player->GetXpLevel(), std::numeric_limits::max()))); + Pkt.WriteBEInt16(static_cast(std::max(Player->GetCurrentXp(), std::numeric_limits::max()))); } @@ -1026,11 +1027,11 @@ void cProtocol172::SendExperienceOrb(const cExpOrb & a_ExpOrb) ASSERT((a_ExpOrb.GetReward() >= 0) && (a_ExpOrb.GetReward() < SHRT_MAX)); cPacketizer Pkt(*this, 0x11); - Pkt.WriteVarInt(a_ExpOrb.GetUniqueID()); + Pkt.WriteVarInt32(a_ExpOrb.GetUniqueID()); Pkt.WriteFPInt(a_ExpOrb.GetPosX()); Pkt.WriteFPInt(a_ExpOrb.GetPosY()); Pkt.WriteFPInt(a_ExpOrb.GetPosZ()); - Pkt.WriteShort(static_cast(a_ExpOrb.GetReward())); + Pkt.WriteBEInt16(static_cast(a_ExpOrb.GetReward())); } @@ -1044,7 +1045,7 @@ void cProtocol172::SendScoreboardObjective(const AString & a_Name, const AString cPacketizer Pkt(*this, 0x3b); Pkt.WriteString(a_Name); Pkt.WriteString(a_DisplayName); - Pkt.WriteByte(a_Mode); + Pkt.WriteBEUInt8(a_Mode); } @@ -1057,12 +1058,12 @@ void cProtocol172::SendScoreUpdate(const AString & a_Objective, const AString & cPacketizer Pkt(*this, 0x3c); Pkt.WriteString(a_Player); - Pkt.WriteByte(a_Mode); + Pkt.WriteBEUInt8(a_Mode); if (a_Mode != 1) { Pkt.WriteString(a_Objective); - Pkt.WriteInt((int) a_Score); + Pkt.WriteBEInt32((int) a_Score); } } @@ -1075,7 +1076,7 @@ void cProtocol172::SendDisplayObjective(const AString & a_Objective, cScoreboard ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x3d); - Pkt.WriteByte(static_cast(a_Display)); + Pkt.WriteBEUInt8(static_cast(a_Display)); Pkt.WriteString(a_Objective); } @@ -1089,11 +1090,11 @@ void cProtocol172::SendSoundEffect(const AString & a_SoundName, double a_X, doub cPacketizer Pkt(*this, 0x29); // Sound Effect packet Pkt.WriteString(a_SoundName); - Pkt.WriteInt(static_cast(a_X * 8.0)); - Pkt.WriteInt(static_cast(a_Y * 8.0)); - Pkt.WriteInt(static_cast(a_Z * 8.0)); - Pkt.WriteFloat(a_Volume); - Pkt.WriteByte(static_cast(a_Pitch * 63)); + Pkt.WriteBEInt32(static_cast(a_X * 8.0)); + Pkt.WriteBEInt32(static_cast(a_Y * 8.0)); + Pkt.WriteBEInt32(static_cast(a_Z * 8.0)); + Pkt.WriteBEFloat(a_Volume); + Pkt.WriteBEUInt8(static_cast(a_Pitch * 63)); } @@ -1106,11 +1107,11 @@ void cProtocol172::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_Src ASSERT((a_SrcY >= 0) && (a_SrcY < 256)); cPacketizer Pkt(*this, 0x28); // Effect packet - Pkt.WriteInt(a_EffectID); - Pkt.WriteInt(a_SrcX); - Pkt.WriteByte(static_cast(a_SrcY)); - Pkt.WriteInt(a_SrcZ); - Pkt.WriteInt(a_Data); + Pkt.WriteBEInt32(a_EffectID); + Pkt.WriteBEInt32(a_SrcX); + Pkt.WriteBEUInt8(static_cast(a_SrcY)); + Pkt.WriteBEInt32(a_SrcZ); + Pkt.WriteBEInt32(a_Data); Pkt.WriteBool(false); } @@ -1123,17 +1124,17 @@ void cProtocol172::SendSpawnFallingBlock(const cFallingBlock & a_FallingBlock) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x0e); // Spawn Object packet - Pkt.WriteVarInt(a_FallingBlock.GetUniqueID()); - Pkt.WriteByte(70); // Falling block + Pkt.WriteVarInt32(a_FallingBlock.GetUniqueID()); + Pkt.WriteBEUInt8(70); // Falling block Pkt.WriteFPInt(a_FallingBlock.GetPosX()); Pkt.WriteFPInt(a_FallingBlock.GetPosY()); Pkt.WriteFPInt(a_FallingBlock.GetPosZ()); Pkt.WriteByteAngle(a_FallingBlock.GetYaw()); Pkt.WriteByteAngle(a_FallingBlock.GetPitch()); - Pkt.WriteInt((static_cast(a_FallingBlock.GetBlockType()) | ((static_cast(a_FallingBlock.GetBlockMeta()) << 16)))); - Pkt.WriteShort(static_cast(a_FallingBlock.GetSpeedX() * 400)); - Pkt.WriteShort(static_cast(a_FallingBlock.GetSpeedY() * 400)); - Pkt.WriteShort(static_cast(a_FallingBlock.GetSpeedZ() * 400)); + Pkt.WriteBEInt32((static_cast(a_FallingBlock.GetBlockType()) | ((static_cast(a_FallingBlock.GetBlockMeta()) << 16)))); + Pkt.WriteBEInt16(static_cast(a_FallingBlock.GetSpeedX() * 400)); + Pkt.WriteBEInt16(static_cast(a_FallingBlock.GetSpeedY() * 400)); + Pkt.WriteBEInt16(static_cast(a_FallingBlock.GetSpeedZ() * 400)); } @@ -1145,19 +1146,19 @@ void cProtocol172::SendSpawnMob(const cMonster & a_Mob) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x0f); // Spawn Mob packet - Pkt.WriteVarInt(a_Mob.GetUniqueID()); - Pkt.WriteByte((Byte)a_Mob.GetMobType()); + Pkt.WriteVarInt32(a_Mob.GetUniqueID()); + Pkt.WriteBEUInt8((Byte)a_Mob.GetMobType()); Pkt.WriteFPInt(a_Mob.GetPosX()); Pkt.WriteFPInt(a_Mob.GetPosY()); Pkt.WriteFPInt(a_Mob.GetPosZ()); Pkt.WriteByteAngle(a_Mob.GetPitch()); Pkt.WriteByteAngle(a_Mob.GetHeadYaw()); Pkt.WriteByteAngle(a_Mob.GetYaw()); - Pkt.WriteShort(static_cast(a_Mob.GetSpeedX() * 400)); - Pkt.WriteShort(static_cast(a_Mob.GetSpeedY() * 400)); - Pkt.WriteShort(static_cast(a_Mob.GetSpeedZ() * 400)); + Pkt.WriteBEInt16(static_cast(a_Mob.GetSpeedX() * 400)); + Pkt.WriteBEInt16(static_cast(a_Mob.GetSpeedY() * 400)); + Pkt.WriteBEInt16(static_cast(a_Mob.GetSpeedZ() * 400)); WriteEntityMetadata(Pkt, a_Mob); - Pkt.WriteByte(0x7f); // Metadata terminator + Pkt.WriteBEUInt8(0x7f); // Metadata terminator } @@ -1169,19 +1170,19 @@ void cProtocol172::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0xe); // Spawn Object packet - Pkt.WriteVarInt(a_Entity.GetUniqueID()); - Pkt.WriteChar(a_ObjectType); + Pkt.WriteVarInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEInt8(a_ObjectType); Pkt.WriteFPInt(a_Entity.GetPosX()); Pkt.WriteFPInt(a_Entity.GetPosY()); Pkt.WriteFPInt(a_Entity.GetPosZ()); Pkt.WriteByteAngle(a_Entity.GetPitch()); Pkt.WriteByteAngle(a_Entity.GetYaw()); - Pkt.WriteInt(a_ObjectData); + Pkt.WriteBEInt32(a_ObjectData); if (a_ObjectData != 0) { - Pkt.WriteShort(static_cast(a_Entity.GetSpeedX() * 400)); - Pkt.WriteShort(static_cast(a_Entity.GetSpeedY() * 400)); - Pkt.WriteShort(static_cast(a_Entity.GetSpeedZ() * 400)); + Pkt.WriteBEInt16(static_cast(a_Entity.GetSpeedX() * 400)); + Pkt.WriteBEInt16(static_cast(a_Entity.GetSpeedY() * 400)); + Pkt.WriteBEInt16(static_cast(a_Entity.GetSpeedZ() * 400)); } } @@ -1194,19 +1195,19 @@ void cProtocol172::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleTyp ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0xe); // Spawn Object packet - Pkt.WriteVarInt(a_Vehicle.GetUniqueID()); - Pkt.WriteChar(a_VehicleType); + Pkt.WriteVarInt32(a_Vehicle.GetUniqueID()); + Pkt.WriteBEInt8(a_VehicleType); Pkt.WriteFPInt(a_Vehicle.GetPosX()); Pkt.WriteFPInt(a_Vehicle.GetPosY()); Pkt.WriteFPInt(a_Vehicle.GetPosZ()); Pkt.WriteByteAngle(a_Vehicle.GetPitch()); Pkt.WriteByteAngle(a_Vehicle.GetYaw()); - Pkt.WriteInt(a_VehicleSubType); + Pkt.WriteBEInt32(a_VehicleSubType); if (a_VehicleSubType != 0) { - Pkt.WriteShort(static_cast(a_Vehicle.GetSpeedX() * 400)); - Pkt.WriteShort(static_cast(a_Vehicle.GetSpeedY() * 400)); - Pkt.WriteShort(static_cast(a_Vehicle.GetSpeedZ() * 400)); + Pkt.WriteBEInt16(static_cast(a_Vehicle.GetSpeedX() * 400)); + Pkt.WriteBEInt16(static_cast(a_Vehicle.GetSpeedY() * 400)); + Pkt.WriteBEInt16(static_cast(a_Vehicle.GetSpeedZ() * 400)); } } @@ -1219,7 +1220,7 @@ void cProtocol172::SendStatistics(const cStatManager & a_Manager) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x37); - Pkt.WriteVarInt(statCount); // TODO 2014-05-11 xdot: Optimization: Send "dirty" statistics only + Pkt.WriteVarInt32(statCount); // TODO 2014-05-11 xdot: Optimization: Send "dirty" statistics only for (size_t i = 0; i < (size_t)statCount; ++i) { @@ -1227,7 +1228,7 @@ void cProtocol172::SendStatistics(const cStatManager & a_Manager) const AString & StatName = cStatInfo::GetName((eStatistic) i); Pkt.WriteString(StatName); - Pkt.WriteVarInt(static_cast(Value)); + Pkt.WriteVarInt32(static_cast(Value)); } } @@ -1240,7 +1241,7 @@ void cProtocol172::SendTabCompletionResults(const AStringVector & a_Results) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x3a); // Tab-Complete packet - Pkt.WriteVarInt(static_cast(a_Results.size())); + Pkt.WriteVarInt32(static_cast(a_Results.size())); for (AStringVector::const_iterator itr = a_Results.begin(), end = a_Results.end(); itr != end; ++itr) { @@ -1257,7 +1258,7 @@ void cProtocol172::SendTeleportEntity(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x18); - Pkt.WriteUInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); Pkt.WriteFPInt(a_Entity.GetPosX()); Pkt.WriteFPInt(a_Entity.GetPosY()); Pkt.WriteFPInt(a_Entity.GetPosZ()); @@ -1274,8 +1275,8 @@ void cProtocol172::SendThunderbolt(int a_BlockX, int a_BlockY, int a_BlockZ) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x2c); // Spawn Global Entity packet - Pkt.WriteVarInt(0); // EntityID = 0, always - Pkt.WriteByte(1); // Type = Thunderbolt + Pkt.WriteVarInt32(0); // EntityID = 0, always + Pkt.WriteBEUInt8(1); // Type = Thunderbolt Pkt.WriteFPInt(a_BlockX); Pkt.WriteFPInt(a_BlockY); Pkt.WriteFPInt(a_BlockZ); @@ -1295,8 +1296,8 @@ void cProtocol172::SendTimeUpdate(Int64 a_WorldAge, Int64 a_TimeOfDay, bool a_Do } cPacketizer Pkt(*this, 0x03); - Pkt.WriteInt64(a_WorldAge); - Pkt.WriteInt64(a_TimeOfDay); + Pkt.WriteBEInt64(a_WorldAge); + Pkt.WriteBEInt64(a_TimeOfDay); } @@ -1308,12 +1309,12 @@ void cProtocol172::SendUnloadChunk(int a_ChunkX, int a_ChunkZ) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x21); // Chunk Data packet - Pkt.WriteInt(a_ChunkX); - Pkt.WriteInt(a_ChunkZ); + Pkt.WriteBEInt32(a_ChunkX); + Pkt.WriteBEInt32(a_ChunkZ); Pkt.WriteBool(true); - Pkt.WriteShort(0); // Primary bitmap - Pkt.WriteShort(0); // Add bitmap - Pkt.WriteInt(0); // Compressed data size + Pkt.WriteBEInt16(0); // Primary bitmap + Pkt.WriteBEInt16(0); // Add bitmap + Pkt.WriteBEInt32(0); // Compressed data size } @@ -1324,9 +1325,9 @@ void cProtocol172::SendUpdateBlockEntity(cBlockEntity & a_BlockEntity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x35); // Update tile entity packet - Pkt.WriteInt(a_BlockEntity.GetPosX()); - Pkt.WriteShort(static_cast(a_BlockEntity.GetPosY())); - Pkt.WriteInt(a_BlockEntity.GetPosZ()); + Pkt.WriteBEInt32(a_BlockEntity.GetPosX()); + Pkt.WriteBEInt16(static_cast(a_BlockEntity.GetPosY())); + Pkt.WriteBEInt32(a_BlockEntity.GetPosZ()); Byte Action = 0; switch (a_BlockEntity.GetBlockType()) @@ -1338,7 +1339,7 @@ void cProtocol172::SendUpdateBlockEntity(cBlockEntity & a_BlockEntity) case E_BLOCK_FLOWER_POT: Action = 5; break; // Update flower pot default: ASSERT(!"Unhandled or unimplemented BlockEntity update request!"); break; } - Pkt.WriteByte(Action); + Pkt.WriteBEUInt8(Action); WriteBlockEntity(Pkt, a_BlockEntity); } @@ -1353,9 +1354,9 @@ void cProtocol172::SendUpdateSign(int a_BlockX, int a_BlockY, int a_BlockZ, cons ASSERT((a_BlockY >= 0) && (a_BlockY < cChunkDef::Height)); cPacketizer Pkt(*this, 0x33); - Pkt.WriteInt(a_BlockX); - Pkt.WriteShort((short)a_BlockY); - Pkt.WriteInt(a_BlockZ); + Pkt.WriteBEInt32(a_BlockX); + Pkt.WriteBEInt16(static_cast(a_BlockY)); + Pkt.WriteBEInt32(a_BlockZ); // Need to send only up to 15 chars, otherwise the client crashes (#598) Pkt.WriteString(a_Line1.substr(0, 15)); Pkt.WriteString(a_Line2.substr(0, 15)); @@ -1373,10 +1374,10 @@ void cProtocol172::SendUseBed(const cEntity & a_Entity, int a_BlockX, int a_Bloc ASSERT((a_BlockY >= 0) && (a_BlockY < cChunkDef::Height)); cPacketizer Pkt(*this, 0x0a); - Pkt.WriteUInt32(a_Entity.GetUniqueID()); - Pkt.WriteInt(a_BlockX); - Pkt.WriteByte(static_cast(a_BlockY)); - Pkt.WriteInt(a_BlockZ); + Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEInt32(a_BlockX); + Pkt.WriteBEUInt8(static_cast(a_BlockY)); + Pkt.WriteBEInt32(a_BlockZ); } @@ -1389,8 +1390,8 @@ void cProtocol172::SendWeather(eWeather a_Weather) { cPacketizer Pkt(*this, 0x2b); // Change Game State packet - Pkt.WriteByte((a_Weather == wSunny) ? 1 : 2); // End rain / begin rain - Pkt.WriteFloat(0); // Unused for weather + Pkt.WriteBEUInt8((a_Weather == wSunny) ? 1 : 2); // End rain / begin rain + Pkt.WriteBEFloat(0); // Unused for weather } // TODO: Fade effect, somehow @@ -1405,8 +1406,8 @@ void cProtocol172::SendWholeInventory(const cWindow & a_Window) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x30); // Window Items packet - Pkt.WriteChar(a_Window.GetWindowID()); - Pkt.WriteShort(static_cast(a_Window.GetNumSlots())); + Pkt.WriteBEInt8(a_Window.GetWindowID()); + Pkt.WriteBEInt16(static_cast(a_Window.GetNumSlots())); cItems Slots; a_Window.GetSlots(*(m_Client->GetPlayer()), Slots); for (cItems::const_iterator itr = Slots.begin(), end = Slots.end(); itr != end; ++itr) @@ -1424,7 +1425,7 @@ void cProtocol172::SendWindowClose(const cWindow & a_Window) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x2e); - Pkt.WriteChar(a_Window.GetWindowID()); + Pkt.WriteBEInt8(a_Window.GetWindowID()); } @@ -1442,14 +1443,14 @@ void cProtocol172::SendWindowOpen(const cWindow & a_Window) } cPacketizer Pkt(*this, 0x2d); - Pkt.WriteChar(a_Window.GetWindowID()); - Pkt.WriteChar(static_cast(a_Window.GetWindowType())); + Pkt.WriteBEInt8(a_Window.GetWindowID()); + Pkt.WriteBEInt8(static_cast(a_Window.GetWindowType())); Pkt.WriteString(a_Window.GetWindowTitle()); - Pkt.WriteChar(static_cast(a_Window.GetNumNonInventorySlots())); + Pkt.WriteBEInt8(static_cast(a_Window.GetNumNonInventorySlots())); Pkt.WriteBool(true); if (a_Window.GetWindowType() == cWindow::wtAnimalChest) { - Pkt.WriteInt(0); // TODO: The animal's EntityID + Pkt.WriteBEInt32(0); // TODO: The animal's EntityID } } @@ -1462,9 +1463,9 @@ void cProtocol172::SendWindowProperty(const cWindow & a_Window, short a_Property ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x31); // Window Property packet - Pkt.WriteChar(a_Window.GetWindowID()); - Pkt.WriteShort(a_Property); - Pkt.WriteShort(a_Value); + Pkt.WriteBEInt8(a_Window.GetWindowID()); + Pkt.WriteBEInt16(a_Property); + Pkt.WriteBEInt16(a_Value); } @@ -1709,7 +1710,7 @@ void cProtocol172::HandlePacketStatusPing(cByteBuffer & a_ByteBuffer) HANDLE_READ(a_ByteBuffer, ReadBEInt64, Int64, Timestamp); cPacketizer Pkt(*this, 0x01); // Ping packet - Pkt.WriteInt64(Timestamp); + Pkt.WriteBEInt64(Timestamp); } @@ -1858,10 +1859,10 @@ void cProtocol172::HandlePacketLoginStart(cByteBuffer & a_ByteBuffer) cPacketizer Pkt(*this, 0x01); Pkt.WriteString(Server->GetServerID()); const AString & PubKeyDer = Server->GetPublicKeyDER(); - Pkt.WriteShort((short)PubKeyDer.size()); + Pkt.WriteBEUInt16(static_cast(PubKeyDer.size())); Pkt.WriteBuf(PubKeyDer.data(), PubKeyDer.size()); - Pkt.WriteShort(4); - Pkt.WriteInt((int)(intptr_t)this); // Using 'this' as the cryptographic nonce, so that we don't have to generate one each time :) + Pkt.WriteBEInt16(4); + Pkt.WriteBEUInt32(static_cast(reinterpret_cast(this))); // Using 'this' as the cryptographic nonce, so that we don't have to generate one each time :) m_Client->SetUsername(Username); return; } @@ -2389,7 +2390,7 @@ void cProtocol172::SendPacket(cPacketizer & a_Packet) // Send the packet length UInt32 PacketLen = static_cast(m_OutPacketBuffer.GetUsedSpace()); - m_OutPacketLenBuffer.WriteVarInt(PacketLen); + m_OutPacketLenBuffer.WriteVarInt32(PacketLen); m_OutPacketLenBuffer.ReadAll(DataToSend); SendData(DataToSend.data(), DataToSend.size()); m_OutPacketLenBuffer.CommitRead(); @@ -2586,17 +2587,17 @@ void cProtocol172::WriteItem(cPacketizer & a_Pkt, const cItem & a_Item) if (a_Item.IsEmpty()) { - a_Pkt.WriteShort(-1); + a_Pkt.WriteBEInt16(-1); return; } - a_Pkt.WriteShort(ItemType); - a_Pkt.WriteChar (a_Item.m_ItemCount); - a_Pkt.WriteShort(a_Item.m_ItemDamage); + a_Pkt.WriteBEInt16(ItemType); + a_Pkt.WriteBEInt8(a_Item.m_ItemCount); + a_Pkt.WriteBEInt16(a_Item.m_ItemDamage); if (a_Item.m_Enchantments.IsEmpty() && a_Item.IsBothNameAndLoreEmpty() && (a_Item.m_ItemType != E_ITEM_FIREWORK_ROCKET) && (a_Item.m_ItemType != E_ITEM_FIREWORK_STAR)) { - a_Pkt.WriteShort(-1); + a_Pkt.WriteBEInt16(-1); return; } @@ -2772,21 +2773,21 @@ void cProtocol172::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_En { Flags |= 0x20; } - a_Pkt.WriteByte(0); // Byte(0) + index 0 - a_Pkt.WriteByte(Flags); + a_Pkt.WriteBEUInt8(0); // Byte(0) + index 0 + a_Pkt.WriteBEUInt8(Flags); switch (a_Entity.GetEntityType()) { case cEntity::etPlayer: break; // TODO? case cEntity::etPickup: { - a_Pkt.WriteByte((5 << 5) | 10); // Slot(5) + index 10 + a_Pkt.WriteBEUInt8((5 << 5) | 10); // Slot(5) + index 10 WriteItem(a_Pkt, reinterpret_cast(a_Entity).GetItem()); break; } case cEntity::etMinecart: { - a_Pkt.WriteByte(0x51); + a_Pkt.WriteBEUInt8(0x51); // The following expression makes Minecarts shake more with less health or higher damage taken // It gets half the maximum health, and takes it away from the current health minus the half health: @@ -2796,11 +2797,11 @@ void cProtocol172::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_En Health: 1 | 3 - (1 - 3) = 5 */ auto & Minecart = reinterpret_cast(a_Entity); - a_Pkt.WriteInt((((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * Minecart.LastDamage()) * 4); - a_Pkt.WriteByte(0x52); - a_Pkt.WriteInt(1); // Shaking direction, doesn't seem to affect anything - a_Pkt.WriteByte(0x73); - a_Pkt.WriteFloat(static_cast(Minecart.LastDamage() + 10)); // Damage taken / shake effect multiplyer + a_Pkt.WriteBEInt32((((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * Minecart.LastDamage()) * 4); + a_Pkt.WriteBEUInt8(0x52); + a_Pkt.WriteBEInt32(1); // Shaking direction, doesn't seem to affect anything + a_Pkt.WriteBEUInt8(0x73); + a_Pkt.WriteBEFloat(static_cast(Minecart.LastDamage() + 10)); // Damage taken / shake effect multiplyer if (Minecart.GetPayload() == cMinecart::mpNone) { @@ -2808,20 +2809,20 @@ void cProtocol172::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_En const cItem & MinecartContent = RideableMinecart.GetContent(); if (!MinecartContent.IsEmpty()) { - a_Pkt.WriteByte(0x54); + a_Pkt.WriteBEUInt8(0x54); int Content = MinecartContent.m_ItemType; Content |= MinecartContent.m_ItemDamage << 8; - a_Pkt.WriteInt(Content); - a_Pkt.WriteByte(0x55); - a_Pkt.WriteInt(RideableMinecart.GetBlockHeight()); - a_Pkt.WriteByte(0x56); - a_Pkt.WriteByte(1); + a_Pkt.WriteBEInt32(Content); + a_Pkt.WriteBEUInt8(0x55); + a_Pkt.WriteBEInt32(RideableMinecart.GetBlockHeight()); + a_Pkt.WriteBEUInt8(0x56); + a_Pkt.WriteBEUInt8(1); } } else if (Minecart.GetPayload() == cMinecart::mpFurnace) { - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte(reinterpret_cast(Minecart).IsFueled() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8(reinterpret_cast(Minecart).IsFueled() ? 1 : 0); } break; } // case etMinecart @@ -2833,13 +2834,13 @@ void cProtocol172::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_En { case cProjectileEntity::pkArrow: { - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte(reinterpret_cast(a_Entity).IsCritical() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8(reinterpret_cast(a_Entity).IsCritical() ? 1 : 0); break; } case cProjectileEntity::pkFirework: { - a_Pkt.WriteByte(0xa8); + a_Pkt.WriteBEUInt8(0xa8); WriteItem(a_Pkt, reinterpret_cast(a_Entity).GetItem()); break; } @@ -2857,10 +2858,10 @@ void cProtocol172::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_En case cEntity::etItemFrame: { auto & Frame = reinterpret_cast(a_Entity); - a_Pkt.WriteByte(0xa2); + a_Pkt.WriteBEUInt8(0xa2); WriteItem(a_Pkt, Frame.GetItem()); - a_Pkt.WriteByte(0x03); - a_Pkt.WriteByte(Frame.GetItemRotation() / 2); + a_Pkt.WriteBEUInt8(0x03); + a_Pkt.WriteBEUInt8(Frame.GetItemRotation() / 2); break; } // case etItemFrame @@ -2881,36 +2882,36 @@ void cProtocol172::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_Mob) { case mtBat: { - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte(reinterpret_cast(a_Mob).IsHanging() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8(reinterpret_cast(a_Mob).IsHanging() ? 1 : 0); break; } // case mtBat case mtCreeper: { - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte(reinterpret_cast(a_Mob).IsBlowing() ? 1 : 0); - a_Pkt.WriteByte(0x11); - a_Pkt.WriteByte(reinterpret_cast(a_Mob).IsCharged() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8(reinterpret_cast(a_Mob).IsBlowing() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x11); + a_Pkt.WriteBEUInt8(reinterpret_cast(a_Mob).IsCharged() ? 1 : 0); break; } // case mtCreeper case mtEnderman: { auto & Enderman = reinterpret_cast(a_Mob); - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte((Byte)(Enderman.GetCarriedBlock())); - a_Pkt.WriteByte(0x11); - a_Pkt.WriteByte((Byte)(Enderman.GetCarriedMeta())); - a_Pkt.WriteByte(0x12); - a_Pkt.WriteByte(Enderman.IsScreaming() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8((Byte)(Enderman.GetCarriedBlock())); + a_Pkt.WriteBEUInt8(0x11); + a_Pkt.WriteBEUInt8((Byte)(Enderman.GetCarriedMeta())); + a_Pkt.WriteBEUInt8(0x12); + a_Pkt.WriteBEUInt8(Enderman.IsScreaming() ? 1 : 0); break; } // case mtEnderman case mtGhast: { - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte(reinterpret_cast(a_Mob).IsCharging()); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8(reinterpret_cast(a_Mob).IsCharging()); break; } // case mtGhast @@ -2946,81 +2947,81 @@ void cProtocol172::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_Mob) { Flags |= 0x80; } - a_Pkt.WriteByte(0x50); // Int at index 16 - a_Pkt.WriteInt(Flags); - a_Pkt.WriteByte(0x13); // Byte at index 19 - a_Pkt.WriteByte(static_cast(Horse.GetHorseType())); - a_Pkt.WriteByte(0x54); // Int at index 20 + a_Pkt.WriteBEUInt8(0x50); // Int at index 16 + a_Pkt.WriteBEInt32(Flags); + a_Pkt.WriteBEUInt8(0x13); // Byte at index 19 + a_Pkt.WriteBEUInt8(static_cast(Horse.GetHorseType())); + a_Pkt.WriteBEUInt8(0x54); // Int at index 20 int Appearance = 0; Appearance = Horse.GetHorseColor(); Appearance |= Horse.GetHorseStyle() << 8; - a_Pkt.WriteInt(Appearance); - a_Pkt.WriteByte(0x56); // Int at index 22 - a_Pkt.WriteInt(Horse.GetHorseArmour()); + a_Pkt.WriteBEInt32(Appearance); + a_Pkt.WriteBEUInt8(0x56); // Int at index 22 + a_Pkt.WriteBEInt32(Horse.GetHorseArmour()); break; } // case mtHorse case mtMagmaCube: { - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte(static_cast(reinterpret_cast(a_Mob).GetSize())); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8(static_cast(reinterpret_cast(a_Mob).GetSize())); break; } // case mtMagmaCube case mtPig: { - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte(reinterpret_cast(a_Mob).IsSaddled() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8(reinterpret_cast(a_Mob).IsSaddled() ? 1 : 0); break; } // case mtPig case mtSheep: { - a_Pkt.WriteByte(0x10); + a_Pkt.WriteBEUInt8(0x10); auto & Sheep = reinterpret_cast(a_Mob); Byte SheepMetadata = static_cast(Sheep.GetFurColor() & 0x0f); if (Sheep.IsSheared()) { SheepMetadata |= 0x10; } - a_Pkt.WriteByte(SheepMetadata); + a_Pkt.WriteBEUInt8(SheepMetadata); break; } // case mtSheep case mtSkeleton: { - a_Pkt.WriteByte(0x0d); - a_Pkt.WriteByte(reinterpret_cast(a_Mob).IsWither() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x0d); + a_Pkt.WriteBEUInt8(reinterpret_cast(a_Mob).IsWither() ? 1 : 0); break; } // case mtSkeleton case mtSlime: { - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte(static_cast(reinterpret_cast(a_Mob).GetSize())); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8(static_cast(reinterpret_cast(a_Mob).GetSize())); break; } // case mtSlime case mtVillager: { - a_Pkt.WriteByte(0x50); - a_Pkt.WriteInt(reinterpret_cast(a_Mob).GetVilType()); + a_Pkt.WriteBEUInt8(0x50); + a_Pkt.WriteBEInt32(reinterpret_cast(a_Mob).GetVilType()); break; } // case mtVillager case mtWitch: { - a_Pkt.WriteByte(0x15); - a_Pkt.WriteByte(reinterpret_cast(a_Mob).IsAngry() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x15); + a_Pkt.WriteBEUInt8(reinterpret_cast(a_Mob).IsAngry() ? 1 : 0); break; } // case mtWitch case mtWither: { - a_Pkt.WriteByte(0x54); // Int at index 20 - a_Pkt.WriteInt(static_cast(reinterpret_cast(a_Mob).GetWitherInvulnerableTicks())); - a_Pkt.WriteByte(0x66); // Float at index 6 - a_Pkt.WriteFloat(static_cast(a_Mob.GetHealth())); + a_Pkt.WriteBEUInt8(0x54); // Int at index 20 + a_Pkt.WriteBEInt32(static_cast(reinterpret_cast(a_Mob).GetWitherInvulnerableTicks())); + a_Pkt.WriteBEUInt8(0x66); // Float at index 6 + a_Pkt.WriteBEFloat(static_cast(a_Mob.GetHealth())); break; } // case mtWither @@ -3040,27 +3041,27 @@ void cProtocol172::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_Mob) { WolfStatus |= 0x4; } - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte(WolfStatus); - - a_Pkt.WriteByte(0x72); - a_Pkt.WriteFloat(static_cast(a_Mob.GetHealth())); - a_Pkt.WriteByte(0x13); - a_Pkt.WriteByte(Wolf.IsBegging() ? 1 : 0); - a_Pkt.WriteByte(0x14); - a_Pkt.WriteByte(static_cast(Wolf.GetCollarColor())); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8(WolfStatus); + + a_Pkt.WriteBEUInt8(0x72); + a_Pkt.WriteBEFloat(static_cast(a_Mob.GetHealth())); + a_Pkt.WriteBEUInt8(0x13); + a_Pkt.WriteBEUInt8(Wolf.IsBegging() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x14); + a_Pkt.WriteBEUInt8(static_cast(Wolf.GetCollarColor())); break; } // case mtWolf case mtZombie: { auto & Zombie = reinterpret_cast(a_Mob); - a_Pkt.WriteByte(0x0c); - a_Pkt.WriteByte(Zombie.IsBaby() ? 1 : 0); - a_Pkt.WriteByte(0x0d); - a_Pkt.WriteByte(Zombie.IsVillagerZombie() ? 1 : 0); - a_Pkt.WriteByte(0x0e); - a_Pkt.WriteByte(Zombie.IsConverting() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x0c); + a_Pkt.WriteBEUInt8(Zombie.IsBaby() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x0d); + a_Pkt.WriteBEUInt8(Zombie.IsVillagerZombie() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x0e); + a_Pkt.WriteBEUInt8(Zombie.IsConverting() ? 1 : 0); break; } // case mtZombie @@ -3074,10 +3075,10 @@ void cProtocol172::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_Mob) // Custom name: if (a_Mob.HasCustomName()) { - a_Pkt.WriteByte(0x8a); + a_Pkt.WriteBEUInt8(0x8a); a_Pkt.WriteString(a_Mob.GetCustomName()); - a_Pkt.WriteByte(0x0b); - a_Pkt.WriteByte(a_Mob.IsCustomNameAlwaysVisible() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x0b); + a_Pkt.WriteBEUInt8(a_Mob.IsCustomNameAlwaysVisible() ? 1 : 0); } } @@ -3090,7 +3091,7 @@ void cProtocol172::WriteEntityProperties(cPacketizer & a_Pkt, const cEntity & a_ if (!a_Entity.IsMob()) { // No properties for anything else than mobs - a_Pkt.WriteInt(0); + a_Pkt.WriteBEInt32(0); return; } @@ -3098,7 +3099,7 @@ void cProtocol172::WriteEntityProperties(cPacketizer & a_Pkt, const cEntity & a_ // TODO: Send properties and modifiers based on the mob type - a_Pkt.WriteInt(0); // NumProperties + a_Pkt.WriteBEInt32(0); // NumProperties } @@ -3121,7 +3122,7 @@ void cProtocol176::SendPlayerSpawn(const cPlayer & a_Player) { // Called to spawn another player for the client cPacketizer Pkt(*this, 0x0c); // Spawn Player packet - Pkt.WriteVarInt(a_Player.GetUniqueID()); + Pkt.WriteVarInt32(a_Player.GetUniqueID()); Pkt.WriteString(cMojangAPI::MakeUUIDDashed(a_Player.GetClientHandle()->GetUUID())); if (a_Player.HasCustomName()) { @@ -3133,7 +3134,7 @@ void cProtocol176::SendPlayerSpawn(const cPlayer & a_Player) } const Json::Value & Properties = a_Player.GetClientHandle()->GetProperties(); - Pkt.WriteVarInt(Properties.size()); + Pkt.WriteVarInt32(Properties.size()); for (Json::Value::iterator itr = Properties.begin(), end = Properties.end(); itr != end; ++itr) { @@ -3148,10 +3149,10 @@ void cProtocol176::SendPlayerSpawn(const cPlayer & a_Player) Pkt.WriteByteAngle(a_Player.GetYaw()); Pkt.WriteByteAngle(a_Player.GetPitch()); short ItemType = a_Player.GetEquippedItem().IsEmpty() ? 0 : a_Player.GetEquippedItem().m_ItemType; - Pkt.WriteShort(ItemType); - Pkt.WriteByte((3 << 5) | 6); // Metadata: float + index 6 - Pkt.WriteFloat((float)a_Player.GetHealth()); - Pkt.WriteByte(0x7f); // Metadata: end + Pkt.WriteBEInt16(ItemType); + Pkt.WriteBEUInt8((3 << 5) | 6); // Metadata: float + index 6 + Pkt.WriteBEFloat(static_cast(a_Player.GetHealth())); + Pkt.WriteBEUInt8(0x7f); // Metadata: end } diff --git a/src/Protocol/Protocol18x.cpp b/src/Protocol/Protocol18x.cpp index c48ab14ad..a1ca25200 100644 --- a/src/Protocol/Protocol18x.cpp +++ b/src/Protocol/Protocol18x.cpp @@ -161,8 +161,8 @@ void cProtocol180::SendAttachEntity(const cEntity & a_Entity, const cEntity * a_ ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x1b); // Attach Entity packet - Pkt.WriteUInt32(a_Entity.GetUniqueID()); - Pkt.WriteUInt32((a_Vehicle != nullptr) ? a_Vehicle->GetUniqueID() : 0); + Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEUInt32((a_Vehicle != nullptr) ? a_Vehicle->GetUniqueID() : 0); Pkt.WriteBool(false); } @@ -175,10 +175,10 @@ void cProtocol180::SendBlockAction(int a_BlockX, int a_BlockY, int a_BlockZ, cha ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x24); // Block Action packet - Pkt.WritePosition(a_BlockX, a_BlockY, a_BlockZ); - Pkt.WriteByte(a_Byte1); - Pkt.WriteByte(a_Byte2); - Pkt.WriteVarInt(a_BlockType); + Pkt.WritePosition64(a_BlockX, a_BlockY, a_BlockZ); + Pkt.WriteBEInt8(a_Byte1); + Pkt.WriteBEInt8(a_Byte2); + Pkt.WriteVarInt32(a_BlockType); } @@ -190,9 +190,9 @@ void cProtocol180::SendBlockBreakAnim(UInt32 a_EntityID, int a_BlockX, int a_Blo ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x25); // Block Break Animation packet - Pkt.WriteVarInt(a_EntityID); - Pkt.WritePosition(a_BlockX, a_BlockY, a_BlockZ); - Pkt.WriteChar(a_Stage); + Pkt.WriteVarInt32(a_EntityID); + Pkt.WritePosition64(a_BlockX, a_BlockY, a_BlockZ); + Pkt.WriteBEInt8(a_Stage); } @@ -204,8 +204,8 @@ void cProtocol180::SendBlockChange(int a_BlockX, int a_BlockY, int a_BlockZ, BLO ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x23); // Block Change packet - Pkt.WritePosition(a_BlockX, a_BlockY, a_BlockZ); - Pkt.WriteVarInt(((UInt32)a_BlockType << 4) | ((UInt32)a_BlockMeta & 15)); + Pkt.WritePosition64(a_BlockX, a_BlockY, a_BlockZ); + Pkt.WriteVarInt32(((UInt32)a_BlockType << 4) | ((UInt32)a_BlockMeta & 15)); } @@ -217,14 +217,14 @@ void cProtocol180::SendBlockChanges(int a_ChunkX, int a_ChunkZ, const sSetBlockV ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x22); // Multi Block Change packet - Pkt.WriteInt(a_ChunkX); - Pkt.WriteInt(a_ChunkZ); - Pkt.WriteVarInt((UInt32)a_Changes.size()); + Pkt.WriteBEInt32(a_ChunkX); + Pkt.WriteBEInt32(a_ChunkZ); + Pkt.WriteVarInt32((UInt32)a_Changes.size()); for (sSetBlockVector::const_iterator itr = a_Changes.begin(), end = a_Changes.end(); itr != end; ++itr) { - short Coords = (short) (itr->m_RelY | (itr->m_RelZ << 8) | (itr->m_RelX << 12)); - Pkt.WriteShort(Coords); - Pkt.WriteVarInt((itr->m_BlockType & 0xFFF) << 4 | (itr->m_BlockMeta & 0xF)); + Int16 Coords = static_cast(itr->m_RelY | (itr->m_RelZ << 8) | (itr->m_RelX << 12)); + Pkt.WriteBEInt16(Coords); + Pkt.WriteVarInt32((itr->m_BlockType & 0xFFF) << 4 | (itr->m_BlockMeta & 0xF)); } // for itr - a_Changes[] } @@ -238,7 +238,7 @@ void cProtocol180::SendChat(const AString & a_Message) cPacketizer Pkt(*this, 0x02); // Chat Message packet Pkt.WriteString(Printf("{\"text\":\"%s\"}", EscapeString(a_Message).c_str())); - Pkt.WriteChar(0); + Pkt.WriteBEInt8(0); } @@ -255,7 +255,7 @@ void cProtocol180::SendChat(const cCompositeChat & a_Message) // Send the message to the client: cPacketizer Pkt(*this, 0x02); Pkt.WriteString(a_Message.CreateJsonString(ShouldUseChatPrefixes)); - Pkt.WriteChar(0); + Pkt.WriteBEInt8(0); } @@ -283,8 +283,8 @@ void cProtocol180::SendCollectEntity(const cEntity & a_Entity, const cPlayer & a ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x0d); // Collect Item packet - Pkt.WriteVarInt(a_Entity.GetUniqueID()); - Pkt.WriteVarInt(a_Player.GetUniqueID()); + Pkt.WriteVarInt32(a_Entity.GetUniqueID()); + Pkt.WriteVarInt32(a_Player.GetUniqueID()); } @@ -296,8 +296,8 @@ void cProtocol180::SendDestroyEntity(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x13); // Destroy Entities packet - Pkt.WriteVarInt(1); - Pkt.WriteVarInt(a_Entity.GetUniqueID()); + Pkt.WriteVarInt32(1); + Pkt.WriteVarInt32(a_Entity.GetUniqueID()); } @@ -334,7 +334,7 @@ void cProtocol180::SendEditSign(int a_BlockX, int a_BlockY, int a_BlockZ) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x36); // Sign Editor Open packet - Pkt.WritePosition(a_BlockX, a_BlockY, a_BlockZ); + Pkt.WritePosition64(a_BlockX, a_BlockY, a_BlockZ); } @@ -346,10 +346,10 @@ void cProtocol180::SendEntityEffect(const cEntity & a_Entity, int a_EffectID, in ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x1D); // Entity Effect packet - Pkt.WriteVarInt(a_Entity.GetUniqueID()); - Pkt.WriteByte(a_EffectID); - Pkt.WriteByte(a_Amplifier); - Pkt.WriteVarInt((UInt32)a_Duration); + Pkt.WriteVarInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEUInt8(static_cast(a_EffectID)); + Pkt.WriteBEUInt8(static_cast(a_Amplifier)); + Pkt.WriteVarInt32((UInt32)a_Duration); Pkt.WriteBool(false); // Hide particles } @@ -362,8 +362,8 @@ void cProtocol180::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x04); // Entity Equipment packet - Pkt.WriteVarInt(a_Entity.GetUniqueID()); - Pkt.WriteShort(a_SlotNum); + Pkt.WriteVarInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEInt16(a_SlotNum); WriteItem(Pkt, a_Item); } @@ -376,7 +376,7 @@ void cProtocol180::SendEntityHeadLook(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x19); // Entity Head Look packet - Pkt.WriteVarInt(a_Entity.GetUniqueID()); + Pkt.WriteVarInt32(a_Entity.GetUniqueID()); Pkt.WriteByteAngle(a_Entity.GetHeadYaw()); } @@ -389,7 +389,7 @@ void cProtocol180::SendEntityLook(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x16); // Entity Look packet - Pkt.WriteVarInt(a_Entity.GetUniqueID()); + Pkt.WriteVarInt32(a_Entity.GetUniqueID()); Pkt.WriteByteAngle(a_Entity.GetYaw()); Pkt.WriteByteAngle(a_Entity.GetPitch()); Pkt.WriteBool(a_Entity.IsOnGround()); @@ -404,9 +404,9 @@ void cProtocol180::SendEntityMetadata(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x1c); // Entity Metadata packet - Pkt.WriteVarInt(a_Entity.GetUniqueID()); + Pkt.WriteVarInt32(a_Entity.GetUniqueID()); WriteEntityMetadata(Pkt, a_Entity); - Pkt.WriteByte(0x7f); // The termination byte + Pkt.WriteBEUInt8(0x7f); // The termination byte } @@ -418,7 +418,7 @@ void cProtocol180::SendEntityProperties(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x20); // Entity Properties packet - Pkt.WriteVarInt(a_Entity.GetUniqueID()); + Pkt.WriteVarInt32(a_Entity.GetUniqueID()); WriteEntityProperties(Pkt, a_Entity); } @@ -431,10 +431,10 @@ void cProtocol180::SendEntityRelMove(const cEntity & a_Entity, char a_RelX, char ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x15); // Entity Relative Move packet - Pkt.WriteVarInt(a_Entity.GetUniqueID()); - Pkt.WriteChar(a_RelX); - Pkt.WriteChar(a_RelY); - Pkt.WriteChar(a_RelZ); + Pkt.WriteVarInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEInt8(a_RelX); + Pkt.WriteBEInt8(a_RelY); + Pkt.WriteBEInt8(a_RelZ); Pkt.WriteBool(a_Entity.IsOnGround()); } @@ -447,10 +447,10 @@ void cProtocol180::SendEntityRelMoveLook(const cEntity & a_Entity, char a_RelX, ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x17); // Entity Look And Relative Move packet - Pkt.WriteVarInt(a_Entity.GetUniqueID()); - Pkt.WriteChar(a_RelX); - Pkt.WriteChar(a_RelY); - Pkt.WriteChar(a_RelZ); + Pkt.WriteVarInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEInt8(a_RelX); + Pkt.WriteBEInt8(a_RelY); + Pkt.WriteBEInt8(a_RelZ); Pkt.WriteByteAngle(a_Entity.GetYaw()); Pkt.WriteByteAngle(a_Entity.GetPitch()); Pkt.WriteBool(a_Entity.IsOnGround()); @@ -465,8 +465,8 @@ void cProtocol180::SendEntityStatus(const cEntity & a_Entity, char a_Status) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x1a); // Entity Status packet - Pkt.WriteUInt32(a_Entity.GetUniqueID()); - Pkt.WriteChar(a_Status); + Pkt.WriteBEUInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEInt8(a_Status); } @@ -478,11 +478,11 @@ void cProtocol180::SendEntityVelocity(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x12); // Entity Velocity packet - Pkt.WriteVarInt(a_Entity.GetUniqueID()); + Pkt.WriteVarInt32(a_Entity.GetUniqueID()); // 400 = 8000 / 20 ... Conversion from our speed in m/s to 8000 m/tick - Pkt.WriteShort((short)(a_Entity.GetSpeedX() * 400)); - Pkt.WriteShort((short)(a_Entity.GetSpeedY() * 400)); - Pkt.WriteShort((short)(a_Entity.GetSpeedZ() * 400)); + Pkt.WriteBEInt16((short)(a_Entity.GetSpeedX() * 400)); + Pkt.WriteBEInt16((short)(a_Entity.GetSpeedY() * 400)); + Pkt.WriteBEInt16((short)(a_Entity.GetSpeedZ() * 400)); } @@ -494,20 +494,20 @@ void cProtocol180::SendExplosion(double a_BlockX, double a_BlockY, double a_Bloc ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x27); // Explosion packet - Pkt.WriteFloat((float)a_BlockX); - Pkt.WriteFloat((float)a_BlockY); - Pkt.WriteFloat((float)a_BlockZ); - Pkt.WriteFloat((float)a_Radius); - Pkt.WriteInt((int)a_BlocksAffected.size()); + Pkt.WriteBEFloat(static_cast(a_BlockX)); + Pkt.WriteBEFloat(static_cast(a_BlockY)); + Pkt.WriteBEFloat(static_cast(a_BlockZ)); + Pkt.WriteBEFloat(static_cast(a_Radius)); + Pkt.WriteBEUInt32(static_cast(a_BlocksAffected.size())); for (cVector3iArray::const_iterator itr = a_BlocksAffected.begin(), end = a_BlocksAffected.end(); itr != end; ++itr) { - Pkt.WriteChar((char)itr->x); - Pkt.WriteChar((char)itr->y); - Pkt.WriteChar((char)itr->z); + Pkt.WriteBEInt8(static_cast(itr->x)); + Pkt.WriteBEInt8(static_cast(itr->y)); + Pkt.WriteBEInt8(static_cast(itr->z)); } // for itr - a_BlockAffected[] - Pkt.WriteFloat((float)a_PlayerMotion.x); - Pkt.WriteFloat((float)a_PlayerMotion.y); - Pkt.WriteFloat((float)a_PlayerMotion.z); + Pkt.WriteBEFloat(static_cast(a_PlayerMotion.x)); + Pkt.WriteBEFloat(static_cast(a_PlayerMotion.y)); + Pkt.WriteBEFloat(static_cast(a_PlayerMotion.z)); } @@ -519,8 +519,8 @@ void cProtocol180::SendGameMode(eGameMode a_GameMode) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x2b); // Change Game State packet - Pkt.WriteByte(3); // Reason: Change game mode - Pkt.WriteFloat((float)a_GameMode); + Pkt.WriteBEUInt8(3); // Reason: Change game mode + Pkt.WriteBEFloat(static_cast(a_GameMode)); // The protocol really represents the value with a float! } @@ -533,9 +533,9 @@ void cProtocol180::SendHealth(void) cPacketizer Pkt(*this, 0x06); // Update Health packet cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteFloat((float)Player->GetHealth()); - Pkt.WriteVarInt((UInt32)Player->GetFoodLevel()); - Pkt.WriteFloat((float)Player->GetFoodSaturationLevel()); + Pkt.WriteBEFloat(static_cast(Player->GetHealth())); + Pkt.WriteVarInt32((UInt32)Player->GetFoodLevel()); + Pkt.WriteBEFloat(static_cast(Player->GetFoodSaturationLevel())); } @@ -547,8 +547,8 @@ void cProtocol180::SendInventorySlot(char a_WindowID, short a_SlotNum, const cIt ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x2f); // Set Slot packet - Pkt.WriteChar(a_WindowID); - Pkt.WriteShort(a_SlotNum); + Pkt.WriteBEInt8(a_WindowID); + Pkt.WriteBEInt16(a_SlotNum); WriteItem(Pkt, a_Item); } @@ -566,7 +566,7 @@ void cProtocol180::SendKeepAlive(int a_PingID) } cPacketizer Pkt(*this, 0x00); // Keep Alive packet - Pkt.WriteVarInt(a_PingID); + Pkt.WriteVarInt32(a_PingID); } @@ -579,11 +579,11 @@ void cProtocol180::SendLogin(const cPlayer & a_Player, const cWorld & a_World) { cServer * Server = cRoot::Get()->GetServer(); cPacketizer Pkt(*this, 0x01); // Join Game packet - Pkt.WriteUInt32(a_Player.GetUniqueID()); - Pkt.WriteByte((Byte)a_Player.GetEffectiveGameMode() | (Server->IsHardcore() ? 0x08 : 0)); // Hardcore flag bit 4 - Pkt.WriteChar((char)a_World.GetDimension()); - Pkt.WriteByte(2); // TODO: Difficulty (set to Normal) - Pkt.WriteByte(Server->GetMaxPlayers()); + Pkt.WriteBEUInt32(a_Player.GetUniqueID()); + Pkt.WriteBEUInt8((Byte)a_Player.GetEffectiveGameMode() | (Server->IsHardcore() ? 0x08 : 0)); // Hardcore flag bit 4 + Pkt.WriteBEInt8((char)a_World.GetDimension()); + Pkt.WriteBEUInt8(2); // TODO: Difficulty (set to Normal) + Pkt.WriteBEUInt8(Server->GetMaxPlayers()); Pkt.WriteString("default"); // Level type - wtf? Pkt.WriteBool(false); // Reduced Debug Info - wtf? } @@ -592,13 +592,13 @@ void cProtocol180::SendLogin(const cPlayer & a_Player, const cWorld & a_World) // Send the spawn position: { cPacketizer Pkt(*this, 0x05); // Spawn Position packet - Pkt.WritePosition((int)a_World.GetSpawnX(), (int)a_World.GetSpawnY(), (int)a_World.GetSpawnZ()); + Pkt.WritePosition64((int)a_World.GetSpawnX(), (int)a_World.GetSpawnY(), (int)a_World.GetSpawnZ()); } // Send the server difficulty: { cPacketizer Pkt(*this, 0x41); - Pkt.WriteChar(1); + Pkt.WriteBEInt8(1); } // Send player abilities: @@ -615,7 +615,7 @@ void cProtocol180::SendLoginSuccess(void) // Enable compression: { cPacketizer Pkt(*this, 0x03); // Set compression packet - Pkt.WriteVarInt(256); + Pkt.WriteVarInt32(256); } m_State = 3; // State = Game @@ -639,10 +639,10 @@ void cProtocol180::SendPaintingSpawn(const cPainting & a_Painting) double PosZ = a_Painting.GetPosZ(); cPacketizer Pkt(*this, 0x10); // Spawn Painting packet - Pkt.WriteVarInt(a_Painting.GetUniqueID()); + Pkt.WriteVarInt32(a_Painting.GetUniqueID()); Pkt.WriteString(a_Painting.GetName().c_str()); - Pkt.WritePosition((int)PosX, (int)PosY, (int)PosZ); - Pkt.WriteChar(a_Painting.GetProtocolFacing()); + Pkt.WritePosition64((int)PosX, (int)PosY, (int)PosZ); + Pkt.WriteBEInt8(a_Painting.GetProtocolFacing()); } @@ -654,19 +654,19 @@ void cProtocol180::SendMapColumn(int a_ID, int a_X, int a_Y, const Byte * a_Colo ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x34); - Pkt.WriteVarInt(a_ID); - Pkt.WriteByte(m_Scale); + Pkt.WriteVarInt32(a_ID); + Pkt.WriteBEUInt8(m_Scale); - Pkt.WriteVarInt(0); - Pkt.WriteByte(1); - Pkt.WriteByte(a_Length); - Pkt.WriteByte(a_X); - Pkt.WriteByte(a_Y); + Pkt.WriteVarInt32(0); + Pkt.WriteBEUInt8(1); + Pkt.WriteBEUInt8(a_Length); + Pkt.WriteBEUInt8(a_X); + Pkt.WriteBEUInt8(a_Y); - Pkt.WriteVarInt(a_Length); + Pkt.WriteVarInt32(a_Length); for (unsigned int i = 0; i < a_Length; ++i) { - Pkt.WriteByte(a_Colors[i]); + Pkt.WriteBEUInt8(a_Colors[i]); } } @@ -679,18 +679,18 @@ void cProtocol180::SendMapDecorators(int a_ID, const cMapDecoratorList & a_Decor ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x34); - Pkt.WriteVarInt(a_ID); - Pkt.WriteByte(m_Scale); - Pkt.WriteVarInt(a_Decorators.size()); + Pkt.WriteVarInt32(a_ID); + Pkt.WriteBEUInt8(m_Scale); + Pkt.WriteVarInt32(a_Decorators.size()); for (cMapDecoratorList::const_iterator it = a_Decorators.begin(); it != a_Decorators.end(); ++it) { - Pkt.WriteByte((it->GetType() << 4) | (it->GetRot() & 0xf)); - Pkt.WriteByte(it->GetPixelX()); - Pkt.WriteByte(it->GetPixelZ()); + Pkt.WriteBEUInt8((it->GetType() << 4) | (it->GetRot() & 0xf)); + Pkt.WriteBEUInt8(it->GetPixelX()); + Pkt.WriteBEUInt8(it->GetPixelZ()); } - Pkt.WriteByte(0); + Pkt.WriteBEUInt8(0); } @@ -715,22 +715,22 @@ void cProtocol180::SendPickupSpawn(const cPickup & a_Pickup) { cPacketizer Pkt(*this, 0x0e); // Spawn Object packet - Pkt.WriteVarInt(a_Pickup.GetUniqueID()); - Pkt.WriteByte(2); // Type = Pickup + Pkt.WriteVarInt32(a_Pickup.GetUniqueID()); + Pkt.WriteBEUInt8(2); // Type = Pickup Pkt.WriteFPInt(a_Pickup.GetPosX()); Pkt.WriteFPInt(a_Pickup.GetPosY()); Pkt.WriteFPInt(a_Pickup.GetPosZ()); Pkt.WriteByteAngle(a_Pickup.GetYaw()); Pkt.WriteByteAngle(a_Pickup.GetPitch()); - Pkt.WriteInt(0); // No object data + Pkt.WriteBEInt32(0); // No object data } { cPacketizer Pkt(*this, 0x1c); // Entity Metadata packet - Pkt.WriteVarInt(a_Pickup.GetUniqueID()); - Pkt.WriteByte((0x05 << 5) | 10); // Slot type + index 10 + Pkt.WriteVarInt32(a_Pickup.GetUniqueID()); + Pkt.WriteBEUInt8((0x05 << 5) | 10); // Slot type + index 10 WriteItem(Pkt, a_Pickup.GetItem()); - Pkt.WriteByte(0x7f); // End of metadata + Pkt.WriteBEUInt8(0x7f); // End of metadata } } @@ -758,9 +758,9 @@ void cProtocol180::SendPlayerAbilities(void) { Flags |= 0x04; } - Pkt.WriteByte(Flags); - Pkt.WriteFloat((float)(0.05 * Player->GetFlyingMaxSpeed())); - Pkt.WriteFloat((float)(0.1 * Player->GetNormalMaxSpeed())); + Pkt.WriteBEUInt8(Flags); + Pkt.WriteBEFloat(static_cast(0.05 * Player->GetFlyingMaxSpeed())); + Pkt.WriteBEFloat(static_cast(0.1 * Player->GetNormalMaxSpeed())); } @@ -772,8 +772,8 @@ void cProtocol180::SendEntityAnimation(const cEntity & a_Entity, char a_Animatio ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x0b); // Animation packet - Pkt.WriteVarInt(a_Entity.GetUniqueID()); - Pkt.WriteChar(a_Animation); + Pkt.WriteVarInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEInt8(a_Animation); } @@ -786,16 +786,16 @@ void cProtocol180::SendParticleEffect(const AString & a_ParticleName, float a_Sr int ParticleID = GetParticleID(a_ParticleName); cPacketizer Pkt(*this, 0x2A); - Pkt.WriteInt(ParticleID); + Pkt.WriteBEInt32(ParticleID); Pkt.WriteBool(false); - Pkt.WriteFloat(a_SrcX); - Pkt.WriteFloat(a_SrcY); - Pkt.WriteFloat(a_SrcZ); - Pkt.WriteFloat(a_OffsetX); - Pkt.WriteFloat(a_OffsetY); - Pkt.WriteFloat(a_OffsetZ); - Pkt.WriteFloat(a_ParticleData); - Pkt.WriteInt(a_ParticleAmount); + Pkt.WriteBEFloat(a_SrcX); + Pkt.WriteBEFloat(a_SrcY); + Pkt.WriteBEFloat(a_SrcZ); + Pkt.WriteBEFloat(a_OffsetX); + Pkt.WriteBEFloat(a_OffsetY); + Pkt.WriteBEFloat(a_OffsetZ); + Pkt.WriteBEFloat(a_ParticleData); + Pkt.WriteBEInt32(a_ParticleAmount); } @@ -807,13 +807,13 @@ void cProtocol180::SendPlayerListAddPlayer(const cPlayer & a_Player) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x38); // Playerlist Item packet - Pkt.WriteVarInt(0); - Pkt.WriteVarInt(1); + 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.WriteVarInt(Properties.size()); + Pkt.WriteVarInt32(Properties.size()); for (Json::Value::iterator itr = Properties.begin(), end = Properties.end(); itr != end; ++itr) { Pkt.WriteString(((Json::Value)*itr).get("name", "").asString()); @@ -830,8 +830,8 @@ void cProtocol180::SendPlayerListAddPlayer(const cPlayer & a_Player) } } - Pkt.WriteVarInt((UInt32)a_Player.GetGameMode()); - Pkt.WriteVarInt((UInt32)a_Player.GetClientHandle()->GetPing()); + Pkt.WriteVarInt32((UInt32)a_Player.GetGameMode()); + Pkt.WriteVarInt32((UInt32)a_Player.GetClientHandle()->GetPing()); Pkt.WriteBool(false); } @@ -844,8 +844,8 @@ void cProtocol180::SendPlayerListRemovePlayer(const cPlayer & a_Player) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x38); // Playerlist Item packet - Pkt.WriteVarInt(4); - Pkt.WriteVarInt(1); + Pkt.WriteVarInt32(4); + Pkt.WriteVarInt32(1); Pkt.WriteUUID(a_Player.GetUUID()); } @@ -858,10 +858,10 @@ void cProtocol180::SendPlayerListUpdateGameMode(const cPlayer & a_Player) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x38); // Playerlist Item packet - Pkt.WriteVarInt(1); - Pkt.WriteVarInt(1); + Pkt.WriteVarInt32(1); + Pkt.WriteVarInt32(1); Pkt.WriteUUID(a_Player.GetUUID()); - Pkt.WriteVarInt((UInt32)a_Player.GetGameMode()); + Pkt.WriteVarInt32((UInt32)a_Player.GetGameMode()); } @@ -876,10 +876,10 @@ void cProtocol180::SendPlayerListUpdatePing(const cPlayer & a_Player) if (ClientHandle != nullptr) { cPacketizer Pkt(*this, 0x38); // Playerlist Item packet - Pkt.WriteVarInt(2); - Pkt.WriteVarInt(1); + Pkt.WriteVarInt32(2); + Pkt.WriteVarInt32(1); Pkt.WriteUUID(a_Player.GetUUID()); - Pkt.WriteVarInt(static_cast(ClientHandle->GetPing())); + Pkt.WriteVarInt32(static_cast(ClientHandle->GetPing())); } } @@ -892,8 +892,8 @@ void cProtocol180::SendPlayerListUpdateDisplayName(const cPlayer & a_Player, con ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x38); // Playerlist Item packet - Pkt.WriteVarInt(3); - Pkt.WriteVarInt(1); + Pkt.WriteVarInt32(3); + Pkt.WriteVarInt32(1); Pkt.WriteUUID(a_Player.GetUUID()); if (a_CustomName.empty()) @@ -917,22 +917,22 @@ void cProtocol180::SendPlayerMaxSpeed(void) cPacketizer Pkt(*this, 0x20); // Entity Properties cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteVarInt(Player->GetUniqueID()); - Pkt.WriteInt(1); // Count + 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.WriteDouble(0.1 * Player->GetNormalMaxSpeed()); + Pkt.WriteBEDouble(0.1 * Player->GetNormalMaxSpeed()); if (Player->IsSprinting()) { - Pkt.WriteVarInt(1); // Modifier count - Pkt.WriteInt64(0x662a6b8dda3e4c1c); - Pkt.WriteInt64(0x881396ea6097278d); // UUID of the modifier - Pkt.WriteDouble(Player->GetSprintingMaxSpeed() - Player->GetNormalMaxSpeed()); - Pkt.WriteByte(2); + 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.WriteVarInt(0); // Modifier count + Pkt.WriteVarInt32(0); // Modifier count } } @@ -946,15 +946,15 @@ void cProtocol180::SendPlayerMoveLook(void) cPacketizer Pkt(*this, 0x08); // Player Position And Look packet cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteDouble(Player->GetPosX()); + Pkt.WriteBEDouble(Player->GetPosX()); // The "+ 0.001" is there because otherwise the player falls through the block they were standing on. - Pkt.WriteDouble(Player->GetPosY() + 0.001); + Pkt.WriteBEDouble(Player->GetPosY() + 0.001); - Pkt.WriteDouble(Player->GetPosZ()); - Pkt.WriteFloat((float)Player->GetYaw()); - Pkt.WriteFloat((float)Player->GetPitch()); - Pkt.WriteByte(0); + Pkt.WriteBEDouble(Player->GetPosZ()); + Pkt.WriteBEFloat(static_cast(Player->GetYaw())); + Pkt.WriteBEFloat(static_cast(Player->GetPitch())); + Pkt.WriteBEUInt8(0); } @@ -975,7 +975,7 @@ void cProtocol180::SendPlayerSpawn(const cPlayer & a_Player) { // Called to spawn another player for the client cPacketizer Pkt(*this, 0x0c); // Spawn Player packet - Pkt.WriteVarInt(a_Player.GetUniqueID()); + Pkt.WriteVarInt32(a_Player.GetUniqueID()); Pkt.WriteUUID(cMojangAPI::MakeUUIDShort(a_Player.GetUUID())); Pkt.WriteFPInt(a_Player.GetPosX()); Pkt.WriteFPInt(a_Player.GetPosY() + 0.001); // The "+ 0.001" is there because otherwise the player falls through the block they were standing on. @@ -983,12 +983,12 @@ void cProtocol180::SendPlayerSpawn(const cPlayer & a_Player) Pkt.WriteByteAngle(a_Player.GetYaw()); Pkt.WriteByteAngle(a_Player.GetPitch()); short ItemType = a_Player.GetEquippedItem().IsEmpty() ? 0 : a_Player.GetEquippedItem().m_ItemType; - Pkt.WriteShort(ItemType); - Pkt.WriteByte((3 << 5) | 6); // Metadata: float + index 6 - Pkt.WriteFloat((float)a_Player.GetHealth()); - Pkt.WriteByte((4 << 5 | (2 & 0x1F)) & 0xFF); + Pkt.WriteBEInt16(ItemType); + Pkt.WriteBEUInt8((3 << 5) | 6); // Metadata: float + index 6 + Pkt.WriteBEFloat(static_cast(a_Player.GetHealth())); + Pkt.WriteBEUInt8((4 << 5 | (2 & 0x1F)) & 0xFF); Pkt.WriteString(a_Player.GetName()); - Pkt.WriteByte(0x7f); // Metadata: end + Pkt.WriteBEUInt8(0x7f); // Metadata: end } @@ -1013,8 +1013,8 @@ void cProtocol180::SendRemoveEntityEffect(const cEntity & a_Entity, int a_Effect ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x1e); - Pkt.WriteVarInt(a_Entity.GetUniqueID()); - Pkt.WriteByte(a_EffectID); + Pkt.WriteVarInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEUInt8(a_EffectID); } @@ -1031,9 +1031,9 @@ void cProtocol180::SendRespawn(eDimension a_Dimension, bool a_ShouldIgnoreDimens cPacketizer Pkt(*this, 0x07); // Respawn packet cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteInt((int)a_Dimension); - Pkt.WriteByte(2); // TODO: Difficulty (set to Normal) - Pkt.WriteByte((Byte)Player->GetEffectiveGameMode()); + Pkt.WriteBEInt32((int)a_Dimension); + Pkt.WriteBEUInt8(2); // TODO: Difficulty (set to Normal) + Pkt.WriteBEUInt8((Byte)Player->GetEffectiveGameMode()); Pkt.WriteString("default"); m_LastSentDimension = a_Dimension; } @@ -1048,9 +1048,9 @@ void cProtocol180::SendExperience(void) cPacketizer Pkt(*this, 0x1f); // Experience Packet cPlayer * Player = m_Client->GetPlayer(); - Pkt.WriteFloat(Player->GetXpPercentage()); - Pkt.WriteVarInt((UInt32)Player->GetXpLevel()); - Pkt.WriteVarInt((UInt32)Player->GetCurrentXp()); + Pkt.WriteBEFloat(Player->GetXpPercentage()); + Pkt.WriteVarInt32((UInt32)Player->GetXpLevel()); + Pkt.WriteVarInt32((UInt32)Player->GetCurrentXp()); } @@ -1062,11 +1062,11 @@ void cProtocol180::SendExperienceOrb(const cExpOrb & a_ExpOrb) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x11); - Pkt.WriteVarInt(a_ExpOrb.GetUniqueID()); + Pkt.WriteVarInt32(a_ExpOrb.GetUniqueID()); Pkt.WriteFPInt(a_ExpOrb.GetPosX()); Pkt.WriteFPInt(a_ExpOrb.GetPosY()); Pkt.WriteFPInt(a_ExpOrb.GetPosZ()); - Pkt.WriteShort(a_ExpOrb.GetReward()); + Pkt.WriteBEInt16(a_ExpOrb.GetReward()); } @@ -1079,7 +1079,7 @@ void cProtocol180::SendScoreboardObjective(const AString & a_Name, const AString cPacketizer Pkt(*this, 0x3b); Pkt.WriteString(a_Name); - Pkt.WriteByte(a_Mode); + Pkt.WriteBEUInt8(a_Mode); if ((a_Mode == 0) || (a_Mode == 2)) { Pkt.WriteString(a_DisplayName); @@ -1097,12 +1097,12 @@ void cProtocol180::SendScoreUpdate(const AString & a_Objective, const AString & cPacketizer Pkt(*this, 0x3c); Pkt.WriteString(a_Player); - Pkt.WriteByte(a_Mode); + Pkt.WriteBEUInt8(a_Mode); Pkt.WriteString(a_Objective); if (a_Mode != 1) { - Pkt.WriteVarInt((UInt32) a_Score); + Pkt.WriteVarInt32((UInt32) a_Score); } } @@ -1115,7 +1115,7 @@ void cProtocol180::SendDisplayObjective(const AString & a_Objective, cScoreboard ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x3d); - Pkt.WriteByte((int) a_Display); + Pkt.WriteBEUInt8((int) a_Display); Pkt.WriteString(a_Objective); } @@ -1129,11 +1129,11 @@ void cProtocol180::SendSoundEffect(const AString & a_SoundName, double a_X, doub cPacketizer Pkt(*this, 0x29); // Sound Effect packet Pkt.WriteString(a_SoundName); - Pkt.WriteInt((int)(a_X * 8.0)); - Pkt.WriteInt((int)(a_Y * 8.0)); - Pkt.WriteInt((int)(a_Z * 8.0)); - Pkt.WriteFloat(a_Volume); - Pkt.WriteByte((Byte)(a_Pitch * 63)); + Pkt.WriteBEInt32((int)(a_X * 8.0)); + Pkt.WriteBEInt32((int)(a_Y * 8.0)); + Pkt.WriteBEInt32((int)(a_Z * 8.0)); + Pkt.WriteBEFloat(a_Volume); + Pkt.WriteBEUInt8((Byte)(a_Pitch * 63)); } @@ -1145,9 +1145,9 @@ void cProtocol180::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_Src ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x28); // Effect packet - Pkt.WriteInt(a_EffectID); - Pkt.WritePosition(a_SrcX, a_SrcY, a_SrcZ); - Pkt.WriteInt(a_Data); + Pkt.WriteBEInt32(a_EffectID); + Pkt.WritePosition64(a_SrcX, a_SrcY, a_SrcZ); + Pkt.WriteBEInt32(a_Data); Pkt.WriteBool(false); } @@ -1160,17 +1160,17 @@ void cProtocol180::SendSpawnFallingBlock(const cFallingBlock & a_FallingBlock) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x0e); // Spawn Object packet - Pkt.WriteVarInt(a_FallingBlock.GetUniqueID()); - Pkt.WriteByte(70); // Falling block + Pkt.WriteVarInt32(a_FallingBlock.GetUniqueID()); + Pkt.WriteBEUInt8(70); // Falling block Pkt.WriteFPInt(a_FallingBlock.GetPosX()); Pkt.WriteFPInt(a_FallingBlock.GetPosY()); Pkt.WriteFPInt(a_FallingBlock.GetPosZ()); Pkt.WriteByteAngle(a_FallingBlock.GetYaw()); Pkt.WriteByteAngle(a_FallingBlock.GetPitch()); - Pkt.WriteInt(((int)a_FallingBlock.GetBlockType()) | (((int)a_FallingBlock.GetBlockMeta()) << 12)); - Pkt.WriteShort((short)(a_FallingBlock.GetSpeedX() * 400)); - Pkt.WriteShort((short)(a_FallingBlock.GetSpeedY() * 400)); - Pkt.WriteShort((short)(a_FallingBlock.GetSpeedZ() * 400)); + Pkt.WriteBEInt32(((int)a_FallingBlock.GetBlockType()) | (((int)a_FallingBlock.GetBlockMeta()) << 12)); + Pkt.WriteBEInt16((short)(a_FallingBlock.GetSpeedX() * 400)); + Pkt.WriteBEInt16((short)(a_FallingBlock.GetSpeedY() * 400)); + Pkt.WriteBEInt16((short)(a_FallingBlock.GetSpeedZ() * 400)); } @@ -1182,19 +1182,19 @@ void cProtocol180::SendSpawnMob(const cMonster & a_Mob) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x0f); // Spawn Mob packet - Pkt.WriteVarInt(a_Mob.GetUniqueID()); - Pkt.WriteByte((Byte)a_Mob.GetMobType()); + Pkt.WriteVarInt32(a_Mob.GetUniqueID()); + Pkt.WriteBEUInt8((Byte)a_Mob.GetMobType()); Pkt.WriteFPInt(a_Mob.GetPosX()); Pkt.WriteFPInt(a_Mob.GetPosY()); Pkt.WriteFPInt(a_Mob.GetPosZ()); Pkt.WriteByteAngle(a_Mob.GetPitch()); Pkt.WriteByteAngle(a_Mob.GetHeadYaw()); Pkt.WriteByteAngle(a_Mob.GetYaw()); - Pkt.WriteShort((short)(a_Mob.GetSpeedX() * 400)); - Pkt.WriteShort((short)(a_Mob.GetSpeedY() * 400)); - Pkt.WriteShort((short)(a_Mob.GetSpeedZ() * 400)); + Pkt.WriteBEInt16((short)(a_Mob.GetSpeedX() * 400)); + Pkt.WriteBEInt16((short)(a_Mob.GetSpeedY() * 400)); + Pkt.WriteBEInt16((short)(a_Mob.GetSpeedZ() * 400)); WriteEntityMetadata(Pkt, a_Mob); - Pkt.WriteByte(0x7f); // Metadata terminator + Pkt.WriteBEUInt8(0x7f); // Metadata terminator } @@ -1213,19 +1213,19 @@ void cProtocol180::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, } cPacketizer Pkt(*this, 0xe); // Spawn Object packet - Pkt.WriteVarInt(a_Entity.GetUniqueID()); - Pkt.WriteByte(a_ObjectType); + Pkt.WriteVarInt32(a_Entity.GetUniqueID()); + Pkt.WriteBEUInt8(a_ObjectType); Pkt.WriteFPInt(PosX); Pkt.WriteFPInt(a_Entity.GetPosY()); Pkt.WriteFPInt(PosZ); Pkt.WriteByteAngle(a_Entity.GetPitch()); Pkt.WriteByteAngle(Yaw); - Pkt.WriteInt(a_ObjectData); + Pkt.WriteBEInt32(a_ObjectData); if (a_ObjectData != 0) { - Pkt.WriteShort((short)(a_Entity.GetSpeedX() * 400)); - Pkt.WriteShort((short)(a_Entity.GetSpeedY() * 400)); - Pkt.WriteShort((short)(a_Entity.GetSpeedZ() * 400)); + Pkt.WriteBEInt16((short)(a_Entity.GetSpeedX() * 400)); + Pkt.WriteBEInt16((short)(a_Entity.GetSpeedY() * 400)); + Pkt.WriteBEInt16((short)(a_Entity.GetSpeedZ() * 400)); } } @@ -1238,19 +1238,19 @@ void cProtocol180::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleTyp ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0xe); // Spawn Object packet - Pkt.WriteVarInt(a_Vehicle.GetUniqueID()); - Pkt.WriteByte(a_VehicleType); + Pkt.WriteVarInt32(a_Vehicle.GetUniqueID()); + Pkt.WriteBEUInt8(a_VehicleType); Pkt.WriteFPInt(a_Vehicle.GetPosX()); Pkt.WriteFPInt(a_Vehicle.GetPosY()); Pkt.WriteFPInt(a_Vehicle.GetPosZ()); Pkt.WriteByteAngle(a_Vehicle.GetPitch()); Pkt.WriteByteAngle(a_Vehicle.GetYaw()); - Pkt.WriteInt(a_VehicleSubType); + Pkt.WriteBEInt32(a_VehicleSubType); if (a_VehicleSubType != 0) { - Pkt.WriteShort((short)(a_Vehicle.GetSpeedX() * 400)); - Pkt.WriteShort((short)(a_Vehicle.GetSpeedY() * 400)); - Pkt.WriteShort((short)(a_Vehicle.GetSpeedZ() * 400)); + Pkt.WriteBEInt16((short)(a_Vehicle.GetSpeedX() * 400)); + Pkt.WriteBEInt16((short)(a_Vehicle.GetSpeedY() * 400)); + Pkt.WriteBEInt16((short)(a_Vehicle.GetSpeedZ() * 400)); } } @@ -1263,7 +1263,7 @@ void cProtocol180::SendStatistics(const cStatManager & a_Manager) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x37); - Pkt.WriteVarInt(statCount); // TODO 2014-05-11 xdot: Optimization: Send "dirty" statistics only + Pkt.WriteVarInt32(statCount); // TODO 2014-05-11 xdot: Optimization: Send "dirty" statistics only for (size_t i = 0; i < (size_t)statCount; ++i) { @@ -1271,7 +1271,7 @@ void cProtocol180::SendStatistics(const cStatManager & a_Manager) const AString & StatName = cStatInfo::GetName((eStatistic) i); Pkt.WriteString(StatName); - Pkt.WriteVarInt(Value); + Pkt.WriteVarInt32(Value); } } @@ -1284,7 +1284,7 @@ void cProtocol180::SendTabCompletionResults(const AStringVector & a_Results) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x3a); // Tab-Complete packet - Pkt.WriteVarInt((int)a_Results.size()); + Pkt.WriteVarInt32((int)a_Results.size()); for (AStringVector::const_iterator itr = a_Results.begin(), end = a_Results.end(); itr != end; ++itr) { @@ -1301,7 +1301,7 @@ void cProtocol180::SendTeleportEntity(const cEntity & a_Entity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x18); - Pkt.WriteVarInt(a_Entity.GetUniqueID()); + Pkt.WriteVarInt32(a_Entity.GetUniqueID()); Pkt.WriteFPInt(a_Entity.GetPosX()); Pkt.WriteFPInt(a_Entity.GetPosY()); Pkt.WriteFPInt(a_Entity.GetPosZ()); @@ -1319,8 +1319,8 @@ void cProtocol180::SendThunderbolt(int a_BlockX, int a_BlockY, int a_BlockZ) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x2c); // Spawn Global Entity packet - Pkt.WriteVarInt(0); // EntityID = 0, always - Pkt.WriteByte(1); // Type = Thunderbolt + Pkt.WriteVarInt32(0); // EntityID = 0, always + Pkt.WriteBEUInt8(1); // Type = Thunderbolt Pkt.WriteFPInt(a_BlockX); Pkt.WriteFPInt(a_BlockY); Pkt.WriteFPInt(a_BlockZ); @@ -1340,8 +1340,8 @@ void cProtocol180::SendTimeUpdate(Int64 a_WorldAge, Int64 a_TimeOfDay, bool a_Do } cPacketizer Pkt(*this, 0x03); - Pkt.WriteInt64(a_WorldAge); - Pkt.WriteInt64(a_TimeOfDay); + Pkt.WriteBEInt64(a_WorldAge); + Pkt.WriteBEInt64(a_TimeOfDay); } @@ -1353,11 +1353,11 @@ void cProtocol180::SendUnloadChunk(int a_ChunkX, int a_ChunkZ) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x21); // Chunk Data packet - Pkt.WriteInt(a_ChunkX); - Pkt.WriteInt(a_ChunkZ); + Pkt.WriteBEInt32(a_ChunkX); + Pkt.WriteBEInt32(a_ChunkZ); Pkt.WriteBool(true); - Pkt.WriteShort(0); // Primary bitmap - Pkt.WriteVarInt(0); // Data size + Pkt.WriteBEInt16(0); // Primary bitmap + Pkt.WriteVarInt32(0); // Data size } @@ -1368,7 +1368,7 @@ void cProtocol180::SendUpdateBlockEntity(cBlockEntity & a_BlockEntity) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x35); // Update tile entity packet - Pkt.WritePosition(a_BlockEntity.GetPosX(), a_BlockEntity.GetPosY(), a_BlockEntity.GetPosZ()); + Pkt.WritePosition64(a_BlockEntity.GetPosX(), a_BlockEntity.GetPosY(), a_BlockEntity.GetPosZ()); Byte Action = 0; switch (a_BlockEntity.GetBlockType()) @@ -1380,7 +1380,7 @@ void cProtocol180::SendUpdateBlockEntity(cBlockEntity & a_BlockEntity) case E_BLOCK_FLOWER_POT: Action = 5; break; // Update flower pot default: ASSERT(!"Unhandled or unimplemented BlockEntity update request!"); break; } - Pkt.WriteByte(Action); + Pkt.WriteBEUInt8(Action); WriteBlockEntity(Pkt, a_BlockEntity); } @@ -1394,7 +1394,7 @@ void cProtocol180::SendUpdateSign(int a_BlockX, int a_BlockY, int a_BlockZ, cons ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x33); - Pkt.WritePosition(a_BlockX, a_BlockY, a_BlockZ); + Pkt.WritePosition64(a_BlockX, a_BlockY, a_BlockZ); Json::StyledWriter JsonWriter; AString Lines[] = { a_Line1, a_Line2, a_Line3, a_Line4 }; @@ -1415,8 +1415,8 @@ void cProtocol180::SendUseBed(const cEntity & a_Entity, int a_BlockX, int a_Bloc ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x0a); - Pkt.WriteVarInt(a_Entity.GetUniqueID()); - Pkt.WritePosition(a_BlockX, a_BlockY, a_BlockZ); + Pkt.WriteVarInt32(a_Entity.GetUniqueID()); + Pkt.WritePosition64(a_BlockX, a_BlockY, a_BlockZ); } @@ -1429,8 +1429,8 @@ void cProtocol180::SendWeather(eWeather a_Weather) { cPacketizer Pkt(*this, 0x2b); // Change Game State packet - Pkt.WriteByte((a_Weather == wSunny) ? 1 : 2); // End rain / begin rain - Pkt.WriteFloat(0); // Unused for weather + Pkt.WriteBEUInt8((a_Weather == wSunny) ? 1 : 2); // End rain / begin rain + Pkt.WriteBEFloat(0); // Unused for weather } // TODO: Fade effect, somehow @@ -1445,8 +1445,8 @@ void cProtocol180::SendWholeInventory(const cWindow & a_Window) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x30); // Window Items packet - Pkt.WriteChar(a_Window.GetWindowID()); - Pkt.WriteShort(a_Window.GetNumSlots()); + Pkt.WriteBEInt8(a_Window.GetWindowID()); + Pkt.WriteBEInt16(a_Window.GetNumSlots()); cItems Slots; a_Window.GetSlots(*(m_Client->GetPlayer()), Slots); for (cItems::const_iterator itr = Slots.begin(), end = Slots.end(); itr != end; ++itr) @@ -1464,7 +1464,7 @@ void cProtocol180::SendWindowClose(const cWindow & a_Window) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x2e); - Pkt.WriteChar(a_Window.GetWindowID()); + Pkt.WriteBEInt8(a_Window.GetWindowID()); } @@ -1482,7 +1482,7 @@ void cProtocol180::SendWindowOpen(const cWindow & a_Window) } cPacketizer Pkt(*this, 0x2d); - Pkt.WriteChar(a_Window.GetWindowID()); + Pkt.WriteBEInt8(a_Window.GetWindowID()); Pkt.WriteString(a_Window.GetWindowTypeName()); Pkt.WriteString(Printf("{\"text\":\"%s\"}", a_Window.GetWindowTitle().c_str())); @@ -1492,19 +1492,19 @@ void cProtocol180::SendWindowOpen(const cWindow & a_Window) case cWindow::wtEnchantment: case cWindow::wtAnvil: { - Pkt.WriteChar(0); + Pkt.WriteBEInt8(0); break; } default: { - Pkt.WriteChar(a_Window.GetNumNonInventorySlots()); + Pkt.WriteBEInt8(a_Window.GetNumNonInventorySlots()); break; } } if (a_Window.GetWindowType() == cWindow::wtAnimalChest) { - Pkt.WriteInt(0); // TODO: The animal's EntityID + Pkt.WriteBEInt32(0); // TODO: The animal's EntityID } } @@ -1517,9 +1517,9 @@ void cProtocol180::SendWindowProperty(const cWindow & a_Window, short a_Property ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x31); // Window Property packet - Pkt.WriteChar(a_Window.GetWindowID()); - Pkt.WriteShort(a_Property); - Pkt.WriteShort(a_Value); + Pkt.WriteBEInt8(a_Window.GetWindowID()); + Pkt.WriteBEInt16(a_Property); + Pkt.WriteBEInt16(a_Value); } @@ -1538,7 +1538,10 @@ bool cProtocol180::CompressPacket(const AString & a_Packet, AString & a_Compress return false; } - int Status = compress2((Bytef*)CompressedData, &CompressedSize, (const Bytef*)a_Packet.data(), a_Packet.size(), Z_DEFAULT_COMPRESSION); + int Status = compress2( + reinterpret_cast(CompressedData), &CompressedSize, + reinterpret_cast(a_Packet.data()), a_Packet.size(), Z_DEFAULT_COMPRESSION + ); if (Status != Z_OK) { return false; @@ -1546,12 +1549,12 @@ bool cProtocol180::CompressPacket(const AString & a_Packet, AString & a_Compress AString LengthData; cByteBuffer Buffer(20); - Buffer.WriteVarInt((UInt32)a_Packet.size()); + Buffer.WriteVarInt32(static_cast(a_Packet.size())); Buffer.ReadAll(LengthData); Buffer.CommitRead(); - Buffer.WriteVarInt(CompressedSize + LengthData.size()); - Buffer.WriteVarInt(a_Packet.size()); + Buffer.WriteVarInt32(CompressedSize + LengthData.size()); + Buffer.WriteVarInt32(static_cast(a_Packet.size())); Buffer.ReadAll(LengthData); Buffer.CommitRead(); @@ -1949,7 +1952,7 @@ void cProtocol180::HandlePacketStatusPing(cByteBuffer & a_ByteBuffer) HANDLE_READ(a_ByteBuffer, ReadBEInt64, Int64, Timestamp); cPacketizer Pkt(*this, 0x01); // Ping packet - Pkt.WriteInt64(Timestamp); + Pkt.WriteBEInt64(Timestamp); } @@ -2086,10 +2089,10 @@ void cProtocol180::HandlePacketLoginStart(cByteBuffer & a_ByteBuffer) cPacketizer Pkt(*this, 0x01); Pkt.WriteString(Server->GetServerID()); const AString & PubKeyDer = Server->GetPublicKeyDER(); - Pkt.WriteVarInt((short)PubKeyDer.size()); + Pkt.WriteVarInt32((short)PubKeyDer.size()); Pkt.WriteBuf(PubKeyDer.data(), PubKeyDer.size()); - Pkt.WriteVarInt(4); - Pkt.WriteInt((int)(intptr_t)this); // Using 'this' as the cryptographic nonce, so that we don't have to generate one each time :) + Pkt.WriteVarInt32(4); + Pkt.WriteBEInt32((int)(intptr_t)this); // Using 'this' as the cryptographic nonce, so that we don't have to generate one each time :) m_Client->SetUsername(Username); return; } @@ -2115,7 +2118,7 @@ void cProtocol180::HandlePacketBlockDig(cByteBuffer & a_ByteBuffer) HANDLE_READ(a_ByteBuffer, ReadBEUInt8, UInt8, Status); int BlockX, BlockY, BlockZ; - if (!a_ByteBuffer.ReadPosition(BlockX, BlockY, BlockZ)) + if (!a_ByteBuffer.ReadPosition64(BlockX, BlockY, BlockZ)) { return; } @@ -2131,7 +2134,7 @@ void cProtocol180::HandlePacketBlockDig(cByteBuffer & a_ByteBuffer) void cProtocol180::HandlePacketBlockPlace(cByteBuffer & a_ByteBuffer) { int BlockX, BlockY, BlockZ; - if (!a_ByteBuffer.ReadPosition(BlockX, BlockY, BlockZ)) + if (!a_ByteBuffer.ReadPosition64(BlockX, BlockY, BlockZ)) { return; } @@ -2410,7 +2413,7 @@ void cProtocol180::HandlePacketTabComplete(cByteBuffer & a_ByteBuffer) void cProtocol180::HandlePacketUpdateSign(cByteBuffer & a_ByteBuffer) { int BlockX, BlockY, BlockZ; - if (!a_ByteBuffer.ReadPosition(BlockX, BlockY, BlockZ)) + if (!a_ByteBuffer.ReadPosition64(BlockX, BlockY, BlockZ)) { return; } @@ -2799,8 +2802,8 @@ void cProtocol180::SendPacket(cPacketizer & a_Pkt) else if (m_State == 3) { // The packet is not compressed, indicate this in the packet header: - m_OutPacketLenBuffer.WriteVarInt(PacketLen + 1); - m_OutPacketLenBuffer.WriteVarInt(0); + m_OutPacketLenBuffer.WriteVarInt32(PacketLen + 1); + m_OutPacketLenBuffer.WriteVarInt32(0); AString LengthData; m_OutPacketLenBuffer.ReadAll(LengthData); SendData(LengthData.data(), LengthData.size()); @@ -2808,7 +2811,7 @@ void cProtocol180::SendPacket(cPacketizer & a_Pkt) else { // Compression doesn't apply to this state, send raw data: - m_OutPacketLenBuffer.WriteVarInt(PacketLen); + m_OutPacketLenBuffer.WriteVarInt32(PacketLen); AString LengthData; m_OutPacketLenBuffer.ReadAll(LengthData); SendData(LengthData.data(), LengthData.size()); @@ -2853,17 +2856,17 @@ void cProtocol180::WriteItem(cPacketizer & a_Pkt, const cItem & a_Item) if (a_Item.IsEmpty()) { - a_Pkt.WriteShort(-1); + a_Pkt.WriteBEInt16(-1); return; } - a_Pkt.WriteShort(ItemType); - a_Pkt.WriteByte (a_Item.m_ItemCount); - a_Pkt.WriteShort(a_Item.m_ItemDamage); + a_Pkt.WriteBEInt16(ItemType); + a_Pkt.WriteBEInt8(a_Item.m_ItemCount); + a_Pkt.WriteBEInt16(a_Item.m_ItemDamage); if (a_Item.m_Enchantments.IsEmpty() && a_Item.IsBothNameAndLoreEmpty() && (a_Item.m_ItemType != E_ITEM_FIREWORK_ROCKET) && (a_Item.m_ItemType != E_ITEM_FIREWORK_STAR)) { - a_Pkt.WriteChar(0); + a_Pkt.WriteBEInt8(0); return; } @@ -2913,7 +2916,7 @@ void cProtocol180::WriteItem(cPacketizer & a_Pkt, const cItem & a_Item) AString Result = Writer.GetResult(); if (Result.size() == 0) { - a_Pkt.WriteChar(0); + a_Pkt.WriteBEInt8(0); return; } a_Pkt.WriteBuf(Result.data(), Result.size()); @@ -3038,21 +3041,21 @@ void cProtocol180::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_En { Flags |= 0x20; } - a_Pkt.WriteByte(0); // Byte(0) + index 0 - a_Pkt.WriteByte(Flags); + a_Pkt.WriteBEUInt8(0); // Byte(0) + index 0 + a_Pkt.WriteBEUInt8(Flags); switch (a_Entity.GetEntityType()) { case cEntity::etPlayer: break; // TODO? case cEntity::etPickup: { - a_Pkt.WriteByte((5 << 5) | 10); // Slot(5) + index 10 + a_Pkt.WriteBEUInt8((5 << 5) | 10); // Slot(5) + index 10 WriteItem(a_Pkt, reinterpret_cast(a_Entity).GetItem()); break; } case cEntity::etMinecart: { - a_Pkt.WriteByte(0x51); + a_Pkt.WriteBEUInt8(0x51); // The following expression makes Minecarts shake more with less health or higher damage taken // It gets half the maximum health, and takes it away from the current health minus the half health: @@ -3062,11 +3065,11 @@ void cProtocol180::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_En Health: 1 | 3 - (1 - 3) = 5 */ auto & Minecart = reinterpret_cast(a_Entity); - a_Pkt.WriteInt((((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * Minecart.LastDamage()) * 4); - a_Pkt.WriteByte(0x52); - a_Pkt.WriteInt(1); // Shaking direction, doesn't seem to affect anything - a_Pkt.WriteByte(0x73); - a_Pkt.WriteFloat(static_cast(Minecart.LastDamage() + 10)); // Damage taken / shake effect multiplyer + a_Pkt.WriteBEInt32((((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * Minecart.LastDamage()) * 4); + a_Pkt.WriteBEUInt8(0x52); + a_Pkt.WriteBEInt32(1); // Shaking direction, doesn't seem to affect anything + a_Pkt.WriteBEUInt8(0x73); + a_Pkt.WriteBEFloat(static_cast(Minecart.LastDamage() + 10)); // Damage taken / shake effect multiplyer if (Minecart.GetPayload() == cMinecart::mpNone) { @@ -3074,20 +3077,20 @@ void cProtocol180::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_En const cItem & MinecartContent = RideableMinecart.GetContent(); if (!MinecartContent.IsEmpty()) { - a_Pkt.WriteByte(0x54); + a_Pkt.WriteBEUInt8(0x54); int Content = MinecartContent.m_ItemType; Content |= MinecartContent.m_ItemDamage << 8; - a_Pkt.WriteInt(Content); - a_Pkt.WriteByte(0x55); - a_Pkt.WriteInt(RideableMinecart.GetBlockHeight()); - a_Pkt.WriteByte(0x56); - a_Pkt.WriteByte(1); + a_Pkt.WriteBEInt32(Content); + a_Pkt.WriteBEUInt8(0x55); + a_Pkt.WriteBEInt32(RideableMinecart.GetBlockHeight()); + a_Pkt.WriteBEUInt8(0x56); + a_Pkt.WriteBEUInt8(1); } } else if (Minecart.GetPayload() == cMinecart::mpFurnace) { - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte(reinterpret_cast(Minecart).IsFueled() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8(reinterpret_cast(Minecart).IsFueled() ? 1 : 0); } break; } // case etMinecart @@ -3099,13 +3102,13 @@ void cProtocol180::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_En { case cProjectileEntity::pkArrow: { - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte(reinterpret_cast(Projectile).IsCritical() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8(reinterpret_cast(Projectile).IsCritical() ? 1 : 0); break; } case cProjectileEntity::pkFirework: { - a_Pkt.WriteByte(0xa8); + a_Pkt.WriteBEUInt8(0xa8); WriteItem(a_Pkt, reinterpret_cast(Projectile).GetItem()); break; } @@ -3126,10 +3129,10 @@ void cProtocol180::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a_En case cEntity::etItemFrame: { auto & Frame = reinterpret_cast(a_Entity); - a_Pkt.WriteByte(0xa8); + a_Pkt.WriteBEUInt8(0xa8); WriteItem(a_Pkt, Frame.GetItem()); - a_Pkt.WriteByte(0x09); - a_Pkt.WriteByte(Frame.GetItemRotation()); + a_Pkt.WriteBEUInt8(0x09); + a_Pkt.WriteBEUInt8(Frame.GetItemRotation()); break; } // case etItemFrame @@ -3151,38 +3154,38 @@ void cProtocol180::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_Mob) case mtBat: { auto & Bat = reinterpret_cast(a_Mob); - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte(Bat.IsHanging() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8(Bat.IsHanging() ? 1 : 0); break; } // case mtBat case mtCreeper: { auto & Creeper = reinterpret_cast(a_Mob); - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte(Creeper.IsBlowing() ? 1 : -1); - a_Pkt.WriteByte(0x11); - a_Pkt.WriteByte(Creeper.IsCharged() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8(Creeper.IsBlowing() ? 1 : -1); + a_Pkt.WriteBEUInt8(0x11); + a_Pkt.WriteBEUInt8(Creeper.IsCharged() ? 1 : 0); break; } // case mtCreeper case mtEnderman: { auto & Enderman = reinterpret_cast(a_Mob); - a_Pkt.WriteByte(0x30); - a_Pkt.WriteShort(static_cast(Enderman.GetCarriedBlock())); - a_Pkt.WriteByte(0x11); - a_Pkt.WriteByte(static_cast(Enderman.GetCarriedMeta())); - a_Pkt.WriteByte(0x12); - a_Pkt.WriteByte(Enderman.IsScreaming() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x30); + a_Pkt.WriteBEInt16(static_cast(Enderman.GetCarriedBlock())); + a_Pkt.WriteBEUInt8(0x11); + a_Pkt.WriteBEUInt8(static_cast(Enderman.GetCarriedMeta())); + a_Pkt.WriteBEUInt8(0x12); + a_Pkt.WriteBEUInt8(Enderman.IsScreaming() ? 1 : 0); break; } // case mtEnderman case mtGhast: { auto & Ghast = reinterpret_cast(a_Mob); - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte(Ghast.IsCharging()); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8(Ghast.IsCharging()); break; } // case mtGhast @@ -3218,89 +3221,89 @@ void cProtocol180::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_Mob) { Flags |= 0x80; } - a_Pkt.WriteByte(0x50); // Int at index 16 - a_Pkt.WriteInt(Flags); - a_Pkt.WriteByte(0x13); // Byte at index 19 - a_Pkt.WriteByte(Horse.GetHorseType()); - a_Pkt.WriteByte(0x54); // Int at index 20 + a_Pkt.WriteBEUInt8(0x50); // Int at index 16 + a_Pkt.WriteBEInt32(Flags); + a_Pkt.WriteBEUInt8(0x13); // Byte at index 19 + a_Pkt.WriteBEUInt8(Horse.GetHorseType()); + a_Pkt.WriteBEUInt8(0x54); // Int at index 20 int Appearance = 0; Appearance = Horse.GetHorseColor(); Appearance |= Horse.GetHorseStyle() << 8; - a_Pkt.WriteInt(Appearance); - a_Pkt.WriteByte(0x56); // Int at index 22 - a_Pkt.WriteInt(Horse.GetHorseArmour()); + a_Pkt.WriteBEInt32(Appearance); + a_Pkt.WriteBEUInt8(0x56); // Int at index 22 + a_Pkt.WriteBEInt32(Horse.GetHorseArmour()); break; } // case mtHorse case mtMagmaCube: { auto & MagmaCube = reinterpret_cast(a_Mob); - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte(MagmaCube.GetSize()); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8(MagmaCube.GetSize()); break; } // case mtMagmaCube case mtPig: { auto & Pig = reinterpret_cast(a_Mob); - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte(Pig.IsSaddled() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8(Pig.IsSaddled() ? 1 : 0); break; } // case mtPig case mtSheep: { auto & Sheep = reinterpret_cast(a_Mob); - a_Pkt.WriteByte(0x10); + a_Pkt.WriteBEUInt8(0x10); Byte SheepMetadata = 0; SheepMetadata = Sheep.GetFurColor(); if (Sheep.IsSheared()) { SheepMetadata |= 0x10; } - a_Pkt.WriteByte(SheepMetadata); + a_Pkt.WriteBEUInt8(SheepMetadata); break; } // case mtSheep case mtSkeleton: { auto & Skeleton = reinterpret_cast(a_Mob); - a_Pkt.WriteByte(0x0d); - a_Pkt.WriteByte(Skeleton.IsWither() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x0d); + a_Pkt.WriteBEUInt8(Skeleton.IsWither() ? 1 : 0); break; } // case mtSkeleton case mtSlime: { auto & Slime = reinterpret_cast(a_Mob); - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte(Slime.GetSize()); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8(Slime.GetSize()); break; } // case mtSlime case mtVillager: { auto & Villager = reinterpret_cast(a_Mob); - a_Pkt.WriteByte(0x50); - a_Pkt.WriteInt(Villager.GetVilType()); + a_Pkt.WriteBEUInt8(0x50); + a_Pkt.WriteBEInt32(Villager.GetVilType()); break; } // case mtVillager case mtWitch: { auto & Witch = reinterpret_cast(a_Mob); - a_Pkt.WriteByte(0x15); - a_Pkt.WriteByte(Witch.IsAngry() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x15); + a_Pkt.WriteBEUInt8(Witch.IsAngry() ? 1 : 0); break; } // case mtWitch case mtWither: { auto & Wither = reinterpret_cast(a_Mob); - a_Pkt.WriteByte(0x54); // Int at index 20 - a_Pkt.WriteInt(Wither.GetWitherInvulnerableTicks()); - a_Pkt.WriteByte(0x66); // Float at index 6 - a_Pkt.WriteFloat(static_cast(a_Mob.GetHealth())); + a_Pkt.WriteBEUInt8(0x54); // Int at index 20 + a_Pkt.WriteBEInt32(Wither.GetWitherInvulnerableTicks()); + a_Pkt.WriteBEUInt8(0x66); // Float at index 6 + a_Pkt.WriteBEFloat(static_cast(a_Mob.GetHealth())); break; } // case mtWither @@ -3320,27 +3323,27 @@ void cProtocol180::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_Mob) { WolfStatus |= 0x4; } - a_Pkt.WriteByte(0x10); - a_Pkt.WriteByte(WolfStatus); - - a_Pkt.WriteByte(0x72); - a_Pkt.WriteFloat(static_cast(a_Mob.GetHealth())); - a_Pkt.WriteByte(0x13); - a_Pkt.WriteByte(Wolf.IsBegging() ? 1 : 0); - a_Pkt.WriteByte(0x14); - a_Pkt.WriteByte(Wolf.GetCollarColor()); + a_Pkt.WriteBEUInt8(0x10); + a_Pkt.WriteBEUInt8(WolfStatus); + + a_Pkt.WriteBEUInt8(0x72); + a_Pkt.WriteBEFloat(static_cast(a_Mob.GetHealth())); + a_Pkt.WriteBEUInt8(0x13); + a_Pkt.WriteBEUInt8(Wolf.IsBegging() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x14); + a_Pkt.WriteBEUInt8(Wolf.GetCollarColor()); break; } // case mtWolf case mtZombie: { auto & Zombie = reinterpret_cast(a_Mob); - a_Pkt.WriteByte(0x0c); - a_Pkt.WriteByte(Zombie.IsBaby() ? 1 : 0); - a_Pkt.WriteByte(0x0d); - a_Pkt.WriteByte(Zombie.IsVillagerZombie() ? 1 : 0); - a_Pkt.WriteByte(0x0e); - a_Pkt.WriteByte(Zombie.IsConverting() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x0c); + a_Pkt.WriteBEUInt8(Zombie.IsBaby() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x0d); + a_Pkt.WriteBEUInt8(Zombie.IsVillagerZombie() ? 1 : 0); + a_Pkt.WriteBEUInt8(0x0e); + a_Pkt.WriteBEUInt8(Zombie.IsConverting() ? 1 : 0); break; } // case mtZombie } // switch (a_Mob.GetType()) @@ -3355,7 +3358,7 @@ void cProtocol180::WriteEntityProperties(cPacketizer & a_Pkt, const cEntity & a_ if (!a_Entity.IsMob()) { // No properties for anything else than mobs - a_Pkt.WriteInt(0); + a_Pkt.WriteBEInt32(0); return; } @@ -3363,7 +3366,7 @@ void cProtocol180::WriteEntityProperties(cPacketizer & a_Pkt, const cEntity & a_ // TODO: Send properties and modifiers based on the mob type - a_Pkt.WriteInt(0); // NumProperties + a_Pkt.WriteBEInt32(0); // NumProperties } -- cgit v1.2.3