PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
ListTag.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20*/
21
22declare(strict_types=1);
23
24namespace pocketmine\nbt\tag;
25
31use function func_num_args;
32use function get_class;
33use function iterator_to_array;
34use function str_repeat;
35
39final class ListTag extends Tag implements \Countable, \IteratorAggregate{
40 use NoDynamicFieldsTrait;
41
43 private $tagType;
48 private $value;
49
53 public function __construct(array $value = [], int $tagType = NBT::TAG_End){
54 self::restrictArgCount(__METHOD__, func_num_args(), 2);
55 $this->tagType = $tagType;
56 $this->value = new \SplDoublyLinkedList();
57 foreach($value as $tag){
58 $this->push($tag);
59 }
60 }
61
65 public function getValue() : array{
66 $value = [];
67 foreach($this->value as $k => $v){
68 $value[$k] = $v;
69 }
70
71 return $value;
72 }
73
79 public function getAllValues() : array{
80 $result = [];
81 foreach($this->value as $tag){
82 $result[] = $tag->getValue();
83 }
84
85 return $result;
86 }
87
88 public function count() : int{
89 return $this->value->count();
90 }
91
92 public function getCount() : int{
93 return $this->value->count();
94 }
95
99 public function push(Tag $tag) : void{
100 $this->checkTagType($tag);
101 $this->value->push($tag);
102 }
103
107 public function pop() : Tag{
108 return $this->value->pop();
109 }
110
114 public function unshift(Tag $tag) : void{
115 $this->checkTagType($tag);
116 $this->value->unshift($tag);
117 }
118
122 public function shift() : Tag{
123 return $this->value->shift();
124 }
125
133 public function insert(int $offset, Tag $tag){
134 $this->checkTagType($tag);
135 $this->value->add($offset, $tag);
136 }
137
141 public function remove(int $offset) : void{
142 unset($this->value[$offset]);
143 }
144
150 public function get(int $offset) : Tag{
151 if(!isset($this->value[$offset])){
152 throw new \OutOfRangeException("No such tag at offset $offset");
153 }
154 return $this->value[$offset];
155 }
156
160 public function first() : Tag{
161 return $this->value->bottom();
162 }
163
167 public function last() : Tag{
168 return $this->value->top();
169 }
170
176 public function set(int $offset, Tag $tag) : void{
177 $this->checkTagType($tag);
178 $this->value[$offset] = $tag;
179 }
180
184 public function isset(int $offset) : bool{
185 return isset($this->value[$offset]);
186 }
187
191 public function empty() : bool{
192 return $this->value->isEmpty();
193 }
194
195 protected function getTypeName() : string{
196 return "List";
197 }
198
199 public function getType() : int{
200 return NBT::TAG_List;
201 }
202
206 public function getTagType() : int{
207 return $this->tagType;
208 }
209
217 public function setTagType(int $type){
218 if(!$this->value->isEmpty()){
219 throw new \LogicException("Cannot change tag type of non-empty ListTag");
220 }
221 $this->tagType = $type;
222 }
223
229 private function checkTagType(Tag $tag) : void{
230 $type = $tag->getType();
231 if($type !== $this->tagType){
232 if($this->tagType === NBT::TAG_End){
233 $this->tagType = $type;
234 }else{
235 //TODO: reintroduce type info
236 throw new \TypeError("Invalid tag of type " . get_class($tag) . " assigned to ListTag");
237 }
238 }
239 }
240
241 public static function read(NbtStreamReader $reader, ReaderTracker $tracker) : self{
242 $value = [];
243 $tagType = $reader->readByte();
244 $size = $reader->readInt();
245
246 if($size > 0){
247 if($tagType === NBT::TAG_End){
248 throw new NbtDataException("Unexpected non-empty list of TAG_End");
249 }
250
251 $tracker->protectDepth(static function() use($size, $tagType, $reader, $tracker, &$value) : void{
252 for($i = 0; $i < $size; ++$i){
253 $value[] = NBT::createTag($tagType, $reader, $tracker);
254 }
255 });
256 }else{
257 $tagType = NBT::TAG_End; //Some older NBT implementations used TAG_Byte for empty lists.
258 }
259 return new self($value, $tagType);
260 }
261
262 public function write(NbtStreamWriter $writer) : void{
263 $writer->writeByte($this->tagType);
264 $writer->writeInt($this->value->count());
266 foreach($this->value as $tag){
267 $tag->write($writer);
268 }
269 }
270
271 protected function stringifyValue(int $indentation) : string{
272 $str = "{\n";
274 foreach($this->value as $tag){
275 $str .= str_repeat(" ", $indentation + 1) . $tag->toString($indentation + 1) . "\n";
276 }
277 return $str . str_repeat(" ", $indentation) . "}";
278 }
279
280 public function __clone(){
282 $new = new \SplDoublyLinkedList();
283
284 foreach($this->value as $tag){
285 $new->push($tag->safeClone());
286 }
287
288 $this->value = $new;
289 }
290
291 protected function makeCopy(){
292 return clone $this;
293 }
294
299 public function getIterator() : \Generator{
300 //we technically don't need iterator_to_array() here, but I don't feel comfortable relying on "yield from" to
301 //copy the underlying dataset referenced by SplDoublyLinkedList
302 yield from iterator_to_array($this->value, true);
303 }
304
305 public function equals(Tag $that) : bool{
306 if(!($that instanceof $this) or $this->count() !== $that->count()){
307 return false;
308 }
309
310 foreach($this as $k => $v){
311 if(!$v->equals($that->get($k))){
312 return false;
313 }
314 }
315
316 return true;
317 }
318}
__construct(array $value=[], int $tagType=NBT::TAG_End)
Definition: ListTag.php:53
insert(int $offset, Tag $tag)
Definition: ListTag.php:133