AuotoAttack: add support for multiple interact modes (#2044)

* Adds support for multiple interact modes
* Entity interaction: Implement enum

Co-authored-by: ORelio <ORelio@users.noreply.github.com>
This commit is contained in:
Booquefius 2022-08-15 17:31:17 -04:00 committed by GitHub
parent fd7f79402f
commit 613f52d3ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 116 additions and 80 deletions

View file

@ -22,8 +22,9 @@ namespace MinecraftClient.ChatBots
private float health = 100;
private bool singleMode = true;
private bool priorityDistance = true;
private InteractType interactMode;
public AutoAttack(string mode, string priority, bool overrideAttackSpeed = false, double cooldownSeconds = 1)
public AutoAttack(string mode, string priority, bool overrideAttackSpeed = false, double cooldownSeconds = 1, InteractType interaction = InteractType.Attack)
{
if (mode == "single")
singleMode = true;
@ -37,6 +38,8 @@ namespace MinecraftClient.ChatBots
priorityDistance = false;
else LogToConsoleTranslated("bot.autoAttack.priority", priority);
interactMode = interaction;
if (overrideAttackSpeed)
{
if (cooldownSeconds <= 0)
@ -103,7 +106,7 @@ namespace MinecraftClient.ChatBots
// check entity distance and health again
if (shouldAttackEntity(entitiesToAttack[priorityEntity]))
{
InteractEntity(priorityEntity, 1); // hit the entity!
InteractEntity(priorityEntity, interactMode); // hit the entity!
SendAnimation(Inventory.Hand.MainHand); // Arm animation
}
}
@ -114,7 +117,7 @@ namespace MinecraftClient.ChatBots
// check that we are in range once again.
if (shouldAttackEntity(entity.Value))
{
InteractEntity(entity.Key, 1); // hit the entity!
InteractEntity(entity.Key, interactMode); // hit the entity!
}
}
SendAnimation(Inventory.Hand.MainHand); // Arm animation

View file

@ -33,10 +33,10 @@ namespace MinecraftClient.Commands
switch (action)
{
case "attack":
handler.InteractEntity(entityID, 1);
handler.InteractEntity(entityID, InteractType.Attack);
return Translations.Get("cmd.entityCmd.attacked");
case "use":
handler.InteractEntity(entityID, 0);
handler.InteractEntity(entityID, InteractType.Interact);
return Translations.Get("cmd.entityCmd.used");
default:
Entity entity = handler.GetEntities()[entityID];
@ -113,13 +113,13 @@ namespace MinecraftClient.Commands
: "list";
if (action == "attack")
{
handler.InteractEntity(entity2.Key, 1);
handler.InteractEntity(entity2.Key, InteractType.Attack);
actionst = "cmd.entityCmd.attacked";
actioncount++;
}
else if (action == "use")
{
handler.InteractEntity(entity2.Key, 0);
handler.InteractEntity(entity2.Key, InteractType.Interact);
actionst = "cmd.entityCmd.used";
actioncount++;
}

View file

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MinecraftClient.Mapping
{
public enum InteractType
{
Interact = 0,
Attack = 1,
InteractAt = 2,
}
}

View file

@ -212,7 +212,7 @@ namespace MinecraftClient
if (Settings.ScriptScheduler_Enabled) { BotLoad(new ChatBots.ScriptScheduler(Settings.ExpandVars(Settings.ScriptScheduler_TasksFile))); }
if (Settings.RemoteCtrl_Enabled) { BotLoad(new ChatBots.RemoteControl()); }
if (Settings.AutoRespond_Enabled) { BotLoad(new ChatBots.AutoRespond(Settings.AutoRespond_Matches)); }
if (Settings.AutoAttack_Enabled) { BotLoad(new ChatBots.AutoAttack(Settings.AutoAttack_Mode, Settings.AutoAttack_Priority, Settings.AutoAttack_OverrideAttackSpeed, Settings.AutoAttack_CooldownSeconds)); }
if (Settings.AutoAttack_Enabled) { BotLoad(new ChatBots.AutoAttack(Settings.AutoAttack_Mode, Settings.AutoAttack_Priority, Settings.AutoAttack_OverrideAttackSpeed, Settings.AutoAttack_CooldownSeconds, Settings.AutoAttack_Interaction)); }
if (Settings.AutoFishing_Enabled) { BotLoad(new ChatBots.AutoFishing()); }
if (Settings.AutoEat_Enabled) { BotLoad(new ChatBots.AutoEat(Settings.AutoEat_hungerThreshold)); }
if (Settings.Mailer_Enabled) { BotLoad(new ChatBots.Mailer()); }
@ -1632,23 +1632,23 @@ namespace MinecraftClient
/// Interact with an entity
/// </summary>
/// <param name="EntityID"></param>
/// <param name="type">0: interact, 1: attack, 2: interact at</param>
/// <param name="type">Type of interaction (interact, attack...)</param>
/// <param name="hand">Hand.MainHand or Hand.OffHand</param>
/// <returns>TRUE if interaction succeeded</returns>
public bool InteractEntity(int entityID, int type, Hand hand = Hand.MainHand)
public bool InteractEntity(int entityID, InteractType type, Hand hand = Hand.MainHand)
{
if (InvokeRequired)
return InvokeOnMainThread(() => InteractEntity(entityID, type, hand));
if (entities.ContainsKey(entityID))
{
if (type == 0)
if (type == InteractType.Interact)
{
return handler.SendInteractEntity(entityID, type, (int)hand);
return handler.SendInteractEntity(entityID, (int)type, (int)hand);
}
else
{
return handler.SendInteractEntity(entityID, type);
return handler.SendInteractEntity(entityID, (int)type);
}
}
else return false;

View file

@ -1,67 +1,67 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<OutputType>Exe</OutputType>
<PublishUrl>publish\</PublishUrl>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<LangVersion>default</LangVersion>
<Nullable>enable</Nullable>
<PublishSingleFile>true</PublishSingleFile>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
</PropertyGroup>
<PropertyGroup>
<SignManifests>false</SignManifests>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>Resources\AppIcon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup>
<StartupObject>MinecraftClient.Program</StartupObject>
</PropertyGroup>
<ItemGroup>
<Content Include="Resources\AppIcon.ico" />
<Content Include="Resources\containers\ContainerType.BrewingStand.txt" />
<Content Include="Resources\containers\ContainerType.Crafting.txt" />
<Content Include="Resources\containers\ContainerType.Generic_3x3.txt" />
<Content Include="Resources\containers\ContainerType.Generic_9x3.txt" />
<Content Include="Resources\containers\ContainerType.Generic_9x6.txt" />
<Content Include="Resources\containers\ContainerType.PlayerInventory.txt" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="DnsClient" Version="1.5.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0-1.final" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.2.233001">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="5.0.2" />
<PackageReference Include="SingleFileExtractor.Core" Version="1.0.1" />
</ItemGroup>
<ItemGroup>
<Compile Remove="config\ChatBots\AutoLook.cs" />
<Compile Remove="config\ChatBots\AutoTree.cs" />
<Compile Remove="config\ChatBots\ClckRuAPI.cs" />
<Compile Remove="config\ChatBots\CobblestoneMiner.cs" />
<Compile Remove="config\ChatBots\DiscordWebhook.cs" />
<Compile Remove="config\ChatBots\OreMiner.cs" />
<Compile Remove="config\ChatBots\PayKassa.cs" />
<Compile Remove="config\ChatBots\QIWIAPI.cs" />
<Compile Remove="config\ChatBots\SugarCaneMiner.cs" />
<Compile Remove="config\ChatBots\TreeFarmer.cs" />
<Compile Remove="config\ChatBots\VkMessager.cs" />
<Compile Remove="config\sample-script-extended.cs" />
<Compile Remove="config\sample-script-pm-forwarder.cs" />
<Compile Remove="config\sample-script-random-command.cs" />
<Compile Remove="config\sample-script-with-chatbot.cs" />
<Compile Remove="config\sample-script-with-http-request.cs" />
<Compile Remove="config\sample-script-with-task.cs" />
<Compile Remove="config\sample-script-with-world-access.cs" />
<Compile Remove="config\sample-script.cs" />
<Compile Remove="config\ChatBots\MineCube.cs" />
<Compile Remove="config\ChatBots\SugarCaneFarmer.cs" />
<Compile Remove="Mapping\VillagerInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ConsoleInteractive\ConsoleInteractive\ConsoleInteractive\ConsoleInteractive.csproj" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<OutputType>Exe</OutputType>
<PublishUrl>publish\</PublishUrl>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<LangVersion>default</LangVersion>
<Nullable>enable</Nullable>
<PublishSingleFile>true</PublishSingleFile>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
</PropertyGroup>
<PropertyGroup>
<SignManifests>false</SignManifests>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>Resources\AppIcon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup>
<StartupObject>MinecraftClient.Program</StartupObject>
</PropertyGroup>
<ItemGroup>
<Content Include="Resources\AppIcon.ico" />
<Content Include="Resources\containers\ContainerType.BrewingStand.txt" />
<Content Include="Resources\containers\ContainerType.Crafting.txt" />
<Content Include="Resources\containers\ContainerType.Generic_3x3.txt" />
<Content Include="Resources\containers\ContainerType.Generic_9x3.txt" />
<Content Include="Resources\containers\ContainerType.Generic_9x6.txt" />
<Content Include="Resources\containers\ContainerType.PlayerInventory.txt" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="DnsClient" Version="1.5.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0-1.final" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.2.233001">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="5.0.2" />
<PackageReference Include="SingleFileExtractor.Core" Version="1.0.1" />
</ItemGroup>
<ItemGroup>
<Compile Remove="config\ChatBots\AutoLook.cs" />
<Compile Remove="config\ChatBots\AutoTree.cs" />
<Compile Remove="config\ChatBots\ClckRuAPI.cs" />
<Compile Remove="config\ChatBots\CobblestoneMiner.cs" />
<Compile Remove="config\ChatBots\DiscordWebhook.cs" />
<Compile Remove="config\ChatBots\OreMiner.cs" />
<Compile Remove="config\ChatBots\PayKassa.cs" />
<Compile Remove="config\ChatBots\QIWIAPI.cs" />
<Compile Remove="config\ChatBots\SugarCaneMiner.cs" />
<Compile Remove="config\ChatBots\TreeFarmer.cs" />
<Compile Remove="config\ChatBots\VkMessager.cs" />
<Compile Remove="config\sample-script-extended.cs" />
<Compile Remove="config\sample-script-pm-forwarder.cs" />
<Compile Remove="config\sample-script-random-command.cs" />
<Compile Remove="config\sample-script-with-chatbot.cs" />
<Compile Remove="config\sample-script-with-http-request.cs" />
<Compile Remove="config\sample-script-with-task.cs" />
<Compile Remove="config\sample-script-with-world-access.cs" />
<Compile Remove="config\sample-script.cs" />
<Compile Remove="config\ChatBots\MineCube.cs" />
<Compile Remove="config\ChatBots\SugarCaneFarmer.cs" />
<Compile Remove="Mapping\VillagerInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ConsoleInteractive\ConsoleInteractive\ConsoleInteractive\ConsoleInteractive.csproj" />
</ItemGroup>
</Project>

View file

@ -196,6 +196,7 @@ enabled=false
mode=single # single or multi. single target one mob per attack. multi target all mobs in range per attack
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)
[AutoFishing]
# Automatically catch fish using a fishing rod

View file

@ -1190,7 +1190,20 @@ namespace MinecraftClient
/// <param name="type">0: interact, 1: attack, 2: interact at</param>
/// <param name="hand">Hand.MainHand or Hand.OffHand</param>
/// <returns>TRUE in case of success</returns>
[Obsolete("Prefer using InteractType enum instead of int for interaction type")]
protected bool InteractEntity(int EntityID, int type, Hand hand = Hand.MainHand)
{
return Handler.InteractEntity(EntityID, (InteractType)type, hand);
}
/// <summary>
/// Interact with an entity
/// </summary>
/// <param name="EntityID"></param>
/// <param name="type">Interaction type (InteractType.Interact, Attack or AttackAt)</param>
/// <param name="hand">Hand.MainHand or Hand.OffHand</param>
/// <returns>TRUE in case of success</returns>
protected bool InteractEntity(int EntityID, InteractType type, Hand hand = Hand.MainHand)
{
return Handler.InteractEntity(EntityID, type, hand);
}

View file

@ -6,6 +6,7 @@ using System.IO;
using System.Text.RegularExpressions;
using MinecraftClient.Protocol.Session;
using MinecraftClient.Protocol;
using MinecraftClient.Mapping;
namespace MinecraftClient
{
@ -192,6 +193,7 @@ namespace MinecraftClient
public static string AutoAttack_Priority = "distance";
public static bool AutoAttack_OverrideAttackSpeed = false;
public static double AutoAttack_CooldownSeconds = 1;
public static InteractType AutoAttack_Interaction = InteractType.Attack;
//Auto Fishing
public static bool AutoFishing_Enabled = false;
@ -693,6 +695,8 @@ namespace MinecraftClient
AutoAttack_CooldownSeconds = str2float(argValue);
}
return true;
case "interaction":
return Enum.TryParse(argValue, true, out AutoAttack_Interaction);
}
break;