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