Skip to content

Commit

Permalink
BadPacketsX, PacketOrder, and LineOfSightPlace improvements
Browse files Browse the repository at this point in the history
* Refactor and improve numerous PacketOrder checks
* Fix some BadPacketsX falses
* Fix some LineOfSightPlace falses
  • Loading branch information
Axionize committed Dec 18, 2024
1 parent b4820b5 commit b880f51
Show file tree
Hide file tree
Showing 42 changed files with 365 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public void process(final RotationUpdate rotationUpdate) {

if (this.xRotMode.size() > SIGNIFICANT_SAMPLES_THRESHOLD) {
Pair<Double, Integer> modeX = this.xRotMode.getMode();
if (modeX.getSecond() > SIGNIFICANT_SAMPLES_THRESHOLD) {
this.modeX = modeX.getFirst();
if (modeX.second() > SIGNIFICANT_SAMPLES_THRESHOLD) {
this.modeX = modeX.first();
this.sensitivityX = convertToSensitivity(this.modeX);
}
}
if (this.yRotMode.size() > SIGNIFICANT_SAMPLES_THRESHOLD) {
Pair<Double, Integer> modeY = this.yRotMode.getMode();
if (modeY.getSecond() > SIGNIFICANT_SAMPLES_THRESHOLD) {
this.modeY = modeY.getFirst();
if (modeY.second() > SIGNIFICANT_SAMPLES_THRESHOLD) {
this.modeY = modeY.first();
this.sensitivityY = convertToSensitivity(this.modeY);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void onPacketReceive(PacketReceiveEvent event) {
boolean hasID = false;

for (Pair<Long, Long> iterator : keepaliveMap) {
if (iterator.getFirst() == id) {
if (iterator.first() == id) {
hasID = true;
break;
}
Expand All @@ -55,7 +55,7 @@ public void onPacketReceive(PacketReceiveEvent event) {
do {
data = keepaliveMap.poll();
if (data == null) break;
} while (data.getFirst() != id);
} while (data.first() != id);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
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.change.BlockModification;
import com.github.retrooper.packetevents.protocol.item.type.ItemTypes;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.player.DiggingAction;
import com.github.retrooper.packetevents.protocol.world.states.type.StateType;
import com.github.retrooper.packetevents.protocol.world.states.type.StateTypes;
import com.github.retrooper.packetevents.util.Vector3i;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@CheckData(name = "BadPacketsX")
public class BadPacketsX extends Check implements BlockBreakCheck {
public BadPacketsX(GrimPlayer player) {
Expand Down Expand Up @@ -45,26 +50,44 @@ public void onBlockBreak(BlockBreak blockBreak) {
&& lastBlockType.getBlastResistance() == 0.0F
&& block == StateTypes.WATER
) return;
lastTick = newTick;
lastBreakLoc = blockBreak.position;
lastBlockType = block;

// the block does not have a hitbox
boolean invalid = (block == StateTypes.LIGHT && !(player.getInventory().getHeldItem().is(ItemTypes.LIGHT) || player.getInventory().getOffHand().is(ItemTypes.LIGHT)))
|| block.isAir()
|| block == StateTypes.WATER
|| block == StateTypes.LAVA
|| block == StateTypes.BUBBLE_COLUMN
|| block == StateTypes.MOVING_PISTON
|| block == StateTypes.FIRE && noFireHitbox
// or the client claims to have broken an unbreakable block
|| block.getHardness() == -1.0f && blockBreak.action == DiggingAction.FINISHED_DIGGING;
// prevents rare false on rapidly breaking short grass
List<StateType> previousBlockStates = player.blockHistory.modificationQueue.stream()
.filter((blockModification) -> blockModification.getLocation().equals(blockBreak.position)
&& newTick - blockModification.getTick() < 2
&& (blockModification.getCause() == BlockModification.Cause.START_DIGGING || blockModification.getCause() == BlockModification.Cause.HANDLE_NETTY_SYNC_TRANSACTION))
.flatMap(mod -> Stream.of(mod.getOldBlockContents().getType()))
.collect(Collectors.toList());

previousBlockStates.add(0, block);

if (invalid && flagAndAlert("block=" + block.getName() + ", type=" + blockBreak.action) && shouldModifyPackets()) {
boolean invalid = false;
for (StateType possibleBlockState : previousBlockStates) {
// the block does not have a hitbox
invalid = (possibleBlockState == StateTypes.LIGHT && !(player.getInventory().getHeldItem().is(ItemTypes.LIGHT) || player.getInventory().getOffHand().is(ItemTypes.LIGHT)))
|| possibleBlockState.isAir()
|| possibleBlockState == StateTypes.WATER
|| possibleBlockState == StateTypes.LAVA
|| possibleBlockState == StateTypes.BUBBLE_COLUMN
|| possibleBlockState == StateTypes.MOVING_PISTON
|| possibleBlockState == StateTypes.FIRE && noFireHitbox
// or the client claims to have broken an unbreakable block
|| possibleBlockState.getHardness() == -1.0f && blockBreak.action == DiggingAction.FINISHED_DIGGING;
if (!invalid) {
break;
}
}

if (invalid && flagAndAlert("block=" + block.getName() + ", type=" + blockBreak.action)) {
didLastFlag = true;
blockBreak.cancel();
if (shouldModifyPackets()) {
blockBreak.cancel();
}
} else {
didLastFlag = false;
}
lastTick = newTick;
lastBreakLoc = blockBreak.position;
lastBlockType = block;
}
}
12 changes: 6 additions & 6 deletions src/main/java/ac/grim/grimac/checks/impl/combat/Reach.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ private void tickBetterReachCheckWithAngle(boolean isFlying) {
if (reachEntity != null) {
Pair<Class<? extends PacketCheck>, String> result = checkReach(reachEntity, attack.getValue(), false);
if (result != null) {
PacketCheck check = player.checkManager.getPacketCheck(result.getFirst());
PacketCheck check = player.checkManager.getPacketCheck(result.first());
if (reachEntity.getType() == EntityTypes.PLAYER) {
((Check)check).flagAndAlert(result.getSecond());
((Check)check).flagAndAlert(result.second());
} else {
((Check) check).flagAndAlert(result.getSecond() + "type=" + reachEntity.getType().getName().getKey());
((Check) check).flagAndAlert(result.second() + "type=" + reachEntity.getType().getName().getKey());
}
}
}
Expand Down Expand Up @@ -242,7 +242,7 @@ private Pair<Class<? extends PacketCheck>, String> checkReach(PacketEntity reach
Vector eyePos = new Vector(from.getX(), from.getY() + eye, from.getZ());
Vector endReachPos = eyePos.clone().add(new Vector(lookVec.getX() * distance, lookVec.getY() * distance, lookVec.getZ() * distance));

Vector intercept = ReachUtils.calculateIntercept(targetBox, eyePos, endReachPos).getFirst();
Vector intercept = ReachUtils.calculateIntercept(targetBox, eyePos, endReachPos).first();

if (ReachUtils.isVecInside(targetBox, eyePos)) {
minDistance = 0;
Expand Down Expand Up @@ -270,9 +270,9 @@ private Pair<Class<? extends PacketCheck>, String> checkReach(PacketEntity reach
// - This filters out when the ray trace hits the target entity without having to do an expensive .equals()
// - You may have to adjust the epsilon if you increase the reach threshold, especially by a lot
// ...but there is literally no reason you would ever want to increase it, only decrease, so that doesn't matter.
if (hitResult != null && (minDistance * minDistance) - hitResult.getFirst() > ENTITY_HITBOX_REACH_EPSILON) { // returned double is distanceSq
if (hitResult != null && (minDistance * minDistance) - hitResult.first() > ENTITY_HITBOX_REACH_EPSILON) { // returned double is distanceSq
minDistance = Double.MIN_VALUE;
foundHitData = hitResult.getSecond();
foundHitData = hitResult.second();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import ac.grim.grimac.utils.anticheat.update.PredictionComplete;
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientClickWindow;
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientClickWindow.WindowClickType;

Expand All @@ -26,7 +25,7 @@ public void onPacketReceive(PacketReceiveEvent event) {

if ((clickType == WindowClickType.PICKUP || clickType == WindowClickType.PICKUP_ALL) && player.packetOrderProcessor.isQuickMoveClicking()
|| clickType == WindowClickType.QUICK_MOVE && player.packetOrderProcessor.isPickUpClicking()) {
if (player.getClientVersion().isOlderThanOrEquals(ClientVersion.V_1_8)) {
if (!player.canSkipTicks()) {
if (flagAndAlert() && shouldModifyPackets()) {
event.setCancelled(true);
player.onPacketCancel();
Expand All @@ -40,8 +39,7 @@ public void onPacketReceive(PacketReceiveEvent event) {

@Override
public void onPredictionComplete(PredictionComplete predictionComplete) {
// we don't need to check pre-1.9 players here (no tick skipping)
if (player.getClientVersion().isOlderThanOrEquals(ClientVersion.V_1_8)) return;
if (!player.canSkipTicks()) return;

if (player.isTickingReliablyFor(3) && !player.uncertaintyHandler.lastVehicleSwitch.hasOccurredSince(0)) {
for (; invalid >= 1; invalid--) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ac.grim.grimac.checks.impl.packetorder;

import ac.grim.grimac.checks.Check;
import ac.grim.grimac.checks.CheckData;
import ac.grim.grimac.checks.type.PacketCheck;
import ac.grim.grimac.player.GrimPlayer;

@CheckData(name = "PacketOrderD")
public class PacketOrderD extends Check implements PacketCheck {
public PacketOrderD(GrimPlayer player) {
super(player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import ac.grim.grimac.utils.anticheat.update.PredictionComplete;
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;

@CheckData(name = "PacketOrderE", experimental = true)
public class PacketOrderE extends Check implements PostPredictionCheck {
Expand All @@ -31,7 +30,7 @@ public void onPacketReceive(PacketReceiveEvent event) {
|| player.packetOrderProcessor.isStartingToGlide()
|| player.packetOrderProcessor.isJumpingWithMount()
) {
if (player.getClientVersion().isNewerThan(ClientVersion.V_1_8) || flagAndAlert()) {
if (player.canSkipTicks() || flagAndAlert()) {
invalid++;

if (player.packetOrderProcessor.isUsing()) {
Expand All @@ -44,7 +43,7 @@ public void onPacketReceive(PacketReceiveEvent event) {

@Override
public void onPredictionComplete(PredictionComplete predictionComplete) {
if (player.getClientVersion().isOlderThanOrEquals(ClientVersion.V_1_8)) {
if (!player.canSkipTicks()) {
if (setback) {
setback = false;
setbackIfAboveSetbackVL();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import ac.grim.grimac.utils.anticheat.update.PredictionComplete;
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.player.DiggingAction;
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientClientStatus;
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientPlayerDigging;
Expand All @@ -30,7 +29,7 @@ public void onPacketReceive(PacketReceiveEvent event) {
|| (event.getPacketType() == PacketType.Play.Client.CLIENT_STATUS
&& new WrapperPlayClientClientStatus(event).getAction() == WrapperPlayClientClientStatus.Action.OPEN_INVENTORY_ACHIEVEMENT)
) if (player.packetOrderProcessor.isSprinting() || player.packetOrderProcessor.isSneaking()) {
if (player.getClientVersion().isOlderThanOrEquals(ClientVersion.V_1_8)) {
if (!player.canSkipTicks()) {
if (flagAndAlert() && shouldModifyPackets()) {
if (event.getPacketType() == PacketType.Play.Client.PLAYER_DIGGING
&& new WrapperPlayClientPlayerDigging(event).getAction() == DiggingAction.RELEASE_USE_ITEM
Expand All @@ -47,8 +46,7 @@ && new WrapperPlayClientPlayerDigging(event).getAction() == DiggingAction.RELEAS

@Override
public void onPredictionComplete(PredictionComplete predictionComplete) {
// we don't need to check pre-1.9 players here (no tick skipping)
if (player.getClientVersion().isOlderThanOrEquals(ClientVersion.V_1_8)) return;
if (!player.canSkipTicks()) return;

if (player.isTickingReliablyFor(3) && !player.uncertaintyHandler.lastVehicleSwitch.hasOccurredSince(0)) {
for (; invalid >= 1; invalid--) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import ac.grim.grimac.utils.anticheat.update.PredictionComplete;
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.player.DiggingAction;
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientClientStatus;
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientPlayerDigging;
Expand Down Expand Up @@ -41,7 +40,7 @@ && new WrapperPlayClientClientStatus(event).getAction() == WrapperPlayClientClie
|| player.packetOrderProcessor.isPicking()
|| player.packetOrderProcessor.isDigging()
) {
if (player.getClientVersion().isOlderThanOrEquals(ClientVersion.V_1_8)) {
if (!player.canSkipTicks()) {
if (flagAndAlert() && shouldModifyPackets()) {
event.setCancelled(true);
player.onPacketCancel();
Expand All @@ -55,8 +54,7 @@ && new WrapperPlayClientClientStatus(event).getAction() == WrapperPlayClientClie

@Override
public void onPredictionComplete(PredictionComplete predictionComplete) {
// we don't need to check pre-1.9 players here (no tick skipping)
if (player.getClientVersion().isOlderThanOrEquals(ClientVersion.V_1_8)) return;
if (!player.canSkipTicks()) return;

if (player.isTickingReliablyFor(3) && !player.uncertaintyHandler.lastVehicleSwitch.hasOccurredSince(0)) {
for (; invalid >= 1; invalid--) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,32 @@ public void onPacketReceive(PacketReceiveEvent event) {
switch (new WrapperPlayClientEntityAction(event).getAction()) {
case START_SPRINTING:
case STOP_SPRINTING:
if (player.packetOrderProcessor.isSneaking()) {
if (player.getClientVersion().isOlderThanOrEquals(ClientVersion.V_1_8)) {
if (player.getClientVersion().isOlderThan(ClientVersion.V_1_21_2) && player.packetOrderProcessor.isSneaking()) {
if (!player.canSkipTicks()) {
flagAndAlert();
} else {
invalid++;
}
}
break;

case START_SNEAKING:
case STOP_SNEAKING:
if (player.getClientVersion().isNewerThanOrEquals(ClientVersion.V_1_21_2) && player.packetOrderProcessor.isSprinting()) {
if (!player.canSkipTicks()) {
flagAndAlert();
} else {
invalid++;
}
}
break;
}
}
}

@Override
public void onPredictionComplete(PredictionComplete predictionComplete) {
// we don't need to check pre-1.9 players here (no tick skipping)
if (player.getClientVersion().isOlderThanOrEquals(ClientVersion.V_1_8)) return;
if (!player.canSkipTicks()) return;

if (player.isTickingReliablyFor(3) && !player.uncertaintyHandler.lastVehicleSwitch.hasOccurredSince(0)) {
for (; invalid >= 1; invalid--) {
Expand Down
Loading

0 comments on commit b880f51

Please sign in to comment.