From 545365786f729b3157f296f216793bcab1ca9e6c Mon Sep 17 00:00:00 2001 From: FlorianMichael Date: Fri, 18 Oct 2024 21:57:06 +0200 Subject: [PATCH] Handle PLAYER_ROTATION packet using PLAYER_LOOK_AT --- .../v1_21_2to1_21/Protocol1_21_2To1_21.java | 2 + .../rewriter/EntityPacketRewriter1_21_2.java | 53 +++++++++++++++++-- .../v1_21_2to1_21/storage/PlayerStorage.java | 42 +++++++++++++++ 3 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/storage/PlayerStorage.java diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/Protocol1_21_2To1_21.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/Protocol1_21_2To1_21.java index 857c984f..a6182aec 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/Protocol1_21_2To1_21.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/Protocol1_21_2To1_21.java @@ -26,6 +26,7 @@ import com.viaversion.viabackwards.protocol.v1_21_2to1_21.rewriter.ComponentRewr import com.viaversion.viabackwards.protocol.v1_21_2to1_21.rewriter.EntityPacketRewriter1_21_2; import com.viaversion.viabackwards.protocol.v1_21_2to1_21.storage.InventoryStateIdStorage; import com.viaversion.viabackwards.protocol.v1_21_2to1_21.storage.ItemTagStorage; +import com.viaversion.viabackwards.protocol.v1_21_2to1_21.storage.PlayerStorage; import com.viaversion.viabackwards.protocol.v1_21_2to1_21.storage.RecipeStorage; import com.viaversion.viabackwards.protocol.v1_21_2to1_21.storage.SneakingStorage; import com.viaversion.viaversion.api.connection.UserConnection; @@ -153,6 +154,7 @@ public final class Protocol1_21_2To1_21 extends BackwardsProtocol { - // TODO Send PLAYER_LOOK_AT via currently tracked location - wrapper.cancel(); + protocol.registerClientbound(ClientboundPackets1_21_2.PLAYER_ROTATION, ClientboundPackets1_21.PLAYER_LOOK_AT, wrapper -> { + wrapper.passthrough(Types.FLOAT); // Y rot + wrapper.passthrough(Types.FLOAT); // X rot + + final double yaw = Math.toRadians(wrapper.get(Types.FLOAT, 0)); + final double pitch = Math.toRadians(wrapper.get(Types.FLOAT, 1)); + + 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 PlayerStorage storage = wrapper.user().get(PlayerStorage.class); + wrapper.write(Types.VAR_INT, 0); // From anchor + wrapper.write(Types.DOUBLE, storage.x() + deltaX); // X + wrapper.write(Types.DOUBLE, storage.y() + deltaY); // Y + wrapper.write(Types.DOUBLE, storage.z() + deltaZ); // Z + wrapper.write(Types.BOOLEAN, false); // At entity + + final PacketWrapper entityMotionPacket = PacketWrapper.create(ServerboundPackets1_21_2.MOVE_PLAYER_ROT, wrapper.user()); + entityMotionPacket.write(Types.FLOAT, wrapper.get(Types.FLOAT, 0)); + entityMotionPacket.write(Types.FLOAT, wrapper.get(Types.FLOAT, 1)); + entityMotionPacket.write(Types.UNSIGNED_BYTE, (short) 0); // On ground and horizontal collision + entityMotionPacket.sendToServer(Protocol1_21_2To1_21.class); }); protocol.registerClientbound(ClientboundPackets1_21_2.TELEPORT_ENTITY, wrapper -> { @@ -176,6 +198,10 @@ public final class EntityPacketRewriter1_21_2 extends EntityRewriter { wrapper.passthrough(Types.DOUBLE); // X @@ -279,13 +308,31 @@ public final class EntityPacketRewriter1_21_2 extends EntityRewriter { wrapper.passthrough(Types.FLOAT); // Yaw wrapper.passthrough(Types.FLOAT); // Pitch fixOnGround(wrapper); + + final PlayerStorage storage = wrapper.user().get(PlayerStorage.class); + storage.setRotation(wrapper); }); 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 PlayerStorage storage = wrapper.user().get(PlayerStorage.class); + storage.setCoordinates(wrapper, false); + storage.setRotation(wrapper); + }); protocol.registerClientbound(ClientboundPackets1_21_2.PLAYER_INFO_UPDATE, wrapper -> { final BitSet actions = wrapper.passthrough(Types.PROFILE_ACTIONS_ENUM); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/storage/PlayerStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/storage/PlayerStorage.java new file mode 100644 index 00000000..1a71e0eb --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/storage/PlayerStorage.java @@ -0,0 +1,42 @@ +/* + * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viabackwards.protocol.v1_21_2to1_21.storage; + +import com.viaversion.viabackwards.api.entities.storage.PlayerPositionStorage; +import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; +import com.viaversion.viaversion.api.type.Types; + +public final class PlayerStorage extends PlayerPositionStorage { + + private float yaw; + private float pitch; + + public float yaw() { + return yaw; + } + + public float pitch() { + return pitch; + } + + public void setRotation(final PacketWrapper wrapper) { + this.yaw = wrapper.get(Types.FLOAT, 0); + this.pitch = wrapper.get(Types.FLOAT, 1); + } + +}