Convert BlockFactory to singleton

This commit is contained in:
Dylan K. Taylor 2020-04-23 23:45:13 +01:00
parent accc0da0cb
commit 13d784cd0c
24 changed files with 1121 additions and 1121 deletions

View File

@ -27,7 +27,6 @@ declare(strict_types=1);
*/
namespace pocketmine;
use pocketmine\block\BlockFactory;
use pocketmine\command\CommandReader;
use pocketmine\command\CommandSender;
use pocketmine\command\ConsoleCommandSender;
@ -985,7 +984,6 @@ class Server{
$this->commandMap = new SimpleCommandMap($this);
EntityFactory::init();
BlockFactory::init();
Enchantment::init();
ItemFactory::init();
CreativeInventory::init();

File diff suppressed because it is too large Load Diff

View File

@ -54,7 +54,7 @@ class ConcretePowder extends Opaque implements Fallable{
continue;
}
if($this->getSide($i) instanceof Water){
return BlockFactory::get(BlockLegacyIds::CONCRETE, $this->idInfo->getVariant());
return BlockFactory::getInstance()->get(BlockLegacyIds::CONCRETE, $this->idInfo->getVariant());
}
}

File diff suppressed because it is too large Load Diff

View File

@ -44,7 +44,7 @@ class FlowerPot extends Spawnable{
public function readSaveData(CompoundTag $nbt) : void{
if($nbt->hasTag(self::TAG_ITEM, ShortTag::class) and $nbt->hasTag(self::TAG_ITEM_DATA, IntTag::class)){
try{
$this->setPlant(BlockFactory::get($nbt->getShort(self::TAG_ITEM), $nbt->getInt(self::TAG_ITEM_DATA)));
$this->setPlant(BlockFactory::getInstance()->get($nbt->getShort(self::TAG_ITEM), $nbt->getInt(self::TAG_ITEM_DATA)));
}catch(\InvalidArgumentException $e){
//noop
}

View File

@ -185,7 +185,7 @@ class ParticleCommand extends VanillaCommand{
break;
case "terrain":
if($data !== null and $data !== 0){
return new TerrainParticle(BlockFactory::get($data));
return new TerrainParticle(BlockFactory::getInstance()->get($data));
}
break;
case "heart":
@ -213,7 +213,7 @@ class ParticleCommand extends VanillaCommand{
}elseif(strpos($name, "blockcrack_") === 0){
$d = explode("_", $name);
if(count($d) === 2){
return new TerrainParticle(BlockFactory::get(((int) $d[1]) & 0xff, ((int) $d[1]) >> 12));
return new TerrainParticle(BlockFactory::getInstance()->get(((int) $d[1]) & 0xff, ((int) $d[1]) >> 12));
}
}elseif(strpos($name, "blockdust_") === 0){
$d = explode("_", $name);

View File

@ -71,7 +71,7 @@ class FallingBlock extends Entity{
$damage = $nbt->getByte("Data", 0);
$this->block = BlockFactory::get($blockId, $damage);
$this->block = BlockFactory::getInstance()->get($blockId, $damage);
}
public function canCollideWith(Entity $entity) : bool{

View File

@ -100,7 +100,7 @@ abstract class Projectile extends Entity{
break;
}
$this->blockHit = BlockFactory::get($blockId, $blockData);
$this->blockHit = BlockFactory::getInstance()->get($blockId, $blockData);
$this->blockHit->position($this->getWorld(), $blockPos->getFloorX(), $blockPos->getFloorY(), $blockPos->getFloorZ());
}while(false);
}

View File

@ -50,7 +50,7 @@ class ItemBlock extends Item{
}
public function getBlock() : Block{
return BlockFactory::get($this->blockId, $this->meta === -1 ? 0 : $this->meta & 0xf);
return BlockFactory::getInstance()->get($this->blockId, $this->meta === -1 ? 0 : $this->meta & 0xf);
}
public function getFuelTime() : int{

View File

@ -467,7 +467,7 @@ class ItemFactory{
*/
public static function isRegistered(int $id, int $variant = 0) : bool{
if($id < 256){
return BlockFactory::isRegistered($id);
return BlockFactory::getInstance()->isRegistered($id);
}
return isset(self::$list[self::getListOffset($id, $variant)]);

View File

@ -93,6 +93,7 @@ class Explosion{
}
$vector = new Vector3(0, 0, 0);
$blockFactory = BlockFactory::getInstance();
$currentChunk = null;
$currentSubChunk = null;
@ -127,10 +128,10 @@ class Explosion{
$state = $this->subChunkHandler->currentSubChunk->getFullBlock($vBlockX & 0x0f, $vBlockY & 0x0f, $vBlockZ & 0x0f);
if($state !== 0){
$blastForce -= (BlockFactory::$blastResistance[$state] / 5 + 0.3) * $this->stepLen;
$blastForce -= ($blockFactory->blastResistance[$state] / 5 + 0.3) * $this->stepLen;
if($blastForce > 0){
if(!isset($this->affectedBlocks[$index = World::blockHash($vBlockX, $vBlockY, $vBlockZ)])){
$_block = BlockFactory::fromFullBlock($state);
$_block = $blockFactory->fromFullBlock($state);
$_block->position($this->world, $vBlockX, $vBlockY, $vBlockZ);
$this->affectedBlocks[$index] = $_block;
}

View File

@ -51,7 +51,7 @@ class SimpleChunkManager implements ChunkManager{
public function getBlockAt(int $x, int $y, int $z) : Block{
if($this->terrainPointer->moveTo($x, $y, $z, false)){
return BlockFactory::fromFullBlock($this->terrainPointer->currentSubChunk->getFullBlock($x & 0xf, $y & 0xf, $z & 0xf));
return BlockFactory::getInstance()->fromFullBlock($this->terrainPointer->currentSubChunk->getFullBlock($x & 0xf, $y & 0xf, $z & 0xf));
}
return VanillaBlocks::AIR();
}

View File

@ -346,7 +346,7 @@ class World implements ChunkManager{
$dontTickBlocks = array_fill_keys($this->server->getProperty("chunk-ticking.disable-block-ticking", []), true);
foreach(BlockFactory::getAllKnownStates() as $state){
foreach(BlockFactory::getInstance()->getAllKnownStates() as $state){
if(!isset($dontTickBlocks[$state->getId()]) and $state->ticksRandomly()){
$this->randomTickBlocks[$state->getFullId()] = true;
}
@ -945,7 +945,7 @@ class World implements ChunkManager{
if(isset($this->randomTickBlocks[$state])){
/** @var Block $block */
$block = BlockFactory::fromFullBlock($state);
$block = BlockFactory::getInstance()->fromFullBlock($state);
$block->position($this, $chunkX * 16 + $x, ($Y << 4) + $y, $chunkZ * 16 + $z);
$block->onRandomTick();
}
@ -1303,7 +1303,7 @@ class World implements ChunkManager{
}
}
$block = BlockFactory::fromFullBlock($fullState);
$block = BlockFactory::getInstance()->fromFullBlock($fullState);
$block->position($this, $x, $y, $z);
static $dynamicStateRead = false;

View File

@ -23,7 +23,6 @@ declare(strict_types=1);
namespace pocketmine\world\generator;
use pocketmine\block\BlockFactory;
use pocketmine\scheduler\AsyncTask;
use pocketmine\world\biome\Biome;
use pocketmine\world\World;
@ -56,7 +55,6 @@ class GeneratorRegisterTask extends AsyncTask{
}
public function onRun() : void{
BlockFactory::init();
Biome::init();
$manager = new GeneratorChunkManager($this->worldHeight);
$this->worker->saveToThreadStore("generation.world{$this->worldId}.manager", $manager);

View File

@ -119,8 +119,9 @@ class PopulationTask extends AsyncTask{
$chunk = $manager->getChunk($chunk->getX(), $chunk->getZ());
$chunk->setPopulated();
$chunk->recalculateHeightMap(BlockFactory::$lightFilter, BlockFactory::$diffusesSkyLight);
$chunk->populateSkyLight(BlockFactory::$lightFilter);
$blockFactory = BlockFactory::getInstance();
$chunk->recalculateHeightMap($blockFactory->lightFilter, $blockFactory->diffusesSkyLight);
$chunk->populateSkyLight($blockFactory->lightFilter);
$chunk->setLightPopulated();
$this->chunk = FastChunkSerializer::serialize($chunk);

View File

@ -36,6 +36,7 @@ class GroundCover extends Populator{
public function populate(ChunkManager $world, int $chunkX, int $chunkZ, Random $random) : void{
$chunk = $world->getChunk($chunkX, $chunkZ);
$factory = BlockFactory::getInstance();
for($x = 0; $x < 16; ++$x){
for($z = 0; $z < 16; ++$z){
$biome = Biome::getBiome($chunk->getBiomeId($x, $z));
@ -48,7 +49,7 @@ class GroundCover extends Populator{
$startY = 127;
for(; $startY > 0; --$startY){
if(!BlockFactory::fromFullBlock($chunk->getFullBlock($x, $startY, $z))->isTransparent()){
if(!$factory->fromFullBlock($chunk->getFullBlock($x, $startY, $z))->isTransparent()){
break;
}
}
@ -56,7 +57,7 @@ class GroundCover extends Populator{
$endY = $startY - count($cover);
for($y = $startY; $y > $endY and $y >= 0; --$y){
$b = $cover[$startY - $y];
$id = BlockFactory::fromFullBlock($chunk->getFullBlock($x, $y, $z));
$id = $factory->fromFullBlock($chunk->getFullBlock($x, $y, $z));
if($id->getId() === BlockLegacyIds::AIR and $b->isSolid()){
break;
}

View File

@ -33,6 +33,6 @@ class BlockLightUpdate extends LightUpdate{
public function recalculateNode(int $x, int $y, int $z) : void{
$block = $this->world->getBlockAt($x, $y, $z);
$this->setAndUpdateLight($x, $y, $z, max($block->getLightLevel(), $this->getHighestAdjacentLight($x, $y, $z) - BlockFactory::$lightFilter[$block->getFullId()]));
$this->setAndUpdateLight($x, $y, $z, max($block->getLightLevel(), $this->getHighestAdjacentLight($x, $y, $z) - BlockFactory::getInstance()->lightFilter[$block->getFullId()]));
}
}

View File

@ -57,14 +57,12 @@ class LightPopulationTask extends AsyncTask{
}
public function onRun() : void{
if(!BlockFactory::isInit()){
BlockFactory::init();
}
/** @var Chunk $chunk */
$chunk = FastChunkSerializer::deserialize($this->chunk);
$chunk->recalculateHeightMap(BlockFactory::$lightFilter, BlockFactory::$diffusesSkyLight);
$chunk->populateSkyLight(BlockFactory::$lightFilter);
$blockFactory = BlockFactory::getInstance();
$chunk->recalculateHeightMap($blockFactory->lightFilter, $blockFactory->diffusesSkyLight);
$chunk->populateSkyLight($blockFactory->lightFilter);
$chunk->setLightPopulated();
$this->resultHeightMap = igbinary_serialize($chunk->getHeightMapArray());

View File

@ -188,7 +188,7 @@ abstract class LightUpdate{
protected function computeSpreadLight(int $x, int $y, int $z, int $newAdjacentLevel) : void{
$current = $this->currentLightArray->get($x & 0xf, $y & 0xf, $z & 0xf);
$potentialLight = $newAdjacentLevel - BlockFactory::$lightFilter[$this->subChunkHandler->currentSubChunk->getFullBlock($x & 0x0f, $y & 0x0f, $z & 0x0f)];
$potentialLight = $newAdjacentLevel - BlockFactory::getInstance()->lightFilter[$this->subChunkHandler->currentSubChunk->getFullBlock($x & 0x0f, $y & 0x0f, $z & 0x0f)];
if($current < $potentialLight){
$this->currentLightArray->set($x & 0xf, $y & 0xf, $z & 0xf, $potentialLight);

View File

@ -51,7 +51,7 @@ class SkyLightUpdate extends LightUpdate{
$yPlusOne = $y + 1;
if($yPlusOne === $oldHeightMap){ //Block changed directly beneath the heightmap. Check if a block was removed or changed to a different light-filter.
$newHeightMap = $chunk->recalculateHeightMapColumn($x & 0x0f, $z & 0x0f, BlockFactory::$lightFilter, BlockFactory::$diffusesSkyLight);
$newHeightMap = $chunk->recalculateHeightMapColumn($x & 0x0f, $z & 0x0f, BlockFactory::getInstance()->lightFilter, BlockFactory::getInstance()->diffusesSkyLight);
}elseif($yPlusOne > $oldHeightMap){ //Block changed above the heightmap.
if($source->getLightFilter() > 0 or $source->diffusesSkyLight()){
$chunk->setHeightMap($x & 0xf, $z & 0xf, $yPlusOne);
@ -72,7 +72,7 @@ class SkyLightUpdate extends LightUpdate{
$this->setAndUpdateLight($x, $i, $z, 15);
}
}else{ //No heightmap change, block changed "underground"
$this->setAndUpdateLight($x, $y, $z, max(0, $this->getHighestAdjacentLight($x, $y, $z) - BlockFactory::$lightFilter[$source->getFullId()]));
$this->setAndUpdateLight($x, $y, $z, max(0, $this->getHighestAdjacentLight($x, $y, $z) - BlockFactory::getInstance()->lightFilter[$source->getFullId()]));
}
}
}

View File

@ -29,8 +29,11 @@ use function json_decode;
class BlockTest extends TestCase{
/** @var BlockFactory */
private $blockFactory;
public function setUp() : void{
BlockFactory::init();
$this->blockFactory = new BlockFactory();
}
/**
@ -39,7 +42,7 @@ class BlockTest extends TestCase{
public function testAccidentalOverrideBlock() : void{
$block = new MyCustomBlock(new BlockIdentifier(BlockLegacyIds::COBBLESTONE), "Cobblestone", BlockBreakInfo::instant());
$this->expectException(\InvalidArgumentException::class);
BlockFactory::register($block);
$this->blockFactory->register($block);
}
/**
@ -47,8 +50,8 @@ class BlockTest extends TestCase{
*/
public function testDeliberateOverrideBlock() : void{
$block = new MyCustomBlock(new BlockIdentifier(BlockLegacyIds::COBBLESTONE), "Cobblestone", BlockBreakInfo::instant());
BlockFactory::register($block, true);
self::assertInstanceOf(MyCustomBlock::class, BlockFactory::get($block->getId()));
$this->blockFactory->register($block, true);
self::assertInstanceOf(MyCustomBlock::class, $this->blockFactory->get($block->getId()));
}
/**
@ -56,10 +59,10 @@ class BlockTest extends TestCase{
*/
public function testRegisterNewBlock() : void{
for($i = 0; $i < 256; ++$i){
if(!BlockFactory::isRegistered($i)){
if(!$this->blockFactory->isRegistered($i)){
$b = new StrangeNewBlock(new BlockIdentifier($i), "Strange New Block", BlockBreakInfo::instant());
BlockFactory::register($b);
self::assertInstanceOf(StrangeNewBlock::class, BlockFactory::get($b->getId()));
$this->blockFactory->register($b);
self::assertInstanceOf(StrangeNewBlock::class, $this->blockFactory->get($b->getId()));
return;
}
}
@ -72,7 +75,7 @@ class BlockTest extends TestCase{
*/
public function testRegisterIdTooLarge() : void{
self::expectException(\RuntimeException::class);
BlockFactory::register(new OutOfBoundsBlock(new BlockIdentifier(25555), "Out Of Bounds Block", BlockBreakInfo::instant()));
$this->blockFactory->register(new OutOfBoundsBlock(new BlockIdentifier(25555), "Out Of Bounds Block", BlockBreakInfo::instant()));
}
/**
@ -80,7 +83,7 @@ class BlockTest extends TestCase{
*/
public function testRegisterIdTooSmall() : void{
self::expectException(\RuntimeException::class);
BlockFactory::register(new OutOfBoundsBlock(new BlockIdentifier(-1), "Out Of Bounds Block", BlockBreakInfo::instant()));
$this->blockFactory->register(new OutOfBoundsBlock(new BlockIdentifier(-1), "Out Of Bounds Block", BlockBreakInfo::instant()));
}
/**
@ -90,8 +93,8 @@ class BlockTest extends TestCase{
*/
public function testBlockFactoryClone() : void{
for($i = 0; $i < 256; ++$i){
$b1 = BlockFactory::get($i);
$b2 = BlockFactory::get($i);
$b1 = $this->blockFactory->get($i);
$b2 = $this->blockFactory->get($i);
self::assertNotSame($b1, $b2);
}
}
@ -117,7 +120,7 @@ class BlockTest extends TestCase{
* @param int $meta
*/
public function testBlockGet(int $id, int $meta) : void{
$block = BlockFactory::get($id, $meta);
$block = $this->blockFactory->get($id, $meta);
self::assertEquals($id, $block->getId());
self::assertEquals($meta, $block->getMeta());
@ -125,7 +128,7 @@ class BlockTest extends TestCase{
public function testBlockIds() : void{
for($i = 0; $i < 256; ++$i){
$b = BlockFactory::get($i);
$b = $this->blockFactory->get($i);
self::assertContains($i, $b->getIdInfo()->getAllBlockIds());
}
}
@ -135,7 +138,7 @@ class BlockTest extends TestCase{
* (like freezes) when doing light population.
*/
public function testLightFiltersValid() : void{
foreach(BlockFactory::$lightFilter as $id => $value){
foreach($this->blockFactory->lightFilter as $id => $value){
self::assertNotNull($value, "Light filter value missing for $id");
self::assertLessThanOrEqual(15, $value, "Light filter value for $id is larger than the expected 15");
self::assertGreaterThan(0, $value, "Light filter value for $id must be larger than 0");
@ -144,7 +147,7 @@ class BlockTest extends TestCase{
public function testConsistency() : void{
$list = json_decode(file_get_contents(__DIR__ . '/block_factory_consistency_check.json'), true);
$states = BlockFactory::getAllKnownStates();
$states = $this->blockFactory->getAllKnownStates();
foreach($states as $k => $state){
self::assertArrayHasKey($k, $list, "New block state $k (" . $state->getName() . ") - consistency check may need regenerating");
self::assertSame($list[$k], $state->getName());

View File

@ -25,14 +25,14 @@ require dirname(__DIR__, 3) . '/vendor/autoload.php';
/* This script needs to be re-run after any intentional blockfactory change (adding or removing a block state). */
\pocketmine\block\BlockFactory::init();
$factory = new \pocketmine\block\BlockFactory();
$old = json_decode(file_get_contents(__DIR__ . '/block_factory_consistency_check.json'), true);
$new = array_map(
function(\pocketmine\block\Block $block) : string{
return $block->getName();
},
\pocketmine\block\BlockFactory::getAllKnownStates()
$factory->getAllKnownStates()
);
foreach($old as $k => $name){
if(!isset($new[$k])){

View File

@ -29,7 +29,6 @@ use pocketmine\block\BlockFactory;
class ItemFactoryTest extends TestCase{
public function setUp() : void{
BlockFactory::init();
ItemFactory::init();
}
@ -38,7 +37,7 @@ class ItemFactoryTest extends TestCase{
*/
public function testItemBlockRegistered() : void{
for($id = 0; $id < 256; ++$id){
self::assertEquals(BlockFactory::isRegistered($id), ItemFactory::isRegistered($id));
self::assertEquals(BlockFactory::getInstance()->isRegistered($id), ItemFactory::isRegistered($id));
}
}

View File

@ -24,14 +24,12 @@ declare(strict_types=1);
namespace pocketmine\item;
use PHPUnit\Framework\TestCase;
use pocketmine\block\BlockFactory;
use pocketmine\item\enchantment\Enchantment;
use pocketmine\item\enchantment\EnchantmentInstance;
class ItemTest extends TestCase{
public static function setUpBeforeClass() : void{
BlockFactory::init();
ItemFactory::init();
Enchantment::init();
}