Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix velocity precision errors #1074

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@

public class WrapperPlayServerEntityVelocity extends PacketWrapper<WrapperPlayServerEntityVelocity> {
private int entityID;
private Vector3d velocity;
private int rawX;
private int rawY;
private int rawZ;
private Vector3d cachedVelocity;

public WrapperPlayServerEntityVelocity(PacketSendEvent event) {
super(event);
Expand All @@ -35,7 +38,8 @@ public WrapperPlayServerEntityVelocity(PacketSendEvent event) {
public WrapperPlayServerEntityVelocity(int entityID, Vector3d velocity) {
super(PacketType.Play.Server.ENTITY_VELOCITY);
this.entityID = entityID;
this.velocity = velocity;
this.cachedVelocity = velocity;
this.updateRawValues();
}

@Override
Expand All @@ -45,10 +49,15 @@ public void read() {
} else {
entityID = readVarInt();
}
double velX = (double) readShort() / 8000.0;
double velY = (double) readShort() / 8000.0;
double velZ = (double) readShort() / 8000.0;
velocity = new Vector3d(velX, velY, velZ);

this.rawX = readShort();
this.rawY = readShort();
this.rawZ = readShort();

double velX = (double) this.rawX / 8000.0;
double velY = (double) this.rawY / 8000.0;
double velZ = (double) this.rawZ / 8000.0;
this.cachedVelocity = new Vector3d(velX, velY, velZ);
}

@Override
Expand All @@ -58,15 +67,24 @@ public void write() {
} else {
writeVarInt(entityID);
}
writeShort((int) (velocity.x * 8000.0));
writeShort((int) (velocity.y * 8000.0));
writeShort((int) (velocity.z * 8000.0));

writeShort(rawX);
writeShort(rawY);
writeShort(rawZ);
}

@Override
public void copy(WrapperPlayServerEntityVelocity wrapper) {
entityID = wrapper.entityID;
velocity = wrapper.velocity;
rawX = wrapper.rawX;
rawY = wrapper.rawY;
rawZ = wrapper.rawZ;
}

private void updateRawValues() {
this.rawX = (int) (cachedVelocity.x * 8000.0);
this.rawY = (int) (cachedVelocity.y * 8000.0);
this.rawZ = (int) (cachedVelocity.z * 8000.0);
}

public int getEntityId() {
Expand All @@ -78,10 +96,11 @@ public void setEntityId(int entityID) {
}

public Vector3d getVelocity() {
return velocity;
return cachedVelocity;
}

public void setVelocity(Vector3d velocity) {
this.velocity = velocity;
this.cachedVelocity = velocity;
this.updateRawValues();
}
}