From 9a1e7ca83ce73e8e9e23463824f991c09e0569b9 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Thu, 28 Aug 2014 23:43:04 +0200 Subject: [PATCH 1/2] Implemented NBT::getArray() and NBT::setArray() --- src/pocketmine/nbt/NBT.php | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/pocketmine/nbt/NBT.php b/src/pocketmine/nbt/NBT.php index b4d2be5a9..1a76b95ba 100644 --- a/src/pocketmine/nbt/NBT.php +++ b/src/pocketmine/nbt/NBT.php @@ -39,6 +39,7 @@ use pocketmine\nbt\tag\Short; use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\Tag; use pocketmine\utils\Binary; +use pocketmine\utils\Utils; /** * Named Binary Tag encoder/decoder @@ -247,6 +248,59 @@ class NBT{ $this->buffer .= $v; } + public function getArray(){ + $data = []; + $this->toArray($data, $this->data); + } + + private function toArray(array &$data, Tag $tag){ + /** @var Compound[]|Enum[]|IntArray[] $tag */ + foreach($tag as $key => $value){ + if($value instanceof Compound or $value instanceof Enum or $value instanceof IntArray){ + $data[$key] = []; + $this->toArray($data[$key], $value); + }else{ + $data[$key] = $value->getValue(); + } + } + } + + private function fromArray(Tag $tag, array $data){ + foreach($data as $key => $value){ + if(is_array($value)){ + $isNumeric = true; + $isIntArray = true; + foreach($value as $k => $v){ + if(!is_numeric($k)){ + $isNumeric = false; + break; + }elseif(!is_int($v)){ + $isIntArray = false; + } + } + $tag{$key} = $isNumeric ? ($isIntArray ? new IntArray($key, []) : new Enum($key, [])) : new Compound($key, []); + $this->fromArray($tag->{$key}, $value); + }elseif(is_int($value)){ + $tag{$key} = new Int($key, $value); + }elseif(is_float($value)){ + $tag{$key} = new Float($key, $value); + }elseif(is_string($value)){ + if(Utils::printable($value) !== $value){ + $tag{$key} = new ByteArray($key, $value); + }else{ + $tag{$key} = new String($key, $value); + } + }elseif(is_bool($value)){ + $tag{$key} = new Byte($key, $value ? 1 : 0); + } + } + } + + public function setArray(array $data){ + $this->data = new Compound(null, []); + $this->fromArray($this->data, $data); + } + public function getData(){ return $this->data; } From 43a0ef433eecda51b4d3120a4142397c140b1078 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Thu, 28 Aug 2014 23:44:19 +0200 Subject: [PATCH 2/2] API version bump --- src/pocketmine/PocketMine.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pocketmine/PocketMine.php b/src/pocketmine/PocketMine.php index 3f5cd24fc..4de8fe6a3 100644 --- a/src/pocketmine/PocketMine.php +++ b/src/pocketmine/PocketMine.php @@ -73,7 +73,7 @@ namespace pocketmine { use raklib\RakLib; const VERSION = "Alpha_1.4dev"; - const API_VERSION = "1.3.1"; + const API_VERSION = "1.4.0"; const CODENAME = "絶好(Zekkou)ケーキ(Cake)"; const MINECRAFT_VERSION = "v0.9.5 alpha";