|
23 | 23 |
|
24 | 24 | namespace pocketmine\nbt; |
25 | 25 |
|
| 26 | +use pocketmine\nbt\tag\Tag; |
26 | 27 | use pocketmine\utils\Binary; |
27 | 28 | use pocketmine\utils\BinaryDataException; |
28 | 29 | use pocketmine\utils\BinaryStream; |
@@ -73,6 +74,24 @@ public function read(string $buffer, int &$offset = 0, int $maxDepth = 0) : Tree |
73 | 74 | return $data; |
74 | 75 | } |
75 | 76 |
|
| 77 | + /** |
| 78 | + * Reads a tag without a header from the buffer and returns it. The tag does not have a name, and the type is not |
| 79 | + * specified by the binary data. Only the tag's raw binary value is present. |
| 80 | + * |
| 81 | + * This could be used if the expected root type is always the same, but it's not usually seen in the wild. |
| 82 | + * However, this is used in some places in the Minecraft: Bedrock network protocol. |
| 83 | + * |
| 84 | + * @throws NbtDataException |
| 85 | + */ |
| 86 | + public function readHeadless(string $buffer, int $rootType, int &$offset = 0, int $maxDepth = 0) : Tag{ |
| 87 | + $this->buffer = new BinaryStream($buffer, $offset); |
| 88 | + |
| 89 | + $data = NBT::createTag($rootType, $this, new ReaderTracker($maxDepth)); |
| 90 | + $offset = $this->buffer->getOffset(); |
| 91 | + |
| 92 | + return $data; |
| 93 | + } |
| 94 | + |
76 | 95 | /** |
77 | 96 | * Decodes a list of NBT tags into objects and returns them. |
78 | 97 | * |
@@ -111,6 +130,18 @@ public function write(TreeRoot $data) : string{ |
111 | 130 | return $this->buffer->getBuffer(); |
112 | 131 | } |
113 | 132 |
|
| 133 | + /** |
| 134 | + * Writes a nameless tag without any header information. The reader of the data must know what type to expect, as |
| 135 | + * it is not specified in the data. |
| 136 | + * |
| 137 | + * @see BaseNbtSerializer::readHeadless() |
| 138 | + */ |
| 139 | + public function writeHeadless(Tag $data) : string{ |
| 140 | + $this->buffer = new BinaryStream(); |
| 141 | + $data->write($this); |
| 142 | + return $this->buffer->getBuffer(); |
| 143 | + } |
| 144 | + |
114 | 145 | /** |
115 | 146 | * @param TreeRoot[] $data |
116 | 147 | */ |
|
0 commit comments