From 6abef6b22d1495c4befc8b591c194a32eacf51cc Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 29 Aug 2017 10:52:50 +0100 Subject: [PATCH] Made block and item factory lists private to stop people doing stupid things with them --- src/pocketmine/block/BlockFactory.php | 14 ++++++++++++-- src/pocketmine/item/Item.php | 2 +- src/pocketmine/item/ItemFactory.php | 2 +- src/pocketmine/level/Level.php | 19 ++++++------------- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/pocketmine/block/BlockFactory.php b/src/pocketmine/block/BlockFactory.php index ec785a99d..2891407d2 100644 --- a/src/pocketmine/block/BlockFactory.php +++ b/src/pocketmine/block/BlockFactory.php @@ -31,9 +31,10 @@ use pocketmine\level\Position; */ class BlockFactory{ /** @var \SplFixedArray */ - public static $list = null; + private static $list = null; /** @var \SplFixedArray */ - public static $fullList = null; + private static $fullList = null; + /** @var \SplFixedArray */ public static $solid = null; /** @var \SplFixedArray */ @@ -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; + } } \ No newline at end of file diff --git a/src/pocketmine/item/Item.php b/src/pocketmine/item/Item.php index dc569b17a..0d90a6790 100644 --- a/src/pocketmine/item/Item.php +++ b/src/pocketmine/item/Item.php @@ -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(); } diff --git a/src/pocketmine/item/ItemFactory.php b/src/pocketmine/item/ItemFactory.php index c6ca3a8bd..79f059a91 100644 --- a/src/pocketmine/item/ItemFactory.php +++ b/src/pocketmine/item/ItemFactory.php @@ -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){ diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 57a51aa51..7f209cf30 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -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; } }