Move AutoEat to a ChatBot

This commit is contained in:
ReinforceZwei 2020-04-08 18:23:19 +08:00 committed by ORelio
parent 044c19114c
commit 97b0b03c33
6 changed files with 116 additions and 65 deletions

View file

@ -168,6 +168,10 @@ namespace MinecraftClient
/// <param name="entity">Entity wich has just disappeared</param>
public virtual void OnEntityDespawn(Mapping.Entity entity) { }
public virtual void OnHeldItemChange(byte slot) { }
public virtual void OnHealthUpdate(float health, int food) { }
/* =================================================================== */
/* ToolBox - Methods below might be useful while creating your bot. */
/* You should not need to interact with other classes of the program. */
@ -816,6 +820,10 @@ namespace MinecraftClient
return Handler.SendPluginChannelMessage(channel, data, sendEvenIfNotRegistered);
}
/// <summary>
/// Get server current TPS (tick per second)
/// </summary>
/// <returns>tps</returns>
protected Double GetServerTPS()
{
return Handler.GetServerTPS();
@ -852,13 +860,13 @@ namespace MinecraftClient
}
/// <summary>
/// Check if player is eating or not
/// Change player selected hotbar
/// </summary>
/// <remarks>Some bot like AutoAttack need this. We don't want to attack while eating</remarks>
/// <returns>True if is eating</returns>
protected bool GetIsEating()
/// <param name="slot"></param>
/// <returns>True if success</returns>
protected bool ChangeSlot(short slot)
{
return Handler.GetIsEating();
return Handler.ChangeSlot(slot);
}
}
}

View file

@ -32,7 +32,7 @@ namespace MinecraftClient.ChatBots
public override void Update()
{
if (!GetIsEating())
if (!AutoEat.Eating)
{
if (attackCooldownCounter == 0)
{

View file

@ -0,0 +1,76 @@
using MinecraftClient.Inventory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.ChatBots
{
class AutoEat : ChatBot
{
byte LastSlot = 0;
byte CurrentSlot;
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 = true;
FindFoodAndEat();
}
// keep eating until full
if (food < 20 && Eating)
{
FindFoodAndEat();
}
if (food >= 20 && Eating)
{
Eating = false;
ChangeSlot(LastSlot);
}
}
public override void OnHeldItemChange(byte slot)
{
CurrentSlot = slot;
}
/// <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;
LastSlot = CurrentSlot;
if (inventory.Items.ContainsKey(CurrentSlot + 36) && inventory.Items[CurrentSlot + 36].IsFood())
{
// no need to change slot
found = true;
}
else
{
for (int i = 36; i <= 44; i++)
{
if (!inventory.Items.ContainsKey(i)) continue;
if (inventory.Items[i].IsFood())
{
int slot = i - 36;
ChangeSlot((short)slot);
found = true;
break;
}
}
}
if (found) UseItemOnHand();
return found;
}
}
}

View file

@ -62,10 +62,7 @@ namespace MinecraftClient
// player health and hunger
private float playerHealth;
private int playerFoodSaturation;
private bool Eating = false;
private int HungerThreshold = 6;
private byte CurrentSlot = 0;
private byte LastSlot = 0; // for switch back to origin slot after eating
// Entity handling
private Dictionary<int, Entity> entities = new Dictionary<int, Entity>();
@ -86,7 +83,6 @@ namespace MinecraftClient
public float GetHealth() { return playerHealth; }
public int GetSaturation() { return playerFoodSaturation; }
public byte GetCurrentSlot() { return CurrentSlot; }
public bool GetIsEating() { return Eating; }
// get bots list for unloading them by commands
public List<ChatBot> GetLoadedChatBots()
@ -173,6 +169,7 @@ namespace MinecraftClient
if (Settings.AutoRespond_Enabled) { BotLoad(new ChatBots.AutoRespond(Settings.AutoRespond_Matches)); }
if (Settings.AutoAttack_Enabled) { BotLoad(new ChatBots.AutoAttack()); }
if (Settings.AutoFishing_Enabled) { BotLoad(new ChatBots.AutoFishing()); }
if (Settings.AutoEat_Enabled) { BotLoad(new ChatBots.AutoEat(Settings.AutoEat_hungerThreshold)); }
//Add your ChatBot here by uncommenting and adapting
//BotLoad(new ChatBots.YourBot());
}
@ -1566,61 +1563,15 @@ namespace MinecraftClient
ConsoleIO.WriteLogLine("You are dead. Type /respawn to respawn.");
}
}
if (Settings.AutoEat)
{
if (food <= HungerThreshold || (food < 20 && health < 20))
{
Eating = true;
FindFoodAndEat();
}
// keep eating until full
if (food < 20 && Eating)
{
FindFoodAndEat();
}
if (food >= 20 && Eating)
{
Eating = false;
ChangeSlot(LastSlot);
}
}
foreach (ChatBot bot in bots.ToArray())
bot.OnHealthUpdate(health, food);
}
public void OnHeldItemChange(byte slot)
{
foreach (ChatBot bot in bots.ToArray())
bot.OnHeldItemChange(slot);
CurrentSlot = slot;
}
/// <summary>
/// Try to find food in the hotbar and eat it
/// </summary>
/// <returns>True if found</returns>
public bool FindFoodAndEat()
{
Container inventory = inventories[0];
bool found = false;
LastSlot = CurrentSlot;
if (inventory.Items.ContainsKey(CurrentSlot + 36) && inventory.Items[CurrentSlot + 36].IsFood())
{
// no need to change slot
found = true;
}
else
{
for (int i = 36; i <= 44; i++)
{
if (!inventory.Items.ContainsKey(i)) continue;
if (inventory.Items[i].IsFood())
{
int slot = i - 36;
ChangeSlot((short)slot);
found = true;
break;
}
}
}
if (found) UseItemOnHand();
return found;
}
}
}

