2020-04-08 18:23:19 +08:00
|
|
|
|
using MinecraftClient.Inventory;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.ChatBots
|
|
|
|
|
|
{
|
|
|
|
|
|
class AutoEat : ChatBot
|
|
|
|
|
|
{
|
|
|
|
|
|
byte LastSlot = 0;
|
|
|
|
|
|
public static bool Eating = false;
|
|
|
|
|
|
private int HungerThreshold = 6;
|
2020-04-13 22:21:01 +08:00
|
|
|
|
private int DelayCounter = 0;
|
2020-04-08 18:23:19 +08:00
|
|
|
|
|
|
|
|
|
|
public AutoEat(int Threshold)
|
|
|
|
|
|
{
|
|
|
|
|
|
HungerThreshold = Threshold;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-13 22:21:01 +08:00
|
|
|
|
public override void Update()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (DelayCounter > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
DelayCounter--;
|
|
|
|
|
|
if (DelayCounter == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Eating = FindFoodAndEat();
|
|
|
|
|
|
if (!Eating)
|
|
|
|
|
|
ChangeSlot(LastSlot);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-08 18:23:19 +08:00
|
|
|
|
public override void OnHealthUpdate(float health, int food)
|
|
|
|
|
|
{
|
2020-04-11 14:33:22 +08:00
|
|
|
|
if (((food <= HungerThreshold) || (food < 20 && health < 20)) && !Eating)
|
2020-04-08 18:23:19 +08:00
|
|
|
|
{
|
2020-04-11 14:33:22 +08:00
|
|
|
|
Eating = FindFoodAndEat();
|
|
|
|
|
|
if (!Eating)
|
|
|
|
|
|
ChangeSlot(LastSlot);
|
2020-04-11 14:23:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
// keep eating until full
|
|
|
|
|
|
if (food < 20 && Eating)
|
|
|
|
|
|
{
|
2020-04-13 22:21:01 +08:00
|
|
|
|
// delay 300ms
|
|
|
|
|
|
DelayCounter = 3;
|
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;
|
2020-04-11 14:33:22 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-05-03 10:49:51 -05:00
|
|
|
|
if (found) UseItemInHand();
|
2020-04-08 18:23:19 +08:00
|
|
|
|
return found;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|