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()
{
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>

View file

@ -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();

View file

@ -1615,8 +1615,21 @@ namespace MinecraftClient
}
}
foreach (ChatBot bot in bots.ToArray())
{
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>
/// Called when experience updates
@ -1629,8 +1642,21 @@ namespace MinecraftClient
playerLevel = Level;
playerTotalExperience = TotalExperience;
foreach (ChatBot bot in bots.ToArray())
{
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>
/// Called when and explosion occurs on the server
@ -1641,8 +1667,21 @@ namespace MinecraftClient
public void OnExplosion(Location location, float strength, int affectedBlocks)
{
foreach (ChatBot bot in bots.ToArray())
{
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>
/// Called when Latency is updated
@ -1656,8 +1695,21 @@ namespace MinecraftClient
{
playerName = onlinePlayers[uuid];
foreach (ChatBot bot in bots.ToArray())
{
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
}
}
}
}
/// <summary>
@ -1669,7 +1721,20 @@ namespace MinecraftClient
public void OnHeldItemChange(byte slot)
{
foreach (ChatBot bot in bots.ToArray())
{
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;
}
}