From 2ce0311949e931729d95ffb48b77bf1a84ec41f8 Mon Sep 17 00:00:00 2001 From: Anon Date: Tue, 12 Mar 2024 15:05:47 +0100 Subject: [PATCH] Fixed a crash in SendPlayerBlockPlacement --- .../Protocol/Handlers/Protocol18.cs | 67 +++++++++++-------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/MinecraftClient/Protocol/Handlers/Protocol18.cs b/MinecraftClient/Protocol/Handlers/Protocol18.cs index 10a28af3..9fff7bfe 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol18.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol18.cs @@ -3918,41 +3918,54 @@ namespace MinecraftClient.Protocol.Handlers public bool SendPlayerBlockPlacement(int hand, Location location, Direction face, int sequenceId) { - if (protocolVersion < MC_1_14_Version) - { - var playerInventory = handler.GetInventory(0); - - if (playerInventory == null) - return false; - - var packet = new List(); - - packet.AddRange(dataTypes.GetLocation(location)); - packet.Add(dataTypes.GetBlockFace(face)); - - var item = playerInventory.Items[((McClient)handler).GetCurrentSlot()]; - packet.AddRange(dataTypes.GetItemSlot(item, itemPalette)); - - packet.Add(0); // cursorX - packet.Add(0); // cursorY - packet.Add(0); // cursorZ - - SendPacket(PacketTypesOut.PlayerBlockPlacement, packet); - return true; - } - try { var packet = new List(); - packet.AddRange(DataTypes.GetVarInt(hand)); - packet.AddRange(dataTypes.GetLocation(location)); - packet.AddRange(DataTypes.GetVarInt(dataTypes.GetBlockFace(face))); + + switch (protocolVersion) + { + case < MC_1_9_Version: + packet.AddRange(dataTypes.GetLocation(location)); + packet.Add(dataTypes.GetBlockFace(face)); + + var playerInventory = handler.GetInventory(0); + + if (playerInventory?.Items is null) + return false; + + var slotWindowIds = new int[]{ 36, 37, 38, 39, 40, 41, 42, 43, 44 }; + var currentSlot = ((McClient)handler).GetCurrentSlot(); + + playerInventory.Items.TryGetValue(slotWindowIds[currentSlot], out var item); + packet.AddRange(dataTypes.GetItemSlot(item, itemPalette)); + + packet.Add(0); // cursorX + packet.Add(0); // cursorY + packet.Add(0); // cursorZ + + return true; + case < MC_1_14_Version: + packet.AddRange(dataTypes.GetLocation(location)); + packet.AddRange(DataTypes.GetVarInt(dataTypes.GetBlockFace(face))); + packet.AddRange(DataTypes.GetVarInt(hand)); + break; + default: + packet.AddRange(DataTypes.GetVarInt(hand)); + packet.AddRange(dataTypes.GetLocation(location)); + packet.AddRange(DataTypes.GetVarInt(dataTypes.GetBlockFace(face))); + break; + } + packet.AddRange(dataTypes.GetFloat(0.5f)); // cursorX packet.AddRange(dataTypes.GetFloat(0.5f)); // cursorY packet.AddRange(dataTypes.GetFloat(0.5f)); // cursorZ - packet.Add(0); // insideBlock = false; + + if(protocolVersion >= MC_1_14_Version) + packet.Add(0); // insideBlock = false + if (protocolVersion >= MC_1_19_Version) packet.AddRange(DataTypes.GetVarInt(sequenceId)); + SendPacket(PacketTypesOut.PlayerBlockPlacement, packet); return true; }