From ab05d697ef6aa6f3a6a7eac20903c34b7c4b23e7 Mon Sep 17 00:00:00 2001 From: ReinforceZwei <39955851+ReinforceZwei@users.noreply.github.com> Date: Sun, 14 Jun 2020 21:14:51 +0800 Subject: [PATCH] Fix AutoEat crash (#1068) * Fix AutoEat keep eating after player dead * Fix null reference on GetInventory method * Add error handle for ChatBot API events * Fix minor mistakes --- MinecraftClient/ChatBot.cs | 2 +- MinecraftClient/ChatBots/AutoEat.cs | 1 + MinecraftClient/McTcpClient.cs | 75 +++++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 6 deletions(-) diff --git a/MinecraftClient/ChatBot.cs b/MinecraftClient/ChatBot.cs index d1cd57d6..62a1e043 100644 --- a/MinecraftClient/ChatBot.cs +++ b/MinecraftClient/ChatBot.cs @@ -996,7 +996,7 @@ namespace MinecraftClient protected Container GetPlayerInventory() { Container container = Handler.GetPlayerInventory(); - return new Container(container.ID, container.Type, container.Title, container.Items); + return container == null ? null : new Container(container.ID, container.Type, container.Title, container.Items); } /// diff --git a/MinecraftClient/ChatBots/AutoEat.cs b/MinecraftClient/ChatBots/AutoEat.cs index 0a32d7ac..6e18d934 100644 --- a/MinecraftClient/ChatBots/AutoEat.cs +++ b/MinecraftClient/ChatBots/AutoEat.cs @@ -34,6 +34,7 @@ namespace MinecraftClient.ChatBots public override void OnHealthUpdate(float health, int food) { + if (health <= 0) return; // player dead if (((food <= HungerThreshold) || (food < 20 && health < 20)) && !Eating) { Eating = FindFoodAndEat(); diff --git a/MinecraftClient/McTcpClient.cs b/MinecraftClient/McTcpClient.cs index 94723480..a652ff7f 100644 --- a/MinecraftClient/McTcpClient.cs +++ b/MinecraftClient/McTcpClient.cs @@ -1615,7 +1615,20 @@ namespace MinecraftClient } } foreach (ChatBot bot in bots.ToArray()) - bot.OnHealthUpdate(health, food); + { + try + { + bot.OnHealthUpdate(health, food); + } + catch (Exception e) + { + if (!(e is ThreadAbortException)) + { + ConsoleIO.WriteLogLine("OnHealthUpdate: Got error from " + bot.ToString() + ": " + e.ToString()); + } + else throw; //ThreadAbortException should not be caught + } + } } /// @@ -1629,7 +1642,20 @@ namespace MinecraftClient playerLevel = Level; playerTotalExperience = TotalExperience; foreach (ChatBot bot in bots.ToArray()) - bot.OnSetExperience(Experiencebar, Level, TotalExperience); + { + try + { + bot.OnSetExperience(Experiencebar, Level, TotalExperience); + } + catch (Exception e) + { + if (!(e is ThreadAbortException)) + { + ConsoleIO.WriteLogLine("OnSetExperience: Got error from " + bot.ToString() + ": " + e.ToString()); + } + else throw; //ThreadAbortException should not be caught + } + } } /// @@ -1641,7 +1667,20 @@ namespace MinecraftClient public void OnExplosion(Location location, float strength, int affectedBlocks) { foreach (ChatBot bot in bots.ToArray()) - bot.OnExplosion(location, strength, affectedBlocks); + { + try + { + bot.OnExplosion(location, strength, affectedBlocks); + } + catch (Exception e) + { + if (!(e is ThreadAbortException)) + { + ConsoleIO.WriteLogLine("OnExplosion: Got error from " + bot.ToString() + ": " + e.ToString()); + } + else throw; //ThreadAbortException should not be caught + } + } } /// @@ -1656,7 +1695,20 @@ namespace MinecraftClient { playerName = onlinePlayers[uuid]; foreach (ChatBot bot in bots.ToArray()) - bot.OnLatencyUpdate(playerName, uuid, latency); + { + try + { + bot.OnLatencyUpdate(playerName, uuid, latency); + } + catch (Exception e) + { + if (!(e is ThreadAbortException)) + { + ConsoleIO.WriteLogLine("OnLatencyUpdate: Got error from " + bot.ToString() + ": " + e.ToString()); + } + else throw; //ThreadAbortException should not be caught + } + } } } @@ -1669,7 +1721,20 @@ namespace MinecraftClient public void OnHeldItemChange(byte slot) { foreach (ChatBot bot in bots.ToArray()) - bot.OnHeldItemChange(slot); + { + try + { + bot.OnHeldItemChange(slot); + } + catch (Exception e) + { + if (!(e is ThreadAbortException)) + { + ConsoleIO.WriteLogLine("OnHeldItemChange: Got error from " + bot.ToString() + ": " + e.ToString()); + } + else throw; //ThreadAbortException should not be caught + } + } CurrentSlot = slot; } }