From 58b171cec09c7386c462466be46843114382aa9f Mon Sep 17 00:00:00 2001 From: Milutinke Date: Tue, 27 Sep 2022 13:35:29 +0200 Subject: [PATCH 1/3] Updated the AutoAttack Chat Bot settings to include attacking passive mobs and players if enabled, and a blacklisted entities possibility. --- MinecraftClient/ChatBots/AutoAttack.cs | 52 +++++++++++++++++-- .../Mapping/EntityTypeExtensions.cs | 46 ++++++++++++++++ .../Resources/config/MinecraftClient.ini | 7 +++ MinecraftClient/Settings.cs | 12 +++++ 4 files changed, 112 insertions(+), 5 deletions(-) diff --git a/MinecraftClient/ChatBots/AutoAttack.cs b/MinecraftClient/ChatBots/AutoAttack.cs index 7085099a..9dee72e3 100644 --- a/MinecraftClient/ChatBots/AutoAttack.cs +++ b/MinecraftClient/ChatBots/AutoAttack.cs @@ -1,8 +1,7 @@ using MinecraftClient.Mapping; using System; using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.IO; namespace MinecraftClient.ChatBots { @@ -23,8 +22,13 @@ namespace MinecraftClient.ChatBots private bool singleMode = true; private bool priorityDistance = true; private InteractType interactMode; + private bool attackHostile = true; + private bool attackPassive = false; + private bool attackPlayers = false; + private List blacklistedEntityTypes = new(); - public AutoAttack(string mode, string priority, bool overrideAttackSpeed = false, double cooldownSeconds = 1, InteractType interaction = InteractType.Attack) + public AutoAttack( + string mode, string priority, bool overrideAttackSpeed = false, double cooldownSeconds = 1, InteractType interaction = InteractType.Attack) { if (mode == "single") singleMode = true; @@ -53,6 +57,24 @@ namespace MinecraftClient.ChatBots attackCooldown = Convert.ToInt32(Math.Truncate(attackCooldownSeconds / 0.1) + 1); } } + + this.attackHostile = Settings.AutoAttack_Attack_Hostile; + this.attackPassive = Settings.AutoAttack_Attack_Passive; + this.attackPlayers = Settings.AutoAttack_Attack_Players; + + if (File.Exists(Settings.AutoAttack_Blacklist)) + { + string[] blacklist = LoadDistinctEntriesFromFile(Settings.AutoAttack_Blacklist); + + if (blacklist.Length > 0) + { + foreach (var item in blacklist) + { + if (Enum.TryParse(item, true, out EntityType resultingType)) + blacklistedEntityTypes.Add(resultingType); + } + } + } } public override void Initialize() @@ -149,7 +171,7 @@ namespace MinecraftClient.ChatBots public override void OnEntityHealth(Entity entity, float health) { - if (!entity.Type.IsHostile()) + if (!IsAllowedToAttack(entity)) return; if (entitiesToAttack.ContainsKey(entity.ID)) @@ -163,6 +185,25 @@ namespace MinecraftClient.ChatBots } } + private bool IsAllowedToAttack(Entity entity) + { + bool result = false; + + if (attackHostile && entity.Type.IsHostile()) + result = true; + + if (attackPassive && entity.Type.IsPassive()) + result = true; + + if (attackPlayers && entity.Type == EntityType.Player) + result = true; + + if (blacklistedEntityTypes.Contains(entity.Type)) + result = false; + + return result; + } + public override void OnEntityMove(Entity entity) { shouldAttackEntity(entity); @@ -197,6 +238,7 @@ namespace MinecraftClient.ChatBots { if (overrideAttackSpeed) return; + serverTPS = tps; // re-calculate attack speed attackCooldownSeconds = 1 / attackSpeed * (serverTPS / 20.0); // server tps will affect the cooldown @@ -211,7 +253,7 @@ namespace MinecraftClient.ChatBots /// If the entity should be attacked public bool shouldAttackEntity(Entity entity) { - if (!entity.Type.IsHostile() || entity.Health <= 0) + if (!IsAllowedToAttack(entity) || entity.Health <= 0) return false; bool isBeingAttacked = entitiesToAttack.ContainsKey(entity.ID); diff --git a/MinecraftClient/Mapping/EntityTypeExtensions.cs b/MinecraftClient/Mapping/EntityTypeExtensions.cs index a467b3ad..b022394b 100644 --- a/MinecraftClient/Mapping/EntityTypeExtensions.cs +++ b/MinecraftClient/Mapping/EntityTypeExtensions.cs @@ -54,6 +54,52 @@ namespace MinecraftClient.Mapping } } + /// + /// Return TRUE if the Entity is a passive mob + /// + /// New mobs added in newer Minecraft versions might be absent from the list + /// TRUE if a passive mob + public static bool IsPassive(this EntityType e) + { + switch (e) + { + case EntityType.Bat: + case EntityType.Cat: + case EntityType.Chicken: + case EntityType.Cod: + case EntityType.Cow: + case EntityType.Dolphin: + case EntityType.Donkey: + case EntityType.Fox: + case EntityType.Frog: + case EntityType.GlowSquid: + case EntityType.Goat: + case EntityType.Horse: + case EntityType.IronGolem: + case EntityType.Llama: + case EntityType.Mooshroom: + case EntityType.Mule: + case EntityType.Ocelot: + case EntityType.Panda: + case EntityType.Parrot: + case EntityType.Pig: + case EntityType.Salmon: + case EntityType.Sheep: + case EntityType.Silverfish: + case EntityType.SnowGolem: + case EntityType.Squid: + case EntityType.Turtle: + case EntityType.Villager: + case EntityType.WanderingTrader: + case EntityType.Wolf: + case EntityType.ZombieHorse: + case EntityType.SkeletonHorse: + return true; + default: + return false; + } + } + /// /// Indicates whether the entity type contains an inner item /// diff --git a/MinecraftClient/Resources/config/MinecraftClient.ini b/MinecraftClient/Resources/config/MinecraftClient.ini index 8507fba4..4e8856bf 100644 --- a/MinecraftClient/Resources/config/MinecraftClient.ini +++ b/MinecraftClient/Resources/config/MinecraftClient.ini @@ -202,6 +202,13 @@ mode=single # single or multi. single target one mob per priority=distance # health or distance. Only needed when using single mode cooldownseconds=auto # How long to wait between each attack. Use auto to let MCC calculate it interaction=Attack # Possible values: Interact, Attack (default), InteractAt (Interact and Attack) +attackhostile=true # Allow attacking hostile mobs +attackpassive=false # Allow attacking passive mobs +attackplayers=false # Allow attacking players +# A path to the file which contains blacklisted entities (will not attack them), entity types are written on a new line +# All entity types can be found here: https://bit.ly/3Rg68lp +# The file is not created by default. +blacklist=autoattack-blacklist.txt [AutoFishing] # Automatically catch fish using a fishing rod diff --git a/MinecraftClient/Settings.cs b/MinecraftClient/Settings.cs index ce270300..9cf02f49 100644 --- a/MinecraftClient/Settings.cs +++ b/MinecraftClient/Settings.cs @@ -204,6 +204,10 @@ namespace MinecraftClient public static bool AutoAttack_OverrideAttackSpeed = false; public static double AutoAttack_CooldownSeconds = 1; public static InteractType AutoAttack_Interaction = InteractType.Attack; + public static bool AutoAttack_Attack_Hostile = true; + public static bool AutoAttack_Attack_Passive = false; + public static bool AutoAttack_Attack_Players = false; + public static string AutoAttack_Blacklist = "attack-blacklist.txt"; //Auto Fishing public static bool AutoFishing_Enabled = false; @@ -727,6 +731,14 @@ namespace MinecraftClient return true; case "interaction": return Enum.TryParse(argValue, true, out AutoAttack_Interaction); + case "attackhostile": + AutoAttack_Attack_Hostile = str2bool(argValue); return true; + case "attackpassive": + AutoAttack_Attack_Passive = str2bool(argValue); return true; + case "attackplayers": + AutoAttack_Attack_Players = str2bool(argValue); return true; + case "blacklist": + AutoAttack_Blacklist = argValue; return true; } break; From 932d25f1257f254321c35afb0f1ba2a86662f886 Mon Sep 17 00:00:00 2001 From: Milutinke Date: Tue, 27 Sep 2022 14:23:19 +0200 Subject: [PATCH 2/3] Removed ability to attack players, added an option to have a whitelist or a blacklist mode. --- MinecraftClient/ChatBots/AutoAttack.cs | 36 ++++++++++++------- .../Resources/config/MinecraftClient.ini | 6 ++-- MinecraftClient/Resources/lang/en.ini | 1 + MinecraftClient/Settings.cs | 10 +++--- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/MinecraftClient/ChatBots/AutoAttack.cs b/MinecraftClient/ChatBots/AutoAttack.cs index 9dee72e3..f4839a2c 100644 --- a/MinecraftClient/ChatBots/AutoAttack.cs +++ b/MinecraftClient/ChatBots/AutoAttack.cs @@ -24,8 +24,8 @@ namespace MinecraftClient.ChatBots private InteractType interactMode; private bool attackHostile = true; private bool attackPassive = false; - private bool attackPlayers = false; - private List blacklistedEntityTypes = new(); + private string listMode = "blacklist"; + private List listedEntites = new(); public AutoAttack( string mode, string priority, bool overrideAttackSpeed = false, double cooldownSeconds = 1, InteractType interaction = InteractType.Attack) @@ -60,18 +60,28 @@ namespace MinecraftClient.ChatBots this.attackHostile = Settings.AutoAttack_Attack_Hostile; this.attackPassive = Settings.AutoAttack_Attack_Passive; - this.attackPlayers = Settings.AutoAttack_Attack_Players; - if (File.Exists(Settings.AutoAttack_Blacklist)) + if (Settings.AutoAttack_ListMode.Length > 0) { - string[] blacklist = LoadDistinctEntriesFromFile(Settings.AutoAttack_Blacklist); + listMode = Settings.AutoAttack_ListMode.ToLower(); - if (blacklist.Length > 0) + if (!(listMode.Equals("whitelist", StringComparison.OrdinalIgnoreCase) || listMode.Equals("blacklist", StringComparison.OrdinalIgnoreCase))) { - foreach (var item in blacklist) + LogToConsole(Translations.TryGet("bot.autoAttack.invalidlist")); + listMode = "blacklist"; + } + } + + if (File.Exists(Settings.AutoAttack_ListFile)) + { + string[] entityList = LoadDistinctEntriesFromFile(Settings.AutoAttack_ListFile); + + if (entityList.Length > 0) + { + foreach (var item in entityList) { if (Enum.TryParse(item, true, out EntityType resultingType)) - blacklistedEntityTypes.Add(resultingType); + listedEntites.Add(resultingType); } } } @@ -195,11 +205,11 @@ namespace MinecraftClient.ChatBots if (attackPassive && entity.Type.IsPassive()) result = true; - if (attackPlayers && entity.Type == EntityType.Player) - result = true; - - if (blacklistedEntityTypes.Contains(entity.Type)) - result = false; + if (listedEntites.Count > 0) + { + bool inList = listedEntites.Contains(entity.Type); + result = listMode.Equals("blacklist") ? (inList ? false : result) : (inList ? true : false); + } return result; } diff --git a/MinecraftClient/Resources/config/MinecraftClient.ini b/MinecraftClient/Resources/config/MinecraftClient.ini index 4e8856bf..1fdb59bb 100644 --- a/MinecraftClient/Resources/config/MinecraftClient.ini +++ b/MinecraftClient/Resources/config/MinecraftClient.ini @@ -204,11 +204,11 @@ cooldownseconds=auto # How long to wait between each attack. Use a interaction=Attack # Possible values: Interact, Attack (default), InteractAt (Interact and Attack) attackhostile=true # Allow attacking hostile mobs attackpassive=false # Allow attacking passive mobs -attackplayers=false # Allow attacking players -# A path to the file which contains blacklisted entities (will not attack them), entity types are written on a new line +listmode=blacklist # Wether to treat the list from the file bellow as a whitelist or as a blacklist +# A path to the file which contains blacklisted or whitelisted entities, entity types are written on a new line # All entity types can be found here: https://bit.ly/3Rg68lp # The file is not created by default. -blacklist=autoattack-blacklist.txt +listfile=autoattack-list.txt [AutoFishing] # Automatically catch fish using a fishing rod diff --git a/MinecraftClient/Resources/lang/en.ini b/MinecraftClient/Resources/lang/en.ini index 119bf5a2..0ee5163d 100644 --- a/MinecraftClient/Resources/lang/en.ini +++ b/MinecraftClient/Resources/lang/en.ini @@ -431,6 +431,7 @@ cmd.useitem.use=Used an item bot.autoAttack.mode=Unknown attack mode: {0}. Using single mode as default. bot.autoAttack.priority=Unknown priority: {0}. Using distance priority as default. bot.autoAttack.invalidcooldown=Attack cooldown value cannot be smaller than 0. Using auto as default +bot.autoAttack.invalidlist=Invalid list type provided, using the default list mode of: 'blacklist' # AutoCraft bot.autoCraft.cmd=Auto-crafting ChatBot command diff --git a/MinecraftClient/Settings.cs b/MinecraftClient/Settings.cs index 9cf02f49..70a21d5b 100644 --- a/MinecraftClient/Settings.cs +++ b/MinecraftClient/Settings.cs @@ -206,8 +206,8 @@ namespace MinecraftClient public static InteractType AutoAttack_Interaction = InteractType.Attack; public static bool AutoAttack_Attack_Hostile = true; public static bool AutoAttack_Attack_Passive = false; - public static bool AutoAttack_Attack_Players = false; - public static string AutoAttack_Blacklist = "attack-blacklist.txt"; + public static string AutoAttack_ListMode = "blacklist"; + public static string AutoAttack_ListFile = "autoattack-list.txt"; //Auto Fishing public static bool AutoFishing_Enabled = false; @@ -735,10 +735,10 @@ namespace MinecraftClient AutoAttack_Attack_Hostile = str2bool(argValue); return true; case "attackpassive": AutoAttack_Attack_Passive = str2bool(argValue); return true; - case "attackplayers": - AutoAttack_Attack_Players = str2bool(argValue); return true; + case "listmode": + AutoAttack_ListMode = argValue; return true; case "blacklist": - AutoAttack_Blacklist = argValue; return true; + AutoAttack_ListFile = argValue; return true; } break; From 36a97f4955cd4b2f21a4a528934f25bb0ba8451c Mon Sep 17 00:00:00 2001 From: Milutinke Date: Tue, 27 Sep 2022 14:34:10 +0200 Subject: [PATCH 3/3] Fixed a bug. --- MinecraftClient/ChatBots/AutoAttack.cs | 1 + MinecraftClient/Settings.cs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/MinecraftClient/ChatBots/AutoAttack.cs b/MinecraftClient/ChatBots/AutoAttack.cs index f4839a2c..55bc156d 100644 --- a/MinecraftClient/ChatBots/AutoAttack.cs +++ b/MinecraftClient/ChatBots/AutoAttack.cs @@ -71,6 +71,7 @@ namespace MinecraftClient.ChatBots listMode = "blacklist"; } } + else LogToConsole(Translations.TryGet("bot.autoAttack.invalidlist")); if (File.Exists(Settings.AutoAttack_ListFile)) { diff --git a/MinecraftClient/Settings.cs b/MinecraftClient/Settings.cs index 70a21d5b..4704661a 100644 --- a/MinecraftClient/Settings.cs +++ b/MinecraftClient/Settings.cs @@ -737,7 +737,7 @@ namespace MinecraftClient AutoAttack_Attack_Passive = str2bool(argValue); return true; case "listmode": AutoAttack_ListMode = argValue; return true; - case "blacklist": + case "listfile": AutoAttack_ListFile = argValue; return true; } break;