Moved Auto attack and auto fishing to ChatBots and added ChatBotAPI for entity handling

This commit is contained in:
ReinforceZwei 2020-03-23 19:59:00 +08:00 committed by ORelio
parent 8b8f3a719b
commit dbf5334758
16 changed files with 602 additions and 465 deletions

View file

@ -0,0 +1,99 @@
using MinecraftClient.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.ChatBots
{
class AutoAttack : ChatBot
{
private Dictionary<int, Entity> entitiesToAttack = new Dictionary<int, Entity>(); // mobs within attack range
private Dictionary<int, Entity> entitiesToTrack = new Dictionary<int, Entity>(); // all mobs in view distance
private int attackCooldown = 6;
private int attackCooldownCounter = 6;
private Double attackSpeed = 4;
private Double attackCooldownSecond;
private int attackRange = 4;
private Double serverTPS;
public override void Initialize()
{
if (!GetEntityHandlingEnabled())
{
ConsoleIO.WriteLine("[AutoAttack] Entity Handling is not enabled in the config file!");
ConsoleIO.WriteLine("Please enable it to use this bot.");
UnloadBot();
}
}
public override void Update()
{
if (attackCooldownCounter == 0)
{
attackCooldownCounter = attackCooldown;
if (entitiesToAttack.Count > 0)
{
foreach (KeyValuePair<int, Entity> a in entitiesToAttack)
{
InteractEntity(a.Key, 1);
}
}
}
else
{
attackCooldownCounter--;
}
}
public override void OnEntitySpawn(Entity entity)
{
if (entity.GetMobName() != "")
{
entitiesToTrack.Add(entity.ID, entity);
}
}
public override void OnEntityDespawn(int EntityID)
{
if (entitiesToTrack.ContainsKey(EntityID))
{
entitiesToTrack.Remove(EntityID);
}
}
public override void OnEntityMove(Entity entity)
{
if (entitiesToTrack.ContainsKey(entity.ID))
{
Double distance = Entity.CalculateDistance(GetCurrentLocation(), entity.Location);
if(distance < attackRange)
{
if(!entitiesToAttack.ContainsKey(entity.ID))
entitiesToAttack.Add(entity.ID, entity);
}
}
}
public override void OnPlayerProperty(Dictionary<string, double> prop)
{
// adjust auto attack cooldown for maximum attack damage
if (prop.ContainsKey("generic.attackSpeed"))
{
if (attackSpeed != prop["generic.attackSpeed"])
{
GetServerTPS();
attackSpeed = prop["generic.attackSpeed"];
attackCooldownSecond = 1 / attackSpeed * (serverTPS / 20.0); // server tps will affect the cooldown
attackCooldown = Convert.ToInt32(Math.Truncate(attackCooldownSecond / 0.1) + 1);
}
}
}
public override void OnServerTpsUpdate(double tps)
{
serverTPS = tps;
// re-calculate attack speed
attackCooldownSecond = 1 / attackSpeed * (serverTPS / 20.0); // server tps will affect the cooldown
attackCooldown = Convert.ToInt32(Math.Truncate(attackCooldownSecond / 0.1) + 1);
}
}
}

View file

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using MinecraftClient.Mapping;
namespace MinecraftClient.ChatBots
{
class AutoFishing : ChatBot
{
private Dictionary<int, Entity> fishingRod = new Dictionary<int, Entity>();
private Double fishingHookThreshold = 0.2;
private Location LastPos = new Location();
private DateTime CaughtTime = DateTime.Now;
public override void Initialize()
{
if (!GetEntityHandlingEnabled())
{
ConsoleIO.WriteLine("[AutoFishing] Entity Handling is not enabled in the config file!");
ConsoleIO.WriteLine("Please enable it to use this bot.");
UnloadBot();
}
}
public override void OnEntitySpawn(Entity entity)
{
if (entity.Type == 102)
{
ConsoleIO.WriteLine("Threw a fishing rod");
fishingRod.Add(entity.ID, entity);
LastPos = entity.Location;
}
}
public override void OnEntityMove(Entity entity)
{
if (fishingRod.ContainsKey(entity.ID))
{
Location Pos = entity.Location;
Double Dx = LastPos.X - Pos.X;
Double Dy = LastPos.Y - Pos.Y;
Double Dz = LastPos.Z - Pos.Z;
LastPos = Pos;
// check if fishing hook is stationary
if (Dx == 0 && Dz == 0)
{
if (Math.Abs(Dy) > fishingHookThreshold)
{
// caught
// prevent triggering multiple time
if ((DateTime.Now - CaughtTime).TotalSeconds > 1)
{
OnCaughtFish();
CaughtTime = DateTime.Now;
}
}
}
fishingRod[entity.ID] = entity;
}
}
// TODO: Move into ChatBot
/// <summary>
/// Called when detected a fish is caught
/// </summary>
public void OnCaughtFish()
{
ConsoleIO.WriteLine("Caught a fish!");
// retract fishing rod
UseItemOnHand();
// non-blocking delay
Task.Factory.StartNew(delegate
{
// retract fishing rod need some time
Thread.Sleep(500);
// throw again
// TODO: to check if hand have fishing rod
UseItemOnHand();
});
}
}
}