diff --git a/MinecraftClient/ChatBot.cs b/MinecraftClient/ChatBot.cs index f30f1544..48ce77c0 100644 --- a/MinecraftClient/ChatBot.cs +++ b/MinecraftClient/ChatBot.cs @@ -168,6 +168,10 @@ namespace MinecraftClient /// Entity wich has just disappeared public virtual void OnEntityDespawn(Mapping.Entity entity) { } + public virtual void OnHeldItemChange(byte slot) { } + + public virtual void OnHealthUpdate(float health, int food) { } + /* =================================================================== */ /* ToolBox - Methods below might be useful while creating your bot. */ /* You should not need to interact with other classes of the program. */ @@ -816,6 +820,10 @@ namespace MinecraftClient return Handler.SendPluginChannelMessage(channel, data, sendEvenIfNotRegistered); } + /// + /// Get server current TPS (tick per second) + /// + /// tps protected Double GetServerTPS() { return Handler.GetServerTPS(); @@ -852,13 +860,13 @@ namespace MinecraftClient } /// - /// Check if player is eating or not + /// Change player selected hotbar /// - /// Some bot like AutoAttack need this. We don't want to attack while eating - /// True if is eating - protected bool GetIsEating() + /// + /// True if success + protected bool ChangeSlot(short slot) { - return Handler.GetIsEating(); + return Handler.ChangeSlot(slot); } } } diff --git a/MinecraftClient/ChatBots/AutoAttack.cs b/MinecraftClient/ChatBots/AutoAttack.cs index f7b0a8d8..e6c937fd 100644 --- a/MinecraftClient/ChatBots/AutoAttack.cs +++ b/MinecraftClient/ChatBots/AutoAttack.cs @@ -32,7 +32,7 @@ namespace MinecraftClient.ChatBots public override void Update() { - if (!GetIsEating()) + if (!AutoEat.Eating) { if (attackCooldownCounter == 0) { diff --git a/MinecraftClient/ChatBots/AutoEat.cs b/MinecraftClient/ChatBots/AutoEat.cs new file mode 100644 index 00000000..1b058c0d --- /dev/null +++ b/MinecraftClient/ChatBots/AutoEat.cs @@ -0,0 +1,76 @@ +using MinecraftClient.Inventory; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace MinecraftClient.ChatBots +{ + class AutoEat : ChatBot + { + byte LastSlot = 0; + byte CurrentSlot; + public static bool Eating = false; + private int HungerThreshold = 6; + + public AutoEat(int Threshold) + { + HungerThreshold = Threshold; + } + + public override void OnHealthUpdate(float health, int food) + { + if (food <= HungerThreshold || (food < 20 && health < 20)) + { + Eating = true; + FindFoodAndEat(); + } + // keep eating until full + if (food < 20 && Eating) + { + FindFoodAndEat(); + } + if (food >= 20 && Eating) + { + Eating = false; + ChangeSlot(LastSlot); + } + } + + public override void OnHeldItemChange(byte slot) + { + CurrentSlot = slot; + } + /// + /// Try to find food in the hotbar and eat it + /// + /// True if found + public bool FindFoodAndEat() + { + Container inventory = GetPlayerInventory(); + bool found = false; + LastSlot = CurrentSlot; + if (inventory.Items.ContainsKey(CurrentSlot + 36) && inventory.Items[CurrentSlot + 36].IsFood()) + { + // no need to change slot + found = true; + } + else + { + for (int i = 36; i <= 44; i++) + { + if (!inventory.Items.ContainsKey(i)) continue; + if (inventory.Items[i].IsFood()) + { + int slot = i - 36; + ChangeSlot((short)slot); + found = true; + break; + } + } + } + if (found) UseItemOnHand(); + return found; + } + } +} diff --git a/MinecraftClient/McTcpClient.cs b/MinecraftClient/McTcpClient.cs index e248118c..e7b82812 100644 --- a/MinecraftClient/McTcpClient.cs +++ b/MinecraftClient/McTcpClient.cs @@ -62,10 +62,7 @@ namespace MinecraftClient // player health and hunger private float playerHealth; private int playerFoodSaturation; - private bool Eating = false; - private int HungerThreshold = 6; private byte CurrentSlot = 0; - private byte LastSlot = 0; // for switch back to origin slot after eating // Entity handling private Dictionary entities = new Dictionary(); @@ -86,7 +83,6 @@ namespace MinecraftClient public float GetHealth() { return playerHealth; } public int GetSaturation() { return playerFoodSaturation; } public byte GetCurrentSlot() { return CurrentSlot; } - public bool GetIsEating() { return Eating; } // get bots list for unloading them by commands public List GetLoadedChatBots() @@ -173,6 +169,7 @@ namespace MinecraftClient if (Settings.AutoRespond_Enabled) { BotLoad(new ChatBots.AutoRespond(Settings.AutoRespond_Matches)); } if (Settings.AutoAttack_Enabled) { BotLoad(new ChatBots.AutoAttack()); } if (Settings.AutoFishing_Enabled) { BotLoad(new ChatBots.AutoFishing()); } + if (Settings.AutoEat_Enabled) { BotLoad(new ChatBots.AutoEat(Settings.AutoEat_hungerThreshold)); } //Add your ChatBot here by uncommenting and adapting //BotLoad(new ChatBots.YourBot()); } @@ -1566,61 +1563,15 @@ namespace MinecraftClient ConsoleIO.WriteLogLine("You are dead. Type /respawn to respawn."); } } - if (Settings.AutoEat) - { - if (food <= HungerThreshold || (food < 20 && health < 20)) - { - Eating = true; - FindFoodAndEat(); - } - // keep eating until full - if (food < 20 && Eating) - { - FindFoodAndEat(); - } - if (food >= 20 && Eating) - { - Eating = false; - ChangeSlot(LastSlot); - } - } + foreach (ChatBot bot in bots.ToArray()) + bot.OnHealthUpdate(health, food); } public void OnHeldItemChange(byte slot) { + foreach (ChatBot bot in bots.ToArray()) + bot.OnHeldItemChange(slot); CurrentSlot = slot; } - - /// - /// Try to find food in the hotbar and eat it - /// - /// True if found - public bool FindFoodAndEat() - { - Container inventory = inventories[0]; - bool found = false; - LastSlot = CurrentSlot; - if (inventory.Items.ContainsKey(CurrentSlot + 36) && inventory.Items[CurrentSlot + 36].IsFood()) - { - // no need to change slot - found = true; - } - else - { - for (int i = 36; i <= 44; i++) - { - if (!inventory.Items.ContainsKey(i)) continue; - if (inventory.Items[i].IsFood()) - { - int slot = i - 36; - ChangeSlot((short)slot); - found = true; - break; - } - } - } - if (found) UseItemOnHand(); - return found; - } } } diff --git a/MinecraftClient/MinecraftClient.csproj b/MinecraftClient/MinecraftClient.csproj index e5d7bcad..80731ca5 100644 --- a/MinecraftClient/MinecraftClient.csproj +++ b/MinecraftClient/MinecraftClient.csproj @@ -76,6 +76,7 @@ + diff --git a/MinecraftClient/Settings.cs b/MinecraftClient/Settings.cs index 79fab392..fca92a25 100644 --- a/MinecraftClient/Settings.cs +++ b/MinecraftClient/Settings.cs @@ -97,7 +97,6 @@ namespace MinecraftClient public static bool ResolveSrvRecordsShortTimeout = true; public static bool EntityHandling = false; public static bool AutoRespawn = false; - public static bool AutoEat = false; //AntiAFK Settings public static bool AntiAFK_Enabled = false; @@ -159,12 +158,16 @@ namespace MinecraftClient //Auto Fishing public static bool AutoFishing_Enabled = false; + //Auto Eating + public static bool AutoEat_Enabled = false; + public static int AutoEat_hungerThreshold = 6; + //Custom app variables and Minecraft accounts private static readonly Dictionary AppVars = new Dictionary(); private static readonly Dictionary> Accounts = new Dictionary>(); private static readonly Dictionary> Servers = new Dictionary>(); - private enum ParseMode { Default, Main, AppVars, Proxy, MCSettings, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, ScriptScheduler, RemoteControl, ChatFormat, AutoRespond, AutoAttack, AutoFishing }; + private enum ParseMode { Default, Main, AppVars, Proxy, MCSettings, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, ScriptScheduler, RemoteControl, ChatFormat, AutoRespond, AutoAttack, AutoFishing, AutoEat }; /// /// Load settings from the give INI file @@ -207,6 +210,7 @@ namespace MinecraftClient case "chatformat": pMode = ParseMode.ChatFormat; break; case "autoattack": pMode = ParseMode.AutoAttack; break; case "autofishing": pMode = ParseMode.AutoFishing; break; + case "autoeat": pMode = ParseMode.AutoEat; break; default: pMode = ParseMode.Default; break; } } @@ -245,7 +249,6 @@ namespace MinecraftClient case "botmessagedelay": botMessageDelay = TimeSpan.FromSeconds(str2int(argValue)); break; case "debugmessages": DebugMessages = str2bool(argValue); break; case "autorespawn": AutoRespawn = str2bool(argValue); break; - case "autoeat": AutoEat = str2bool(argValue); break; case "botowners": Bots_Owners.Clear(); @@ -478,6 +481,14 @@ namespace MinecraftClient } break; + case ParseMode.AutoEat: + switch (argName.ToLower()) + { + case "enabled": AutoEat_Enabled = str2bool(argValue); break; + case "threshold": AutoEat_hungerThreshold = str2int(argValue); break; + } + break; + case ParseMode.MCSettings: switch (argName.ToLower()) { @@ -591,7 +602,6 @@ namespace MinecraftClient + "scriptcache=true # Cache compiled scripts for faster load on low-end devices\r\n" + "timestamps=false # Prepend timestamps to chat messages\r\n" + "autorespawn=false # Toggle auto respawn if client player was dead (make sure your spawn point is safe)\r\n" - + "autoeat=false # Toggle auto eat when player is hungry\r\n" + "\r\n" + "[AppVars]\r\n" + "# yourvar=yourvalue\r\n" @@ -679,7 +689,12 @@ namespace MinecraftClient + "\r\n" + "[AutoFishing]\r\n" + "# Entity Handling NEED to be enabled first\r\n" - + "enabled=false", Encoding.UTF8); + + "enabled=false" + + "\r\n" + + "[AutoEat]\r\n" + + "# Inventory Handling NEED to be enabled first\r\n" + + "enabled=false\r\n" + + "threshold=6", Encoding.UTF8); } ///