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

@ -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])){