Cleaned up SubChunk implementation

This commit is contained in:
Dylan K. Taylor 2017-06-19 15:40:31 +01:00
parent 938452bfe9
commit e11f1e94e9
8 changed files with 228 additions and 29 deletions

View File

@ -54,7 +54,7 @@ class Chunk{
protected $height = Chunk::MAX_SUBCHUNKS;
/** @var SubChunk[] */
/** @var SubChunkInterface[] */
protected $subChunks = [];
/** @var EmptySubChunk */
@ -84,7 +84,7 @@ class Chunk{
/**
* @param int $chunkX
* @param int $chunkZ
* @param SubChunk[] $subChunks
* @param SubChunkInterface[] $subChunks
* @param CompoundTag[] $entities
* @param CompoundTag[] $tiles
* @param string $biomeIds
@ -514,7 +514,7 @@ class Chunk{
public function getBlockSkyLightColumn(int $x, int $z) : string{
$result = "";
foreach($this->subChunks as $subChunk){
$result .= $subChunk->getSkyLightColumn($x, $z);
$result .= $subChunk->getBlockSkyLightColumn($x, $z);
}
return $result;
}
@ -797,9 +797,9 @@ class Chunk{
* @param int $y
* @param bool $generateNew Whether to create a new, modifiable subchunk if there is not one in place
*
* @return SubChunk|EmptySubChunk
* @return SubChunkInterface
*/
public function getSubChunk(int $y, bool $generateNew = false) : SubChunk{
public function getSubChunk(int $y, bool $generateNew = false) : SubChunkInterface{
if($y < 0 or $y >= $this->height){
return $this->emptySubChunk;
}elseif($generateNew and $this->subChunks[$y] instanceof EmptySubChunk){
@ -811,13 +811,14 @@ class Chunk{
/**
* Sets a subchunk in the chunk index
*
* @param int $y
* @param SubChunk|null $subChunk
* @param SubChunkInterface|null $subChunk
* @param bool $allowEmpty Whether to check if the chunk is empty, and if so replace it with an empty stub
*
* @return bool
*/
public function setSubChunk(int $y, SubChunk $subChunk = null, bool $allowEmpty = false) : bool{
public function setSubChunk(int $y, SubChunkInterface $subChunk = null, bool $allowEmpty = false) : bool{
if($y < 0 or $y >= $this->height){
return false;
}

View File

@ -23,11 +23,7 @@ declare(strict_types=1);
namespace pocketmine\level\format;
class EmptySubChunk extends SubChunk{
public function __construct(){
}
class EmptySubChunk implements SubChunkInterface{
public function isEmpty() : bool{
return true;
@ -73,6 +69,10 @@ class EmptySubChunk extends SubChunk{
return false;
}
public function getHighestBlockAt(int $x, int $z) : int{
return -1;
}
public function getBlockIdColumn(int $x, int $z) : string{
return "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
}
@ -85,7 +85,7 @@ class EmptySubChunk extends SubChunk{
return "\x00\x00\x00\x00\x00\x00\x00\x00";
}
public function getSkyLightColumn(int $x, int $z) : string{
public function getBlockSkyLightColumn(int $x, int $z) : string{
return "\xff\xff\xff\xff\xff\xff\xff\xff";
}
@ -101,7 +101,7 @@ class EmptySubChunk extends SubChunk{
return str_repeat("\x00", 2048);
}
public function getSkyLightArray() : string{
public function getBlockSkyLightArray() : string{
return str_repeat("\xff", 2048);
}

View File

@ -23,7 +23,7 @@ declare(strict_types=1);
namespace pocketmine\level\format;
class SubChunk{
class SubChunk implements SubChunkInterface{
protected $ids;
protected $data;
@ -182,7 +182,7 @@ class SubChunk{
return substr($this->blockLight, (($x << 7) | ($z << 3)), 8);
}
public function getSkyLightColumn(int $x, int $z) : string{
public function getBlockSkyLightColumn(int $x, int $z) : string{
return substr($this->skyLight, (($x << 7) | ($z << 3)), 8);
}
@ -196,7 +196,7 @@ class SubChunk{
return $this->data;
}
public function getSkyLightArray() : string{
public function getBlockSkyLightArray() : string{
assert(strlen($this->skyLight) === 2048, "Wrong length of skylight array, expecting 2048 bytes, got " . strlen($this->skyLight));
return $this->skyLight;
}

View File

@ -0,0 +1,198 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\level\format;
interface SubChunkInterface{
/**
* @return bool
*/
public function isEmpty() : bool;
/**
* @param int $x
* @param int $y
* @param int $z
*
* @return int
*/
public function getBlockId(int $x, int $y, int $z) : int;
/**
* @param int $x
* @param int $y
* @param int $z
* @param int $id
*
* @return bool
*/
public function setBlockId(int $x, int $y, int $z, int $id) : bool;
/**
* @param int $x
* @param int $y
* @param int $z
*
* @return int
*/
public function getBlockData(int $x, int $y, int $z) : int;
/**
* @param int $x
* @param int $y
* @param int $z
* @param int $data
*
* @return bool
*/
public function setBlockData(int $x, int $y, int $z, int $data) : bool;
/**
* @param int $x
* @param int $y
* @param int $z
*
* @return int
*/
public function getFullBlock(int $x, int $y, int $z) : int;
/**
* @param int $x
* @param int $y
* @param int $z
* @param int|null $id
* @param int|null $data
*
* @return bool
*/
public function setBlock(int $x, int $y, int $z, $id = null, $data = null) : bool;
/**
* @param int $x
* @param int $y
* @param int $z
*
* @return int
*/
public function getBlockLight(int $x, int $y, int $z) : int;
/**
* @param int $x
* @param int $y
* @param int $z
* @param int $level
*
* @return bool
*/
public function setBlockLight(int $x, int $y, int $z, int $level) : bool;
/**
* @param int $x
* @param int $y
* @param int $z
*
* @return int
*/
public function getBlockSkyLight(int $x, int $y, int $z) : int;
/**
* @param int $x
* @param int $y
* @param int $z
* @param int $level
*
* @return bool
*/
public function setBlockSkyLight(int $x, int $y, int $z, int $level) : bool;
/**
* @param int $x
* @param int $z
*
* @return int
*/
public function getHighestBlockAt(int $x, int $z) : int;
/**
* @param int $x
* @param int $z
*
* @return string
*/
public function getBlockIdColumn(int $x, int $z) : string;
/**
* @param int $x
* @param int $z
*
* @return string
*/
public function getBlockDataColumn(int $x, int $z) : string;
/**
* @param int $x
* @param int $z
*
* @return string
*/
public function getBlockLightColumn(int $x, int $z) : string;
/**
* @param int $x
* @param int $z
*
* @return string
*/
public function getBlockSkyLightColumn(int $x, int $z) : string;
/**
* @return string
*/
public function getBlockIdArray() : string;
/**
* @return string
*/
public function getBlockDataArray() : string;
/**
* @return string
*/
public function getBlockSkyLightArray() : string;
/**
* @return string
*/
public function getBlockLightArray() : string;
/**
* @return string
*/
public function networkSerialize() : string;
/**
* @return string
*/
public function fastSerialize() : string;
}

View File

@ -437,7 +437,7 @@ class LevelDB extends BaseLevelProvider{
"\x00" . //Subchunk version byte
$subChunks[$y]->getBlockIdArray() .
$subChunks[$y]->getBlockDataArray() .
$subChunks[$y]->getSkyLightArray() .
$subChunks[$y]->getBlockSkyLightArray() .
$subChunks[$y]->getBlockLightArray()
);
}

View File

@ -60,7 +60,7 @@ class Anvil extends McRegion{
"Y" => new ByteTag("Y", $y),
"Blocks" => new ByteArrayTag("Blocks", ChunkUtils::reorderByteArray($subChunk->getBlockIdArray())), //Generic in-memory chunks are currently always XZY
"Data" => new ByteArrayTag("Data", ChunkUtils::reorderNibbleArray($subChunk->getBlockDataArray())),
"SkyLight" => new ByteArrayTag("SkyLight", ChunkUtils::reorderNibbleArray($subChunk->getSkyLightArray(), "\xff")),
"SkyLight" => new ByteArrayTag("SkyLight", ChunkUtils::reorderNibbleArray($subChunk->getBlockSkyLightArray(), "\xff")),
"BlockLight" => new ByteArrayTag("BlockLight", ChunkUtils::reorderNibbleArray($subChunk->getBlockLightArray()))
]);
}

View File

@ -72,7 +72,7 @@ class McRegion extends BaseLevelProvider{
$subChunk = $subChunks[$y];
$ids .= $subChunk->getBlockIdColumn($x, $z);
$data .= $subChunk->getBlockDataColumn($x, $z);
$skyLight .= $subChunk->getSkyLightColumn($x, $z);
$skyLight .= $subChunk->getBlockSkyLightColumn($x, $z);
$blockLight .= $subChunk->getBlockLightColumn($x, $z);
}
}

View File

@ -63,7 +63,7 @@ class PMAnvil extends Anvil{
"Y" => new ByteTag("Y", $y),
"Blocks" => new ByteArrayTag("Blocks", $subChunk->getBlockIdArray()),
"Data" => new ByteArrayTag("Data", $subChunk->getBlockDataArray()),
"SkyLight" => new ByteArrayTag("SkyLight", $subChunk->getSkyLightArray()),
"SkyLight" => new ByteArrayTag("SkyLight", $subChunk->getBlockSkyLightArray()),
"BlockLight" => new ByteArrayTag("BlockLight", $subChunk->getBlockLightArray())
]);
}