Skip to content

Commit

Permalink
recoded BadPacketsR
Browse files Browse the repository at this point in the history
  • Loading branch information
AoElite committed Nov 22, 2023
1 parent 3457c2f commit 9ffac94
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 24 deletions.
8 changes: 8 additions & 0 deletions src/main/java/ac/grim/grimac/checks/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import ac.grim.grimac.GrimAPI;
import ac.grim.grimac.events.FlagEvent;
import ac.grim.grimac.player.GrimPlayer;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon;
import github.scarsz.configuralize.DynamicConfig;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -114,5 +116,11 @@ public boolean setbackIfAboveSetbackVL() {
public String formatOffset(double offset) {
return offset > 0.001 ? String.format("%.5f", offset) : String.format("%.2E", offset);
}

public boolean isTransaction(PacketTypeCommon packetType) {
return packetType == PacketType.Play.Client.PONG ||
packetType == PacketType.Play.Client.WINDOW_CONFIRMATION;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,43 @@
import ac.grim.grimac.player.GrimPlayer;
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.packettype.PacketType.Play.Client;
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientEntityAction;
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientEntityAction.Action;

@CheckData(name = "BadPacketsR")
public class BadPacketsR extends Check implements PacketCheck {
public BadPacketsR(final GrimPlayer player) {
super(player);
}

private long lastTransaction = 0;
private int positions = 0;
private long clock = 0;
private long lastTransTime;
private int oldTransId = 0;

@Override
public void onPacketReceive(PacketReceiveEvent event) {
if (event.getPacketType() == PacketType.Play.Client.WINDOW_CONFIRMATION ||
event.getPacketType() == PacketType.Play.Client.PONG) {
final long time = System.currentTimeMillis();
final long diff = time - lastTransaction;

if (diff > 1000) {
if (positions == 0 && lastTransaction != 0) {
flagAndAlert("time=" + diff + " positions=" + positions);
player.compensatedWorld.removeInvalidPistonLikeStuff();
public void onPacketReceive(final PacketReceiveEvent event) {
if (isTransaction(event.getPacketType()) && player.packetStateData.lastTransactionPacketWasValid) {
long ms = (player.getPlayerClockAtLeast() - clock) / 1000000L;
long diff = (System.currentTimeMillis() - lastTransTime);
if (diff > 1500 && ms > 1500) {
if (positions == 0 && clock != 0) {
flagAndAlert("time=" + ms + "ms, " + "lst=" + diff + "ms, positions=" + positions);
} else {
reward();
}
player.compensatedWorld.removeInvalidPistonLikeStuff(oldTransId);
positions = 0;
lastTransaction = time;
clock = player.getPlayerClockAtLeast();
lastTransTime = System.currentTimeMillis();
oldTransId = player.lastTransactionSent.get();
}
}
//
if (event.getPacketType() == PacketType.Play.Client.PLAYER_POSITION_AND_ROTATION ||
event.getPacketType() == PacketType.Play.Client.PLAYER_POSITION
|| event.getPacketType() == Client.STEER_VEHICLE) {
if ((event.getPacketType() == PacketType.Play.Client.PLAYER_POSITION_AND_ROTATION ||
event.getPacketType() == PacketType.Play.Client.PLAYER_POSITION) && !player.compensatedEntities.getSelf().inVehicle()) {
positions++;
} else if (event.getPacketType() == PacketType.Play.Client.STEER_VEHICLE && player.compensatedEntities.getSelf().inVehicle()) {
positions++;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public void onPacketReceive(PacketReceiveEvent event) {
GrimPlayer player = GrimAPI.INSTANCE.getPlayerDataManager().getPlayer(event.getUser());
if (player == null) return;

player.packetStateData.lastTransactionPacketWasValid = false;
// Check if we sent this packet before cancelling it
if (player.addTransactionResponse(id)) {
player.packetStateData.lastTransactionPacketWasValid = true;
event.setCancelled(true);
}
}
Expand All @@ -51,7 +53,9 @@ public void onPacketReceive(PacketReceiveEvent event) {
GrimPlayer player = GrimAPI.INSTANCE.getPlayerDataManager().getPlayer(event.getUser());
if (player == null) return;
short shortID = ((short) id);
player.packetStateData.lastTransactionPacketWasValid = false;
if (player.addTransactionResponse(shortID)) {
player.packetStateData.lastTransactionPacketWasValid = true;
// Not needed for vanilla as vanilla ignores this packet, needed for packet limiters
event.setCancelled(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class PacketStateData {
public boolean packetPlayerOnGround = false;
public boolean lastPacketWasTeleport = false;
public boolean lastPacketWasOnePointSeventeenDuplicate = false;
public boolean lastTransactionPacketWasValid = false;
public int lastSlotSelected;
public InteractionHand eatingHand = InteractionHand.MAIN_HAND;
public long lastRiptide = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ac/grim/grimac/utils/data/ShulkerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class ShulkerData {
public final int lastTransactionSent;
private final boolean isClosing;
public final boolean isClosing;

// Keep track of one of these two things, so we can remove this later
public PacketEntity entity = null;
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/ac/grim/grimac/utils/latency/CompensatedWorld.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ac.grim.grimac.utils.latency;

import ac.grim.grimac.GrimAPI;
import ac.grim.grimac.manager.init.start.ViaBackwardsManager;
import ac.grim.grimac.player.GrimPlayer;
import ac.grim.grimac.utils.chunks.Column;
import ac.grim.grimac.utils.collisions.CollisionData;
Expand Down Expand Up @@ -396,13 +395,18 @@ public void tickPlayerInPistonPushingArea() {
player.uncertaintyHandler.pistonY.add(modY);
player.uncertaintyHandler.pistonZ.add(modZ);

removeInvalidPistonLikeStuff();
removeInvalidPistonLikeStuff(0);
}

public void removeInvalidPistonLikeStuff() {
public void removeInvalidPistonLikeStuff(int transactionId) {
// Tick the pistons and remove them if they can no longer exist
activePistons.removeIf(PistonData::tickIfGuaranteedFinished);
openShulkerBoxes.removeIf(ShulkerData::tickIfGuaranteedFinished);
if (transactionId != 0) {
activePistons.removeIf(data -> data.lastTransactionSent < transactionId);
openShulkerBoxes.removeIf(data -> data.isClosing && data.lastTransactionSent < transactionId);
} else {
activePistons.removeIf(PistonData::tickIfGuaranteedFinished);
openShulkerBoxes.removeIf(ShulkerData::tickIfGuaranteedFinished);
}
// Remove if a shulker is not in this block position anymore
openShulkerBoxes.removeIf(box -> {
if (box.blockPos != null) { // Block is no longer valid
Expand Down

0 comments on commit 9ffac94

Please sign in to comment.