forked from minevadi/libReplay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplayClient.php
More file actions
258 lines (240 loc) · 7.38 KB
/
ReplayClient.php
File metadata and controls
258 lines (240 loc) · 7.38 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
declare(strict_types=1);
namespace libReplay;
use libPhysX\internal\Rotation;
use pocketmine\entity\Skin;
use pocketmine\math\Vector3;
use pocketmine\Player;
/**
* Class ReplayClient
* @package libReplay
*/
class ReplayClient
{
private const TAG_CLIENT_ID = 0;
private const TAG_POSITION = 1;
private const TAG_ROTATION = 2;
private const TAG_SKIN = 3;
private const TAG_CUSTOM_NAME = 4;
private const TAG_X = 0;
private const TAG_Y = 1;
private const TAG_Z = 2;
private const TAG_YAW = 0;
private const TAG_PITCH = 1;
private const TAG_SKIN_ID = 0;
private const TAG_SKIN_REGULAR_DATA = 1;
private const TAG_SKIN_CAPE_DATA = 2;
private const TAG_SKIN_GEOMETRY_NAME = 3;
private const TAG_SKIN_GEOMETRY_DATA = 4;
/** @var string This is the player's name */
private string $clientId;
/** @var Skin */
private Skin $skin;
/** @var Vector3 */
private Vector3 $position;
/** @var Rotation */
private Rotation $rotation;
/** @var string A custom name for the actor at replay time */
private string $customName;
/** @var bool */
private bool $recorded = false;
/**
* ReplayClient constructor.
* @param string $clientId
* @param Skin $skin
* @param Vector3 $position
* @param Rotation $rotation
* @param string $customName
*
* @internal
*/
public function __construct(string $clientId, Skin $skin, Vector3 $position, Rotation $rotation,
string $customName = '')
{
$this->clientId = $clientId;
$this->skin = $skin;
$this->position = $position;
$this->rotation = $rotation;
$this->customName = $customName;
}
/**
* Construct a client from non volatile.
*
* @param array $nonVolatileClient
* @return ReplayClient|null
*
* @internal
*/
public static function constructFromNonVolatile(array $nonVolatileClient): ?ReplayClient
{
if (isset(
$nonVolatileClient[self::TAG_CLIENT_ID],
$nonVolatileClient[self::TAG_POSITION],
$nonVolatileClient[self::TAG_ROTATION],
$nonVolatileClient[self::TAG_SKIN],
$nonVolatileClient[self::TAG_CUSTOM_NAME]
)) {
$position = null;
$positionProperty = $nonVolatileClient[self::TAG_POSITION];
$positionIsValid = isset(
$positionProperty[self::TAG_X],
$positionProperty[self::TAG_Y],
$positionProperty[self::TAG_Z]
);
if ($positionIsValid) {
$position = new Vector3(
$positionProperty[self::TAG_X],
$positionProperty[self::TAG_Y],
$positionProperty[self::TAG_Z]
);
}
$rotation = null;
$rotationProperty = $nonVolatileClient[self::TAG_ROTATION];
$rotationIsValid = isset($rotationProperty[self::TAG_YAW], $rotationProperty[self::TAG_PITCH]);
if ($rotationIsValid) {
$rotation = new Rotation($rotationProperty[self::TAG_YAW], $rotationProperty[self::TAG_PITCH]);
}
$skin = null;
$skinProperty = $nonVolatileClient[self::TAG_SKIN];
$skinIsValid = isset(
$skinProperty[self::TAG_SKIN_ID],
$skinProperty[self::TAG_SKIN_REGULAR_DATA],
$skinProperty[self::TAG_SKIN_CAPE_DATA],
$skinProperty[self::TAG_SKIN_GEOMETRY_NAME],
$skinProperty[self::TAG_SKIN_GEOMETRY_DATA]
);
if ($skinIsValid) {
$skin = new Skin(
utf8_decode($skinProperty[self::TAG_SKIN_ID]),
utf8_decode($skinProperty[self::TAG_SKIN_REGULAR_DATA]),
utf8_decode($skinProperty[self::TAG_SKIN_CAPE_DATA]),
utf8_decode($skinProperty[self::TAG_SKIN_GEOMETRY_NAME]),
utf8_decode($skinProperty[self::TAG_SKIN_GEOMETRY_DATA])
);
}
if ($position instanceof Vector3 && $rotation instanceof Rotation && $skin instanceof Skin) {
return new self(
$nonVolatileClient[self::TAG_CLIENT_ID],
$skin,
$position,
$rotation,
$nonVolatileClient[self::TAG_CUSTOM_NAME]
);
}
}
return null;
}
/**
* Read a replay client referenced as a player.
*
* @param Player $player
* @return ReplayClient
*
* @internal
*/
public static function readFromPlayer(Player $player): ReplayClient
{
$clientId = $player->getName();
$customName = $player->getNameTag();
$player->saveNBT();
$skin = $player->getSkin();
$position = $player->asVector3();
$rotation = new Rotation($player->yaw, $player->pitch);
return new self($clientId, $skin, $position, $rotation, $customName);
}
/**
* Get the client's id.
* Also called the name.
*
* @return string
*/
public function getClientId(): string
{
return $this->clientId;
}
/**
* Get the skin.
*
* @return Skin
*/
public function getSkin(): Skin
{
return $this->skin;
}
/**
* Check if the client is being recorded.
*
* @return bool
*/
public function isRecorded(): bool
{
return $this->recorded;
}
/**
* Get original position of the client.
*
* @return Vector3
*/
public function getPosition(): Vector3
{
return $this->position;
}
/**
* Get original rotation of the client.
*
* @return Rotation
*/
public function getRotation(): Rotation
{
return $this->rotation;
}
/**
* Get the custom name of the client.
*
* @return string
*/
public function getCustomName(): string
{
return $this->customName;
}
/**
* Start or stop recording the client.
*
* @return void
*/
public function toggleRecord(): void
{
$this->recorded = !$this->recorded;
}
/**
* Convert the client into a safe data-transmittable
* format.
*
* @return array
*
* @internal
*/
public function convertToNonVolatile(): array
{
return [
self::TAG_CLIENT_ID => $this->clientId,
self::TAG_POSITION => [
self::TAG_X => $this->position->x,
self::TAG_Y => $this->position->y,
self::TAG_Z => $this->position->z
],
self::TAG_ROTATION => [
self::TAG_YAW => $this->rotation->yaw,
self::TAG_PITCH => $this->rotation->pitch
],
self::TAG_SKIN => [
self::TAG_SKIN_ID => utf8_encode($this->skin->getSkinId()),
self::TAG_SKIN_REGULAR_DATA => utf8_encode($this->skin->getSkinData()),
self::TAG_SKIN_CAPE_DATA => utf8_encode($this->skin->getCapeData()),
self::TAG_SKIN_GEOMETRY_NAME => utf8_encode($this->skin->getGeometryName()),
self::TAG_SKIN_GEOMETRY_DATA => utf8_encode($this->skin->getGeometryData())
],
self::TAG_CUSTOM_NAME => $this->customName
];
}
}