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

105 lines
2.9 KiB
C#
Raw Normal View History

2022-10-05 15:02:30 +08:00
using System;
using MinecraftClient.Inventory;
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)
{
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
{
Eating = FindFoodAndEat();
if (!Eating)
ChangeSlot(LastSlot);
}
// 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;
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) UseItemInHand();
2020-04-08 18:23:19 +08:00
return found;
}
}
}