PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
PacketShapeData.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\types;
16
17use pmmp\encoding\Byte;
18use pmmp\encoding\ByteBufferReader;
19use pmmp\encoding\ByteBufferWriter;
20use pmmp\encoding\LE;
21use pmmp\encoding\VarInt;
26
30final class PacketShapeData{
31
32 public function __construct(
33 private int $networkId,
34 private ?ScriptDebugShapeType $type,
35 private ?Vector3 $location,
36 private ?float $scale,
37 private ?Vector3 $rotation,
38 private ?float $totalTimeLeft,
39 private ?Color $color,
40 private ?string $text,
41 private ?Vector3 $boxBound,
42 private ?Vector3 $lineEndLocation,
43 private ?float $arrowHeadLength,
44 private ?float $arrowHeadRadius,
45 private ?int $segments,
46 ){}
47
48 public static function remove(int $networkId) : self{
49 return new self($networkId, null, null, null, null, null, null, null, null, null, null, null, null);
50 }
51
52 public static function line(int $networkId, Vector3 $location, Vector3 $lineEndLocation, ?Color $color = null) : self{
53 return new self(
54 networkId: $networkId,
55 type: ScriptDebugShapeType::LINE,
56 location: $location,
57 scale: null,
58 rotation: null,
59 totalTimeLeft: null,
60 color: $color,
61 text: null,
62 boxBound: null,
63 lineEndLocation: $lineEndLocation,
64 arrowHeadLength: null,
65 arrowHeadRadius: null,
66 segments: null
67 );
68 }
69
70 public static function box(int $networkId, Vector3 $location, Vector3 $boxBound, ?float $scale = null, ?Color $color = null) : self{
71 return new self(
72 networkId: $networkId,
73 type: ScriptDebugShapeType::BOX,
74 location: $location,
75 scale: $scale,
76 rotation: null,
77 totalTimeLeft: null,
78 color: $color,
79 text: null,
80 boxBound: $boxBound,
81 lineEndLocation: null,
82 arrowHeadLength: null,
83 arrowHeadRadius: null,
84 segments: null
85 );
86 }
87
88 public static function sphere(int $networkId, Vector3 $location, ?float $scale = null, ?Color $color = null, ?int $segments = null) : self{
89 return new self(
90 networkId: $networkId,
91 type: ScriptDebugShapeType::SPHERE,
92 location: $location,
93 scale: $scale,
94 rotation: null,
95 totalTimeLeft: null,
96 color: $color,
97 text: null,
98 boxBound: null,
99 lineEndLocation: null,
100 arrowHeadLength: null,
101 arrowHeadRadius: null,
102 segments: $segments
103 );
104 }
105
106 public static function circle(int $networkId, Vector3 $location, ?float $scale = null, ?Color $color = null, ?int $segments = null) : self{
107 return new self(
108 networkId: $networkId,
109 type: ScriptDebugShapeType::CIRCLE,
110 location: $location,
111 scale: $scale,
112 rotation: null,
113 totalTimeLeft: null,
114 color: $color,
115 text: null,
116 boxBound: null,
117 lineEndLocation: null,
118 arrowHeadLength: null,
119 arrowHeadRadius: null,
120 segments: $segments
121 );
122 }
123
124 public static function text(int $networkId, Vector3 $location, string $text, ?Color $color = null) : self{
125 return new self(
126 networkId: $networkId,
127 type: ScriptDebugShapeType::TEXT,
128 location: $location,
129 scale: null,
130 rotation: null,
131 totalTimeLeft: null,
132 color: $color,
133 text: $text,
134 boxBound: null,
135 lineEndLocation: null,
136 arrowHeadLength: null,
137 arrowHeadRadius: null,
138 segments: null
139 );
140 }
141
142 public static function arrow(int $networkId, Vector3 $location, Vector3 $lineEndLocation, ?float $scale = null, ?Color $color = null, ?float $arrowHeadLength = null, ?float $arrowHeadRadius = null, ?int $segments = null) : self{
143 return new self(
144 networkId: $networkId,
145 type: ScriptDebugShapeType::ARROW,
146 location: $location,
147 scale: $scale,
148 rotation: null,
149 totalTimeLeft: null,
150 color: $color,
151 text: null,
152 boxBound: null,
153 lineEndLocation: $lineEndLocation,
154 arrowHeadLength: $arrowHeadLength,
155 arrowHeadRadius: $arrowHeadRadius,
156 segments: $segments
157 );
158 }
159
160 public function getNetworkId() : int{ return $this->networkId; }
161
162 public function getType() : ?ScriptDebugShapeType{ return $this->type; }
163
164 public function getLocation() : ?Vector3{ return $this->location; }
165
166 public function getScale() : ?float{ return $this->scale; }
167
168 public function getRotation() : ?Vector3{ return $this->rotation; }
169
170 public function getTotalTimeLeft() : ?float{ return $this->totalTimeLeft; }
171
172 public function getColor() : ?Color{ return $this->color; }
173
174 public function getText() : ?string{ return $this->text; }
175
176 public function getBoxBound() : ?Vector3{ return $this->boxBound; }
177
178 public function getLineEndLocation() : ?Vector3{ return $this->lineEndLocation; }
179
180 public function getArrowHeadLength() : ?float{ return $this->arrowHeadLength; }
181
182 public function getArrowHeadRadius() : ?float{ return $this->arrowHeadRadius; }
183
184 public function getSegments() : ?int{ return $this->segments; }
185
186 public static function read(ByteBufferReader $in) : self{
187 $networkId = VarInt::readUnsignedLong($in);
188 $type = CommonTypes::readOptional($in, fn() => ScriptDebugShapeType::fromPacket(Byte::readUnsigned($in)));
189 $location = CommonTypes::readOptional($in, CommonTypes::getVector3(...));
190 $scale = CommonTypes::readOptional($in, LE::readFloat(...));
191 $rotation = CommonTypes::readOptional($in, CommonTypes::getVector3(...));
192 $totalTimeLeft = CommonTypes::readOptional($in, LE::readFloat(...));
193 $color = CommonTypes::readOptional($in, fn() => Color::fromARGB(LE::readUnsignedInt($in)));
194 $text = CommonTypes::readOptional($in, CommonTypes::getString(...));
195 $boxBound = CommonTypes::readOptional($in, CommonTypes::getVector3(...));
196 $lineEndLocation = CommonTypes::readOptional($in, CommonTypes::getVector3(...));
197 $arrowHeadLength = CommonTypes::readOptional($in, LE::readFloat(...));
198 $arrowHeadRadius = CommonTypes::readOptional($in, LE::readFloat(...));
199 $segments = CommonTypes::readOptional($in, Byte::readUnsigned(...));
200
201 return new self(
202 $networkId,
203 $type,
204 $location,
205 $scale,
206 $rotation,
207 $totalTimeLeft,
208 $color,
209 $text,
210 $boxBound,
211 $lineEndLocation,
212 $arrowHeadLength,
213 $arrowHeadRadius,
214 $segments
215 );
216 }
217
218 public function write(ByteBufferWriter $out) : void{
219 VarInt::writeUnsignedLong($out, $this->networkId);
220 CommonTypes::writeOptional($out, $this->type, fn(ByteBufferWriter $out, ScriptDebugShapeType $type) => Byte::writeUnsigned($out, $type->value));
221 CommonTypes::writeOptional($out, $this->location, CommonTypes::putVector3(...));
222 CommonTypes::writeOptional($out, $this->scale, LE::writeFloat(...));
223 CommonTypes::writeOptional($out, $this->rotation, CommonTypes::putVector3(...));
224 CommonTypes::writeOptional($out, $this->totalTimeLeft, LE::writeFloat(...));
225 CommonTypes::writeOptional($out, $this->color, fn(ByteBufferWriter $out, Color $color) => LE::writeUnsignedInt($out, $color->toARGB()));
226 CommonTypes::writeOptional($out, $this->text, CommonTypes::putString(...));
227 CommonTypes::writeOptional($out, $this->boxBound, CommonTypes::putVector3(...));
228 CommonTypes::writeOptional($out, $this->lineEndLocation, CommonTypes::putVector3(...));
229 CommonTypes::writeOptional($out, $this->arrowHeadLength, LE::writeFloat(...));
230 CommonTypes::writeOptional($out, $this->arrowHeadRadius, LE::writeFloat(...));
231 CommonTypes::writeOptional($out, $this->segments, Byte::writeUnsigned(...));
232 }
233}