From 770a82e2d20aeafdb80dea324aa08dcb4c72971f Mon Sep 17 00:00:00 2001 From: OverHash <46231745+OverHash@users.noreply.github.com> Date: Sun, 26 Apr 2020 11:38:03 +1200 Subject: [PATCH] Check player is in range each entity attack This commit should make it so that MCC will check that the player is in range before attacking entities each iteration of the cooldown. This was problematic before, because, if the entities were still they would be added to the attack list, and then if they stayed still and the client teleported away, it would still attempt to attack the entities, even though it is now out of range. --- MinecraftClient/ChatBots/AutoAttack.cs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/MinecraftClient/ChatBots/AutoAttack.cs b/MinecraftClient/ChatBots/AutoAttack.cs index 10a00ca5..6ccd9f58 100644 --- a/MinecraftClient/ChatBots/AutoAttack.cs +++ b/MinecraftClient/ChatBots/AutoAttack.cs @@ -40,9 +40,15 @@ namespace MinecraftClient.ChatBots attackCooldownCounter = attackCooldown; if (entitiesToAttack.Count > 0) { - foreach (KeyValuePair a in entitiesToAttack) + foreach (KeyValuePair entity in entitiesToAttack) { - InteractEntity(a.Key, 1); + // check that we are in range once again. + bool shouldAttack = handleEntity(entity.Value); + if (shouldAttack) + { + // hit the entity! + InteractEntity(entity.Key, 1); + } } } } @@ -98,10 +104,16 @@ namespace MinecraftClient.ChatBots attackCooldown = Convert.ToInt32(Math.Truncate(attackCooldownSecond / 0.1) + 1); } - public void handleEntity(Entity entity) + /// + /// Checks to see if the entity should be attacked. If it should be attacked, it will add the entity + /// To a list of entities to attack every few ticks. + /// + /// The entity to handle + /// If the entity should be attacked + public bool handleEntity(Entity entity) { if (!entity.IsHostile()) - return; + return false; bool isBeingAttacked = entitiesToAttack.ContainsKey(entity.ID); if (GetCurrentLocation().Distance(entity.Location) < attackRange) @@ -111,6 +123,8 @@ namespace MinecraftClient.ChatBots { entitiesToAttack.Add(entity.ID, entity); } + + return true; } else { @@ -119,6 +133,8 @@ namespace MinecraftClient.ChatBots { entitiesToAttack.Remove(entity.ID); } + + return false; } } }