From a51368a85929b43dab42ed9c2140df0b6377bad3 Mon Sep 17 00:00:00 2001 From: ReinforceZwei <39955851+ReinforceZwei@users.noreply.github.com> Date: Tue, 7 Jul 2020 11:38:15 +0800 Subject: [PATCH] Add a proof of concept working demo --- MinecraftClient/ChatBots/AutoCarft.cs | 81 ++++++++++++++++++++++++++ MinecraftClient/McClient.cs | 2 + MinecraftClient/MinecraftClient.csproj | 1 + 3 files changed, 84 insertions(+) create mode 100644 MinecraftClient/ChatBots/AutoCarft.cs diff --git a/MinecraftClient/ChatBots/AutoCarft.cs b/MinecraftClient/ChatBots/AutoCarft.cs new file mode 100644 index 00000000..a8015d4b --- /dev/null +++ b/MinecraftClient/ChatBots/AutoCarft.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using MinecraftClient.Inventory; + +namespace MinecraftClient.ChatBots +{ + class AutoCarft : ChatBot + { + private bool waitingForResult = false; + private int inventoryInUse; + + public override void Initialize() + { + RegisterChatBotCommand("craft", "craft", CraftCommand); + } + + public string CraftCommand(string command, string[] args) + { + Dictionary recipe = new Dictionary + { + { 1, ItemType.Stone } + }; + var inventory = GetInventories()[0]; + int slotToPut = -2; + int slotToTake = -2; + inventoryInUse = 0; + foreach (KeyValuePair slot in recipe) + { + slotToPut = slot.Key + 1; + // Find material in our inventory + foreach (KeyValuePair item in inventory.Items) + { + if (slot.Value == item.Value.Type) + { + slotToTake = item.Key; + break; + } + } + if (slotToTake != -2) + { + // move found material to correct crafting slot + WindowAction(0, slotToTake, WindowActionType.LeftClick); + WindowAction(0, slotToPut, WindowActionType.LeftClick); + } + } + if (slotToPut != -2 && slotToTake != -2) + { + waitingForResult = true; + // Now wait for server to update the slot 0, craft result + return "Waiting for result"; + } + else return "Failed before waiting for result"; + } + + public override void OnInventoryUpdate(int inventoryId) + { + ConsoleIO.WriteLine("Inventory " + inventoryId + " is being updated"); + if (waitingForResult && inventoryInUse == inventoryId) + { + var inventory = GetInventories()[inventoryId]; + if (inventory.Items.ContainsKey(0)) + { + // slot 0 have item, click on it + WindowAction(0, 0, WindowActionType.LeftClick); + // Now wait for server to update our inventory + ConsoleIO.WriteLine("Crafting success"); + } + else if (inventory.Items.ContainsKey(-1)) + { + // Server have updated our cursor to the item we want to take out from craft result + // Now put the item back to our inventory + WindowAction(0, 37, WindowActionType.LeftClick); + ConsoleIO.WriteLine("Moved crafted item to inventory"); + waitingForResult = false; + } + } + } + } +} diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index 220ccf3b..6e49d6a1 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -176,6 +176,8 @@ namespace MinecraftClient if (Settings.AutoFishing_Enabled) { BotLoad(new ChatBots.AutoFishing()); } if (Settings.AutoEat_Enabled) { BotLoad(new ChatBots.AutoEat(Settings.AutoEat_hungerThreshold)); } + BotLoad(new AutoCarft()); + //Add your ChatBot here by uncommenting and adapting //BotLoad(new ChatBots.YourBot()); } diff --git a/MinecraftClient/MinecraftClient.csproj b/MinecraftClient/MinecraftClient.csproj index cc4ff166..9838ac10 100644 --- a/MinecraftClient/MinecraftClient.csproj +++ b/MinecraftClient/MinecraftClient.csproj @@ -77,6 +77,7 @@ +