Skip to content

Commit

Permalink
Work around loss of precision when unpacking entity velocity
Browse files Browse the repository at this point in the history
  • Loading branch information
Beaness and booky10 committed Dec 2, 2024
1 parent a0fc7f6 commit 9819db6
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
import com.github.retrooper.packetevents.wrapper.PacketWrapper;

public class WrapperPlayServerEntityVelocity extends PacketWrapper<WrapperPlayServerEntityVelocity> {

// with larger short values there is a loss of precision and
// re-encoding packets may cause the velocity to change
//
// as vanilla just casts to an int instead of properly rounding,
// packetevents has to add a small number to the calculated velocity
// to work around the loss of precision
private static final double PRECISION_LOSS_FIX = 1e-11d;

private int entityID;
private Vector3d velocity;

Expand All @@ -48,7 +57,11 @@ public void read() {
double velX = (double) readShort() / 8000.0;
double velY = (double) readShort() / 8000.0;
double velZ = (double) readShort() / 8000.0;
velocity = new Vector3d(velX, velY, velZ);
velocity = new Vector3d(
velX + Math.copySign(PRECISION_LOSS_FIX, velX),
velY + Math.copySign(PRECISION_LOSS_FIX, velY),
velZ + Math.copySign(PRECISION_LOSS_FIX, velZ)
);
}

@Override
Expand Down

0 comments on commit 9819db6

Please sign in to comment.