From 1c7017a0562a2f8a99e3d941a55fbb5695dfe753 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sat, 19 Oct 2024 12:55:59 +0200 Subject: [PATCH] Pass position/rotation values directly, fix relative position and rotation handling --- .../storage/EntityPositionHandler.java | 6 +- .../storage/EntityPositionStorage.java | 20 +- .../storage/PlayerPositionStorage.java | 22 +-- .../rewriter/EntityPacketRewriter1_13.java | 16 +- .../rewriter/EntityPacketRewriter1_21_2.java | 173 ++++++++++-------- .../v1_21_2to1_21/storage/PlayerStorage.java | 12 +- 6 files changed, 132 insertions(+), 117 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionHandler.java b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionHandler.java index 70741e84..0d53c9d6 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionHandler.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionHandler.java @@ -75,7 +75,11 @@ public class EntityPositionHandler { } } - positionStorage.setCoordinates(x, y, z, relative); + if (relative) { + positionStorage.addRelativePosition(x, y, z); + } else { + positionStorage.setPosition(x, y, z); + } } public EntityPositionStorage getStorage(UserConnection user, int entityId) { diff --git a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionStorage.java b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionStorage.java index 38df14f5..698715ed 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionStorage.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionStorage.java @@ -34,15 +34,15 @@ public abstract class EntityPositionStorage { return z; } - public void setCoordinates(double x, double y, double z, boolean relative) { - if (relative) { - this.x += x; - this.y += y; - this.z += z; - } else { - this.x = x; - this.y = y; - this.z = z; - } + public void setPosition(double x, double y, double z) { + this.x = x; + this.y = y; + this.z = z; + } + + public void addRelativePosition(double relX, double relY, double relZ) { + this.x += relX; + this.y += relY; + this.z += relZ; } } diff --git a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/PlayerPositionStorage.java b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/PlayerPositionStorage.java index 2e87dd61..7d70e3bb 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/PlayerPositionStorage.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/PlayerPositionStorage.java @@ -18,8 +18,6 @@ package com.viaversion.viabackwards.api.entities.storage; import com.viaversion.viaversion.api.connection.StorableObject; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.type.Types; public abstract class PlayerPositionStorage implements StorableObject { private double x; @@ -53,19 +51,15 @@ public abstract class PlayerPositionStorage implements StorableObject { this.z = z; } - public void setCoordinates(PacketWrapper wrapper, boolean relative) { - setCoordinates(wrapper.get(Types.DOUBLE, 0), wrapper.get(Types.DOUBLE, 1), wrapper.get(Types.DOUBLE, 2), relative); + public void setPosition(double x, double y, double z) { + this.x = x; + this.y = y; + this.z = z; } - public void setCoordinates(double x, double y, double z, boolean relative) { - if (relative) { - this.x += x; - this.y += y; - this.z += z; - } else { - this.x = x; - this.y = y; - this.z = z; - } + public void addRelativePosition(double relX, double relY, double relZ) { + this.x += relX; + this.y += relY; + this.z += relZ; } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_13to1_12_2/rewriter/EntityPacketRewriter1_13.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_13to1_12_2/rewriter/EntityPacketRewriter1_13.java index b103e23b..a1e8cf27 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_13to1_12_2/rewriter/EntityPacketRewriter1_13.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_13to1_12_2/rewriter/EntityPacketRewriter1_13.java @@ -31,10 +31,11 @@ import com.viaversion.viaversion.api.minecraft.ClientWorld; import com.viaversion.viaversion.api.minecraft.Particle; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_13; -import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.minecraft.entitydata.EntityData; import com.viaversion.viaversion.api.minecraft.entitydata.types.EntityDataTypes1_12; +import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.types.version.Types1_12; @@ -237,14 +238,11 @@ public class EntityPacketRewriter1_13 extends LegacyEntityRewriter wrapper.user().get(PlayerPositionStorage1_13.class).setCoordinates(wrapper, false)); - } + PacketHandler movementRemapper = wrapper -> { + final double x = wrapper.passthrough(Types.DOUBLE); + final double y = wrapper.passthrough(Types.DOUBLE); + final double z = wrapper.passthrough(Types.DOUBLE); + wrapper.user().get(PlayerPositionStorage1_13.class).setPosition(x, y, z); }; protocol.registerServerbound(ServerboundPackets1_12_1.MOVE_PLAYER_POS, movementRemapper); // Player Position protocol.registerServerbound(ServerboundPackets1_12_1.MOVE_PLAYER_POS_ROT, movementRemapper); // Player Position And Look (serverbound) diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/rewriter/EntityPacketRewriter1_21_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/rewriter/EntityPacketRewriter1_21_2.java index 811c9135..4e5df52e 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/rewriter/EntityPacketRewriter1_21_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/rewriter/EntityPacketRewriter1_21_2.java @@ -47,6 +47,15 @@ import java.util.List; public final class EntityPacketRewriter1_21_2 extends EntityRewriter { + private static final int REL_X = 0; + private static final int REL_Y = 1; + private static final int REL_Z = 2; + private static final int REL_Y_ROT = 3; + private static final int REL_X_ROT = 4; + private static final int REL_DELTA_X = 5; + private static final int REL_DELTA_Y = 6; + private static final int REL_DELTA_Z = 7; + private static final int REL_ROTATE_DELTA = 8; private boolean warned = ViaBackwards.getConfig().suppressEmulationWarnings(); public EntityPacketRewriter1_21_2(final Protocol1_21_2To1_21 protocol) { @@ -144,20 +153,22 @@ public final class EntityPacketRewriter1_21_2 extends EntityRewriter { - wrapper.passthrough(Types.FLOAT); // Y rot - wrapper.passthrough(Types.FLOAT); // X rot + final float yaw = wrapper.passthrough(Types.FLOAT); + final float pitch = wrapper.passthrough(Types.FLOAT); - final double yaw = Math.toRadians(wrapper.get(Types.FLOAT, 0)); - final double pitch = Math.toRadians(wrapper.get(Types.FLOAT, 1)); + final double yRadians = Math.toRadians(yaw); + final double xRadians = Math.toRadians(pitch); - final double factor = -Math.cos(-pitch); - final double deltaX = Math.sin(-yaw - (float) Math.PI) * factor; - final double deltaY = Math.sin(-pitch); - final double deltaZ = Math.cos(-yaw - (float) Math.PI) * factor; + final double factor = -Math.cos(-xRadians); + final double deltaX = Math.sin(-yRadians - (float) Math.PI) * factor; + final double deltaY = Math.sin(-xRadians); + final double deltaZ = Math.cos(-yRadians - (float) Math.PI) * factor; final PlayerStorage storage = wrapper.user().get(PlayerStorage.class); wrapper.write(Types.VAR_INT, 0); // From anchor @@ -167,46 +178,48 @@ public final class EntityPacketRewriter1_21_2 extends EntityRewriter { wrapper.passthrough(Types.VAR_INT); // Entity ID - wrapper.passthrough(Types.DOUBLE); // X - wrapper.passthrough(Types.DOUBLE); // Y - wrapper.passthrough(Types.DOUBLE); // Z + final double x = wrapper.passthrough(Types.DOUBLE); + final double y = wrapper.passthrough(Types.DOUBLE); + final double z = wrapper.passthrough(Types.DOUBLE); - double movementX = wrapper.read(Types.DOUBLE); - double movementY = wrapper.read(Types.DOUBLE); - double movementZ = wrapper.read(Types.DOUBLE); + final double movementX = wrapper.read(Types.DOUBLE); + final double movementY = wrapper.read(Types.DOUBLE); + final double movementZ = wrapper.read(Types.DOUBLE); - // Pack y and x rot - updateRotation(wrapper); + final float yaw = wrapper.read(Types.FLOAT); + final float pitch = wrapper.read(Types.FLOAT); + writePackedRotation(wrapper, yaw, pitch); - final int relativeArguments = wrapper.read(Types.VAR_INT); + final int relativeArguments = wrapper.read(Types.INT); // Send alongside separate entity motion wrapper.send(Protocol1_21_2To1_21.class); wrapper.cancel(); - handleRelativeArguments(wrapper, relativeArguments, movementX, movementY, movementZ); + + handleRelativeArguments(wrapper, x, y, z, yaw, pitch, relativeArguments, movementX, movementY, movementZ); }); protocol.registerClientbound(ClientboundPackets1_21_2.PLAYER_POSITION, wrapper -> { final int teleportId = wrapper.read(Types.VAR_INT); - wrapper.passthrough(Types.DOUBLE); // X - wrapper.passthrough(Types.DOUBLE); // Y - wrapper.passthrough(Types.DOUBLE); // Z + final double x = wrapper.passthrough(Types.DOUBLE); + final double y = wrapper.passthrough(Types.DOUBLE); + final double z = wrapper.passthrough(Types.DOUBLE); - double movementX = wrapper.read(Types.DOUBLE); - double movementY = wrapper.read(Types.DOUBLE); - double movementZ = wrapper.read(Types.DOUBLE); + final double movementX = wrapper.read(Types.DOUBLE); + final double movementY = wrapper.read(Types.DOUBLE); + final double movementZ = wrapper.read(Types.DOUBLE); - wrapper.passthrough(Types.FLOAT); // Y rot - wrapper.passthrough(Types.FLOAT); // X rot + final float yaw = wrapper.passthrough(Types.FLOAT); + final float pitch = wrapper.passthrough(Types.FLOAT); // Just keep the new values in there final int relativeArguments = wrapper.read(Types.INT); @@ -216,7 +229,8 @@ public final class EntityPacketRewriter1_21_2 extends EntityRewriter { @@ -280,45 +294,45 @@ public final class EntityPacketRewriter1_21_2 extends EntityRewriter { - wrapper.passthrough(Types.DOUBLE); // X - wrapper.passthrough(Types.DOUBLE); // Y - wrapper.passthrough(Types.DOUBLE); // Z + final double x = wrapper.passthrough(Types.DOUBLE); + final double y = wrapper.passthrough(Types.DOUBLE); + final double z = wrapper.passthrough(Types.DOUBLE); fixOnGround(wrapper); final PlayerStorage storage = wrapper.user().get(PlayerStorage.class); - storage.setPosition(wrapper); + storage.setPosition(x, y, z); }); protocol.registerServerbound(ServerboundPackets1_20_5.MOVE_PLAYER_POS_ROT, wrapper -> { - wrapper.passthrough(Types.DOUBLE); // X - wrapper.passthrough(Types.DOUBLE); // Y - wrapper.passthrough(Types.DOUBLE); // Z - wrapper.passthrough(Types.FLOAT); // Yaw - wrapper.passthrough(Types.FLOAT); // Pitch + final double x = wrapper.passthrough(Types.DOUBLE); + final double y = wrapper.passthrough(Types.DOUBLE); + final double z = wrapper.passthrough(Types.DOUBLE); + final float yaw = wrapper.passthrough(Types.FLOAT); + final float pitch = wrapper.passthrough(Types.FLOAT); fixOnGround(wrapper); final PlayerStorage storage = wrapper.user().get(PlayerStorage.class); - storage.setPosition(wrapper); - storage.setRotation(wrapper); + storage.setPosition(x, y, z); + storage.setRotation(yaw, pitch); }); protocol.registerServerbound(ServerboundPackets1_20_5.MOVE_PLAYER_ROT, wrapper -> { - wrapper.passthrough(Types.FLOAT); // Yaw - wrapper.passthrough(Types.FLOAT); // Pitch + final float yaw = wrapper.passthrough(Types.FLOAT); + final float pitch = wrapper.passthrough(Types.FLOAT); fixOnGround(wrapper); final PlayerStorage storage = wrapper.user().get(PlayerStorage.class); - storage.setRotation(wrapper); + storage.setRotation(yaw, pitch); }); protocol.registerServerbound(ServerboundPackets1_20_5.MOVE_PLAYER_STATUS_ONLY, this::fixOnGround); protocol.registerServerbound(ServerboundPackets1_20_5.MOVE_VEHICLE, wrapper -> { - wrapper.passthrough(Types.DOUBLE); // X - wrapper.passthrough(Types.DOUBLE); // Y - wrapper.passthrough(Types.DOUBLE); // Z - wrapper.passthrough(Types.FLOAT); // Yaw - wrapper.passthrough(Types.FLOAT); // Pitch + final double x = wrapper.passthrough(Types.DOUBLE); + final double y = wrapper.passthrough(Types.DOUBLE); + final double z = wrapper.passthrough(Types.DOUBLE); + final float yaw = wrapper.passthrough(Types.FLOAT); + final float pitch = wrapper.passthrough(Types.FLOAT); final PlayerStorage storage = wrapper.user().get(PlayerStorage.class); - storage.setPosition(wrapper); - storage.setRotation(wrapper); + storage.setPosition(x, y, z); + storage.setRotation(yaw, pitch); }); protocol.registerClientbound(ClientboundPackets1_21_2.PLAYER_INFO_UPDATE, wrapper -> { @@ -393,30 +407,39 @@ public final class EntityPacketRewriter1_21_2 extends EntityRewriter