Skip to content

Commit

Permalink
Use BlockBreaker in TreeBot
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander01998 committed Oct 31, 2023
1 parent 2d1c7f2 commit 032111b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 136 deletions.
121 changes: 54 additions & 67 deletions src/main/java/net/wurstclient/hacks/TreeBotHack.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.network.packet.c2s.play.HandSwingC2SPacket;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.Vec3i;
import net.wurstclient.Category;
import net.wurstclient.SearchTags;
import net.wurstclient.WurstClient;
Expand All @@ -44,12 +41,15 @@
import net.wurstclient.hack.Hack;
import net.wurstclient.hacks.treebot.Tree;
import net.wurstclient.hacks.treebot.TreeBotUtils;
import net.wurstclient.settings.FacingSetting;
import net.wurstclient.settings.SliderSetting;
import net.wurstclient.settings.SliderSetting.ValueDisplay;
import net.wurstclient.settings.SwingHandSetting;
import net.wurstclient.util.BlockBreaker;
import net.wurstclient.util.BlockBreaker.BlockBreakingParams;
import net.wurstclient.util.BlockUtils;
import net.wurstclient.util.OverlayRenderer;
import net.wurstclient.util.RenderUtils;
import net.wurstclient.util.RotationUtils;

@SearchTags({"tree bot"})
@DontSaveState
Expand All @@ -60,6 +60,26 @@ public final class TreeBotHack extends Hack
"How far TreeBot will reach to break blocks.", 4.5, 1, 6, 0.05,
ValueDisplay.DECIMAL);

private final FacingSetting facing = FacingSetting.withoutPacketSpam(
"How to face the logs and leaves when breaking them.\n\n"
+ "\u00a7lOff\u00a7r - Don't face the blocks at all. Will be"
+ " detected by anti-cheat plugins.\n\n"
+ "\u00a7lServer-side\u00a7r - Face the blocks on the"
+ " server-side, while still letting you move the camera freely on"
+ " the client-side.\n\n"
+ "\u00a7lClient-side\u00a7r - Face the blocks by moving your"
+ " camera on the client-side. This is the most legit option, but"
+ " can be disorienting to look at.");

private final SwingHandSetting swingHand = new SwingHandSetting(
"How TreeBot should swing your hand when breaking logs and leaves.\n\n"
+ "\u00a7lOff\u00a7r - Don't swing your hand at all. Will be detected"
+ " by anti-cheat plugins.\n\n"
+ "\u00a7lServer-side\u00a7r - Swing your hand on the server-side,"
+ " without playing the animation on the client-side.\n\n"
+ "\u00a7lClient-side\u00a7r - Swing your hand on the client-side."
+ " This is the most legit option.");

private TreeFinder treeFinder;
private AngleFinder angleFinder;
private TreeBotPathProcessor processor;
Expand All @@ -73,6 +93,10 @@ public TreeBotHack()
super("TreeBot");
setCategory(Category.BLOCKS);
addSetting(range);

// settings to be added in v7.39
// addSetting(facing);
// addSetting(swingHand);
}

@Override
Expand Down Expand Up @@ -157,13 +181,8 @@ public void onUpdate()
return;
}

ArrayList<BlockPos> logsInRange = getLogsInRange();

if(!logsInRange.isEmpty())
{
breakBlocks(logsInRange);
if(breakBlocks(tree.getLogs()))
return;
}

if(angleFinder == null)
angleFinder = new AngleFinder();
Expand Down Expand Up @@ -211,60 +230,38 @@ private void goToAngle()
angleFinder = null;
}

private ArrayList<BlockPos> getLogsInRange()
{
Vec3d eyesVec = RotationUtils.getEyesPos().subtract(0.5, 0.5, 0.5);
double rangeSq = Math.pow(range.getValue(), 2);

return tree.getLogs().stream()
.filter(pos -> eyesVec.squaredDistanceTo(Vec3d.of(pos)) <= rangeSq)
.filter(TreeBotUtils::hasLineOfSight)
.collect(Collectors.toCollection(ArrayList::new));
}

