From 84c63c48ca85341571ed19484425dc98173a6503 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Mon, 25 Aug 2014 16:28:46 +0200 Subject: [PATCH] Improved NBT IntArray read/write --- src/pocketmine/level/Level.php | 4 +--- src/pocketmine/nbt/tag/IntArray.php | 31 ++++------------------------- 2 files changed, 5 insertions(+), 30 deletions(-) diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 77f34ddf4..3550d03f8 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -190,9 +190,7 @@ class Level implements ChunkManager, Metadatable{ } public static function getXZ($hash, &$x, &$z){ - $d = explode(":", $hash); - $x = (int) $d[0]; - $z = (int) $d[1]; + list($x, $z) = explode(":", $hash); } /** diff --git a/src/pocketmine/nbt/tag/IntArray.php b/src/pocketmine/nbt/tag/IntArray.php index bc9e8bd3f..7463c6b0c 100644 --- a/src/pocketmine/nbt/tag/IntArray.php +++ b/src/pocketmine/nbt/tag/IntArray.php @@ -33,36 +33,13 @@ class IntArray extends NamedTag{ public function read(NBT $nbt){ $this->value = []; $size = $nbt->getInt(); - $ints = $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; - } - } - + $this->value = unpack($nbt->endianness === NBT::LITTLE_ENDIAN ? "V*" : "N*", $nbt->get($size * 4)); } public function write(NBT $nbt){ $nbt->putInt(count($this->value)); - - $ints = ""; - if($nbt->endianness === NBT::LITTLE_ENDIAN){ - foreach($this->value as $v){ - $ints .= Binary::writeLInt($v); - } - }else{ - foreach($this->value as $v){ - $ints .= Binary::writeInt($v); - } - } - - $nbt->put($ints); + $ints = $this->value; + array_unshift($ints, $nbt->endianness === NBT::LITTLE_ENDIAN ? "V*" : "N*"); + $nbt->put(call_user_func_array("pack", $ints)); } } \ No newline at end of file