diff --git a/src/pocketmine/level/format/FullChunk.php b/src/pocketmine/level/format/FullChunk.php index 1791e2689..97c9244c8 100644 --- a/src/pocketmine/level/format/FullChunk.php +++ b/src/pocketmine/level/format/FullChunk.php @@ -41,9 +41,14 @@ interface FullChunk{ public function setZ($z); /** - * @return \pocketmine\level\format\LevelProvider + * @return LevelProvider */ - public function getLevel(); + public function getProvider(); + + /** + * @param LevelProvider $provider + */ + public function setProvider(LevelProvider $provider); /** diff --git a/src/pocketmine/level/format/anvil/Anvil.php b/src/pocketmine/level/format/anvil/Anvil.php index a8af132dc..b51c19501 100644 --- a/src/pocketmine/level/format/anvil/Anvil.php +++ b/src/pocketmine/level/format/anvil/Anvil.php @@ -95,6 +95,8 @@ class Anvil extends McRegion{ throw new \Exception("Invalid Chunk class"); } + $chunk->setProvider($this); + if($chunk->isPopulated() === false){ $this->unloadChunk($chunkX, $chunkZ, false); $regionX = $regionZ = null; diff --git a/src/pocketmine/level/format/generic/BaseChunk.php b/src/pocketmine/level/format/generic/BaseChunk.php index 50e43c87e..a89438fcb 100644 --- a/src/pocketmine/level/format/generic/BaseChunk.php +++ b/src/pocketmine/level/format/generic/BaseChunk.php @@ -59,7 +59,7 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{ protected $z; /** - * @param LevelProvider $level + * @param LevelProvider $provider * @param int $x * @param int $z * @param ChunkSection[] $sections @@ -70,8 +70,8 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{ * * @throws \Exception */ - protected function __construct($level, $x, $z, array $sections, $biomeIds = null, array $biomeColors = [], array $entities = [], array $tiles = []){ - $this->level = $level instanceof LevelProvider ? new \WeakRef($level) : $level; + protected function __construct($provider, $x, $z, array $sections, $biomeIds = null, array $biomeColors = [], array $entities = [], array $tiles = []){ + $this->provider = $provider; $this->x = (int) $x; $this->z = (int) $z; foreach($sections as $Y => $section){ @@ -99,8 +99,8 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{ $this->biomeColors = array_fill(0, 256, Binary::readInt("\x00\x85\xb2\x4a")); } - if($this->getLevel() instanceof LevelProvider){ - $this->getLevel()->getLevel()->timings->syncChunkLoadEntitiesTimer->startTiming(); + if($this->provider instanceof LevelProvider){ + $this->provider->getLevel()->timings->syncChunkLoadEntitiesTimer->startTiming(); foreach($entities as $nbt){ if($nbt instanceof Compound){ if(!isset($nbt->id)){ @@ -118,9 +118,9 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{ } } } - $this->getLevel()->getLevel()->timings->syncChunkLoadEntitiesTimer->stopTiming(); + $this->getProvider()->getLevel()->timings->syncChunkLoadEntitiesTimer->stopTiming(); - $this->getLevel()->getLevel()->timings->syncChunkLoadTileEntitiesTimer->startTiming(); + $this->getProvider()->getLevel()->timings->syncChunkLoadTileEntitiesTimer->startTiming(); foreach($tiles as $nbt){ if($nbt instanceof Compound){ if(!isset($nbt->id)){ @@ -139,7 +139,7 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{ } } } - $this->getLevel()->getLevel()->timings->syncChunkLoadTileEntitiesTimer->stopTiming(); + $this->getProvider()->getLevel()->timings->syncChunkLoadTileEntitiesTimer->stopTiming(); } } @@ -151,7 +151,7 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{ try{ return $this->sections[$y >> 4]->setBlock($x, $y & 0x0f, $z, $blockId & 0xff, $meta & 0x0f); }catch(\Exception $e){ - $level = $this->getLevel(); + $level = $this->getProvider(); $this->setSection($Y = $y >> 4, $level::createChunkSection($Y)); return $this->setBlock($x, $y, $z, $blockId, $meta); } @@ -165,7 +165,7 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{ try{ $this->sections[$y >> 4]->setBlockId($x, $y & 0x0f, $z, $id); }catch(\Exception $e){ - $level = $this->getLevel(); + $level = $this->getProvider(); $this->setSection($Y = $y >> 4, $level::createChunkSection($Y)); $this->setBlockId($x, $y, $z, $id); } @@ -179,7 +179,7 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{ try{ $this->sections[$y >> 4]->setBlockData($x, $y & 0x0f, $z, $data); }catch(\Exception $e){ - $level = $this->getLevel(); + $level = $this->getProvider(); $this->setSection($Y = $y >> 4, $level::createChunkSection($Y)); $this->setBlockData($x, $y, $z, $data); } @@ -193,7 +193,7 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{ try{ $this->sections[$y >> 4]->getBlockSkyLight($x, $y & 0x0f, $z, $data); }catch(\Exception $e){ - $level = $this->getLevel(); + $level = $this->getProvider(); $this->setSection($Y = $y >> 4, $level::createChunkSection($Y)); $this->setBlockSkyLight($x, $y, $z, $data); } @@ -207,7 +207,7 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{ try{ $this->sections[$y >> 4]->getBlockSkyLight($x, $y & 0x0f, $z, $data); }catch(\Exception $e){ - $level = $this->getLevel(); + $level = $this->getProvider(); $this->setSection($Y = $y >> 4, $level::createChunkSection($Y)); $this->setBlockLight($x, $y, $z, $data); } @@ -262,7 +262,7 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{ } public function load($generate = true){ - return $this->getLevel() === null ? false : $this->getLevel()->getChunk($this->getX(), $this->getZ(), true) instanceof Chunk; + return $this->getProvider() === null ? false : $this->getProvider()->getChunk($this->getX(), $this->getZ(), true) instanceof Chunk; } public function getBlockIdArray(){ diff --git a/src/pocketmine/level/format/generic/BaseFullChunk.php b/src/pocketmine/level/format/generic/BaseFullChunk.php index 232abae55..690920fb5 100644 --- a/src/pocketmine/level/format/generic/BaseFullChunk.php +++ b/src/pocketmine/level/format/generic/BaseFullChunk.php @@ -55,14 +55,14 @@ abstract class BaseFullChunk implements FullChunk{ protected $blockLight; - /** @var \WeakRef */ - protected $level; + /** @var LevelProvider */ + protected $provider; protected $x; protected $z; /** - * @param LevelProvider $level + * @param LevelProvider $provider * @param int $x * @param int $z * @param string $blocks @@ -76,8 +76,8 @@ abstract class BaseFullChunk implements FullChunk{ * * @throws \Exception */ - protected function __construct($level, $x, $z, $blocks, $data, $skyLight, $blockLight, $biomeIds = null, array $biomeColors = [], array $entities = [], array $tiles = []){ - $this->level = $level instanceof LevelProvider ? new \WeakRef($level) : $level; + protected function __construct($provider, $x, $z, $blocks, $data, $skyLight, $blockLight, $biomeIds = null, array $biomeColors = [], array $entities = [], array $tiles = []){ + $this->provider = $provider; $this->x = (int) $x; $this->z = (int) $z; @@ -98,8 +98,8 @@ abstract class BaseFullChunk implements FullChunk{ $this->biomeColors = array_fill(0, 256, Binary::readInt("\x00\x85\xb2\x4a")); } - if($this->getLevel() instanceof LevelProvider){ - $this->getLevel()->getLevel()->timings->syncChunkLoadEntitiesTimer->startTiming(); + if($this->getProvider() instanceof LevelProvider){ + $this->getProvider()->getLevel()->timings->syncChunkLoadEntitiesTimer->startTiming(); foreach($entities as $nbt){ if($nbt instanceof Compound){ if(!isset($nbt->id)){ @@ -117,9 +117,9 @@ abstract class BaseFullChunk implements FullChunk{ } } } - $this->getLevel()->getLevel()->timings->syncChunkLoadEntitiesTimer->stopTiming(); + $this->getProvider()->getLevel()->timings->syncChunkLoadEntitiesTimer->stopTiming(); - $this->getLevel()->getLevel()->timings->syncChunkLoadTileEntitiesTimer->startTiming(); + $this->getProvider()->getLevel()->timings->syncChunkLoadTileEntitiesTimer->startTiming(); foreach($tiles as $nbt){ if($nbt instanceof Compound){ if(!isset($nbt->id)){ @@ -138,7 +138,7 @@ abstract class BaseFullChunk implements FullChunk{ } } } - $this->getLevel()->getLevel()->timings->syncChunkLoadTileEntitiesTimer->stopTiming(); + $this->getProvider()->getLevel()->timings->syncChunkLoadTileEntitiesTimer->stopTiming(); } } @@ -160,9 +160,22 @@ abstract class BaseFullChunk implements FullChunk{ /** * @return LevelProvider + * + * @deprecated */ public function getLevel(){ - return $this->level instanceof \WeakRef ? ($this->level->valid() ? $this->level->get() : null) : $this->level; + return $this->getProvider(); + } + + /** + * @return LevelProvider + */ + public function getProvider(){ + return $this->provider; + } + + public function setProvider(LevelProvider $provider){ + $this->provider = $provider; } public function getBiomeId($x, $z){ @@ -218,22 +231,22 @@ abstract class BaseFullChunk implements FullChunk{ } public function isLoaded(){ - return $this->getLevel() === null ? false : $this->getLevel()->isChunkLoaded($this->getX(), $this->getZ()); + return $this->getProvider() === null ? false : $this->getProvider()->isChunkLoaded($this->getX(), $this->getZ()); } public function load($generate = true){ - return $this->getLevel() === null ? false : $this->getLevel()->getChunk($this->getX(), $this->getZ(), true) instanceof FullChunk; + return $this->getProvider() === null ? false : $this->getProvider()->getChunk($this->getX(), $this->getZ(), true) instanceof FullChunk; } public function unload($save = true, $safe = true){ - $level = $this->getLevel(); + $level = $this->getProvider(); if($level === null){ return true; } if($save === true){ $level->saveChunk($this->getX(), $this->getZ()); } - if($this->getLevel()->unloadChunk($this->getX(), $this->getZ(), $safe)){ + if($this->getProvider()->unloadChunk($this->getX(), $this->getZ(), $safe)){ foreach($this->getEntities() as $entity){ $entity->close(); } diff --git a/src/pocketmine/level/format/mcregion/McRegion.php b/src/pocketmine/level/format/mcregion/McRegion.php index 7fca5c3b0..472951352 100644 --- a/src/pocketmine/level/format/mcregion/McRegion.php +++ b/src/pocketmine/level/format/mcregion/McRegion.php @@ -223,6 +223,8 @@ class McRegion extends BaseLevelProvider{ throw new \Exception("Invalid Chunk class"); } + $chunk->setProvider($this); + if($chunk->isPopulated() === false){ $this->unloadChunk($chunkX, $chunkZ, false); $regionX = $regionZ = null;