Removed ability to attack players, added an option to have a whitelist or a blacklist mode.

This commit is contained in:
Milutinke 2022-09-27 14:23:19 +02:00
parent 58b171cec0
commit 932d25f125
4 changed files with 32 additions and 21 deletions

View file

@ -24,8 +24,8 @@ namespace MinecraftClient.ChatBots
private InteractType interactMode; private InteractType interactMode;
private bool attackHostile = true; private bool attackHostile = true;
private bool attackPassive = false; private bool attackPassive = false;
private bool attackPlayers = false; private string listMode = "blacklist";
private List<EntityType> blacklistedEntityTypes = new(); private List<EntityType> listedEntites = new();
public AutoAttack( public AutoAttack(
string mode, string priority, bool overrideAttackSpeed = false, double cooldownSeconds = 1, InteractType interaction = InteractType.Attack) 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.attackHostile = Settings.AutoAttack_Attack_Hostile;
this.attackPassive = Settings.AutoAttack_Attack_Passive; 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)) 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()) if (attackPassive && entity.Type.IsPassive())
result = true; result = true;
if (attackPlayers && entity.Type == EntityType.Player) if (listedEntites.Count > 0)
result = true; {
bool inList = listedEntites.Contains(entity.Type);
if (blacklistedEntityTypes.Contains(entity.Type)) result = listMode.Equals("blacklist") ? (inList ? false : result) : (inList ? true : false);
result = false; }
return result; return result;
} }

View file

@ -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) interaction=Attack # Possible values: Interact, Attack (default), InteractAt (Interact and Attack)
attackhostile=true # Allow attacking hostile mobs attackhostile=true # Allow attacking hostile mobs
attackpassive=false # Allow attacking passive mobs attackpassive=false # Allow attacking passive mobs
attackplayers=false # Allow attacking players 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 entities (will not attack them), entity types are written on a new line # 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 # All entity types can be found here: https://bit.ly/3Rg68lp
# The file is not created by default. # The file is not created by default.
blacklist=autoattack-blacklist.txt listfile=autoattack-list.txt
[AutoFishing] [AutoFishing]
# Automatically catch fish using a fishing rod # Automatically catch fish using a fishing rod

View file

@ -431,6 +431,7 @@ cmd.useitem.use=Used an item
bot.autoAttack.mode=Unknown attack mode: {0}. Using single mode as default. 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.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.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 # AutoCraft
bot.autoCraft.cmd=Auto-crafting ChatBot command bot.autoCraft.cmd=Auto-crafting ChatBot command

View file

@ -206,8 +206,8 @@ namespace MinecraftClient
public static InteractType AutoAttack_Interaction = InteractType.Attack; public static InteractType AutoAttack_Interaction = InteractType.Attack;
public static bool AutoAttack_Attack_Hostile = true; public static bool AutoAttack_Attack_Hostile = true;
public static bool AutoAttack_Attack_Passive = false; public static bool AutoAttack_Attack_Passive = false;
public static bool AutoAttack_Attack_Players = false; public static string AutoAttack_ListMode = "blacklist";
public static string AutoAttack_Blacklist = "attack-blacklist.txt"; public static string AutoAttack_ListFile = "autoattack-list.txt";
//Auto Fishing //Auto Fishing
public static bool AutoFishing_Enabled = false; public static bool AutoFishing_Enabled = false;
@ -735,10 +735,10 @@ namespace MinecraftClient
AutoAttack_Attack_Hostile = str2bool(argValue); return true; AutoAttack_Attack_Hostile = str2bool(argValue); return true;
case "attackpassive": case "attackpassive":
AutoAttack_Attack_Passive = str2bool(argValue); return true; AutoAttack_Attack_Passive = str2bool(argValue); return true;
case "attackplayers": case "listmode":
AutoAttack_Attack_Players = str2bool(argValue); return true; AutoAttack_ListMode = argValue; return true;
case "blacklist": case "blacklist":
AutoAttack_Blacklist = argValue; return true; AutoAttack_ListFile = argValue; return true;
} }
break; break;