Updated the AutoAttack Chat Bot settings to include attacking passive mobs and players if enabled, and a blacklisted entities possibility.

This commit is contained in:
Milutinke 2022-09-27 13:35:29 +02:00
parent aec38d83c7
commit 58b171cec0
4 changed files with 112 additions and 5 deletions

View file

@ -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<EntityType> 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
/// <returns>If the entity should be attacked</returns>
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);

View file

@ -54,6 +54,52 @@ namespace MinecraftClient.Mapping
}
}
/// <summary>
/// Return TRUE if the Entity is a passive mob
/// </summary>
/// <remarks>New mobs added in newer Minecraft versions might be absent from the list</remarks>
/// <returns>TRUE if a passive mob</returns>
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;
}
}
/// <summary>
/// Indicates whether the entity type contains an inner item
/// </summary>

View file

@ -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

View file

@ -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;