Made block and item factory lists private to stop people doing stupid things with them

This commit is contained in:
Dylan K. Taylor 2017-08-29 10:52:50 +01:00
parent 9902d29734
commit 6abef6b22d
4 changed files with 20 additions and 17 deletions

View File

@ -31,9 +31,10 @@ use pocketmine\level\Position;
*/
class BlockFactory{
/** @var \SplFixedArray<Block> */
public static $list = null;
private static $list = null;
/** @var \SplFixedArray<Block> */
public static $fullList = null;
private static $fullList = null;
/** @var \SplFixedArray<bool> */
public static $solid = null;
/** @var \SplFixedArray<bool> */
@ -59,6 +60,7 @@ class BlockFactory{
if(self::$list === null or $force){
self::$list = new \SplFixedArray(256);
self::$fullList = new \SplFixedArray(4096);
self::$light = new \SplFixedArray(256);
self::$lightFilter = new \SplFixedArray(256);
self::$solid = new \SplFixedArray(256);
@ -393,4 +395,12 @@ class BlockFactory{
return $block;
}
/**
* @internal
* @return \SplFixedArray
*/
public static function getBlockStatesArray() : \SplFixedArray{
return self::$fullList;
}
}

View File

@ -202,7 +202,7 @@ class Item implements ItemIds, \JsonSerializable{
$this->id = $id & 0xffff;
$this->meta = $meta !== -1 ? $meta & 0xffff : -1;
$this->name = $name;
if(!isset($this->block) and $this->id <= 0xff and isset(BlockFactory::$list[$this->id])){
if(!isset($this->block) and $this->id <= 0xff){
$this->block = BlockFactory::get($this->id, $this->meta);
$this->name = $this->block->getName();
}

View File

@ -32,7 +32,7 @@ use pocketmine\nbt\tag\CompoundTag;
class ItemFactory{
/** @var \SplFixedArray */
public static $list = null;
private static $list = null;
public static function init(){
if(self::$list === null){

View File

@ -272,7 +272,7 @@ class Level implements ChunkManager, Metadatable{
* @throws \Exception
*/
public function __construct(Server $server, string $name, string $path, string $provider){
$this->blockStates = BlockFactory::$fullList;
$this->blockStates = BlockFactory::getBlockStatesArray();
$this->levelId = static::$levelIdCounter++;
$this->blockMetadata = new BlockMetadataStore($this);
$this->server = $server;
@ -304,19 +304,12 @@ class Level implements ChunkManager, Metadatable{
$this->clearChunksOnTick = (bool) $this->server->getProperty("chunk-ticking.clear-tick-list", true);
$this->cacheChunks = (bool) $this->server->getProperty("chunk-sending.cache-chunks", false);
$this->randomTickBlocks = \SplFixedArray::fromArray(array_filter(BlockFactory::$list->toArray(), function(Block $block = null){
return $block !== null and $block->ticksRandomly();
}));
$this->randomTickBlocks->setSize(256);
$dontTickBlocks = $this->server->getProperty("chunk-ticking.disable-block-ticking", []);
foreach($dontTickBlocks as $id){
try{
if(isset($this->randomTickBlocks[$id])){
$this->randomTickBlocks[$id] = null;
}
}catch(\RuntimeException $e){
//index out of bounds
$this->randomTickBlocks = new \SplFixedArray(256);
foreach($this->randomTickBlocks as $id => $null){
$block = BlockFactory::get($id); //Make sure it's a copy
if(!isset($dontTickBlocks[$id]) and $block->ticksRandomly()){
$this->randomTickBlocks[$id] = $block;
}
}