PocketMine-MP 5.15.1 git-08c6e63aac45a450fe3b437616ee8aa2500e04e2
PlayerAuthInputPacket.php
1<?php
2
3/*
4 * This file is part of BedrockProtocol.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
6 *
7 * BedrockProtocol is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13declare(strict_types=1);
14
15namespace pocketmine\network\mcpe\protocol;
16
29use function assert;
30use function count;
31
33 public const NETWORK_ID = ProtocolInfo::PLAYER_AUTH_INPUT_PACKET;
34
35 private Vector3 $position;
36 private float $pitch;
37 private float $yaw;
38 private float $headYaw;
39 private float $moveVecX;
40 private float $moveVecZ;
41 private int $inputFlags;
42 private int $inputMode;
43 private int $playMode;
44 private int $interactionMode;
45 private ?Vector3 $vrGazeDirection = null;
46 private int $tick;
47 private Vector3 $delta;
48 private ?ItemInteractionData $itemInteractionData = null;
49 private ?ItemStackRequest $itemStackRequest = null;
51 private ?array $blockActions = null;
52 private ?PlayerAuthInputVehicleInfo $vehicleInfo = null;
53 private float $analogMoveVecX;
54 private float $analogMoveVecZ;
55
64 public static function create(
65 Vector3 $position,
66 float $pitch,
67 float $yaw,
68 float $headYaw,
69 float $moveVecX,
70 float $moveVecZ,
71 int $inputFlags,
72 int $inputMode,
73 int $playMode,
74 int $interactionMode,
75 ?Vector3 $vrGazeDirection,
76 int $tick,
77 Vector3 $delta,
78 ?ItemInteractionData $itemInteractionData,
79 ?ItemStackRequest $itemStackRequest,
80 ?array $blockActions,
81 ?PlayerAuthInputVehicleInfo $vehicleInfo,
82 float $analogMoveVecX,
83 float $analogMoveVecZ
84 ) : self{
85 if($playMode === PlayMode::VR and $vrGazeDirection === null){
86 //yuck, can we get a properly written packet just once? ...
87 throw new \InvalidArgumentException("Gaze direction must be provided for VR play mode");
88 }
89 $result = new self;
90 $result->position = $position->asVector3();
91 $result->pitch = $pitch;
92 $result->yaw = $yaw;
93 $result->headYaw = $headYaw;
94 $result->moveVecX = $moveVecX;
95 $result->moveVecZ = $moveVecZ;
96
97 $result->inputFlags = $inputFlags & ~((1 << PlayerAuthInputFlags::PERFORM_ITEM_STACK_REQUEST) | (1 << PlayerAuthInputFlags::PERFORM_ITEM_INTERACTION) | (1 << PlayerAuthInputFlags::PERFORM_BLOCK_ACTIONS));
98 if($itemStackRequest !== null){
99 $result->inputFlags |= 1 << PlayerAuthInputFlags::PERFORM_ITEM_STACK_REQUEST;
100 }
101 if($itemInteractionData !== null){
102 $result->inputFlags |= 1 << PlayerAuthInputFlags::PERFORM_ITEM_INTERACTION;
103 }
104 if($blockActions !== null){
105 $result->inputFlags |= 1 << PlayerAuthInputFlags::PERFORM_BLOCK_ACTIONS;
106 }
107 if($vehicleInfo !== null){
108 $result->inputFlags |= 1 << PlayerAuthInputFlags::IN_CLIENT_PREDICTED_VEHICLE;
109 }
110
111 $result->inputMode = $inputMode;
112 $result->playMode = $playMode;
113 $result->interactionMode = $interactionMode;
114 if($vrGazeDirection !== null){
115 $result->vrGazeDirection = $vrGazeDirection->asVector3();
116 }
117 $result->tick = $tick;
118 $result->delta = $delta;
119 $result->itemInteractionData = $itemInteractionData;
120 $result->itemStackRequest = $itemStackRequest;
121 $result->blockActions = $blockActions;
122 $result->vehicleInfo = $vehicleInfo;
123 $result->analogMoveVecX = $analogMoveVecX;
124 $result->analogMoveVecZ = $analogMoveVecZ;
125 return $result;
126 }
127
128 public function getPosition() : Vector3{
129 return $this->position;
130 }
131
132 public function getPitch() : float{
133 return $this->pitch;
134 }
135
136 public function getYaw() : float{
137 return $this->yaw;
138 }
139
140 public function getHeadYaw() : float{
141 return $this->headYaw;
142 }
143
144 public function getMoveVecX() : float{
145 return $this->moveVecX;
146 }
147
148 public function getMoveVecZ() : float{
149 return $this->moveVecZ;
150 }
151
155 public function getInputFlags() : int{
156 return $this->inputFlags;
157 }
158
162 public function getInputMode() : int{
163 return $this->inputMode;
164 }
165
169 public function getPlayMode() : int{
170 return $this->playMode;
171 }
172
176 public function getInteractionMode() : int{
177 return $this->interactionMode;
178 }
179
180 public function getVrGazeDirection() : ?Vector3{
181 return $this->vrGazeDirection;
182 }
183
184 public function getTick() : int{
185 return $this->tick;
186 }
187
188 public function getDelta() : Vector3{
189 return $this->delta;
190 }
191
192 public function getItemInteractionData() : ?ItemInteractionData{
193 return $this->itemInteractionData;
194 }
195
196 public function getItemStackRequest() : ?ItemStackRequest{
197 return $this->itemStackRequest;
198 }
199
203 public function getBlockActions() : ?array{
204 return $this->blockActions;
205 }
206
207 public function getVehicleInfo() : ?PlayerAuthInputVehicleInfo{ return $this->vehicleInfo; }
208
209 public function getAnalogMoveVecX() : float{ return $this->analogMoveVecX; }
210
211 public function getAnalogMoveVecZ() : float{ return $this->analogMoveVecZ; }
212
213 public function hasFlag(int $flag) : bool{
214 return ($this->inputFlags & (1 << $flag)) !== 0;
215 }
216
217 protected function decodePayload(PacketSerializer $in) : void{
218 $this->pitch = $in->getLFloat();
219 $this->yaw = $in->getLFloat();
220 $this->position = $in->getVector3();
221 $this->moveVecX = $in->getLFloat();
222 $this->moveVecZ = $in->getLFloat();
223 $this->headYaw = $in->getLFloat();
224 $this->inputFlags = $in->getUnsignedVarLong();
225 $this->inputMode = $in->getUnsignedVarInt();
226 $this->playMode = $in->getUnsignedVarInt();
227 $this->interactionMode = $in->getUnsignedVarInt();
228 if($this->playMode === PlayMode::VR){
229 $this->vrGazeDirection = $in->getVector3();
230 }
231 $this->tick = $in->getUnsignedVarLong();
232 $this->delta = $in->getVector3();
233 if($this->hasFlag(PlayerAuthInputFlags::PERFORM_ITEM_INTERACTION)){
234 $this->itemInteractionData = ItemInteractionData::read($in);
235 }
236 if($this->hasFlag(PlayerAuthInputFlags::PERFORM_ITEM_STACK_REQUEST)){
237 $this->itemStackRequest = ItemStackRequest::read($in);
238 }
239 if($this->hasFlag(PlayerAuthInputFlags::PERFORM_BLOCK_ACTIONS)){
240 $this->blockActions = [];
241 $max = $in->getVarInt();
242 for($i = 0; $i < $max; ++$i){
243 $actionType = $in->getVarInt();
244 $this->blockActions[] = match(true){
245 PlayerBlockActionWithBlockInfo::isValidActionType($actionType) => PlayerBlockActionWithBlockInfo::read($in, $actionType),
246 $actionType === PlayerAction::STOP_BREAK => new PlayerBlockActionStopBreak(),
247 default => throw new PacketDecodeException("Unexpected block action type $actionType")
248 };
249 }
250 }
251 if($this->hasFlag(PlayerAuthInputFlags::IN_CLIENT_PREDICTED_VEHICLE)){
252 $this->vehicleInfo = PlayerAuthInputVehicleInfo::read($in);
253 }
254 $this->analogMoveVecX = $in->getLFloat();
255 $this->analogMoveVecZ = $in->getLFloat();
256 }
257
258 protected function encodePayload(PacketSerializer $out) : void{
259 $out->putLFloat($this->pitch);
260 $out->putLFloat($this->yaw);
261 $out->putVector3($this->position);
262 $out->putLFloat($this->moveVecX);
263 $out->putLFloat($this->moveVecZ);
264 $out->putLFloat($this->headYaw);
265 $out->putUnsignedVarLong($this->inputFlags);
266 $out->putUnsignedVarInt($this->inputMode);
267 $out->putUnsignedVarInt($this->playMode);
268 $out->putUnsignedVarInt($this->interactionMode);
269 if($this->playMode === PlayMode::VR){
270 assert($this->vrGazeDirection !== null);
271 $out->putVector3($this->vrGazeDirection);
272 }
273 $out->putUnsignedVarLong($this->tick);
274 $out->putVector3($this->delta);
275 if($this->itemInteractionData !== null){
276 $this->itemInteractionData->write($out);
277 }
278 if($this->itemStackRequest !== null){
279 $this->itemStackRequest->write($out);
280 }
281 if($this->blockActions !== null){
282 $out->putVarInt(count($this->blockActions));
283 foreach($this->blockActions as $blockAction){
284 $out->putVarInt($blockAction->getActionType());
285 $blockAction->write($out);
286 }
287 }
288 if($this->vehicleInfo !== null){
289 $this->vehicleInfo->write($out);
290 }
291 $out->putLFloat($this->analogMoveVecX);
292 $out->putLFloat($this->analogMoveVecZ);
293 }
294
295 public function handle(PacketHandlerInterface $handler) : bool{
296 return $handler->handlePlayerAuthInput($this);
297 }
298}
static create(Vector3 $position, float $pitch, float $yaw, float $headYaw, float $moveVecX, float $moveVecZ, int $inputFlags, int $inputMode, int $playMode, int $interactionMode, ?Vector3 $vrGazeDirection, int $tick, Vector3 $delta, ?ItemInteractionData $itemInteractionData, ?ItemStackRequest $itemStackRequest, ?array $blockActions, ?PlayerAuthInputVehicleInfo $vehicleInfo, float $analogMoveVecX, float $analogMoveVecZ)