Minecraft-Console-Client/MinecraftClient/ChatBots/AutoEat.cs

83 lines
2.4 KiB
C#
Raw Normal View History

2020-04-08 18:23:19 +08:00
using MinecraftClient.Inventory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
2020-04-08 18:23:19 +08:00
namespace MinecraftClient.ChatBots
{
class AutoEat : ChatBot
{
byte LastSlot = 0;
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)
2020-04-08 18:23:19 +08:00
{
Eating = FindFoodAndEat();
if (!Eating)
ChangeSlot(LastSlot);
}
// keep eating until full
if (food < 20 && Eating)
{
Task.Factory.StartNew(delegate
{
Thread.Sleep(200);
Eating = FindFoodAndEat();
if (!Eating)
ChangeSlot(LastSlot);
});
2020-04-08 18:23:19 +08:00
}
if (food >= 20 && Eating)
{
Eating = false;
ChangeSlot(LastSlot);
}
}
/// <summary>
/// Try to find food in the hotbar and eat it
/// </summary>
/// <returns>True if found</returns>
public bool FindFoodAndEat()
{
Container inventory = GetPlayerInventory();
bool found = false;
byte CurrentSlot = GetCurrentSlot();
if (!Eating)
LastSlot = CurrentSlot;
2020-04-09 21:17:08 +08:00
if (inventory.Items.ContainsKey(CurrentSlot + 36) && inventory.Items[CurrentSlot + 36].Type.IsFood())
2020-04-08 18:23:19 +08:00
{
// no need to change slot
found = true;
}
else
{
for (int i = 36; i <= 44; i++)
{
if (!inventory.Items.ContainsKey(i)) continue;
2020-04-09 21:17:08 +08:00
if (inventory.Items[i].Type.IsFood())
2020-04-08 18:23:19 +08:00
{
int slot = i - 36;
ChangeSlot((short)slot);
found = true;
break;
}
}
}
if (found) UseItemOnHand();
return found;
}
}
}