PocketMine-MP 5.44.3 git-327e8a1690982f1ac3634944d705ebad5d91f4ad
Loading...
Searching...
No Matches
DynamicValueList.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\cereal;
16
17use pmmp\encoding\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\LE;
20use pmmp\encoding\VarInt;
21use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait;
22use function count;
23
24final class DynamicValueList extends DynamicValue{
25 use GetTypeIdFromConstTrait;
26
27 public const ID = DynamicValueType::LIST;
28
33 public function __construct(
34 private array $values
35 ){}
36
41 public function getValues() : array{
42 return $this->values;
43 }
44
45 protected static function readValue(ByteBufferReader $in) : self{
46 $size = VarInt::readUnsignedInt($in);
47 $values = [];
48 for($i = 0; $i < $size; ++$i){
49 $type = LE::readUnsignedInt($in);
50 $values[] = DynamicValue::read($in, $type);
51 }
52 return new self($values);
53 }
54
55 protected function writeValue(ByteBufferWriter $out) : void{
56 VarInt::writeUnsignedInt($out, count($this->values));
57 foreach($this->values as $value){
58 LE::writeUnsignedInt($out, $value?->getTypeId() ?? DynamicValueType::NULL);
59 $value?->write($out);
60 }
61 }
62}