PocketMine-MP 5.18.1 git-9381fc4172e5dce4cada1cb356050c8a2ab57b94
NetworkNbtSerializer.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\serializer;
16
19use function count;
20use function strlen;
21
23
24 public function readShort() : int{
25 return $this->buffer->getLShort();
26 }
27
28 public function readSignedShort() : int{
29 return $this->buffer->getSignedLShort();
30 }
31
32 public function writeShort(int $v) : void{
33 $this->buffer->putLShort($v);
34 }
35
36 public function readInt() : int{
37 return $this->buffer->getVarInt();
38 }
39
40 public function writeInt(int $v) : void{
41 $this->buffer->putVarInt($v);
42 }
43
44 public function readLong() : int{
45 return $this->buffer->getVarLong();
46 }
47
48 public function writeLong(int $v) : void{
49 $this->buffer->putVarLong($v);
50 }
51
52 public function readString() : string{
53 return $this->buffer->get(self::checkReadStringLength($this->buffer->getUnsignedVarInt()));
54 }
55
56 public function writeString(string $v) : void{
57 $this->buffer->putUnsignedVarInt(self::checkWriteStringLength(strlen($v)));
58 $this->buffer->put($v);
59 }
60
61 public function readFloat() : float{
62 return $this->buffer->getLFloat();
63 }
64
65 public function writeFloat(float $v) : void{
66 $this->buffer->putLFloat($v);
67 }
68
69 public function readDouble() : float{
70 return $this->buffer->getLDouble();
71 }
72
73 public function writeDouble(float $v) : void{
74 $this->buffer->putLDouble($v);
75 }
76
77 public function readIntArray() : array{
78 $len = $this->readInt(); //varint
79 if($len < 0){
80 throw new NbtDataException("Array length cannot be less than zero ($len < 0)");
81 }
82 $ret = [];
83 for($i = 0; $i < $len; ++$i){
84 $ret[] = $this->readInt(); //varint
85 }
86
87 return $ret;
88 }
89
90 public function writeIntArray(array $array) : void{
91 $this->writeInt(count($array)); //varint
92 foreach($array as $v){
93 $this->writeInt($v); //varint
94 }
95 }
96}