From fde50c17288727b5b237f461dd9b471410691536 Mon Sep 17 00:00:00 2001 From: Roman Danilov Date: Tue, 5 Mar 2024 18:54:29 +0500 Subject: [PATCH 1/3] AntiCheat fix prevent Block Breaking --- MinecraftClient/ChatBots/AutoDig.cs | 6 +++--- MinecraftClient/ChatBots/Farmer.cs | 2 +- MinecraftClient/ChatBots/WebSocketBot.cs | 7 ++++--- MinecraftClient/Commands/Dig.cs | 4 ++-- MinecraftClient/McClient.cs | 5 ++--- MinecraftClient/Scripting/ChatBot.cs | 5 +++-- 6 files changed, 15 insertions(+), 14 deletions(-) diff --git a/MinecraftClient/ChatBots/AutoDig.cs b/MinecraftClient/ChatBots/AutoDig.cs index cddb6ffd..17c27895 100644 --- a/MinecraftClient/ChatBots/AutoDig.cs +++ b/MinecraftClient/ChatBots/AutoDig.cs @@ -285,7 +285,7 @@ namespace MinecraftClient.ChatBots if (Config.Mode == Configs.ModeType.lookat || (Config.Mode == Configs.ModeType.both && Config._Locations.Contains(blockLoc))) { - if (DigBlock(blockLoc, lookAtBlock: false)) + if (DigBlock(blockLoc, Direction.Down, lookAtBlock: false)) { currentDig = blockLoc; if (Config.Log_Block_Dig) @@ -346,7 +346,7 @@ namespace MinecraftClient.ChatBots if (minDistance <= 6.0) { - if (DigBlock(target, lookAtBlock: true)) + if (DigBlock(target, Direction.Down, lookAtBlock: true)) { currentDig = target; if (Config.Log_Block_Dig) @@ -380,7 +380,7 @@ namespace MinecraftClient.ChatBots ((Config.List_Type == Configs.ListType.whitelist && Config.Blocks.Contains(block.Type)) || (Config.List_Type == Configs.ListType.blacklist && !Config.Blocks.Contains(block.Type)))) { - if (DigBlock(blockLoc, lookAtBlock: true)) + if (DigBlock(blockLoc, Direction.Down, lookAtBlock: true)) { currentDig = blockLoc; if (Config.Log_Block_Dig) diff --git a/MinecraftClient/ChatBots/Farmer.cs b/MinecraftClient/ChatBots/Farmer.cs index f072ba2e..7c15791c 100644 --- a/MinecraftClient/ChatBots/Farmer.cs +++ b/MinecraftClient/ChatBots/Farmer.cs @@ -831,7 +831,7 @@ namespace MinecraftClient.ChatBots // Yoinked from Daenges's Sugarcane Farmer private bool WaitForDigBlock(Location block, int digTimeout = 1000) { - if (!DigBlock(block.ToFloor())) return false; + if (!DigBlock(block.ToFloor(), Direction.Down)) return false; short i = 0; // Maximum wait time of 10 sec. while (GetWorld().GetBlock(block).Type != Material.Air && i <= digTimeout) { diff --git a/MinecraftClient/ChatBots/WebSocketBot.cs b/MinecraftClient/ChatBots/WebSocketBot.cs index 1aa6caa9..d4fc6842 100644 --- a/MinecraftClient/ChatBots/WebSocketBot.cs +++ b/MinecraftClient/ChatBots/WebSocketBot.cs @@ -651,9 +651,10 @@ public class WebSocketBot : ChatBot var result = cmd.Parameters.Length switch { - 3 => DigBlock(location), - 4 => DigBlock(location, (bool)cmd.Parameters[3]), - 5 => DigBlock(location, (bool)cmd.Parameters[3], (bool)cmd.Parameters[4]), + // TODO Get Direction from the arguments + 3 => DigBlock(location, Direction.Down), + 4 => DigBlock(location, Direction.Down, (bool)cmd.Parameters[3]), + 5 => DigBlock(location, Direction.Down, (bool)cmd.Parameters[3], (bool)cmd.Parameters[4]), _ => false }; diff --git a/MinecraftClient/Commands/Dig.cs b/MinecraftClient/Commands/Dig.cs index 3a00c44b..b6ff9420 100644 --- a/MinecraftClient/Commands/Dig.cs +++ b/MinecraftClient/Commands/Dig.cs @@ -58,7 +58,7 @@ namespace MinecraftClient.Commands Block block = handler.GetWorld().GetBlock(blockToBreak); if (block.Type == Material.Air) return r.SetAndReturn(Status.Fail, Translations.cmd_dig_no_block); - else if (handler.DigBlock(blockToBreak, duration: duration)) + else if (handler.DigBlock(blockToBreak, Direction.Down, duration: duration)) { blockToBreak = blockToBreak.ToCenter(); return r.SetAndReturn(Status.Done, string.Format(Translations.cmd_dig_dig, blockToBreak.X, blockToBreak.Y, blockToBreak.Z, block.GetTypeString())); @@ -78,7 +78,7 @@ namespace MinecraftClient.Commands return r.SetAndReturn(Status.Fail, Translations.cmd_dig_too_far); else if (block.Type == Material.Air) return r.SetAndReturn(Status.Fail, Translations.cmd_dig_no_block); - else if (handler.DigBlock(blockLoc, lookAtBlock: false, duration: duration)) + else if (handler.DigBlock(blockLoc, Direction.Down, lookAtBlock: false, duration: duration)) return r.SetAndReturn(Status.Done, string.Format(Translations.cmd_dig_dig, blockLoc.X, blockLoc.Y, blockLoc.Z, block.GetTypeString())); else return r.SetAndReturn(Status.Fail, Translations.cmd_dig_fail); diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index c7296d52..786f35e1 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -2266,16 +2266,15 @@ namespace MinecraftClient /// Location of block to dig /// Also perform the "arm swing" animation /// Also look at the block before digging - public bool DigBlock(Location location, bool swingArms = true, bool lookAtBlock = true, double duration = 0) + public bool DigBlock(Location location, Direction blockFace, bool swingArms = true, bool lookAtBlock = true, double duration = 0) { if (!GetTerrainEnabled()) return false; if (InvokeRequired) - return InvokeOnMainThread(() => DigBlock(location, swingArms, lookAtBlock, duration)); + return InvokeOnMainThread(() => DigBlock(location, blockFace, swingArms, lookAtBlock, duration)); // TODO select best face from current player location - Direction blockFace = Direction.Down; lock (DigLock) { diff --git a/MinecraftClient/Scripting/ChatBot.cs b/MinecraftClient/Scripting/ChatBot.cs index 36c365be..ac77d935 100644 --- a/MinecraftClient/Scripting/ChatBot.cs +++ b/MinecraftClient/Scripting/ChatBot.cs @@ -1072,11 +1072,12 @@ namespace MinecraftClient.Scripting /// Attempt to dig a block at the specified location /// /// Location of block to dig + /// Example: if your player is under a block that is being destroyed, use Down /// Also perform the "arm swing" animation /// Also look at the block before digging - protected bool DigBlock(Location location, bool swingArms = true, bool lookAtBlock = true) + protected bool DigBlock(Location location, Direction direction, bool swingArms = true, bool lookAtBlock = true) { - return Handler.DigBlock(location, swingArms, lookAtBlock); + return Handler.DigBlock(location, direction, swingArms, lookAtBlock); } /// From 91ef890bb615ab140552499891279002fb22af57 Mon Sep 17 00:00:00 2001 From: Roman Danilov Date: Tue, 5 Mar 2024 20:30:58 +0500 Subject: [PATCH 2/3] Auxiliary class for Direction, preparation for autodetection of the broken side of the block --- MinecraftClient/ChatBots/WebSocketBot.cs | 2 +- MinecraftClient/Commands/Dig.cs | 1 + .../Mapping/DirectionExtensions.cs | 39 +++++++++++++++++++ MinecraftClient/McClient.cs | 4 +- 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 MinecraftClient/Mapping/DirectionExtensions.cs diff --git a/MinecraftClient/ChatBots/WebSocketBot.cs b/MinecraftClient/ChatBots/WebSocketBot.cs index d4fc6842..778ff018 100644 --- a/MinecraftClient/ChatBots/WebSocketBot.cs +++ b/MinecraftClient/ChatBots/WebSocketBot.cs @@ -651,7 +651,7 @@ public class WebSocketBot : ChatBot var result = cmd.Parameters.Length switch { - // TODO Get Direction from the arguments + // TODO Get blockFace direction from arguments 3 => DigBlock(location, Direction.Down), 4 => DigBlock(location, Direction.Down, (bool)cmd.Parameters[3]), 5 => DigBlock(location, Direction.Down, (bool)cmd.Parameters[3], (bool)cmd.Parameters[4]), diff --git a/MinecraftClient/Commands/Dig.cs b/MinecraftClient/Commands/Dig.cs index b6ff9420..477e2c77 100644 --- a/MinecraftClient/Commands/Dig.cs +++ b/MinecraftClient/Commands/Dig.cs @@ -22,6 +22,7 @@ namespace MinecraftClient.Commands ); dispatcher.Register(l => l.Literal(CmdName) + // TODO Get blockFace direction from arguments .Executes(r => DigLookAt(r.Source)) .Then(l => l.Argument("Duration", Arguments.Double()) .Executes(r => DigLookAt(r.Source, Arguments.GetDouble(r, "Duration")))) diff --git a/MinecraftClient/Mapping/DirectionExtensions.cs b/MinecraftClient/Mapping/DirectionExtensions.cs new file mode 100644 index 00000000..fe47f020 --- /dev/null +++ b/MinecraftClient/Mapping/DirectionExtensions.cs @@ -0,0 +1,39 @@ +namespace MinecraftClient.Mapping +{ + public static class DirectionExtensions + { + public static Direction GetOpposite(this Direction direction) + { + switch (direction) + { + case Direction.SouthEast: + return Direction.NorthEast; + case Direction.SouthWest: + return Direction.NorthWest; + + case Direction.NorthEast: + return Direction.SouthEast; + case Direction.NorthWest: + return Direction.SouthWest; + + case Direction.West: + return Direction.East; + case Direction.East: + return Direction.West; + + case Direction.North: + return Direction.South; + case Direction.South: + return Direction.North; + + case Direction.Down: + return Direction.Up; + case Direction.Up: + return Direction.Down; + default: + return Direction.Up; + + } + } + } +} diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index 786f35e1..b58e420e 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -2268,14 +2268,14 @@ namespace MinecraftClient /// Also look at the block before digging public bool DigBlock(Location location, Direction blockFace, bool swingArms = true, bool lookAtBlock = true, double duration = 0) { + // TODO select best face from current player location + if (!GetTerrainEnabled()) return false; if (InvokeRequired) return InvokeOnMainThread(() => DigBlock(location, blockFace, swingArms, lookAtBlock, duration)); - // TODO select best face from current player location - lock (DigLock) { if (RemainingDiggingTime > 0 && LastDigPosition != null) From df9443381bc45d19e97d7e6445bb58fec18b43a6 Mon Sep 17 00:00:00 2001 From: Roman Danilov Date: Tue, 5 Mar 2024 21:49:29 +0500 Subject: [PATCH 3/3] Done auxiliary methods for Direction --- .../Mapping/DirectionExtensions.cs | 26 ++++++++++++++++++- MinecraftClient/McClient.cs | 10 +++++++ MinecraftClient/Scripting/ChatBot.cs | 9 +++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/MinecraftClient/Mapping/DirectionExtensions.cs b/MinecraftClient/Mapping/DirectionExtensions.cs index fe47f020..9a42368c 100644 --- a/MinecraftClient/Mapping/DirectionExtensions.cs +++ b/MinecraftClient/Mapping/DirectionExtensions.cs @@ -1,4 +1,6 @@ -namespace MinecraftClient.Mapping +using System; + +namespace MinecraftClient.Mapping { public static class DirectionExtensions { @@ -35,5 +37,27 @@ } } + + + public static Direction[] HORIZONTAL = + { + Direction.South, + Direction.West, + Direction.North, + Direction.East + }; + + public static Direction FromRotation(double rotation) + { + double floor = Math.Floor((rotation / 90.0) + 0.5); + int value = (int)floor & 3; + + return FromHorizontal(value); + } + + public static Direction FromHorizontal(int value) + { + return HORIZONTAL[Math.Abs(value % HORIZONTAL.Length)]; + } } } diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index b58e420e..4a8b6ab0 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -1036,6 +1036,15 @@ namespace MinecraftClient #region Getters: Retrieve data for use in other methods or ChatBots + /// + /// Gets the horizontal direction of the takeoff. + /// + /// Return direction of view + public Direction GetHorizontalFacing() + { + return DirectionExtensions.FromRotation(GetYaw()); + } + /// /// Get max length for chat messages /// @@ -2260,6 +2269,7 @@ namespace MinecraftClient return InvokeOnMainThread(() => handler.SendPlayerBlockPlacement((int)hand, location, blockFace, sequenceId++)); } + /// /// Attempt to dig a block at the specified location /// diff --git a/MinecraftClient/Scripting/ChatBot.cs b/MinecraftClient/Scripting/ChatBot.cs index ac77d935..8e8fc674 100644 --- a/MinecraftClient/Scripting/ChatBot.cs +++ b/MinecraftClient/Scripting/ChatBot.cs @@ -1631,6 +1631,15 @@ namespace MinecraftClient.Scripting return Handler.GetProtocolVersion(); } + /// + /// Gets the horizontal direction of the takeoff. + /// + /// Return direction of view + protected Direction GetHorizontalFacing() + { + return Handler.GetHorizontalFacing(); + } + /// /// Invoke a task on the main thread, wait for completion and retrieve return value. ///