From aaf6dca522134f26773c970683056d7aec2f7982 Mon Sep 17 00:00:00 2001 From: ReinforceZwei <39955851+ReinforceZwei@users.noreply.github.com> Date: Sun, 24 May 2020 15:06:06 +0800 Subject: [PATCH] Add drop item for inventory --- MinecraftClient/Commands/Inventory.cs | 24 ++++++++++++++++--- MinecraftClient/Inventory/WindowActionType.cs | 3 ++- .../Protocol/Handlers/Protocol18.cs | 12 ++++++++-- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/MinecraftClient/Commands/Inventory.cs b/MinecraftClient/Commands/Inventory.cs index 4f2907ac..012949c0 100644 --- a/MinecraftClient/Commands/Inventory.cs +++ b/MinecraftClient/Commands/Inventory.cs @@ -9,7 +9,7 @@ namespace MinecraftClient.Commands class Inventory : Command { public override string CMDName { get { return "inventory"; } } - public override string CMDDesc { get { return "inventory <|player|container> >: Interact with inventories"; } } + public override string CMDDesc { get { return "inventory <|player|container> |click >: Interact with inventories"; } } public override string Run(McTcpClient handler, string command, Dictionary localVars) { @@ -87,9 +87,27 @@ namespace MinecraftClient.Commands if (args.Length >= 3) { int slot = int.Parse(args[2]); - handler.DoWindowAction(inventoryId, slot, WindowActionType.DropItem); + // check item exist + if (!handler.GetInventory(inventoryId).Items.ContainsKey(slot)) + return "No item in slot #" + slot; + WindowActionType actionType = WindowActionType.DropItem; + if (args.Length == 4) + { + if (args[3].ToLower() == "all") + { + actionType = WindowActionType.DropItemStack; + } + } + if (handler.DoWindowAction(inventoryId, slot, actionType)) + { + return "Dropped item from slot #" + slot; + } + else + { + return "Failed"; + } } - return "Dropped"; + else return CMDDesc; default: return CMDDesc; } diff --git a/MinecraftClient/Inventory/WindowActionType.cs b/MinecraftClient/Inventory/WindowActionType.cs index e4e9c23f..53ab536a 100644 --- a/MinecraftClient/Inventory/WindowActionType.cs +++ b/MinecraftClient/Inventory/WindowActionType.cs @@ -10,6 +10,7 @@ namespace MinecraftClient.Inventory LeftClick, RightClick, MiddleClick, - DropItem + DropItem, + DropItemStack } } diff --git a/MinecraftClient/Protocol/Handlers/Protocol18.cs b/MinecraftClient/Protocol/Handlers/Protocol18.cs index bad43541..e95fd60d 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol18.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol18.cs @@ -1366,8 +1366,16 @@ namespace MinecraftClient.Protocol.Handlers button = 0; mode = 4; item = new Item(-1, 0, null); - Container inventory = handler.GetInventory(0); - inventory.Items[slotId].Count--; // server won't update us after dropped + Container inventory = handler.GetInventory(windowId); + if (inventory.Items.ContainsKey(slotId)) + inventory.Items[slotId].Count--; // server won't update us after dropped + break; + case WindowActionType.DropItemStack: + button = 1; + mode = 4; + item = new Item(-1, 0, null); + inventory = handler.GetInventory(windowId); + inventory.Items.Remove(slotId); // server won't update us after dropped break; }