-
Notifications
You must be signed in to change notification settings - Fork 77
/
PlayerAuthInputPacket.php
240 lines (212 loc) · 7.68 KB
/
PlayerAuthInputPacket.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/*
* This file is part of BedrockProtocol.
* Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
*
* BedrockProtocol is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
declare(strict_types=1);
namespace pocketmine\network\mcpe\protocol;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
use pocketmine\network\mcpe\protocol\types\InputMode;
use pocketmine\network\mcpe\protocol\types\inventory\stackrequest\ItemStackRequest;
use pocketmine\network\mcpe\protocol\types\ItemInteractionData;
use pocketmine\network\mcpe\protocol\types\PlayerAction;
use pocketmine\network\mcpe\protocol\types\PlayerAuthInputFlags;
use pocketmine\network\mcpe\protocol\types\PlayerBlockAction;
use pocketmine\network\mcpe\protocol\types\PlayerBlockActionStopBreak;
use pocketmine\network\mcpe\protocol\types\PlayerBlockActionWithBlockInfo;
use pocketmine\network\mcpe\protocol\types\PlayMode;
use function assert;
use function count;
class PlayerAuthInputPacket extends DataPacket implements ServerboundPacket{
public const NETWORK_ID = ProtocolInfo::PLAYER_AUTH_INPUT_PACKET;
private Vector3 $position;
private float $pitch;
private float $yaw;
private float $headYaw;
private float $moveVecX;
private float $moveVecZ;
private int $inputFlags;
private int $inputMode;
private int $playMode;
private ?Vector3 $vrGazeDirection = null;
private int $tick;
private Vector3 $delta;
public ?ItemInteractionData $itemInteractionData = null;
private ?ItemStackRequest $itemStackRequest = null;
/** @var PlayerBlockAction[]|null */
private ?array $blockActions = null;
/**
* @param int $inputFlags @see PlayerAuthInputFlags
* @param int $inputMode @see InputMode
* @param int $playMode @see PlayMode
* @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, float $pitch, float $yaw, float $headYaw, float $moveVecX, float $moveVecZ, int $inputFlags, int $inputMode, int $playMode, ?Vector3 $vrGazeDirection, int $tick, Vector3 $delta, ?ItemInteractionData $itemInteractionData, ?ItemStackRequest $itemStackRequest, ?array $blockActions) : 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");
}
$result = new self;
$result->position = $position->asVector3();
$result->pitch = $pitch;
$result->yaw = $yaw;
$result->headYaw = $headYaw;
$result->moveVecX = $moveVecX;
$result->moveVecZ = $moveVecZ;
$result->inputFlags = $inputFlags & ~((1 << PlayerAuthInputFlags::PERFORM_ITEM_STACK_REQUEST) | (1 << PlayerAuthInputFlags::PERFORM_ITEM_INTERACTION) | (1 << PlayerAuthInputFlags::PERFORM_BLOCK_ACTIONS));
if($itemStackRequest !== null){
$result->inputFlags |= 1 << PlayerAuthInputFlags::PERFORM_ITEM_STACK_REQUEST;
}
if($itemInteractionData !== null){
$result->inputFlags |= 1 << PlayerAuthInputFlags::PERFORM_ITEM_INTERACTION;
}
if($blockActions !== null){
$result->inputFlags |= 1 << PlayerAuthInputFlags::PERFORM_BLOCK_ACTIONS;
}
$result->inputMode = $inputMode;
$result->playMode = $playMode;
if($vrGazeDirection !== null){
$result->vrGazeDirection = $vrGazeDirection->asVector3();
}
$result->tick = $tick;
$result->delta = $delta;
$result->itemInteractionData = $itemInteractionData;
$result->itemStackRequest = $itemStackRequest;
$result->blockActions = $blockActions;
return $result;
}
public function getPosition() : Vector3{
return $this->position;
}
public function getPitch() : float{
return $this->pitch;
}
public function getYaw() : float{
return $this->yaw;
}
public function getHeadYaw() : float{
return $this->headYaw;
}
public function getMoveVecX() : float{
return $this->moveVecX;
}
public function getMoveVecZ() : float{
return $this->moveVecZ;
}
/**
* @see PlayerAuthInputFlags
*/
public function getInputFlags() : int{
return $this->inputFlags;
}
/**
* @see InputMode
*/
public function getInputMode() : int{
return $this->inputMode;
}
/**
* @see PlayMode
*/
public function getPlayMode() : int{
return $this->playMode;
}
public function getVrGazeDirection() : ?Vector3{
return $this->vrGazeDirection;
}
public function getTick() : int{
return $this->tick;
}
public function getDelta() : Vector3{
return $this->delta;
}
public function getItemInteractionData() : ?ItemInteractionData{
return $this->itemInteractionData;
}
public function getItemStackRequest() : ?ItemStackRequest{
return $this->itemStackRequest;
}
/**
* @return PlayerBlockAction[]|null
*/
public function getBlockActions() : ?array{
return $this->blockActions;
}
public function hasFlag(int $flag) : bool{
return ($this->getInputFlags() & (1 << $flag)) !== 0;
}
protected function decodePayload(PacketSerializer $in) : void{
$this->pitch = $in->getLFloat();
$this->yaw = $in->getLFloat();
$this->position = $in->getVector3();
$this->moveVecX = $in->getLFloat();
$this->moveVecZ = $in->getLFloat();
$this->headYaw = $in->getLFloat();
$this->inputFlags = $in->getUnsignedVarLong();
$this->inputMode = $in->getUnsignedVarInt();
$this->playMode = $in->getUnsignedVarInt();
if($this->playMode === PlayMode::VR){
$this->vrGazeDirection = $in->getVector3();
}
$this->tick = $in->getUnsignedVarLong();
$this->delta = $in->getVector3();
if($this->hasFlag(PlayerAuthInputFlags::PERFORM_ITEM_INTERACTION)){
$this->itemInteractionData = ItemInteractionData::read($in);
}
if($this->hasFlag(PlayerAuthInputFlags::PERFORM_ITEM_STACK_REQUEST)){
$this->itemStackRequest = ItemStackRequest::read($in);
}
if($this->hasFlag(PlayerAuthInputFlags::PERFORM_BLOCK_ACTIONS)){
$this->blockActions = [];
$max = $in->getVarInt();
for($i = 0; $i < $max; ++$i){
$actionType = $in->getVarInt();
$this->blockActions[] = match(true){
PlayerBlockActionWithBlockInfo::isValidActionType($actionType) => PlayerBlockActionWithBlockInfo::read($in, $actionType),
$actionType === PlayerAction::STOP_BREAK => new PlayerBlockActionStopBreak(),
default => throw new PacketDecodeException("Unexpected block action type $actionType")
};
}
}
}
protected function encodePayload(PacketSerializer $out) : void{
$out->putLFloat($this->pitch);
$out->putLFloat($this->yaw);
$out->putVector3($this->position);
$out->putLFloat($this->moveVecX);
$out->putLFloat($this->moveVecZ);
$out->putLFloat($this->headYaw);
$out->putUnsignedVarLong($this->inputFlags);
$out->putUnsignedVarInt($this->inputMode);
$out->putUnsignedVarInt($this->playMode);
if($this->playMode === PlayMode::VR){
assert($this->vrGazeDirection !== null);
$out->putVector3($this->vrGazeDirection);
}
$out->putUnsignedVarLong($this->tick);
$out->putVector3($this->delta);
if($this->itemInteractionData !== null){
$this->itemInteractionData->write($out);
}
if($this->itemStackRequest !== null){
$this->itemStackRequest->write($out);
}
if($this->blockActions !== null){
$out->putVarInt(count($this->blockActions));
foreach($this->blockActions as $blockAction){
$out->putVarInt($blockAction->getActionType());
$blockAction->write($out);
}
}
}
public function handle(PacketHandlerInterface $handler) : bool{
return $handler->handlePlayerAuthInput($this);
}
}