PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
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
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 Vector2 $interactRotation;
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 private Vector3 $cameraOrientation;
56
61 private static function internalCreate(
62 Vector3 $position,
63 float $pitch,
64 float $yaw,
65 float $headYaw,
66 float $moveVecX,
67 float $moveVecZ,
68 int $inputFlags,
69 int $inputMode,
70 int $playMode,
71 int $interactionMode,
72 Vector2 $interactRotation,
73 int $tick,
74 Vector3 $delta,
75 ?ItemInteractionData $itemInteractionData,
76 ?ItemStackRequest $itemStackRequest,
77 ?array $blockActions,
78 ?PlayerAuthInputVehicleInfo $vehicleInfo,
79 float $analogMoveVecX,
80 float $analogMoveVecZ,
81 Vector3 $cameraOrientation,
82 ) : self{
83 $result = new self;
84 $result->position = $position;
85 $result->pitch = $pitch;
86 $result->yaw = $yaw;
87 $result->headYaw = $headYaw;
88 $result->moveVecX = $moveVecX;
89 $result->moveVecZ = $moveVecZ;
90 $result->inputFlags = $inputFlags;
91 $result->inputMode = $inputMode;
92 $result->playMode = $playMode;
93 $result->interactionMode = $interactionMode;
94 $result->interactRotation = $interactRotation;
95 $result->tick = $tick;
96 $result->delta = $delta;
97 $result->itemInteractionData = $itemInteractionData;
98 $result->itemStackRequest = $itemStackRequest;
99 $result->blockActions = $blockActions;
100 $result->vehicleInfo = $vehicleInfo;
101 $result->analogMoveVecX = $analogMoveVecX;
102 $result->analogMoveVecZ = $analogMoveVecZ;
103 $result->cameraOrientation = $cameraOrientation;
104 return $result;
105 }
106
114 public static function create(
115 Vector3 $position,
116 float $pitch,
117 float $yaw,
118 float $headYaw,
119 float $moveVecX,
120 float $moveVecZ,
121 int $inputFlags,
122 int $inputMode,
123 int $playMode,
124 int $interactionMode,
125 Vector2 $interactRotation,
126 int $tick,
127 Vector3 $delta,
128 ?ItemInteractionData $itemInteractionData,
129 ?ItemStackRequest $itemStackRequest,
130 ?array $blockActions,
131 ?PlayerAuthInputVehicleInfo $vehicleInfo,
132 float $analogMoveVecX,
133 float $analogMoveVecZ,
134 Vector3 $cameraOrientation
135 ) : self{
136 $realInputFlags = $inputFlags & ~((1 << PlayerAuthInputFlags::PERFORM_ITEM_STACK_REQUEST) | (1 << PlayerAuthInputFlags::PERFORM_ITEM_INTERACTION) | (1 << PlayerAuthInputFlags::PERFORM_BLOCK_ACTIONS));
137 if($itemStackRequest !== null){
138 $realInputFlags |= 1 << PlayerAuthInputFlags::PERFORM_ITEM_STACK_REQUEST;
139 }
140 if($itemInteractionData !== null){
141 $realInputFlags |= 1 << PlayerAuthInputFlags::PERFORM_ITEM_INTERACTION;
142 }
143 if($blockActions !== null){
144 $realInputFlags |= 1 << PlayerAuthInputFlags::PERFORM_BLOCK_ACTIONS;
145 }
146 if($vehicleInfo !== null){
147 $realInputFlags |= 1 << PlayerAuthInputFlags::IN_CLIENT_PREDICTED_VEHICLE;
148 }
149
150 return self::internalCreate(
151 $position,
152 $pitch,
153 $yaw,
154 $headYaw,
155 $moveVecX,
156 $moveVecZ,
157 $realInputFlags,
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 );
172 }
173
174 public function getPosition() : Vector3{
175 return $this->position;
176 }
177
178 public function getPitch() : float{
179 return $this->pitch;
180 }
181
182 public function getYaw() : float{
183 return $this->yaw;
184 }
185
186 public function getHeadYaw() : float{
187 return $this->headYaw;
188 }
189
190 public function getMoveVecX() : float{
191 return $this->moveVecX;
192 }
193
194 public function getMoveVecZ() : float{
195 return $this->moveVecZ;
196 }
197
201 public function getInputFlags() : int{
202 return $this->inputFlags;
203 }
204
208 public function getInputMode() : int{
209 return $this->inputMode;
210 }
211
215 public function getPlayMode() : int{
216 return $this->playMode;
217 }
218
222 public function getInteractionMode() : int{
223 return $this->interactionMode;
224 }
225
226 public function getInteractRotation() : Vector2{ return $this->interactRotation; }
227
228 public function getTick() : int{
229 return $this->tick;
230 }
231
232 public function getDelta() : Vector3{
233 return $this->delta;
234 }
235
236 public function getItemInteractionData() : ?ItemInteractionData{
237 return $this->itemInteractionData;
238 }
239
240 public function getItemStackRequest() : ?ItemStackRequest{
241 return $this->itemStackRequest;
242 }
243
247 public function getBlockActions() : ?array{
248 return $this->blockActions;
249 }
250
251 public function getVehicleInfo() : ?PlayerAuthInputVehicleInfo{ return $this->vehicleInfo; }
252
253 public function getAnalogMoveVecX() : float{ return $this->analogMoveVecX; }
254
255 public function getAnalogMoveVecZ() : float{ return $this->analogMoveVecZ; }
256
257 public function getCameraOrientation() : Vector3{ return $this->cameraOrientation; }
258
259 public function hasFlag(int $flag) : bool{
260 return ($this->inputFlags & (1 << $flag)) !== 0;
261 }
262
263 protected function decodePayload(PacketSerializer $in) : void{
264 $this->pitch = $in->getLFloat();
265 $this->yaw = $in->getLFloat();
266 $this->position = $in->getVector3();
267 $this->moveVecX = $in->getLFloat();
268 $this->moveVecZ = $in->getLFloat();
269 $this->headYaw = $in->getLFloat();
270 $this->inputFlags = $in->getUnsignedVarLong();
271 $this->inputMode = $in->getUnsignedVarInt();
272 $this->playMode = $in->getUnsignedVarInt();
273 $this->interactionMode = $in->getUnsignedVarInt();
274 $this->interactRotation = $in->getVector2();
275 $this->tick = $in->getUnsignedVarLong();
276 $this->delta = $in->getVector3();
277 if($this->hasFlag(PlayerAuthInputFlags::PERFORM_ITEM_INTERACTION)){
278 $this->itemInteractionData = ItemInteractionData::read($in);
279 }
280 if($this->hasFlag(PlayerAuthInputFlags::PERFORM_ITEM_STACK_REQUEST)){
281 $this->itemStackRequest = ItemStackRequest::read($in);
282 }
283 if($this->hasFlag(PlayerAuthInputFlags::PERFORM_BLOCK_ACTIONS)){
284 $this->blockActions = [];
285 $max = $in->getVarInt();
286 for($i = 0; $i < $max; ++$i){
287 $actionType = $in->getVarInt();
288 $this->blockActions[] = match(true){
289 PlayerBlockActionWithBlockInfo::isValidActionType($actionType) => PlayerBlockActionWithBlockInfo::read($in, $actionType),
290 $actionType === PlayerAction::STOP_BREAK => new PlayerBlockActionStopBreak(),
291 default => throw new PacketDecodeException("Unexpected block action type $actionType")
292 };
293 }
294 }
295 if($this->hasFlag(PlayerAuthInputFlags::IN_CLIENT_PREDICTED_VEHICLE)){
296 $this->vehicleInfo = PlayerAuthInputVehicleInfo::read($in);
297 }
298 $this->analogMoveVecX = $in->getLFloat();
299 $this->analogMoveVecZ = $in->getLFloat();
300 $this->cameraOrientation = $in->getVector3();
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 $out->putUnsignedVarLong($this->inputFlags);
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 }
337
338 public function handle(PacketHandlerInterface $handler) : bool{
339 return $handler->handlePlayerAuthInput($this);
340 }
341}
static create(Vector3 $position, float $pitch, float $yaw, float $headYaw, float $moveVecX, float $moveVecZ, int $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)