Add setting for overriding attack speed for autoattack (#1512)

* Add setting for overriding attack speed for autoattack

* Change cooldown value type to float

* Fix minor grammar issue
This commit is contained in:
ReinforceZwei 2021-03-28 19:26:38 +08:00 committed by GitHub
parent 70d3b6175b
commit 53e2413204
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 7 deletions

View file

@ -15,14 +15,15 @@ namespace MinecraftClient.ChatBots
private int attackCooldown = 6;
private int attackCooldownCounter = 6;
private Double attackSpeed = 4;
private Double attackCooldownSecond;
private Double attackCooldownSeconds;
private bool overrideAttackSpeed = false;
private int attackRange = 4;
private Double serverTPS;
private float health = 100;
private bool singleMode = true;
private bool priorityDistance = true;
public AutoAttack(string mode, string priority)
public AutoAttack(string mode, string priority, bool overrideAttackSpeed = false, double cooldownSeconds = 1)
{
if (mode == "single")
singleMode = true;
@ -35,6 +36,20 @@ namespace MinecraftClient.ChatBots
else if (priority == "health")
priorityDistance = false;
else LogToConsoleTranslated("bot.autoAttack.priority", priority);
if (overrideAttackSpeed)
{
if (cooldownSeconds <= 0)
{
LogToConsoleTranslated("bot.autoAttack.invalidcooldown");
}
else
{
this.overrideAttackSpeed = overrideAttackSpeed;
this.attackCooldownSeconds = cooldownSeconds;
attackCooldown = Convert.ToInt32(Math.Truncate(attackCooldownSeconds / 0.1) + 1);
}
}
}
public override void Initialize()
@ -137,6 +152,8 @@ namespace MinecraftClient.ChatBots
public override void OnPlayerProperty(Dictionary<string, double> prop)
{
if (overrideAttackSpeed)
return;
foreach (var attackSpeedKey in new[] { "generic.attackSpeed", "minecraft:generic.attack_speed" })
{
// adjust auto attack cooldown for maximum attack damage
@ -146,8 +163,8 @@ namespace MinecraftClient.ChatBots
{
serverTPS = GetServerTPS();
attackSpeed = prop[attackSpeedKey];
attackCooldownSecond = 1 / attackSpeed * (serverTPS / 20.0); // server tps will affect the cooldown
attackCooldown = Convert.ToInt32(Math.Truncate(attackCooldownSecond / 0.1) + 1);
attackCooldownSeconds = 1 / attackSpeed * (serverTPS / 20.0); // server tps will affect the cooldown
attackCooldown = Convert.ToInt32(Math.Truncate(attackCooldownSeconds / 0.1) + 1);
}
}
}
@ -155,10 +172,12 @@ namespace MinecraftClient.ChatBots
public override void OnServerTpsUpdate(double tps)
{
if (overrideAttackSpeed)
return;
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);
attackCooldownSeconds = 1 / attackSpeed * (serverTPS / 20.0); // server tps will affect the cooldown
attackCooldown = Convert.ToInt32(Math.Truncate(attackCooldownSeconds / 0.1) + 1);
}
/// <summary>