PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
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
31use function count;
32
34 public const NETWORK_ID = ProtocolInfo::PLAYER_AUTH_INPUT_PACKET;
35
36 private Vector3 $position;
37 private float $pitch;
38 private float $yaw;
39 private float $headYaw;
40 private float $moveVecX;
41 private float $moveVecZ;
42 private BitSet $inputFlags;
43 private int $inputMode;
44 private int $playMode;
45 private int $interactionMode;
46 private Vector2 $interactRotation;
47 private int $tick;
48 private Vector3 $delta;
49 private ?ItemInteractionData $itemInteractionData = null;
50 private ?ItemStackRequest $itemStackRequest = null;
52 private ?array $blockActions = null;
53 private ?PlayerAuthInputVehicleInfo $vehicleInfo = null;
54 private float $analogMoveVecX;
55 private float $analogMoveVecZ;
56 private Vector3 $cameraOrientation;
57 private Vector2 $rawMove;
58
63 private static function internalCreate(
64 Vector3 $position,
65 float $pitch,
66 float $yaw,
67 float $headYaw,
68 float $moveVecX,
69 float $moveVecZ,
70 BitSet $inputFlags,
71 int $inputMode,
72 int $playMode,
73 int $interactionMode,
74 Vector2 $interactRotation,
75 int $tick,
76 Vector3 $delta,
77 ?ItemInteractionData $itemInteractionData,
78 ?ItemStackRequest $itemStackRequest,
79 ?array $blockActions,
80 ?PlayerAuthInputVehicleInfo $vehicleInfo,
81 float $analogMoveVecX,
82 float $analogMoveVecZ,
83 Vector3 $cameraOrientation,
84 Vector2 $rawMove,
85 ) : self{
86 $result = new self;
87 $result->position = $position;
88 $result->pitch = $pitch;
89 $result->yaw = $yaw;
90 $result->headYaw = $headYaw;
91 $result->moveVecX = $moveVecX;
92 $result->moveVecZ = $moveVecZ;
93 $result->inputFlags = $inputFlags;
94 $result->inputMode = $inputMode;
95 $result->playMode = $playMode;
96 $result->interactionMode = $interactionMode;
97 $result->interactRotation = $interactRotation;
98 $result->tick = $tick;
99 $result->delta = $delta;
100 $result->itemInteractionData = $itemInteractionData;
101 $result->itemStackRequest = $itemStackRequest;
102 $result->blockActions = $blockActions;
103 $result->vehicleInfo = $vehicleInfo;
104 $result->analogMoveVecX = $analogMoveVecX;
105 $result->analogMoveVecZ = $analogMoveVecZ;
106 $result->cameraOrientation = $cameraOrientation;
107 $result->rawMove = $rawMove;
108 return $result;
109 }
110
118 public static function create(
119 Vector3 $position,
120 float $pitch,
121 float $yaw,
122 float $headYaw,
123 float $moveVecX,
124 float $moveVecZ,
125 BitSet $inputFlags,
126 int $inputMode,
127 int $playMode,
128 int $interactionMode,
129 Vector2 $interactRotation,
130 int $tick,
131 Vector3 $delta,
132 ?ItemInteractionData $itemInteractionData,
133 ?ItemStackRequest $itemStackRequest,
134 ?array $blockActions,
135 ?PlayerAuthInputVehicleInfo $vehicleInfo,
136 float $analogMoveVecX,
137 float $analogMoveVecZ,
138 Vector3 $cameraOrientation,
139 Vector2 $rawMove
140 ) : self{
141 if($inputFlags->getLength() !== 65){
142 throw new \InvalidArgumentException("Input flags must be 65 bits long");
143 }
144
145 $inputFlags->set(PlayerAuthInputFlags::PERFORM_ITEM_STACK_REQUEST, $itemStackRequest !== null);
146 $inputFlags->set(PlayerAuthInputFlags::PERFORM_ITEM_INTERACTION, $itemInteractionData !== null);
147 $inputFlags->set(PlayerAuthInputFlags::PERFORM_BLOCK_ACTIONS, $blockActions !== null);
148 $inputFlags->set(PlayerAuthInputFlags::IN_CLIENT_PREDICTED_VEHICLE, $vehicleInfo !== null);
149
150 return self::internalCreate(
151 $position,
152 $pitch,
153 $yaw,
154 $headYaw,
155 $moveVecX,
156 $moveVecZ,
157 $inputFlags,
158 $inputMode,
159 $playMode,
160 $interactionMode,
161 $interactRotation,
162 $tick,
163 $delta,
164 $itemInteractionData,
165 $itemStackRequest,
166 $blockActions,
167 $vehicleInfo,
168 $analogMoveVecX,
169 $analogMoveVecZ,
170 $cameraOrientation,
171 $rawMove
172 );
173 }
174
175 public function getPosition() : Vector3{
176 return $this->position;
177 }
178
179 public function getPitch() : float{
180 return $this->pitch;
181 }
182
183 public function getYaw() : float{
184 return $this->yaw;
185 }
186
187 public function getHeadYaw() : float{
188 return $this->headYaw;
189 }
190
191 public function getMoveVecX() : float{
192 return $this->moveVecX;
193 }
194
195 public function getMoveVecZ() : float{
196 return $this->moveVecZ;
197 }
198
202 public function getInputFlags() : BitSet{
203 return $this->inputFlags;
204 }
205
209 public function getInputMode() : int{
210 return $this->inputMode;
211 }
212
216 public function getPlayMode() : int{
217 return $this->playMode;
218 }
219
223 public function getInteractionMode() : int{
224 return $this->interactionMode;
225 }
226
227 public function getInteractRotation() : Vector2{ return $this->interactRotation; }
228
229 public function getTick() : int{
230 return $this->tick;
231 }
232
233 public function getDelta() : Vector3{
234 return $this->delta;
235 }
236
237 public function getItemInteractionData() : ?ItemInteractionData{
238 return $this->itemInteractionData;
239 }
240
241 public function getItemStackRequest() : ?ItemStackRequest{
242 return $this->itemStackRequest;
243 }
244
248 public function getBlockActions() : ?array{
249 return $this->blockActions;
250 }
251
252 public function getVehicleInfo() : ?PlayerAuthInputVehicleInfo{ return $this->vehicleInfo; }
253
254 public function getAnalogMoveVecX() : float{ return $this->analogMoveVecX; }
255
256 public function getAnalogMoveVecZ() : float{ return $this->analogMoveVecZ; }
257
258 public function getCameraOrientation() : Vector3{ return $this->cameraOrientation; }
259
260 public function getRawMove() : Vector2{ return $this->rawMove; }
261
262 protected function decodePayload(PacketSerializer $in) : void{
263 $this->pitch = $in->getLFloat();
264 $this->yaw = $in->getLFloat();
265 $this->position = $in->getVector3();
266 $this->moveVecX = $in->getLFloat();
267 $this->moveVecZ = $in->getLFloat();
268 $this->headYaw = $in->getLFloat();
269 $this->inputFlags = BitSet::read($in, 65);
270 $this->inputMode = $in->getUnsignedVarInt();
271 $this->playMode = $in->getUnsignedVarInt();
272 $this->interactionMode = $in->getUnsignedVarInt();
273 $this->interactRotation = $in->getVector2();
274 $this->tick = $in->getUnsignedVarLong();
275 $this->delta = $in->getVector3();
276 if($this->inputFlags->get(PlayerAuthInputFlags::PERFORM_ITEM_INTERACTION)){
277 $this->itemInteractionData = ItemInteractionData::read($in);
278 }
279 if($this->inputFlags->get(PlayerAuthInputFlags::PERFORM_ITEM_STACK_REQUEST)){
280 $this->itemStackRequest = ItemStackRequest::read($in);
281 }
282 if($this->inputFlags->get(PlayerAuthInputFlags::PERFORM_BLOCK_ACTIONS)){
283 $this->blockActions = [];
284 $max = $in->getVarInt();
285 for($i = 0; $i < $max; ++$i){
286 $actionType = $in->getVarInt();
287 $this->blockActions[] = match(true){
288 PlayerBlockActionWithBlockInfo::isValidActionType($actionType) => PlayerBlockActionWithBlockInfo::read($in, $actionType),
289 $actionType === PlayerAction::STOP_BREAK => new PlayerBlockActionStopBreak(),
290 default => throw new PacketDecodeException("Unexpected block action type $actionType")
291 };
292 }
293 }
294 if($this->inputFlags->get(PlayerAuthInputFlags::IN_CLIENT_PREDICTED_VEHICLE)){
295 $this->vehicleInfo = PlayerAuthInputVehicleInfo::read($in);
296 }
297 $this->analogMoveVecX = $in->getLFloat();
298 $this->analogMoveVecZ = $in->getLFloat();
299 $this->cameraOrientation = $in->getVector3();
300 $this->rawMove = $in->getVector2();
301 }
302
303 protected function encodePayload(PacketSerializer $out) : void{
304 $out->putLFloat($this->pitch);
305 $out->putLFloat($this->yaw);
306 $out->putVector3($this->position);
307 $out->putLFloat($this->moveVecX);
308 $out->putLFloat($this->moveVecZ);
309 $out->putLFloat($this->headYaw);
310 $this->inputFlags->write($out);
311 $out->putUnsignedVarInt($this->inputMode);
312 $out->putUnsignedVarInt($this->playMode);
313 $out->putUnsignedVarInt($this->interactionMode);
314 $out->putVector2($this->interactRotation);
315 $out->putUnsignedVarLong($this->tick);
316 $out->putVector3($this->delta);
317 if($this->itemInteractionData !== null){
318 $this->itemInteractionData->write($out);
319 }
320 if($this->itemStackRequest !== null){
321 $this->itemStackRequest->write($out);
322 }
323 if($this->blockActions !== null){
324 $out->putVarInt(count($this->blockActions));
325 foreach($this->blockActions as $blockAction){
326 $out->putVarInt($blockAction->getActionType());
327 $blockAction->write($out);
328 }
329 }
330 if($this->vehicleInfo !== null){
331 $this->vehicleInfo->write($out);
332 }
333 $out->putLFloat($this->analogMoveVecX);
334 $out->putLFloat($this->analogMoveVecZ);
335 $out->putVector3($this->cameraOrientation);
336 $out->putVector2($this->rawMove);
337 }
338
339 public function handle(PacketHandlerInterface $handler) : bool{
340 return $handler->handlePlayerAuthInput($this);
341 }
342}
static create(Vector3 $position, float $pitch, float $yaw, float $headYaw, float $moveVecX, float $moveVecZ, BitSet $inputFlags, int $inputMode, int $playMode, int $interactionMode, Vector2 $interactRotation, int $tick, Vector3 $delta, ?ItemInteractionData $itemInteractionData, ?ItemStackRequest $itemStackRequest, ?array $blockActions, ?PlayerAuthInputVehicleInfo $vehicleInfo, float $analogMoveVecX, float $analogMoveVecZ, Vector3 $cameraOrientation, Vector2 $rawMove)