Modified Chunk provider saving

This commit is contained in:
Shoghi Cervantes 2014-08-06 16:22:12 +02:00
parent 2904aa06ef
commit 232bf5ebe4
5 changed files with 53 additions and 31 deletions

View File

@ -41,9 +41,14 @@ interface FullChunk{
public function setZ($z); 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);
/** /**

View File

@ -95,6 +95,8 @@ class Anvil extends McRegion{
throw new \Exception("Invalid Chunk class"); throw new \Exception("Invalid Chunk class");
} }
$chunk->setProvider($this);
if($chunk->isPopulated() === false){ if($chunk->isPopulated() === false){
$this->unloadChunk($chunkX, $chunkZ, false); $this->unloadChunk($chunkX, $chunkZ, false);
$regionX = $regionZ = null; $regionX = $regionZ = null;

View File

@ -59,7 +59,7 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{
protected $z; protected $z;
/** /**
* @param LevelProvider $level * @param LevelProvider $provider
* @param int $x * @param int $x
* @param int $z * @param int $z
* @param ChunkSection[] $sections * @param ChunkSection[] $sections
@ -70,8 +70,8 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{
* *
* @throws \Exception * @throws \Exception
*/ */
protected function __construct($level, $x, $z, array $sections, $biomeIds = null, array $biomeColors = [], array $entities = [], array $tiles = []){ protected function __construct($provider, $x, $z, array $sections, $biomeIds = null, array $biomeColors = [], array $entities = [], array $tiles = []){
$this->level = $level instanceof LevelProvider ? new \WeakRef($level) : $level; $this->provider = $provider;
$this->x = (int) $x; $this->x = (int) $x;
$this->z = (int) $z; $this->z = (int) $z;
foreach($sections as $Y => $section){ 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")); $this->biomeColors = array_fill(0, 256, Binary::readInt("\x00\x85\xb2\x4a"));
} }
if($this->getLevel() instanceof LevelProvider){ if($this->provider instanceof LevelProvider){
$this->getLevel()->getLevel()->timings->syncChunkLoadEntitiesTimer->startTiming(); $this->provider->getLevel()->timings->syncChunkLoadEntitiesTimer->startTiming();
foreach($entities as $nbt){ foreach($entities as $nbt){
if($nbt instanceof Compound){ if($nbt instanceof Compound){
if(!isset($nbt->id)){ 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){ foreach($tiles as $nbt){
if($nbt instanceof Compound){ if($nbt instanceof Compound){
if(!isset($nbt->id)){ 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{ try{
return $this->sections[$y >> 4]->setBlock($x, $y & 0x0f, $z, $blockId & 0xff, $meta & 0x0f); return $this->sections[$y >> 4]->setBlock($x, $y & 0x0f, $z, $blockId & 0xff, $meta & 0x0f);
}catch(\Exception $e){ }catch(\Exception $e){
$level = $this->getLevel(); $level = $this->getProvider();
$this->setSection($Y = $y >> 4, $level::createChunkSection($Y)); $this->setSection($Y = $y >> 4, $level::createChunkSection($Y));
return $this->setBlock($x, $y, $z, $blockId, $meta); return $this->setBlock($x, $y, $z, $blockId, $meta);
} }
@ -165,7 +165,7 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{
try{ try{
$this->sections[$y >> 4]->setBlockId($x, $y & 0x0f, $z, $id); $this->sections[$y >> 4]->setBlockId($x, $y & 0x0f, $z, $id);
}catch(\Exception $e){ }catch(\Exception $e){
$level = $this->getLevel(); $level = $this->getProvider();
$this->setSection($Y = $y >> 4, $level::createChunkSection($Y)); $this->setSection($Y = $y >> 4, $level::createChunkSection($Y));
$this->setBlockId($x, $y, $z, $id); $this->setBlockId($x, $y, $z, $id);
} }
@ -179,7 +179,7 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{
try{ try{
$this->sections[$y >> 4]->setBlockData($x, $y & 0x0f, $z, $data); $this->sections[$y >> 4]->setBlockData($x, $y & 0x0f, $z, $data);
}catch(\Exception $e){ }catch(\Exception $e){
$level = $this->getLevel(); $level = $this->getProvider();
$this->setSection($Y = $y >> 4, $level::createChunkSection($Y)); $this->setSection($Y = $y >> 4, $level::createChunkSection($Y));
$this->setBlockData($x, $y, $z, $data); $this->setBlockData($x, $y, $z, $data);
} }
@ -193,7 +193,7 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{
try{ try{
$this->sections[$y >> 4]->getBlockSkyLight($x, $y & 0x0f, $z, $data); $this->sections[$y >> 4]->getBlockSkyLight($x, $y & 0x0f, $z, $data);
}catch(\Exception $e){ }catch(\Exception $e){
$level = $this->getLevel(); $level = $this->getProvider();
$this->setSection($Y = $y >> 4, $level::createChunkSection($Y)); $this->setSection($Y = $y >> 4, $level::createChunkSection($Y));
$this->setBlockSkyLight($x, $y, $z, $data); $this->setBlockSkyLight($x, $y, $z, $data);
} }
@ -207,7 +207,7 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{
try{ try{
$this->sections[$y >> 4]->getBlockSkyLight($x, $y & 0x0f, $z, $data); $this->sections[$y >> 4]->getBlockSkyLight($x, $y & 0x0f, $z, $data);
}catch(\Exception $e){ }catch(\Exception $e){
$level = $this->getLevel(); $level = $this->getProvider();
$this->setSection($Y = $y >> 4, $level::createChunkSection($Y)); $this->setSection($Y = $y >> 4, $level::createChunkSection($Y));
$this->setBlockLight($x, $y, $z, $data); $this->setBlockLight($x, $y, $z, $data);
} }
@ -262,7 +262,7 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{
} }
public function load($generate = true){ 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(){ public function getBlockIdArray(){

View File

@ -55,14 +55,14 @@ abstract class BaseFullChunk implements FullChunk{
protected $blockLight; protected $blockLight;
/** @var \WeakRef<LevelProvider> */ /** @var LevelProvider */
protected $level; protected $provider;
protected $x; protected $x;
protected $z; protected $z;
/** /**
* @param LevelProvider $level * @param LevelProvider $provider
* @param int $x * @param int $x
* @param int $z * @param int $z
* @param string $blocks * @param string $blocks
@ -76,8 +76,8 @@ abstract class BaseFullChunk implements FullChunk{
* *
* @throws \Exception * @throws \Exception
*/ */
protected function __construct($level, $x, $z, $blocks, $data, $skyLight, $blockLight, $biomeIds = null, array $biomeColors = [], array $entities = [], array $tiles = []){ protected function __construct($provider, $x, $z, $blocks, $data, $skyLight, $blockLight, $biomeIds = null, array $biomeColors = [], array $entities = [], array $tiles = []){
$this->level = $level instanceof LevelProvider ? new \WeakRef($level) : $level; $this->provider = $provider;
$this->x = (int) $x; $this->x = (int) $x;
$this->z = (int) $z; $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")); $this->biomeColors = array_fill(0, 256, Binary::readInt("\x00\x85\xb2\x4a"));
} }
if($this->getLevel() instanceof LevelProvider){ if($this->getProvider() instanceof LevelProvider){
$this->getLevel()->getLevel()->timings->syncChunkLoadEntitiesTimer->startTiming(); $this->getProvider()->getLevel()->timings->syncChunkLoadEntitiesTimer->startTiming();
foreach($entities as $nbt){ foreach($entities as $nbt){
if($nbt instanceof Compound){ if($nbt instanceof Compound){
if(!isset($nbt->id)){ 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){ foreach($tiles as $nbt){
if($nbt instanceof Compound){ if($nbt instanceof Compound){
if(!isset($nbt->id)){ 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 * @return LevelProvider
*
* @deprecated
*/ */
public function getLevel(){ 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){ public function getBiomeId($x, $z){
@ -218,22 +231,22 @@ abstract class BaseFullChunk implements FullChunk{
} }
public function isLoaded(){ 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){ 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){ public function unload($save = true, $safe = true){
$level = $this->getLevel(); $level = $this->getProvider();
if($level === null){ if($level === null){
return true; return true;
} }
if($save === true){ if($save === true){
$level->saveChunk($this->getX(), $this->getZ()); $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){ foreach($this->getEntities() as $entity){
$entity->close(); $entity->close();
} }

View File

@ -223,6 +223,8 @@ class McRegion extends BaseLevelProvider{
throw new \Exception("Invalid Chunk class"); throw new \Exception("Invalid Chunk class");
} }
$chunk->setProvider($this);
if($chunk->isPopulated() === false){ if($chunk->isPopulated() === false){
$this->unloadChunk($chunkX, $chunkZ, false); $this->unloadChunk($chunkX, $chunkZ, false);
$regionX = $regionZ = null; $regionX = $regionZ = null;