Fix accuracy over network

This commit is contained in:
ewoudje 2023-01-12 21:59:36 +01:00
parent 324729a68e
commit ecab13d0c4
3 changed files with 28 additions and 21 deletions

View file

@ -12,31 +12,32 @@ import net.minecraftforge.network.NetworkEvent.Context;
public class ContraptionStallPacket extends SimplePacketBase { public class ContraptionStallPacket extends SimplePacketBase {
int entityID; int entityID;
float x; double x;
float y; double y;
float z; double z;
float angle; float angle;
public ContraptionStallPacket(int entityID, double posX, double posY, double posZ, float angle) { public ContraptionStallPacket(int entityID, double posX, double posY, double posZ, float angle) {
this.entityID = entityID; this.entityID = entityID;
this.x = (float) posX; this.x = posX;
this.y = (float) posY; this.y = posY;
this.z = (float) posZ; this.z = posZ;
this.angle = angle; this.angle = angle;
} }
public ContraptionStallPacket(FriendlyByteBuf buffer) { public ContraptionStallPacket(FriendlyByteBuf buffer) {
entityID = buffer.readInt(); entityID = buffer.readInt();
x = buffer.readFloat(); x = buffer.readDouble();
y = buffer.readFloat(); y = buffer.readDouble();
z = buffer.readFloat(); z = buffer.readDouble();
angle = buffer.readFloat(); angle = buffer.readFloat();
} }
@Override @Override
public void write(FriendlyByteBuf buffer) { public void write(FriendlyByteBuf buffer) {
buffer.writeInt(entityID); buffer.writeInt(entityID);
writeAll(buffer, x, y, z, angle); writeAll(buffer, x, y, z);
buffer.writeFloat(angle);
} }
@Override @Override
@ -46,9 +47,9 @@ public class ContraptionStallPacket extends SimplePacketBase {
context.get().setPacketHandled(true); context.get().setPacketHandled(true);
} }
private void writeAll(FriendlyByteBuf buffer, float... floats) { private void writeAll(FriendlyByteBuf buffer, double... doubles) {
for (float f : floats) for (double d : doubles)
buffer.writeFloat(f); buffer.writeDouble(d);
} }
} }

View file

@ -26,16 +26,16 @@ public class LimbSwingUpdatePacket extends SimplePacketBase {
public LimbSwingUpdatePacket(FriendlyByteBuf buffer) { public LimbSwingUpdatePacket(FriendlyByteBuf buffer) {
entityId = buffer.readInt(); entityId = buffer.readInt();
position = new Vec3(buffer.readFloat(), buffer.readFloat(), buffer.readFloat()); position = new Vec3(buffer.readDouble(), buffer.readDouble(), buffer.readDouble());
limbSwing = buffer.readFloat(); limbSwing = buffer.readFloat();
} }
@Override @Override
public void write(FriendlyByteBuf buffer) { public void write(FriendlyByteBuf buffer) {
buffer.writeInt(entityId); buffer.writeInt(entityId);
buffer.writeFloat((float) position.x); buffer.writeDouble(position.x);
buffer.writeFloat((float) position.y); buffer.writeDouble(position.y);
buffer.writeFloat((float) position.z); buffer.writeDouble(position.z);
buffer.writeFloat(limbSwing); buffer.writeFloat(limbSwing);
} }

View file

@ -46,7 +46,7 @@ public class TrackNodeLocation extends Vec3i {
} }
public Vec3 getLocation() { public Vec3 getLocation() {
return new Vec3(getX() / 2f, getY() / 2f, getZ() / 2f); return new Vec3(getX() / 2.0, getY() / 2.0, getZ() / 2.0);
} }
public ResourceKey<Level> getDimension() { public ResourceKey<Level> getDimension() {
@ -83,12 +83,18 @@ public class TrackNodeLocation extends Vec3i {
} }
public void send(FriendlyByteBuf buffer, DimensionPalette dimensions) { public void send(FriendlyByteBuf buffer, DimensionPalette dimensions) {
buffer.writeBlockPos(new BlockPos(this)); buffer.writeVarInt(this.getX());
buffer.writeShort(this.getY());
buffer.writeVarInt(this.getZ());
buffer.writeVarInt(dimensions.encode(dimension)); buffer.writeVarInt(dimensions.encode(dimension));
} }
public static TrackNodeLocation receive(FriendlyByteBuf buffer, DimensionPalette dimensions) { public static TrackNodeLocation receive(FriendlyByteBuf buffer, DimensionPalette dimensions) {
TrackNodeLocation location = fromPackedPos(buffer.readBlockPos()); TrackNodeLocation location = fromPackedPos(new BlockPos(
buffer.readVarInt(),
buffer.readShort(),
buffer.readVarInt()
));
location.dimension = dimensions.decode(buffer.readVarInt()); location.dimension = dimensions.decode(buffer.readVarInt());
return location; return location;
} }