PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
Enum.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 
22 namespace pocketmine\nbt\tag;
23 
25 use pocketmine\nbt\tag\Enum as TagEnum;
26 
27 #include <rules/NBT.h>
28 
29 class Enum extends NamedTag implements \ArrayAccess, \Countable{
30 
31  private $tagType;
32 
33  public function __construct($name = "", $value = []){
34  $this->name = $name;
35  foreach($value as $k => $v){
36  $this->{$k} = $v;
37  }
38  }
39 
40  public function &getValue(){
41  $value = [];
42  foreach($this as $k => $v){
43  if($v instanceof Tag){
44  $value[$k] = $v;
45  }
46  }
47 
48  return $value;
49  }
50 
51  public function offsetExists($offset){
52  return isset($this->{$offset});
53  }
54 
55  public function offsetGet($offset){
56  if(isset($this->{$offset}) and $this->{$offset} instanceof Tag){
57  if($this->{$offset} instanceof \ArrayAccess){
58  return $this->{$offset};
59  }else{
60  return $this->{$offset}->getValue();
61  }
62  }
63 
64  return null;
65  }
66 
67  public function offsetSet($offset, $value){
68  if($value instanceof Tag){
69  $this->{$offset} = $value;
70  }elseif($this->{$offset} instanceof Tag){
71  $this->{$offset}->setValue($value);
72  }
73  }
74 
75  public function offsetUnset($offset){
76  unset($this->{$offset});
77  }
78 
79  public function count($mode = COUNT_NORMAL){
80  for($i = 0; true; $i++){
81  if(!isset($this->{$i})){
82  return $i;
83  }
84  if($mode === COUNT_RECURSIVE){
85  if($this->{$i} instanceof \Countable){
86  $i += count($this->{$i});
87  }
88  }
89  }
90 
91  return $i;
92  }
93 
94  public function getType(){
95  return NBT::TAG_Enum;
96  }
97 
98  public function setTagType($type){
99  $this->tagType = $type;
100  }
101 
102  public function getTagType(){
103  return $this->tagType;
104  }
105 
106  public function read(NBT $nbt){
107  $this->value = [];
108  $this->tagType = $nbt->getByte();
109  $size = $nbt->getInt();
110  for($i = 0; $i < $size and !$nbt->feof(); ++$i){
111  switch($this->tagType){
112  case NBT::TAG_Byte:
113  $tag = new Byte(false);
114  $tag->read($nbt);
115  $this->{$i} = $tag;
116  break;
117  case NBT::TAG_Short:
118  $tag = new Short(false);
119  $tag->read($nbt);
120  $this->{$i} = $tag;
121  break;
122  case NBT::TAG_Int:
123  $tag = new Int(false);
124  $tag->read($nbt);
125  $this->{$i} = $tag;
126  break;
127  case NBT::TAG_Long:
128  $tag = new Long(false);
129  $tag->read($nbt);
130  $this->{$i} = $tag;
131  break;
132  case NBT::TAG_Float:
133  $tag = new Float(false);
134  $tag->read($nbt);
135  $this->{$i} = $tag;
136  break;
137  case NBT::TAG_Double:
138  $tag = new Double(false);
139  $tag->read($nbt);
140  $this->{$i} = $tag;
141  break;
142  case NBT::TAG_ByteArray:
143  $tag = new ByteArray(false);
144  $tag->read($nbt);
145  $this->{$i} = $tag;
146  break;
147  case NBT::TAG_String:
148  $tag = new String(false);
149  $tag->read($nbt);
150  $this->{$i} = $tag;
151  break;
152  case NBT::TAG_Enum:
153  $tag = new TagEnum(false);
154  $tag->read($nbt);
155  $this->{$i} = $tag;
156  break;
157  case NBT::TAG_Compound:
158  $tag = new Compound(false);
159  $tag->read($nbt);
160  $this->{$i} = $tag;
161  break;
162  case NBT::TAG_IntArray:
163  $tag = new IntArray(false);
164  $tag->read($nbt);
165  $this->{$i} = $tag;
166  break;
167  }
168  }
169  }
170 
171  public function write(NBT $nbt){
172  if(!isset($this->tagType)){
173  $id = null;
174  foreach($this as $tag){
175  if($tag instanceof Tag){
176  if(!isset($id)){
177  $id = $tag->getType();
178  }elseif($id !== $tag->getType()){
179  return false;
180  }
181  }
182  }
183  $this->tagType = $id;
184  }
185 
186  $nbt->putByte($this->tagType);
187 
189  $tags = [];
190  foreach($this as $tag){
191  if($tag instanceof Tag){
192  $tags[] = $tag;
193  }
194  }
195  $nbt->putInt(count($tags));
196  foreach($tags as $tag){
197  $tag->write($nbt);
198  }
199  }
200 }