PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
AddPlayerPacket.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;
28use Ramsey\Uuid\UuidInterface;
29use function count;
30
32 public const NETWORK_ID = ProtocolInfo::ADD_PLAYER_PACKET;
33
34 public UuidInterface $uuid;
35 public string $username;
36 public int $actorRuntimeId;
37 public string $platformChatId = "";
38 public Vector3 $position;
39 public ?Vector3 $motion = null;
40 public float $pitch = 0.0;
41 public float $yaw = 0.0;
42 public float $headYaw = 0.0;
43 public ItemStackWrapper $item;
44 public int $gameMode;
49 public array $metadata = [];
50 public PropertySyncData $syncedProperties;
51
52 public UpdateAbilitiesPacket $abilitiesPacket;
53
55 public array $links = [];
56 public string $deviceId = ""; //TODO: fill player's device ID (???)
57 public int $buildPlatform = DeviceOS::UNKNOWN;
58
65 public static function create(
66 UuidInterface $uuid,
67 string $username,
68 int $actorRuntimeId,
69 string $platformChatId,
70 Vector3 $position,
71 ?Vector3 $motion,
72 float $pitch,
73 float $yaw,
74 float $headYaw,
75 ItemStackWrapper $item,
76 int $gameMode,
77 array $metadata,
78 PropertySyncData $syncedProperties,
79 UpdateAbilitiesPacket $abilitiesPacket,
80 array $links,
81 string $deviceId,
82 int $buildPlatform,
83 ) : self{
84 $result = new self;
85 $result->uuid = $uuid;
86 $result->username = $username;
87 $result->actorRuntimeId = $actorRuntimeId;
88 $result->platformChatId = $platformChatId;
89 $result->position = $position;
90 $result->motion = $motion;
91 $result->pitch = $pitch;
92 $result->yaw = $yaw;
93 $result->headYaw = $headYaw;
94 $result->item = $item;
95 $result->gameMode = $gameMode;
96 $result->metadata = $metadata;
97 $result->syncedProperties = $syncedProperties;
98 $result->abilitiesPacket = $abilitiesPacket;
99 $result->links = $links;
100 $result->deviceId = $deviceId;
101 $result->buildPlatform = $buildPlatform;
102 return $result;
103 }
104
105 protected function decodePayload(ByteBufferReader $in) : void{
106 $this->uuid = CommonTypes::getUUID($in);
107 $this->username = CommonTypes::getString($in);
108 $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in);
109 $this->platformChatId = CommonTypes::getString($in);
110 $this->position = CommonTypes::getVector3($in);
111 $this->motion = CommonTypes::getVector3($in);
112 $this->pitch = LE::readFloat($in);
113 $this->yaw = LE::readFloat($in);
114 $this->headYaw = LE::readFloat($in);
115 $this->item = CommonTypes::getItemStackWrapper($in);
116 $this->gameMode = VarInt::readSignedInt($in);
117 $this->metadata = CommonTypes::getEntityMetadata($in);
118 $this->syncedProperties = PropertySyncData::read($in);
119
120 $this->abilitiesPacket = new UpdateAbilitiesPacket();
121 $this->abilitiesPacket->decodePayload($in);
122
123 $linkCount = VarInt::readUnsignedInt($in);
124 for($i = 0; $i < $linkCount; ++$i){
125 $this->links[$i] = CommonTypes::getEntityLink($in);
126 }
127
128 $this->deviceId = CommonTypes::getString($in);
129 $this->buildPlatform = LE::readSignedInt($in);
130 }
131
132 protected function encodePayload(ByteBufferWriter $out) : void{
133 CommonTypes::putUUID($out, $this->uuid);
134 CommonTypes::putString($out, $this->username);
135 CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId);
136 CommonTypes::putString($out, $this->platformChatId);
137 CommonTypes::putVector3($out, $this->position);
138 CommonTypes::putVector3Nullable($out, $this->motion);
139 LE::writeFloat($out, $this->pitch);
140 LE::writeFloat($out, $this->yaw);
141 LE::writeFloat($out, $this->headYaw);
142 CommonTypes::putItemStackWrapper($out, $this->item);
143 VarInt::writeSignedInt($out, $this->gameMode);
144 CommonTypes::putEntityMetadata($out, $this->metadata);
145 $this->syncedProperties->write($out);
146
147 $this->abilitiesPacket->encodePayload($out);
148
149 VarInt::writeUnsignedInt($out, count($this->links));
150 foreach($this->links as $link){
151 CommonTypes::putEntityLink($out, $link);
152 }
153
154 CommonTypes::putString($out, $this->deviceId);
155 LE::writeSignedInt($out, $this->buildPlatform);
156 }
157
158 public function handle(PacketHandlerInterface $handler) : bool{
159 return $handler->handleAddPlayer($this);
160 }
161}
static create(UuidInterface $uuid, string $username, int $actorRuntimeId, string $platformChatId, Vector3 $position, ?Vector3 $motion, float $pitch, float $yaw, float $headYaw, ItemStackWrapper $item, int $gameMode, array $metadata, PropertySyncData $syncedProperties, UpdateAbilitiesPacket $abilitiesPacket, array $links, string $deviceId, int $buildPlatform,)