Skip to content

Commit

Permalink
add PositionBreak
Browse files Browse the repository at this point in the history
  • Loading branch information
ManInMyVan committed Dec 31, 2024
1 parent 2171a57 commit 5902fec
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ac.grim.grimac.checks.impl.breaking;

import ac.grim.grimac.checks.Check;
import ac.grim.grimac.checks.CheckData;
import ac.grim.grimac.checks.type.BlockBreakCheck;
import ac.grim.grimac.player.GrimPlayer;
import ac.grim.grimac.utils.anticheat.update.BlockBreak;
import ac.grim.grimac.utils.collisions.datatypes.SimpleCollisionBox;

@CheckData(name = "PositionBreak")
public class PositionBreak extends Check implements BlockBreakCheck {
public PositionBreak(GrimPlayer player) {
super(player);
}

@Override
public void onBlockBreak(BlockBreak blockBreak) {
SimpleCollisionBox combined = blockBreak.getCombinedBox();

final double[] possibleEyeHeights = player.getPossibleEyeHeights();
double minEyeHeight = Double.MAX_VALUE;
double maxEyeHeight = Double.MIN_VALUE;
for (double height : possibleEyeHeights) {
minEyeHeight = Math.min(minEyeHeight, height);
maxEyeHeight = Math.max(maxEyeHeight, height);
}

SimpleCollisionBox eyePositions = new SimpleCollisionBox(player.x, player.y + minEyeHeight, player.z, player.x, player.y + maxEyeHeight, player.z);
if (!player.packetStateData.didLastMovementIncludePosition || player.canSkipTicks()) {
eyePositions.expand(player.getMovementThreshold());
}

// If the player is inside a block, then they can ray trace through the block and hit the other side of the block
if (eyePositions.isIntersected(combined)) {
return;
}

// So now we have the player's possible eye positions
// So then look at the face that the player has clicked
boolean flag = switch (blockBreak.face) {
case NORTH -> eyePositions.minZ > combined.minZ; // Z- face
case SOUTH -> eyePositions.maxZ < combined.maxZ; // Z+ face
case EAST -> eyePositions.maxX < combined.maxX; // X+ face
case WEST -> eyePositions.minX > combined.minX; // X- face
case UP -> eyePositions.maxY < combined.maxY; // Y+ face
case DOWN -> eyePositions.minY > combined.minY; // Y- face
default -> false;
};

if (flag && flagAndAlert("action=" + blockBreak.action + ", face=" + blockBreak.face) && shouldModifyPackets()) {
blockBreak.cancel();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import ac.grim.grimac.player.GrimPlayer;
import ac.grim.grimac.utils.anticheat.update.BlockPlace;
import ac.grim.grimac.utils.collisions.datatypes.SimpleCollisionBox;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.world.states.type.StateTypes;


@CheckData(name = "PositionPlace")
public class PositionPlace extends BlockPlaceCheck {

Expand Down Expand Up @@ -58,10 +56,8 @@ public void onBlockPlace(final BlockPlace place) {
default -> false;
};

if (flag) {
if (flagAndAlert() && shouldModifyPackets() && shouldCancel()) {
place.resync();
}
if (flag && flagAndAlert() && shouldModifyPackets() && shouldCancel()) {
place.resync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ public void onPacketReceive(PacketReceiveEvent event) {
final DiggingAction action = packet.getAction();

if (action == DiggingAction.START_DIGGING || action == DiggingAction.FINISHED_DIGGING || action == DiggingAction.CANCELLED_DIGGING) {
final BlockBreak blockBreak = new BlockBreak(packet.getBlockPosition(), packet.getBlockFace(), packet.getBlockFaceId(), action, player.compensatedWorld.getWrappedBlockStateAt(packet.getBlockPosition()));
final BlockBreak blockBreak = new BlockBreak(player, packet.getBlockPosition(), packet.getBlockFace(), packet.getBlockFaceId(), action, player.compensatedWorld.getWrappedBlockStateAt(packet.getBlockPosition()));

player.checkManager.onBlockBreak(blockBreak);

Expand Down
1 change: 1 addition & 0 deletions src/main/java/ac/grim/grimac/manager/CheckManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public CheckManager(GrimPlayer player) {
.put(NoSwingBreak.class, new NoSwingBreak(player))
.put(FarBreak.class, new FarBreak(player))
.put(InvalidBreak.class, new InvalidBreak(player))
.put(PositionBreak.class, new PositionBreak(player))
.build();

allChecks = new ImmutableClassToInstanceMap.Builder<AbstractCheck>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package ac.grim.grimac.utils.anticheat.update;

import ac.grim.grimac.player.GrimPlayer;
import ac.grim.grimac.utils.collisions.HitboxData;
import ac.grim.grimac.utils.collisions.datatypes.CollisionBox;
import ac.grim.grimac.utils.collisions.datatypes.SimpleCollisionBox;
import com.github.retrooper.packetevents.protocol.player.DiggingAction;
import com.github.retrooper.packetevents.protocol.world.BlockFace;
import com.github.retrooper.packetevents.protocol.world.states.WrappedBlockState;
import com.github.retrooper.packetevents.util.Vector3i;
import lombok.Getter;

public class BlockBreak {
import java.util.ArrayList;
import java.util.List;

public final class BlockBreak {
private final GrimPlayer player;
public final Vector3i position;
public final BlockFace face;
public final int faceId;
public final DiggingAction action;
public final WrappedBlockState block;
@Getter
private boolean cancelled;

public final WrappedBlockState block;

public BlockBreak(Vector3i position, BlockFace face, int faceId, DiggingAction action, WrappedBlockState block) {
public BlockBreak(GrimPlayer player, Vector3i position, BlockFace face, int faceId, DiggingAction action, WrappedBlockState block) {
this.player = player;
this.position = position;
this.face = face;
this.faceId = faceId;
Expand All @@ -27,4 +35,24 @@ public BlockBreak(Vector3i position, BlockFace face, int faceId, DiggingAction a
public void cancel() {
this.cancelled = true;
}

public SimpleCollisionBox getCombinedBox() {
CollisionBox placedOn = HitboxData.getBlockHitbox(player, player.getInventory().getHeldItem().getType().getPlacedType(), player.getClientVersion(), block, position.x, position.y, position.z);

List<SimpleCollisionBox> boxes = new ArrayList<>();
placedOn.downCast(boxes);

SimpleCollisionBox combined = new SimpleCollisionBox(position.x, position.y, position.z);
for (SimpleCollisionBox box : boxes) {
double minX = Math.max(box.minX, combined.minX);
double minY = Math.max(box.minY, combined.minY);
double minZ = Math.max(box.minZ, combined.minZ);
double maxX = Math.min(box.maxX, combined.maxX);
double maxY = Math.min(box.maxY, combined.maxY);
double maxZ = Math.min(box.maxZ, combined.maxZ);
combined = new SimpleCollisionBox(minX, minY, minZ, maxX, maxY, maxZ);
}

return combined;
}
}

0 comments on commit 5902fec

Please sign in to comment.