From f08a412db57f65795e3a23ea9923c268ef55df4c Mon Sep 17 00:00:00 2001 From: EnZaXD Date: Thu, 31 Oct 2024 10:22:57 +0100 Subject: [PATCH] Drop idle packets where only horizontal collision changed in 1.21->1.21.2 (#4220) --- .../v1_21to1_21_2/Protocol1_21To1_21_2.java | 2 + .../rewriter/EntityPacketRewriter1_21_2.java | 27 +++++++++--- .../storage/GroundFlagTracker.java | 43 +++++++++++++++++++ 3 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/storage/GroundFlagTracker.java diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/Protocol1_21To1_21_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/Protocol1_21To1_21_2.java index 89508784d..bd2296787 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/Protocol1_21To1_21_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/Protocol1_21To1_21_2.java @@ -49,6 +49,7 @@ import com.viaversion.viaversion.protocols.v1_21to1_21_2.rewriter.ParticleRewrit import com.viaversion.viaversion.protocols.v1_21to1_21_2.storage.BundleStateTracker; import com.viaversion.viaversion.protocols.v1_21to1_21_2.storage.ChunkLoadTracker; import com.viaversion.viaversion.protocols.v1_21to1_21_2.storage.EntityTracker1_21_2; +import com.viaversion.viaversion.protocols.v1_21to1_21_2.storage.GroundFlagTracker; import com.viaversion.viaversion.protocols.v1_21to1_21_2.storage.PlayerPositionStorage; import com.viaversion.viaversion.rewriter.AttributeRewriter; import com.viaversion.viaversion.rewriter.SoundRewriter; @@ -234,6 +235,7 @@ public final class Protocol1_21To1_21_2 extends AbstractProtocol { final double x = wrapper.passthrough(Types.DOUBLE); // X @@ -322,7 +324,7 @@ public final class EntityPacketRewriter1_21_2 extends EntityRewriter { wrapper.passthrough(Types.FLOAT); // Yaw wrapper.passthrough(Types.FLOAT); // Pitch - readOnGround(wrapper); + handleOnGround(wrapper); + }); + protocol.registerServerbound(ServerboundPackets1_21_2.MOVE_PLAYER_STATUS_ONLY, wrapper -> { + final GroundFlagTracker tracker = wrapper.user().get(GroundFlagTracker.class); + final boolean prevOnGround = tracker.onGround(); + final boolean prevHorizontalCollision = tracker.horizontalCollision(); + + handleOnGround(wrapper); + if (prevOnGround == tracker.onGround() && prevHorizontalCollision != tracker.horizontalCollision()) { + // Newer clients will send idle packets even though the on ground state didn't change, ignore them + wrapper.cancel(); + } }); - protocol.registerServerbound(ServerboundPackets1_21_2.MOVE_PLAYER_STATUS_ONLY, this::readOnGround); protocol.registerServerbound(ServerboundPackets1_21_2.ACCEPT_TELEPORTATION, wrapper -> { final PlayerPositionStorage playerPositionStorage = wrapper.user().get(PlayerPositionStorage.class); if (playerPositionStorage.checkHasPlayerPosition()) { @@ -370,9 +382,12 @@ public final class EntityPacketRewriter1_21_2 extends EntityRewriter. + */ +package com.viaversion.viaversion.protocols.v1_21to1_21_2.storage; + +import com.viaversion.viaversion.api.connection.StorableObject; + +public final class GroundFlagTracker implements StorableObject { + + private boolean onGround; + private boolean horizontalCollision; + + public boolean onGround() { + return this.onGround; + } + + public boolean setOnGround(boolean onGround) { + return this.onGround = onGround; + } + + public boolean horizontalCollision() { + return this.horizontalCollision; + } + + public void setHorizontalCollision(boolean horizontalCollision) { + this.horizontalCollision = horizontalCollision; + } + +}