From 8e99dddb91166fa20c00c8a9ecce649a07c0d940 Mon Sep 17 00:00:00 2001 From: EnZaXD Date: Mon, 17 Jun 2024 16:27:55 +0200 Subject: [PATCH] Write player rotation on USE_ITEM in 1.21->1.20.5 (#791) --- .../v1_21to1_20_5/Protocol1_21To1_20_5.java | 2 + .../rewriter/BlockItemPacketRewriter1_21.java | 7 +++- .../rewriter/EntityPacketRewriter1_21.java | 20 ++++++++++ .../storage/PlayerRotationStorage.java | 38 +++++++++++++++++++ 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/storage/PlayerRotationStorage.java diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/Protocol1_21To1_20_5.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/Protocol1_21To1_20_5.java index 846490f6..0916f90f 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/Protocol1_21To1_20_5.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/Protocol1_21To1_20_5.java @@ -25,6 +25,7 @@ import com.viaversion.viabackwards.protocol.v1_21to1_20_5.rewriter.BlockItemPack import com.viaversion.viabackwards.protocol.v1_21to1_20_5.rewriter.ComponentRewriter1_21; import com.viaversion.viabackwards.protocol.v1_21to1_20_5.rewriter.EntityPacketRewriter1_21; import com.viaversion.viabackwards.protocol.v1_21to1_20_5.storage.EnchantmentsPaintingsStorage; +import com.viaversion.viabackwards.protocol.v1_21to1_20_5.storage.PlayerRotationStorage; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.Holder; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_20_5; @@ -186,6 +187,7 @@ public final class Protocol1_21To1_20_5 extends BackwardsProtocol { wrapper.passthrough(Types.VAR_INT); // Hand wrapper.passthrough(Types.VAR_INT); // Sequence - wrapper.write(Types.FLOAT, 0f); // Y rotation - wrapper.write(Types.FLOAT, 0f); // X rotation + + final PlayerRotationStorage rotation = wrapper.user().get(PlayerRotationStorage.class); + wrapper.write(Types.FLOAT, rotation.yaw()); + wrapper.write(Types.FLOAT, rotation.pitch()); }); new RecipeRewriter1_20_3<>(protocol).register1_20_5(ClientboundPackets1_21.UPDATE_RECIPES); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/rewriter/EntityPacketRewriter1_21.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/rewriter/EntityPacketRewriter1_21.java index 0139af39..2cb8149f 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/rewriter/EntityPacketRewriter1_21.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/rewriter/EntityPacketRewriter1_21.java @@ -22,6 +22,7 @@ import com.viaversion.nbt.tag.Tag; import com.viaversion.viabackwards.api.rewriters.EntityRewriter; import com.viaversion.viabackwards.protocol.v1_21to1_20_5.Protocol1_21To1_20_5; import com.viaversion.viabackwards.protocol.v1_21to1_20_5.storage.EnchantmentsPaintingsStorage; +import com.viaversion.viabackwards.protocol.v1_21to1_20_5.storage.PlayerRotationStorage; import com.viaversion.viaversion.api.minecraft.Holder; import com.viaversion.viaversion.api.minecraft.PaintingVariant; import com.viaversion.viaversion.api.minecraft.RegistryEntry; @@ -29,10 +30,12 @@ import com.viaversion.viaversion.api.minecraft.WolfVariant; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_20_5; import com.viaversion.viaversion.api.minecraft.entitydata.EntityDataType; +import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.types.version.Types1_20_5; import com.viaversion.viaversion.api.type.types.version.Types1_21; +import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ServerboundPackets1_20_5; import com.viaversion.viaversion.protocols.v1_20_5to1_21.data.Paintings1_20_5; import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundConfigurationPackets1_21; import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPacket1_21; @@ -123,6 +126,23 @@ public final class EntityPacketRewriter1_21 extends EntityRewriter { + wrapper.passthrough(Types.DOUBLE); // X + wrapper.passthrough(Types.DOUBLE); // Y + wrapper.passthrough(Types.DOUBLE); // Z + + storePlayerRotation(wrapper); + }); + + protocol.registerServerbound(ServerboundPackets1_20_5.MOVE_PLAYER_ROT, this::storePlayerRotation); + } + + private void storePlayerRotation(final PacketWrapper wrapper) { + final float yaw = wrapper.passthrough(Types.FLOAT); + final float pitch = wrapper.passthrough(Types.FLOAT); + + wrapper.user().get(PlayerRotationStorage.class).setRotation(yaw, pitch); } private int[] paintingMappingsForEntries(final RegistryEntry[] entries) { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/storage/PlayerRotationStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/storage/PlayerRotationStorage.java new file mode 100644 index 00000000..fcd8b66e --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/storage/PlayerRotationStorage.java @@ -0,0 +1,38 @@ +/* + * 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_21to1_20_5.storage; + +import com.viaversion.viaversion.api.connection.StorableObject; + +public final class PlayerRotationStorage implements StorableObject { + + private float yaw, pitch; + + public void setRotation(final float yaw, final float pitch) { + this.yaw = yaw; + this.pitch = pitch; + } + + public float yaw() { + return yaw; + } + + public float pitch() { + return pitch; + } +}