From 7c5b5ef5c285434a61a743271b1d93f4d7e52dc4 Mon Sep 17 00:00:00 2001 From: Dries C Date: Wed, 23 Oct 2024 01:51:49 +0200 Subject: [PATCH] Add 1.21.30 BC support --- src/PlayerAuthInputPacket.php | 38 ++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/PlayerAuthInputPacket.php b/src/PlayerAuthInputPacket.php index c45a43e6..8d82d4e3 100644 --- a/src/PlayerAuthInputPacket.php +++ b/src/PlayerAuthInputPacket.php @@ -42,6 +42,7 @@ class PlayerAuthInputPacket extends DataPacket implements ServerboundPacket{ private int $inputMode; private int $playMode; private int $interactionMode; + private ?Vector3 $vrGazeDirection = null; private Vector2 $interactRotation; private int $tick; private Vector3 $delta; @@ -69,6 +70,7 @@ private static function internalCreate( int $inputMode, int $playMode, int $interactionMode, + ?Vector3 $vrGazeDirection, Vector2 $interactRotation, int $tick, Vector3 $delta, @@ -91,6 +93,7 @@ private static function internalCreate( $result->inputMode = $inputMode; $result->playMode = $playMode; $result->interactionMode = $interactionMode; + $result->vrGazeDirection = $vrGazeDirection; $result->interactRotation = $interactRotation; $result->tick = $tick; $result->delta = $delta; @@ -109,7 +112,8 @@ private static function internalCreate( * @param int $inputMode @see InputMode * @param int $playMode @see PlayMode * @param int $interactionMode @see InteractionMode - * @param PlayerBlockAction[]|null $blockActions Blocks that the client has interacted with + * @param Vector3|null $vrGazeDirection only used when PlayMode::VR + * @param PlayerBlockAction[]|null $blockActions Blocks that the client has interacted with */ public static function create( Vector3 $position, @@ -122,6 +126,7 @@ public static function create( int $inputMode, int $playMode, int $interactionMode, + ?Vector3 $vrGazeDirection, Vector2 $interactRotation, int $tick, Vector3 $delta, @@ -133,6 +138,11 @@ public static function create( float $analogMoveVecZ, Vector3 $cameraOrientation ) : self{ + if($playMode === PlayMode::VR and $vrGazeDirection === null){ + //yuck, can we get a properly written packet just once? ... + throw new \InvalidArgumentException("Gaze direction must be provided for VR play mode"); + } + $realInputFlags = $inputFlags & ~((1 << PlayerAuthInputFlags::PERFORM_ITEM_STACK_REQUEST) | (1 << PlayerAuthInputFlags::PERFORM_ITEM_INTERACTION) | (1 << PlayerAuthInputFlags::PERFORM_BLOCK_ACTIONS)); if($itemStackRequest !== null){ $realInputFlags |= 1 << PlayerAuthInputFlags::PERFORM_ITEM_STACK_REQUEST; @@ -155,6 +165,7 @@ public static function create( $inputMode, $playMode, $interactionMode, + $vrGazeDirection?->asVector3(), $interactRotation, $tick, $delta, @@ -220,6 +231,10 @@ public function getInteractionMode() : int{ return $this->interactionMode; } + public function getVrGazeDirection() : ?Vector3{ + return $this->vrGazeDirection; + } + public function getInteractRotation() : Vector2{ return $this->interactRotation; } public function getTick() : int{ @@ -268,7 +283,11 @@ protected function decodePayload(PacketSerializer $in) : void{ $this->inputMode = $in->getUnsignedVarInt(); $this->playMode = $in->getUnsignedVarInt(); $this->interactionMode = $in->getUnsignedVarInt(); - $this->interactRotation = $in->getVector2(); + if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_40){ + $this->interactRotation = $in->getVector2(); + }elseif($this->playMode === PlayMode::VR){ + $this->vrGazeDirection = $in->getVector3(); + } $this->tick = $in->getUnsignedVarLong(); $this->delta = $in->getVector3(); if($this->hasFlag(PlayerAuthInputFlags::PERFORM_ITEM_INTERACTION)){ @@ -294,7 +313,9 @@ protected function decodePayload(PacketSerializer $in) : void{ } $this->analogMoveVecX = $in->getLFloat(); $this->analogMoveVecZ = $in->getLFloat(); - $this->cameraOrientation = $in->getVector3(); + if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_40){ + $this->cameraOrientation = $in->getVector3(); + } } protected function encodePayload(PacketSerializer $out) : void{ @@ -314,7 +335,12 @@ protected function encodePayload(PacketSerializer $out) : void{ $out->putUnsignedVarInt($this->inputMode); $out->putUnsignedVarInt($this->playMode); $out->putUnsignedVarInt($this->interactionMode); - $out->putVector2($this->interactRotation); + if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_40){ + $out->putVector2($this->interactRotation); + }elseif($this->playMode === PlayMode::VR){ + assert($this->vrGazeDirection !== null); + $out->putVector3($this->vrGazeDirection); + } $out->putUnsignedVarLong($this->tick); $out->putVector3($this->delta); if($this->itemInteractionData !== null){ @@ -335,7 +361,9 @@ protected function encodePayload(PacketSerializer $out) : void{ } $out->putLFloat($this->analogMoveVecX); $out->putLFloat($this->analogMoveVecZ); - $out->putVector3($this->cameraOrientation); + if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_40){ + $out->putVector3($this->cameraOrientation); + } } public function handle(PacketHandlerInterface $handler) : bool{