PocketMine-MP 5.44.3 git-327e8a1690982f1ac3634944d705ebad5d91f4ad
Loading...
Searching...
No Matches
DynamicValue.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;
20
21abstract class DynamicValue{
22 abstract public function getTypeId() : int;
23
24 abstract protected function writeValue(ByteBufferWriter $out) : void;
25
26 final public function write(ByteBufferWriter $out) : void{
27 $this->writeValue($out);
28 }
29
30 final public static function read(ByteBufferReader $in, int $type) : ?self{
31 //TODO: I don't like putting this here (cyclic dependency) but I don't know where else to put it for now.
32 //Really we need to revamp how unions are handled in general, but that's a job for another time
33 return match($type){
34 DynamicValueType::NULL => null,
35 DynamicValueBool::ID => DynamicValueBool::readValue($in),
36 DynamicValueLong::ID => DynamicValueLong::readValue($in),
37 DynamicValueDouble::ID => DynamicValueDouble::readValue($in),
38 DynamicValueString::ID => DynamicValueString::readValue($in),
39 DynamicValueList::ID => DynamicValueList::readValue($in),
40 DynamicValueMap::ID => DynamicValueMap::readValue($in),
41 default => throw new PacketDecodeException("Unknown dynamic value type $type")
42 };
43 }
44}