private void breakBlocks(ArrayList<BlockPos> blocksInRange)
private boolean breakBlocks(ArrayList<BlockPos> blocks)
{
for(BlockPos pos : blocksInRange)
for(BlockPos pos : blocks)
if(breakBlock(pos))
{
WURST.getHax().autoToolHack.equipBestTool(pos, false, true, 0);
currentBlock = pos;
break;
return true;
}

if(currentBlock == null)
MC.interactionManager.cancelBlockBreaking();

if(currentBlock != null && BlockUtils.getHardness(currentBlock) < 1)
overlay.updateProgress();
else
overlay.resetProgress();
return false;
}

private boolean breakBlock(BlockPos pos)
{
Direction side =
TreeBotUtils.getLineOfSightSide(RotationUtils.getEyesPos(), pos);

Vec3d relCenter = BlockUtils.getBoundingBox(pos)
.offset(-pos.getX(), -pos.getY(), -pos.getZ()).getCenter();
Vec3d center = Vec3d.of(pos).add(relCenter);
BlockBreakingParams params = BlockBreaker.getBlockBreakingParams(pos);
if(params == null || !params.lineOfSight()
|| params.distanceSq() > range.getValueSq())
return false;

Vec3i dirVec = side.getVector();
Vec3d relHitVec = new Vec3d(relCenter.x * dirVec.getX(),
relCenter.y * dirVec.getY(), relCenter.z * dirVec.getZ());
Vec3d hitVec = center.add(relHitVec);
// select tool
WURST.getHax().autoToolHack.equipBestTool(pos, false, true, 0);

// face block
WURST.getRotationFaker().faceVectorPacket(hitVec);
facing.getSelected().face(params.hitVec());

// damage block
if(!MC.interactionManager.updateBlockBreakingProgress(pos, side))
return false;
// damage block and swing hand
if(MC.interactionManager.updateBlockBreakingProgress(pos,
params.side()))
swingHand.getSelected().swing(Hand.MAIN_HAND);

// swing arm
MC.player.networkHandler
.sendPacket(new HandSwingC2SPacket(Hand.MAIN_HAND));
// update progress
overlay.updateProgress();

return true;
}
Expand Down Expand Up @@ -379,28 +376,19 @@ public void goToGoal()
return;
}

ArrayList<BlockPos> leaves = getLeavesInRange(pathFinder.getPath());
if(!leaves.isEmpty())
{
breakBlocks(leaves);
if(breakBlocks(getLeavesOnPath()))
return;
}

processor.process();
}

private ArrayList<BlockPos> getLeavesInRange(List<PathPos> path)
private ArrayList<BlockPos> getLeavesOnPath()
{
Vec3d eyesVec = RotationUtils.getEyesPos().subtract(0.5, 0.5, 0.5);
double rangeSq = Math.pow(range.getValue(), 2);

List<PathPos> path = pathFinder.getPath();
path = path.subList(processor.getIndex(), path.size());

return path.stream().flatMap(pos -> Stream.of(pos, pos.up()))
.distinct().filter(TreeBotUtils::isLeaves)
.filter(
pos -> eyesVec.squaredDistanceTo(Vec3d.of(pos)) <= rangeSq)
.filter(TreeBotUtils::hasLineOfSight)
.collect(Collectors.toCollection(ArrayList::new));
}

Expand Down Expand Up @@ -517,19 +505,18 @@ protected boolean checkDone()