View file

@ -76,6 +76,7 @@
<Compile Include="ChatBots\Alerts.cs" />
<Compile Include="ChatBots\AntiAFK.cs" />
<Compile Include="ChatBots\AutoAttack.cs" />
<Compile Include="ChatBots\AutoEat.cs" />
<Compile Include="ChatBots\AutoFishing.cs" />
<Compile Include="ChatBots\AutoRespond.cs" />
<Compile Include="ChatBots\AutoRelog.cs" />

View file

@ -97,7 +97,6 @@ namespace MinecraftClient
public static bool ResolveSrvRecordsShortTimeout = true;
public static bool EntityHandling = false;
public static bool AutoRespawn = false;
public static bool AutoEat = false;
//AntiAFK Settings
public static bool AntiAFK_Enabled = false;
@ -159,12 +158,16 @@ namespace MinecraftClient
//Auto Fishing
public static bool AutoFishing_Enabled = false;
//Auto Eating
public static bool AutoEat_Enabled = false;
public static int AutoEat_hungerThreshold = 6;
//Custom app variables and Minecraft accounts
private static readonly Dictionary<string, object> AppVars = new Dictionary<string, object>();
private static readonly Dictionary<string, KeyValuePair<string, string>> Accounts = new Dictionary<string, KeyValuePair<string, string>>();
private static readonly Dictionary<string, KeyValuePair<string, ushort>> Servers = new Dictionary<string, KeyValuePair<string, ushort>>();
private enum ParseMode { Default, Main, AppVars, Proxy, MCSettings, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, ScriptScheduler, RemoteControl, ChatFormat, AutoRespond, AutoAttack, AutoFishing };
private enum ParseMode { Default, Main, AppVars, Proxy, MCSettings, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, ScriptScheduler, RemoteControl, ChatFormat, AutoRespond, AutoAttack, AutoFishing, AutoEat };
/// <summary>
/// Load settings from the give INI file
@ -207,6 +210,7 @@ namespace MinecraftClient
case "chatformat": pMode = ParseMode.ChatFormat; break;
case "autoattack": pMode = ParseMode.AutoAttack; break;
case "autofishing": pMode = ParseMode.AutoFishing; break;
case "autoeat": pMode = ParseMode.AutoEat; break;
default: pMode = ParseMode.Default; break;
}
}
@ -245,7 +249,6 @@ namespace MinecraftClient
case "botmessagedelay": botMessageDelay = TimeSpan.FromSeconds(str2int(argValue)); break;
case "debugmessages": DebugMessages = str2bool(argValue); break;
case "autorespawn": AutoRespawn = str2bool(argValue); break;
case "autoeat": AutoEat = str2bool(argValue); break;
case "botowners":
Bots_Owners.Clear();
@ -478,6 +481,14 @@ namespace MinecraftClient
}
break;
case ParseMode.AutoEat:
switch (argName.ToLower())
{
case "enabled": AutoEat_Enabled = str2bool(argValue); break;
case "threshold": AutoEat_hungerThreshold = str2int(argValue); break;
}
break;
case ParseMode.MCSettings:
switch (argName.ToLower())
{
@ -591,7 +602,6 @@ namespace MinecraftClient
+ "scriptcache=true # Cache compiled scripts for faster load on low-end devices\r\n"
+ "timestamps=false # Prepend timestamps to chat messages\r\n"
+ "autorespawn=false # Toggle auto respawn if client player was dead (make sure your spawn point is safe)\r\n"
+ "autoeat=false # Toggle auto eat when player is hungry\r\n"
+ "\r\n"
+ "[AppVars]\r\n"
+ "# yourvar=yourvalue\r\n"
@ -679,7 +689,12 @@ namespace MinecraftClient
+ "\r\n"
+ "[AutoFishing]\r\n"
+ "# Entity Handling NEED to be enabled first\r\n"
+ "enabled=false", Encoding.UTF8);
+ "enabled=false"
+ "\r\n"
+ "[AutoEat]\r\n"
+ "# Inventory Handling NEED to be enabled first\r\n"
+ "enabled=false\r\n"
+ "threshold=6", Encoding.UTF8);
}
/// <summary>