Merge pull request #1994 from PocketMine/nbt-array

Added NBT <-> PHP array/type conversion methods
This commit is contained in:
Shoghi Cervantes 2014-08-29 12:17:10 +02:00
commit 69b3ef326b
2 changed files with 55 additions and 1 deletions

View File

@ -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";

View File

@ -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;
}