Improved NBT IntArray read/write

This commit is contained in:
Shoghi Cervantes 2014-08-25 16:28:46 +02:00
parent 20e11bd408
commit 84c63c48ca
2 changed files with 5 additions and 30 deletions

View File

@ -190,9 +190,7 @@ class Level implements ChunkManager, Metadatable{
} }
public static function getXZ($hash, &$x, &$z){ public static function getXZ($hash, &$x, &$z){
$d = explode(":", $hash); list($x, $z) = explode(":", $hash);
$x = (int) $d[0];
$z = (int) $d[1];
} }
/** /**

View File

@ -33,36 +33,13 @@ class IntArray extends NamedTag{
public function read(NBT $nbt){ public function read(NBT $nbt){
$this->value = []; $this->value = [];
$size = $nbt->getInt(); $size = $nbt->getInt();
$ints = $nbt->get($size * 4); $this->value = unpack($nbt->endianness === NBT::LITTLE_ENDIAN ? "V*" : "N*", $nbt->get($size * 4));
$offset = 0;
if($nbt->endianness === NBT::LITTLE_ENDIAN){
for($i = 0; $i < $size and isset($ints{$offset}); ++$i){
$this->value[$i] = Binary::readLInt(substr($ints, $offset, 4));
$offset += 4;
}
}else{
for($i = 0; $i < $size and isset($ints{$offset}); ++$i){
$this->value[$i] = Binary::readInt(substr($ints, $offset, 4));
$offset += 4;
}
}
} }
public function write(NBT $nbt){ public function write(NBT $nbt){
$nbt->putInt(count($this->value)); $nbt->putInt(count($this->value));
$ints = $this->value;
$ints = ""; array_unshift($ints, $nbt->endianness === NBT::LITTLE_ENDIAN ? "V*" : "N*");
if($nbt->endianness === NBT::LITTLE_ENDIAN){ $nbt->put(call_user_func_array("pack", $ints));
foreach($this->value as $v){
$ints .= Binary::writeLInt($v);
}
}else{
foreach($this->value as $v){
$ints .= Binary::writeInt($v);
}
}
$nbt->put($ints);
} }
} }