Skip to content

Commit 2409de2

Browse files
authored
Fix prevent AntiCheat Block Breaking (In Scripts)
Fix prevent AntiCheat Block Breaking (In Scripts)
2 parents c38f92a + df94433 commit 2409de2

File tree

7 files changed

+100
-16
lines changed

7 files changed

+100
-16
lines changed

MinecraftClient/ChatBots/AutoDig.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ private bool DoDigging()
285285
if (Config.Mode == Configs.ModeType.lookat ||
286286
(Config.Mode == Configs.ModeType.both && Config._Locations.Contains(blockLoc)))
287287
{
288-
if (DigBlock(blockLoc, lookAtBlock: false))
288+
if (DigBlock(blockLoc, Direction.Down, lookAtBlock: false))
289289
{
290290
currentDig = blockLoc;
291291
if (Config.Log_Block_Dig)
@@ -346,7 +346,7 @@ private bool DoDigging()
346346

347347
if (minDistance <= 6.0)
348348
{
349-
if (DigBlock(target, lookAtBlock: true))
349+
if (DigBlock(target, Direction.Down, lookAtBlock: true))
350350
{
351351
currentDig = target;
352352
if (Config.Log_Block_Dig)
@@ -380,7 +380,7 @@ private bool DoDigging()
380380
((Config.List_Type == Configs.ListType.whitelist && Config.Blocks.Contains(block.Type)) ||
381381
(Config.List_Type == Configs.ListType.blacklist && !Config.Blocks.Contains(block.Type))))
382382
{
383-
if (DigBlock(blockLoc, lookAtBlock: true))
383+
if (DigBlock(blockLoc, Direction.Down, lookAtBlock: true))
384384
{
385385
currentDig = blockLoc;
386386
if (Config.Log_Block_Dig)

MinecraftClient/ChatBots/Farmer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ private bool WaitForMoveToLocation(Location pos, float tolerance = 2f)
831831
// Yoinked from Daenges's Sugarcane Farmer
832832
private bool WaitForDigBlock(Location block, int digTimeout = 1000)
833833
{
834-
if (!DigBlock(block.ToFloor())) return false;
834+
if (!DigBlock(block.ToFloor(), Direction.Down)) return false;
835835
short i = 0; // Maximum wait time of 10 sec.
836836
while (GetWorld().GetBlock(block).Type != Material.Air && i <= digTimeout)
837837
{

MinecraftClient/ChatBots/WebSocketBot.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -651,9 +651,10 @@ private bool ProcessWebsocketCommand(string sessionId, string password, string m
651651

652652
var result = cmd.Parameters.Length switch
653653
{
654-
3 => DigBlock(location),
655-
4 => DigBlock(location, (bool)cmd.Parameters[3]),
656-
5 => DigBlock(location, (bool)cmd.Parameters[3], (bool)cmd.Parameters[4]),
654+
// TODO Get blockFace direction from arguments
655+
3 => DigBlock(location, Direction.Down),
656+
4 => DigBlock(location, Direction.Down, (bool)cmd.Parameters[3]),
657+
5 => DigBlock(location, Direction.Down, (bool)cmd.Parameters[3], (bool)cmd.Parameters[4]),
657658
_ => false
658659
};
659660

MinecraftClient/Commands/Dig.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public override void RegisterCommand(CommandDispatcher<CmdResult> dispatcher)
2222
);
2323

2424
dispatcher.Register(l => l.Literal(CmdName)
25+
// TODO Get blockFace direction from arguments
2526
.Executes(r => DigLookAt(r.Source))
2627
.Then(l => l.Argument("Duration", Arguments.Double())
2728
.Executes(r => DigLookAt(r.Source, Arguments.GetDouble(r, "Duration"))))
@@ -58,7 +59,7 @@ private int DigAt(CmdResult r, Location blockToBreak, double duration = 0)
5859
Block block = handler.GetWorld().GetBlock(blockToBreak);
5960
if (block.Type == Material.Air)
6061
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_no_block);
61-
else if (handler.DigBlock(blockToBreak, duration: duration))
62+
else if (handler.DigBlock(blockToBreak, Direction.Down, duration: duration))
6263
{
6364
blockToBreak = blockToBreak.ToCenter();
6465
return r.SetAndReturn(Status.Done, string.Format(Translations.cmd_dig_dig, blockToBreak.X, blockToBreak.Y, blockToBreak.Z, block.GetTypeString()));
@@ -78,7 +79,7 @@ private int DigLookAt(CmdResult r, double duration = 0)
7879
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_too_far);
7980
else if (block.Type == Material.Air)
8081
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_no_block);
81-
else if (handler.DigBlock(blockLoc, lookAtBlock: false, duration: duration))
82+
else if (handler.DigBlock(blockLoc, Direction.Down, lookAtBlock: false, duration: duration))
8283
return r.SetAndReturn(Status.Done, string.Format(Translations.cmd_dig_dig, blockLoc.X, blockLoc.Y, blockLoc.Z, block.GetTypeString()));
8384
else
8485
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_fail);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
3+
namespace MinecraftClient.Mapping
4+
{
5+
public static class DirectionExtensions
6+
{
7+
public static Direction GetOpposite(this Direction direction)
8+
{
9+
switch (direction)
10+
{
11+
case Direction.SouthEast:
12+
return Direction.NorthEast;
13+
case Direction.SouthWest:
14+
return Direction.NorthWest;
15+
16+
case Direction.NorthEast:
17+
return Direction.SouthEast;
18+
case Direction.NorthWest:
19+
return Direction.SouthWest;
20+
21+
case Direction.West:
22+
return Direction.East;
23+
case Direction.East:
24+
return Direction.West;
25+
26+
case Direction.North:
27+
return Direction.South;
28+
case Direction.South:
29+
return Direction.North;
30+
31+
case Direction.Down:
32+
return Direction.Up;
33+
case Direction.Up:
34+
return Direction.Down;
35+
default:
36+
return Direction.Up;
37+
38+
}
39+
}
40+
41+
42+
public static Direction[] HORIZONTAL =
43+
{
44+
Direction.South,
45+
Direction.West,
46+
Direction.North,
47+
Direction.East
48+
};
49+
50+
public static Direction FromRotation(double rotation)
51+
{
52+
double floor = Math.Floor((rotation / 90.0) + 0.5);
53+
int value = (int)floor & 3;
54+
55+
return FromHorizontal(value);
56+
}
57+
58+
public static Direction FromHorizontal(int value)
59+
{
60+
return HORIZONTAL[Math.Abs(value % HORIZONTAL.Length)];
61+
}
62+
}
63+
}

MinecraftClient/McClient.cs

+14-5
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,15 @@ public void SetNetworkPacketCaptureEnabled(bool enabled)
10871087

10881088
#region Getters: Retrieve data for use in other methods or ChatBots
10891089

1090+
/// <summary>
1091+
/// Gets the horizontal direction of the takeoff.
1092+
/// </summary>
1093+
/// <returns>Return direction of view</returns>
1094+
public Direction GetHorizontalFacing()
1095+
{
1096+
return DirectionExtensions.FromRotation(GetYaw());
1097+
}
1098+
10901099
/// <summary>
10911100
/// Get max length for chat messages
10921101
/// </summary>
@@ -2311,22 +2320,22 @@ public bool PlaceBlock(Location location, Direction blockFace, Hand hand = Hand.
23112320
return InvokeOnMainThread(() => handler.SendPlayerBlockPlacement((int)hand, location, blockFace, sequenceId++));
23122321
}
23132322

2323+
23142324
/// <summary>
23152325
/// Attempt to dig a block at the specified location
23162326
/// </summary>
23172327
/// <param name="location">Location of block to dig</param>
23182328
/// <param name="swingArms">Also perform the "arm swing" animation</param>
23192329
/// <param name="lookAtBlock">Also look at the block before digging</param>
2320-
public bool DigBlock(Location location, bool swingArms = true, bool lookAtBlock = true, double duration = 0)
2330+
public bool DigBlock(Location location, Direction blockFace, bool swingArms = true, bool lookAtBlock = true, double duration = 0)
23212331
{
2332+
// TODO select best face from current player location
2333+
23222334
if (!GetTerrainEnabled())
23232335
return false;
23242336

23252337
if (InvokeRequired)
2326-
return InvokeOnMainThread(() => DigBlock(location, swingArms, lookAtBlock, duration));
2327-
2328-
// TODO select best face from current player location
2329-
Direction blockFace = Direction.Down;
2338+
return InvokeOnMainThread(() => DigBlock(location, blockFace, swingArms, lookAtBlock, duration));
23302339

23312340
lock (DigLock)
23322341
{

MinecraftClient/Scripting/ChatBot.cs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1075,11 +1075,12 @@ protected bool SendEntityAction(Protocol.EntityActionType entityAction)
10751075
/// Attempt to dig a block at the specified location
10761076
/// </summary>
10771077
/// <param name="location">Location of block to dig</param>
1078+
/// <param name="direction">Example: if your player is under a block that is being destroyed, use Down</param>
10781079
/// <param name="swingArms">Also perform the "arm swing" animation</param>
10791080
/// <param name="lookAtBlock">Also look at the block before digging</param>
1080-
protected bool DigBlock(Location location, bool swingArms = true, bool lookAtBlock = true)
1081+
protected bool DigBlock(Location location, Direction direction, bool swingArms = true, bool lookAtBlock = true)
10811082
{
1082-
return Handler.DigBlock(location, swingArms, lookAtBlock);
1083+
return Handler.DigBlock(location, direction, swingArms, lookAtBlock);
10831084
}
10841085

10851086
/// <summary>
@@ -1633,6 +1634,15 @@ protected int GetProtocolVersion()
16331634
return Handler.GetProtocolVersion();
16341635
}
16351636

1637+
/// <summary>
1638+
/// Gets the horizontal direction of the takeoff.
1639+
/// </summary>
1640+
/// <returns>Return direction of view</returns>
1641+
protected Direction GetHorizontalFacing()
1642+
{
1643+
return Handler.GetHorizontalFacing();
1644+
}
1645+
16361646
/// <summary>
16371647
/// Invoke a task on the main thread, wait for completion and retrieve return value.
16381648
/// </summary>

0 commit comments

Comments
 (0)