2022-10-05 15:02:30 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using MinecraftClient.Inventory;
|
2022-12-06 15:50:17 +08:00
|
|
|
|
using MinecraftClient.Scripting;
|
2022-10-05 15:02:30 +08:00
|
|
|
|
using Tomlet.Attributes;
|
2020-04-08 18:23:19 +08:00
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.ChatBots
|
|
|
|
|
|
{
|
2022-10-05 15:02:30 +08:00
|
|
|
|
public class AutoEat : ChatBot
|
2020-04-08 18:23:19 +08:00
|
|
|
|
{
|
2022-10-05 15:02:30 +08:00
|
|
|
|
public static Configs Config = new();
|
2020-04-08 18:23:19 +08:00
|
|
|
|
|
2022-10-05 15:02:30 +08:00
|
|
|
|
[TomlDoNotInlineObject]
|
|
|
|
|
|
public class Configs
|
2020-04-08 18:23:19 +08:00
|
|
|
|
{
|
2022-10-05 15:02:30 +08:00
|
|
|
|
[NonSerialized]
|
|
|
|
|
|
private const string BotName = "AutoEat";
|
|
|
|
|
|
|
|
|
|
|
|
public bool Enabled = false;
|
|
|
|
|
|
|
|
|
|
|
|
public int Threshold = 6;
|
|
|
|
|
|
|
|
|
|
|
|
public void OnSettingUpdate()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Threshold > 20)
|
|
|
|
|
|
Threshold = 20;
|
|
|
|
|
|
else if (Threshold < 0)
|
|
|
|
|
|
Threshold = 0;
|
|
|
|
|
|
}
|
2020-04-08 18:23:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-05 15:02:30 +08:00
|
|
|
|
byte LastSlot = 0;
|
|
|
|
|
|
public static bool Eating = false;
|
|
|
|
|
|
private int DelayCounter = 0;
|
|
|
|
|
|
|
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-06-14 21:14:51 +08:00
|
|
|
|
if (health <= 0) return; // player dead
|
2022-10-05 15:02:30 +08:00
|
|
|
|
if (((food <= Config.Threshold) || (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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|