PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
AddActorPacket.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;
27use function count;
28
30 public const NETWORK_ID = ProtocolInfo::ADD_ACTOR_PACKET;
31
32 public int $actorUniqueId;
33 public int $actorRuntimeId;
34 public string $type;
35 public Vector3 $position;
36 public ?Vector3 $motion = null;
37 public float $pitch = 0.0;
38 public float $yaw = 0.0;
39 public float $headYaw = 0.0;
40 public float $bodyYaw = 0.0; //???
41
43 public array $attributes = [];
48 public array $metadata = [];
49 public PropertySyncData $syncedProperties;
51 public array $links = [];
52
60 public static function create(
61 int $actorUniqueId,
62 int $actorRuntimeId,
63 string $type,
64 Vector3 $position,
65 ?Vector3 $motion,
66 float $pitch,
67 float $yaw,
68 float $headYaw,
69 float $bodyYaw,
70 array $attributes,
71 array $metadata,
72 PropertySyncData $syncedProperties,
73 array $links,
74 ) : self{
75 $result = new self;
76 $result->actorUniqueId = $actorUniqueId;
77 $result->actorRuntimeId = $actorRuntimeId;
78 $result->type = $type;
79 $result->position = $position;
80 $result->motion = $motion;
81 $result->pitch = $pitch;
82 $result->yaw = $yaw;
83 $result->headYaw = $headYaw;
84 $result->bodyYaw = $bodyYaw;
85 $result->attributes = $attributes;
86 $result->metadata = $metadata;
87 $result->syncedProperties = $syncedProperties;
88 $result->links = $links;
89 return $result;
90 }
91
92 protected function decodePayload(ByteBufferReader $in) : void{
93 $this->actorUniqueId = CommonTypes::getActorUniqueId($in);
94 $this->actorRuntimeId = CommonTypes::getActorRuntimeId($in);
95 $this->type = CommonTypes::getString($in);
96 $this->position = CommonTypes::getVector3($in);
97 $this->motion = CommonTypes::getVector3($in);
98 $this->pitch = LE::readFloat($in);
99 $this->yaw = LE::readFloat($in);
100 $this->headYaw = LE::readFloat($in);
101 $this->bodyYaw = LE::readFloat($in);
102
103 $attrCount = VarInt::readUnsignedInt($in);
104 for($i = 0; $i < $attrCount; ++$i){
105 $id = CommonTypes::getString($in);
106 $min = LE::readFloat($in);
107 $current = LE::readFloat($in);
108 $max = LE::readFloat($in);
109 $this->attributes[] = new Attribute($id, $min, $max, $current, $current, []);
110 }
111
112 $this->metadata = CommonTypes::getEntityMetadata($in);
113 $this->syncedProperties = PropertySyncData::read($in);
114
115 $linkCount = VarInt::readUnsignedInt($in);
116 for($i = 0; $i < $linkCount; ++$i){
117 $this->links[] = CommonTypes::getEntityLink($in);
118 }
119 }
120
121 protected function encodePayload(ByteBufferWriter $out) : void{
122 CommonTypes::putActorUniqueId($out, $this->actorUniqueId);
123 CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId);
124 CommonTypes::putString($out, $this->type);
125 CommonTypes::putVector3($out, $this->position);
126 CommonTypes::putVector3Nullable($out, $this->motion);
127 LE::writeFloat($out, $this->pitch);
128 LE::writeFloat($out, $this->yaw);
129 LE::writeFloat($out, $this->headYaw);
130 LE::writeFloat($out, $this->bodyYaw);
131
132 VarInt::writeUnsignedInt($out, count($this->attributes));
133 foreach($this->attributes as $attribute){
134 CommonTypes::putString($out, $attribute->getId());
135 LE::writeFloat($out, $attribute->getMin());
136 LE::writeFloat($out, $attribute->getCurrent());
137 LE::writeFloat($out, $attribute->getMax());
138 }
139
140 CommonTypes::putEntityMetadata($out, $this->metadata);
141 $this->syncedProperties->write($out);
142
143 VarInt::writeUnsignedInt($out, count($this->links));
144 foreach($this->links as $link){
145 CommonTypes::putEntityLink($out, $link);
146 }
147 }
148
149 public function handle(PacketHandlerInterface $handler) : bool{
150 return $handler->handleAddActor($this);
151 }
152}
static create(int $actorUniqueId, int $actorRuntimeId, string $type, Vector3 $position, ?Vector3 $motion, float $pitch, float $yaw, float $headYaw, float $bodyYaw, array $attributes, array $metadata, PropertySyncData $syncedProperties, array $links,)