diff --git a/src/main/java/ac/grim/grimac/checks/Check.java b/src/main/java/ac/grim/grimac/checks/Check.java index 0877b47d19..bb7a4e9d7f 100644 --- a/src/main/java/ac/grim/grimac/checks/Check.java +++ b/src/main/java/ac/grim/grimac/checks/Check.java @@ -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; @@ -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; + } + } diff --git a/src/main/java/ac/grim/grimac/checks/impl/badpackets/BadPacketsR.java b/src/main/java/ac/grim/grimac/checks/impl/badpackets/BadPacketsR.java index 6b0d4c1486..85f693e102 100644 --- a/src/main/java/ac/grim/grimac/checks/impl/badpackets/BadPacketsR.java +++ b/src/main/java/ac/grim/grimac/checks/impl/badpackets/BadPacketsR.java @@ -6,9 +6,6 @@ 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 { @@ -16,32 +13,36 @@ 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++; } } + } diff --git a/src/main/java/ac/grim/grimac/events/packets/PacketPingListener.java b/src/main/java/ac/grim/grimac/events/packets/PacketPingListener.java index 1b684e4825..dc87c3944d 100644 --- a/src/main/java/ac/grim/grimac/events/packets/PacketPingListener.java +++ b/src/main/java/ac/grim/grimac/events/packets/PacketPingListener.java @@ -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); } } @@ -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); } diff --git a/src/main/java/ac/grim/grimac/utils/data/PacketStateData.java b/src/main/java/ac/grim/grimac/utils/data/PacketStateData.java index d1c3cd08cb..163e2a3da5 100644 --- a/src/main/java/ac/grim/grimac/utils/data/PacketStateData.java +++ b/src/main/java/ac/grim/grimac/utils/data/PacketStateData.java @@ -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; diff --git a/src/main/java/ac/grim/grimac/utils/data/ShulkerData.java b/src/main/java/ac/grim/grimac/utils/data/ShulkerData.java index 432b676cd0..d7abb9b21a 100644 --- a/src/main/java/ac/grim/grimac/utils/data/ShulkerData.java +++ b/src/main/java/ac/grim/grimac/utils/data/ShulkerData.java @@ -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; diff --git a/src/main/java/ac/grim/grimac/utils/latency/CompensatedWorld.java b/src/main/java/ac/grim/grimac/utils/latency/CompensatedWorld.java index d38b7b25a6..2e3d6d31a1 100644 --- a/src/main/java/ac/grim/grimac/utils/latency/CompensatedWorld.java +++ b/src/main/java/ac/grim/grimac/utils/latency/CompensatedWorld.java @@ -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; @@ -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