private boolean hasAngle(PathPos pos)
{
double rangeSq = range.getValueSq();
ClientPlayerEntity player = WurstClient.MC.player;
Vec3d eyes = Vec3d.ofBottomCenter(pos).add(0,
player.getEyeHeight(player.getPose()), 0);

Vec3d eyesVec = eyes.subtract(0.5, 0.5, 0.5);
double rangeSq = Math.pow(range.getValue(), 2);

for(BlockPos log : tree.getLogs())
{
if(eyesVec.squaredDistanceTo(Vec3d.of(log)) > rangeSq)
continue;
BlockBreakingParams params =
BlockBreaker.getBlockBreakingParams(eyes, log);

if(TreeBotUtils.getLineOfSightSide(eyes, log) != null)
if(params != null && params.lineOfSight()
&& params.distanceSq() <= rangeSq)
return true;
}

Expand Down
65 changes: 0 additions & 65 deletions src/main/java/net/wurstclient/hacks/treebot/TreeBotUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.Vec3i;
import net.wurstclient.util.BlockUtils;
import net.wurstclient.util.RotationUtils;

public enum TreeBotUtils
{
Expand All @@ -40,65 +36,4 @@ public static boolean isLeaves(BlockPos pos)
{
return LEAVES_BLOCKS.contains(BlockUtils.getBlock(pos));
}

public static boolean hasLineOfSight(BlockPos pos)
{
return getLineOfSightSide(RotationUtils.getEyesPos(), pos) != null;
}

public static Direction getLineOfSightSide(Vec3d eyesPos, BlockPos pos)
{
Direction[] sides = Direction.values();

Vec3d relCenter = BlockUtils.getBoundingBox(pos)
.offset(-pos.getX(), -pos.getY(), -pos.getZ()).getCenter();
Vec3d center = Vec3d.of(pos).add(relCenter);

Vec3d[] hitVecs = new Vec3d[sides.length];
for(int i = 0; i < sides.length; i++)
{
Vec3i dirVec = sides[i].getVector();
Vec3d relHitVec = new Vec3d(relCenter.x * dirVec.getX(),
relCenter.y * dirVec.getY(), relCenter.z * dirVec.getZ());
hitVecs[i] = center.add(relHitVec);
}

double[] distancesSq = new double[sides.length];
boolean[] linesOfSight = new boolean[sides.length];

double distanceSqToCenter = eyesPos.squaredDistanceTo(center);
for(int i = 0; i < sides.length; i++)
{
distancesSq[i] = eyesPos.squaredDistanceTo(hitVecs[i]);

// no need to raytrace the rear sides,
// they can't possibly have line of sight
if(distancesSq[i] >= distanceSqToCenter)
continue;

linesOfSight[i] = BlockUtils.hasLineOfSight(eyesPos, hitVecs[i]);
}

Direction side = null;
for(int i = 0; i < sides.length; i++)
{
// require line of sight
if(!linesOfSight[i])
continue;

// start with the first side that has LOS
if(side == null)
{
side = sides[i];
continue;
}

// then pick the closest side
if(distancesSq[i] < distancesSq[side.ordinal()])
side = sides[i];
}

// will be null if no LOS was found
return side;
}
}
19 changes: 15 additions & 4 deletions src/main/java/net/wurstclient/util/BlockBreaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ public static boolean breakOneBlock(BlockPos pos)
* sight to that hit vector.
*/
public static BlockBreakingParams getBlockBreakingParams(BlockPos pos)
{
return getBlockBreakingParams(RotationUtils.getEyesPos(), pos);
}

/**
* Returns everything you need to break a block at the given position, such
* as which side to face, the exact hit vector to face that side, the
* squared distance to that hit vector, and whether or not there is line of
* sight to that hit vector.
*/
public static BlockBreakingParams getBlockBreakingParams(Vec3d eyes,
BlockPos pos)
{
Direction[] sides = Direction.values();

Expand All @@ -64,7 +76,6 @@ public static BlockBreakingParams getBlockBreakingParams(BlockPos pos)
if(shape.isEmpty())
return null;

Vec3d eyesPos = RotationUtils.getEyesPos();
Box box = shape.getBoundingBox();
Vec3d halfSize = new Vec3d(box.maxX - box.minX, box.maxY - box.minY,
box.maxZ - box.minZ).multiply(0.5);
Expand All @@ -79,20 +90,20 @@ public static BlockBreakingParams getBlockBreakingParams(BlockPos pos)
hitVecs[i] = center.add(relHitVec);
}

double distanceSqToCenter = eyesPos.squaredDistanceTo(center);
double distanceSqToCenter = eyes.squaredDistanceTo(center);
double[] distancesSq = new double[sides.length];
boolean[] linesOfSight = new boolean[sides.length];

for(int i = 0; i < sides.length; i++)
{
distancesSq[i] = eyesPos.squaredDistanceTo(hitVecs[i]);
distancesSq[i] = eyes.squaredDistanceTo(hitVecs[i]);

// no need to raytrace the rear sides,
// they can't possibly have line of sight
if(distancesSq[i] >= distanceSqToCenter)
continue;

linesOfSight[i] = BlockUtils.hasLineOfSight(eyesPos, hitVecs[i]);
linesOfSight[i] = BlockUtils.hasLineOfSight(eyes, hitVecs[i]);
}

Direction side = sides[0];
Expand Down

0 comments on commit 032111b

Please sign in to comment.