From 866fde5351c90b7d07e1b58d456c1c5038f09e7f Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Sat, 11 Jul 2015 12:03:40 +0200 Subject: [PATCH] Improved speed of some int arrays, fixed block metadata --- src/pocketmine/block/Block.php | 10 +++++++++- src/pocketmine/level/format/mcregion/Chunk.php | 18 ++++-------------- src/pocketmine/nbt/tag/IntArray.php | 7 ++----- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/pocketmine/block/Block.php b/src/pocketmine/block/Block.php index bf2ed20b6..a36c35256 100644 --- a/src/pocketmine/block/Block.php +++ b/src/pocketmine/block/Block.php @@ -862,6 +862,12 @@ class Block extends Position implements Metadatable{ ); } + /** + * @param Vector3 $pos1 + * @param Vector3 $pos2 + * + * @return MovingObjectPosition + */ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2){ $bb = $this->getBoundingBox(); if($bb === null){ @@ -952,8 +958,10 @@ class Block extends Position implements Metadatable{ public function getMetadata($metadataKey){ if($this->getLevel() instanceof Level){ - $this->getLevel()->getBlockMetadata()->getMetadata($this, $metadataKey); + return $this->getLevel()->getBlockMetadata()->getMetadata($this, $metadataKey); } + + return null; } public function hasMetadata($metadataKey){ diff --git a/src/pocketmine/level/format/mcregion/Chunk.php b/src/pocketmine/level/format/mcregion/Chunk.php index 9eed48087..be2356af1 100644 --- a/src/pocketmine/level/format/mcregion/Chunk.php +++ b/src/pocketmine/level/format/mcregion/Chunk.php @@ -327,18 +327,11 @@ class Chunk extends BaseFullChunk{ $chunk->blockLight = substr($data, $offset, 16384); $offset += 16384; - $chunk->heightMap = []; - $chunk->biomeColors = []; - $hm = unpack("C*", substr($data, $offset, 256)); + $chunk->heightMap = array_values(unpack("C*", substr($data, $offset, 256))); $offset += 256; - $bc = unpack("N*", substr($data, $offset, 1024)); + $chunk->biomeColors = array_values(unpack("N*", substr($data, $offset, 1024))); $offset += 1024; - for($i = 0; $i < 256; ++$i){ - $chunk->biomeColors[$i] = $bc[$i + 1]; - $chunk->heightMap[$i] = $hm[$i + 1]; - } - $flags = ord($data{$offset++}); $chunk->nbt->TerrainGenerated = new Byte("TerrainGenerated", $flags & 0b1); @@ -352,9 +345,6 @@ class Chunk extends BaseFullChunk{ } public function toFastBinary(){ - $heightMap = pack("C*", ...$this->getHeightMapArray()); - $biomeColors = pack("N*", ...$this->getBiomeColorArray()); - return Binary::writeInt($this->x) . Binary::writeInt($this->z) . @@ -362,8 +352,8 @@ class Chunk extends BaseFullChunk{ $this->getBlockDataArray() . $this->getBlockSkyLightArray() . $this->getBlockLightArray() . - $heightMap . - $biomeColors . + pack("C*", ...$this->getHeightMapArray()) . + pack("N*", ...$this->getBiomeColorArray()) . chr(($this->isLightPopulated() ? 1 << 2 : 0) + ($this->isPopulated() ? 1 << 1 : 0) + ($this->isGenerated() ? 1 : 0)); } diff --git a/src/pocketmine/nbt/tag/IntArray.php b/src/pocketmine/nbt/tag/IntArray.php index 6d6e13782..106fefd97 100644 --- a/src/pocketmine/nbt/tag/IntArray.php +++ b/src/pocketmine/nbt/tag/IntArray.php @@ -32,12 +32,9 @@ class IntArray extends NamedTag{ } public function read(NBT $nbt){ - $this->value = []; + []; $size = $nbt->getInt(); - $value = unpack($nbt->endianness === NBT::LITTLE_ENDIAN ? "V*" : "N*", $nbt->get($size * 4)); - foreach($value as $i => $v){ - $this->value[$i - 1] = $v; - } + $this->value = array_values(unpack($nbt->endianness === NBT::LITTLE_ENDIAN ? "V*" : "N*", $nbt->get($size * 4))); } public function write(NBT $nbt){