Skip to content

Commit 613f52d

Browse files
casperwtfORelio
andauthored
AuotoAttack: add support for multiple interact modes (#2044)
* Adds support for multiple interact modes * Entity interaction: Implement enum Co-authored-by: ORelio <[email protected]>
1 parent fd7f794 commit 613f52d

File tree

8 files changed

+116
-80
lines changed

8 files changed

+116
-80
lines changed

MinecraftClient/ChatBots/AutoAttack.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ class AutoAttack : ChatBot
2222
private float health = 100;
2323
private bool singleMode = true;
2424
private bool priorityDistance = true;
25+
private InteractType interactMode;
2526

26-
public AutoAttack(string mode, string priority, bool overrideAttackSpeed = false, double cooldownSeconds = 1)
27+
public AutoAttack(string mode, string priority, bool overrideAttackSpeed = false, double cooldownSeconds = 1, InteractType interaction = InteractType.Attack)
2728
{
2829
if (mode == "single")
2930
singleMode = true;
@@ -37,6 +38,8 @@ public AutoAttack(string mode, string priority, bool overrideAttackSpeed = false
3738
priorityDistance = false;
3839
else LogToConsoleTranslated("bot.autoAttack.priority", priority);
3940

41+
interactMode = interaction;
42+
4043
if (overrideAttackSpeed)
4144
{
4245
if (cooldownSeconds <= 0)
@@ -103,7 +106,7 @@ public override void Update()
103106
// check entity distance and health again
104107
if (shouldAttackEntity(entitiesToAttack[priorityEntity]))
105108
{
106-
InteractEntity(priorityEntity, 1); // hit the entity!
109+
InteractEntity(priorityEntity, interactMode); // hit the entity!
107110
SendAnimation(Inventory.Hand.MainHand); // Arm animation
108111
}
109112
}
@@ -114,7 +117,7 @@ public override void Update()
114117
// check that we are in range once again.
115118
if (shouldAttackEntity(entity.Value))
116119
{
117-
InteractEntity(entity.Key, 1); // hit the entity!
120+
InteractEntity(entity.Key, interactMode); // hit the entity!
118121
}
119122
}
120123
SendAnimation(Inventory.Hand.MainHand); // Arm animation

MinecraftClient/Commands/Entitycmd.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ public override string Run(McClient handler, string command, Dictionary<string,
3333
switch (action)
3434
{
3535
case "attack":
36-
handler.InteractEntity(entityID, 1);
36+
handler.InteractEntity(entityID, InteractType.Attack);
3737
return Translations.Get("cmd.entityCmd.attacked");
3838
case "use":
39-
handler.InteractEntity(entityID, 0);
39+
handler.InteractEntity(entityID, InteractType.Interact);
4040
return Translations.Get("cmd.entityCmd.used");
4141
default:
4242
Entity entity = handler.GetEntities()[entityID];
@@ -113,13 +113,13 @@ public override string Run(McClient handler, string command, Dictionary<string,
113113
: "list";
114114
if (action == "attack")
115115
{
116-
handler.InteractEntity(entity2.Key, 1);
116+
handler.InteractEntity(entity2.Key, InteractType.Attack);
117117
actionst = "cmd.entityCmd.attacked";
118118
actioncount++;
119119
}
120120
else if (action == "use")
121121
{
122-
handler.InteractEntity(entity2.Key, 0);
122+
handler.InteractEntity(entity2.Key, InteractType.Interact);
123123
actionst = "cmd.entityCmd.used";
124124
actioncount++;
125125
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MinecraftClient.Mapping
8+
{
9+
public enum InteractType
10+
{
11+
Interact = 0,
12+
Attack = 1,
13+
InteractAt = 2,
14+
}
15+
}

MinecraftClient/McClient.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ private void StartClient(string user, string uuid, string sessionID, PlayerKeyPa
212212
if (Settings.ScriptScheduler_Enabled) { BotLoad(new ChatBots.ScriptScheduler(Settings.ExpandVars(Settings.ScriptScheduler_TasksFile))); }
213213
if (Settings.RemoteCtrl_Enabled) { BotLoad(new ChatBots.RemoteControl()); }
214214
if (Settings.AutoRespond_Enabled) { BotLoad(new ChatBots.AutoRespond(Settings.AutoRespond_Matches)); }
215-
if (Settings.AutoAttack_Enabled) { BotLoad(new ChatBots.AutoAttack(Settings.AutoAttack_Mode, Settings.AutoAttack_Priority, Settings.AutoAttack_OverrideAttackSpeed, Settings.AutoAttack_CooldownSeconds)); }
215+
if (Settings.AutoAttack_Enabled) { BotLoad(new ChatBots.AutoAttack(Settings.AutoAttack_Mode, Settings.AutoAttack_Priority, Settings.AutoAttack_OverrideAttackSpeed, Settings.AutoAttack_CooldownSeconds, Settings.AutoAttack_Interaction)); }
216216
if (Settings.AutoFishing_Enabled) { BotLoad(new ChatBots.AutoFishing()); }
217217
if (Settings.AutoEat_Enabled) { BotLoad(new ChatBots.AutoEat(Settings.AutoEat_hungerThreshold)); }
218218
if (Settings.Mailer_Enabled) { BotLoad(new ChatBots.Mailer()); }
@@ -1632,23 +1632,23 @@ public bool ClearInventories()
16321632
/// Interact with an entity
16331633
/// </summary>
16341634
/// <param name="EntityID"></param>
1635-
/// <param name="type">0: interact, 1: attack, 2: interact at</param>
1635+
/// <param name="type">Type of interaction (interact, attack...)</param>
16361636
/// <param name="hand">Hand.MainHand or Hand.OffHand</param>
16371637
/// <returns>TRUE if interaction succeeded</returns>
1638-
public bool InteractEntity(int entityID, int type, Hand hand = Hand.MainHand)
1638+
public bool InteractEntity(int entityID, InteractType type, Hand hand = Hand.MainHand)
16391639
{
16401640
if (InvokeRequired)
16411641
return InvokeOnMainThread(() => InteractEntity(entityID, type, hand));
16421642

16431643
if (entities.ContainsKey(entityID))
16441644
{
1645-
if (type == 0)
1645+
if (type == InteractType.Interact)
16461646
{
1647-
return handler.SendInteractEntity(entityID, type, (int)hand);
1647+
return handler.SendInteractEntity(entityID, (int)type, (int)hand);
16481648
}
16491649
else
16501650
{
1651-
return handler.SendInteractEntity(entityID, type);
1651+
return handler.SendInteractEntity(entityID, (int)type);
16521652
}
16531653
}
16541654
else return false;
Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,67 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
<PropertyGroup>
3-
<TargetFramework>net6.0</TargetFramework>
4-
<OutputType>Exe</OutputType>
5-
<PublishUrl>publish\</PublishUrl>
6-
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
7-
<LangVersion>default</LangVersion>
8-
<Nullable>enable</Nullable>
9-
<PublishSingleFile>true</PublishSingleFile>
10-
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
11-
</PropertyGroup>
12-
<PropertyGroup>
13-
<SignManifests>false</SignManifests>
14-
</PropertyGroup>
15-
<PropertyGroup>
16-
<ApplicationIcon>Resources\AppIcon.ico</ApplicationIcon>
17-
</PropertyGroup>
18-
<PropertyGroup>
19-
<StartupObject>MinecraftClient.Program</StartupObject>
20-
</PropertyGroup>
21-
<ItemGroup>
22-
<Content Include="Resources\AppIcon.ico" />
23-
<Content Include="Resources\containers\ContainerType.BrewingStand.txt" />
24-
<Content Include="Resources\containers\ContainerType.Crafting.txt" />
25-
<Content Include="Resources\containers\ContainerType.Generic_3x3.txt" />
26-
<Content Include="Resources\containers\ContainerType.Generic_9x3.txt" />
27-
<Content Include="Resources\containers\ContainerType.Generic_9x6.txt" />
28-
<Content Include="Resources\containers\ContainerType.PlayerInventory.txt" />
29-
</ItemGroup>
30-
<ItemGroup>
31-
<PackageReference Include="DnsClient" Version="1.5.0" />
32-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0-1.final" />
33-
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
34-
<PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.2.233001">
35-
<PrivateAssets>all</PrivateAssets>
36-
</PackageReference>
37-
<PackageReference Include="Microsoft.Windows.Compatibility" Version="5.0.2" />
38-
<PackageReference Include="SingleFileExtractor.Core" Version="1.0.1" />
39-
</ItemGroup>
40-
<ItemGroup>
41-
<Compile Remove="config\ChatBots\AutoLook.cs" />
42-
<Compile Remove="config\ChatBots\AutoTree.cs" />
43-
<Compile Remove="config\ChatBots\ClckRuAPI.cs" />
44-
<Compile Remove="config\ChatBots\CobblestoneMiner.cs" />
45-
<Compile Remove="config\ChatBots\DiscordWebhook.cs" />
46-
<Compile Remove="config\ChatBots\OreMiner.cs" />
47-
<Compile Remove="config\ChatBots\PayKassa.cs" />
48-
<Compile Remove="config\ChatBots\QIWIAPI.cs" />
49-
<Compile Remove="config\ChatBots\SugarCaneMiner.cs" />
50-
<Compile Remove="config\ChatBots\TreeFarmer.cs" />
51-
<Compile Remove="config\ChatBots\VkMessager.cs" />
52-
<Compile Remove="config\sample-script-extended.cs" />
53-
<Compile Remove="config\sample-script-pm-forwarder.cs" />
54-
<Compile Remove="config\sample-script-random-command.cs" />
55-
<Compile Remove="config\sample-script-with-chatbot.cs" />
56-
<Compile Remove="config\sample-script-with-http-request.cs" />
57-
<Compile Remove="config\sample-script-with-task.cs" />
58-
<Compile Remove="config\sample-script-with-world-access.cs" />
59-
<Compile Remove="config\sample-script.cs" />
60-
<Compile Remove="config\ChatBots\MineCube.cs" />
61-
<Compile Remove="config\ChatBots\SugarCaneFarmer.cs" />
62-
<Compile Remove="Mapping\VillagerInfo.cs" />
63-
</ItemGroup>
64-
<ItemGroup>
65-
<ProjectReference Include="..\ConsoleInteractive\ConsoleInteractive\ConsoleInteractive\ConsoleInteractive.csproj" />
66-
</ItemGroup>
67-
</Project>
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net6.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<PublishUrl>publish\</PublishUrl>
6+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
7+
<LangVersion>default</LangVersion>
8+
<Nullable>enable</Nullable>
9+
<PublishSingleFile>true</PublishSingleFile>
10+
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
11+
</PropertyGroup>
12+
<PropertyGroup>
13+
<SignManifests>false</SignManifests>
14+
</PropertyGroup>
15+
<PropertyGroup>
16+
<ApplicationIcon>Resources\AppIcon.ico</ApplicationIcon>
17+
</PropertyGroup>
18+
<PropertyGroup>
19+
<StartupObject>MinecraftClient.Program</StartupObject>
20+
</PropertyGroup>
21+
<ItemGroup>
22+
<Content Include="Resources\AppIcon.ico" />
23+
<Content Include="Resources\containers\ContainerType.BrewingStand.txt" />
24+
<Content Include="Resources\containers\ContainerType.Crafting.txt" />
25+
<Content Include="Resources\containers\ContainerType.Generic_3x3.txt" />
26+
<Content Include="Resources\containers\ContainerType.Generic_9x3.txt" />
27+
<Content Include="Resources\containers\ContainerType.Generic_9x6.txt" />
28+
<Content Include="Resources\containers\ContainerType.PlayerInventory.txt" />
29+
</ItemGroup>
30+
<ItemGroup>
31+
<PackageReference Include="DnsClient" Version="1.5.0" />
32+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0-1.final" />
33+
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
34+
<PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.2.233001">
35+
<PrivateAssets>all</PrivateAssets>
36+
</PackageReference>
37+
<PackageReference Include="Microsoft.Windows.Compatibility" Version="5.0.2" />
38+
<PackageReference Include="SingleFileExtractor.Core" Version="1.0.1" />
39+
</ItemGroup>
40+
<ItemGroup>
41+
<Compile Remove="config\ChatBots\AutoLook.cs" />
42+
<Compile Remove="config\ChatBots\AutoTree.cs" />
43+
<Compile Remove="config\ChatBots\ClckRuAPI.cs" />
44+
<Compile Remove="config\ChatBots\CobblestoneMiner.cs" />
45+
<Compile Remove="config\ChatBots\DiscordWebhook.cs" />
46+
<Compile Remove="config\ChatBots\OreMiner.cs" />
47+
<Compile Remove="config\ChatBots\PayKassa.cs" />
48+
<Compile Remove="config\ChatBots\QIWIAPI.cs" />
49+
<Compile Remove="config\ChatBots\SugarCaneMiner.cs" />
50+
<Compile Remove="config\ChatBots\TreeFarmer.cs" />
51+
<Compile Remove="config\ChatBots\VkMessager.cs" />
52+
<Compile Remove="config\sample-script-extended.cs" />
53+
<Compile Remove="config\sample-script-pm-forwarder.cs" />
54+
<Compile Remove="config\sample-script-random-command.cs" />
55+
<Compile Remove="config\sample-script-with-chatbot.cs" />
56+
<Compile Remove="config\sample-script-with-http-request.cs" />
57+
<Compile Remove="config\sample-script-with-task.cs" />
58+
<Compile Remove="config\sample-script-with-world-access.cs" />
59+
<Compile Remove="config\sample-script.cs" />
60+
<Compile Remove="config\ChatBots\MineCube.cs" />
61+
<Compile Remove="config\ChatBots\SugarCaneFarmer.cs" />
62+
<Compile Remove="Mapping\VillagerInfo.cs" />
63+
</ItemGroup>
64+
<ItemGroup>
65+
<ProjectReference Include="..\ConsoleInteractive\ConsoleInteractive\ConsoleInteractive\ConsoleInteractive.csproj" />
66+
</ItemGroup>
67+
</Project>

MinecraftClient/Resources/config/MinecraftClient.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ enabled=false
196196
mode=single # single or multi. single target one mob per attack. multi target all mobs in range per attack
197197
priority=distance # health or distance. Only needed when using single mode
198198
cooldownseconds=auto # How long to wait between each attack. Use auto to let MCC calculate it
199+
interaction=Attack # Possible values: Interact, Attack (default), InteractAt (Interact and Attack)
199200

200201
[AutoFishing]
201202
# Automatically catch fish using a fishing rod

MinecraftClient/Scripting/ChatBot.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,20 @@ protected Double GetServerTPS()
11901190
/// <param name="type">0: interact, 1: attack, 2: interact at</param>
11911191
/// <param name="hand">Hand.MainHand or Hand.OffHand</param>
11921192
/// <returns>TRUE in case of success</returns>
1193+
[Obsolete("Prefer using InteractType enum instead of int for interaction type")]
11931194
protected bool InteractEntity(int EntityID, int type, Hand hand = Hand.MainHand)
1195+
{
1196+
return Handler.InteractEntity(EntityID, (InteractType)type, hand);
1197+
}
1198+
1199+
/// <summary>
1200+
/// Interact with an entity
1201+
/// </summary>
1202+
/// <param name="EntityID"></param>
1203+
/// <param name="type">Interaction type (InteractType.Interact, Attack or AttackAt)</param>
1204+
/// <param name="hand">Hand.MainHand or Hand.OffHand</param>
1205+
/// <returns>TRUE in case of success</returns>
1206+
protected bool InteractEntity(int EntityID, InteractType type, Hand hand = Hand.MainHand)
11941207
{
11951208
return Handler.InteractEntity(EntityID, type, hand);
11961209
}

MinecraftClient/Settings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Text.RegularExpressions;
77
using MinecraftClient.Protocol.Session;
88
using MinecraftClient.Protocol;
9+
using MinecraftClient.Mapping;
910

1011
namespace MinecraftClient
1112
{
@@ -192,6 +193,7 @@ public enum FilterModeEnum { Blacklist, Whitelist }
192193
public static string AutoAttack_Priority = "distance";
193194
public static bool AutoAttack_OverrideAttackSpeed = false;
194195
public static double AutoAttack_CooldownSeconds = 1;
196+
public static InteractType AutoAttack_Interaction = InteractType.Attack;
195197

196198
//Auto Fishing
197199
public static bool AutoFishing_Enabled = false;
@@ -693,6 +695,8 @@ private static bool LoadSingleSetting(Section section, string argName, string ar
693695
AutoAttack_CooldownSeconds = str2float(argValue);
694696
}
695697
return true;
698+
case "interaction":
699+
return Enum.TryParse(argValue, true, out AutoAttack_Interaction);
696700
}
697701
break;
698702

0 commit comments

Comments
 (0)