PocketMine-MP 5.36.1 git-eaa7c4834c8fe2f379d24e7f0ee6cc63cfb18ccc
Loading...
Searching...
No Matches
CommandEnumRawData.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\command\raw;
16
17use pmmp\encoding\Byte;
18use pmmp\encoding\ByteBufferReader;
19use pmmp\encoding\ByteBufferWriter;
20use pmmp\encoding\LE;
21use pmmp\encoding\VarInt;
23use function count;
24
26
31 public function __construct(
32 private string $name,
33 private array $valueIndexes
34 ){}
35
36 public function getName() : string{ return $this->name; }
37
42 public function getValueIndexes() : array{ return $this->valueIndexes; }
43
44 public static function read(ByteBufferReader $in, int $valueListSize) : self{
45 $name = CommonTypes::getString($in);
46 $valueIndexes = [];
47 $size = VarInt::readUnsignedInt($in);
48
49 for($i = 0; $i < $size; $i++){
50 $valueIndexes[] = match(true){
51 $valueListSize < 256 => Byte::readUnsigned($in),
52 $valueListSize < 65536 => LE::readUnsignedShort($in),
53 default => LE::readUnsignedInt($in)
54 };
55 }
56
57 return new self($name, $valueIndexes);
58 }
59
60 public function write(ByteBufferWriter $out, int $valueListSize) : void{
61 CommonTypes::putString($out, $this->name);
62 VarInt::writeUnsignedInt($out, count($this->valueIndexes));
63
64 foreach($this->valueIndexes as $index){
65 match(true){
66 $valueListSize < 256 => Byte::writeUnsigned($out, $index),
67 $valueListSize < 65536 => LE::writeUnsignedShort($out, $index),
68 default => LE::writeUnsignedInt($out, $index)
69 };
70 }
71 }
72}
__construct(private string $name, private array $valueIndexes)