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
This commit is contained in:
ReinforceZwei 2020-06-14 21:14:51 +08:00 committed by GitHub
parent 09bfe92071
commit ab05d697ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 6 deletions

View file

@ -996,7 +996,7 @@ namespace MinecraftClient
protected Container GetPlayerInventory() protected Container GetPlayerInventory()
{ {
Container container = Handler.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);
} }
/// <summary> /// <summary>

View file

@ -34,6 +34,7 @@ namespace MinecraftClient.ChatBots
public override void OnHealthUpdate(float health, int food) public override void OnHealthUpdate(float health, int food)
{ {
if (health <= 0) return; // player dead
if (((food <= HungerThreshold) || (food < 20 && health < 20)) && !Eating) if (((food <= HungerThreshold) || (food < 20 && health < 20)) && !Eating)
{ {
Eating = FindFoodAndEat(); Eating = FindFoodAndEat();

View file

@ -1615,7 +1615,20 @@ namespace MinecraftClient
} }
} }
foreach (ChatBot bot in bots.ToArray()) 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
}
}
} }
/// <summary> /// <summary>
@ -1629,7 +1642,20 @@ namespace MinecraftClient
playerLevel = Level; playerLevel = Level;
playerTotalExperience = TotalExperience; playerTotalExperience = TotalExperience;
foreach (ChatBot bot in bots.ToArray()) 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
}
}
} }
/// <summary> /// <summary>
@ -1641,7 +1667,20 @@ namespace MinecraftClient
public void OnExplosion(Location location, float strength, int affectedBlocks) public void OnExplosion(Location location, float strength, int affectedBlocks)
{ {
foreach (ChatBot bot in bots.ToArray()) 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
}
}
} }
/// <summary> /// <summary>
@ -1656,7 +1695,20 @@ namespace MinecraftClient
{ {
playerName = onlinePlayers[uuid]; playerName = onlinePlayers[uuid];
foreach (ChatBot bot in bots.ToArray()) 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) public void OnHeldItemChange(byte slot)
{ {
foreach (ChatBot bot in bots.ToArray()) 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; CurrentSlot = slot;
} }
} }