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; namespace pocketmine;
use pocketmine\block\BlockFactory;
use pocketmine\command\CommandReader; use pocketmine\command\CommandReader;
use pocketmine\command\CommandSender; use pocketmine\command\CommandSender;
use pocketmine\command\ConsoleCommandSender; use pocketmine\command\ConsoleCommandSender;
@ -985,7 +984,6 @@ class Server{
$this->commandMap = new SimpleCommandMap($this); $this->commandMap = new SimpleCommandMap($this);
EntityFactory::init(); EntityFactory::init();
BlockFactory::init();
Enchantment::init(); Enchantment::init();
ItemFactory::init(); ItemFactory::init();
CreativeInventory::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; continue;
} }
if($this->getSide($i) instanceof Water){ 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{ public function readSaveData(CompoundTag $nbt) : void{
if($nbt->hasTag(self::TAG_ITEM, ShortTag::class) and $nbt->hasTag(self::TAG_ITEM_DATA, IntTag::class)){ if($nbt->hasTag(self::TAG_ITEM, ShortTag::class) and $nbt->hasTag(self::TAG_ITEM_DATA, IntTag::class)){
try{ 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){ }catch(\InvalidArgumentException $e){
//noop //noop
} }

View File

@ -185,7 +185,7 @@ class ParticleCommand extends VanillaCommand{
break; break;
case "terrain": case "terrain":
if($data !== null and $data !== 0){ if($data !== null and $data !== 0){
return new TerrainParticle(BlockFactory::get($data)); return new TerrainParticle(BlockFactory::getInstance()->get($data));
} }
break; break;
case "heart": case "heart":
@ -213,7 +213,7 @@ class ParticleCommand extends VanillaCommand{
}elseif(strpos($name, "blockcrack_") === 0){ }elseif(strpos($name, "blockcrack_") === 0){
$d = explode("_", $name); $d = explode("_", $name);
if(count($d) === 2){ 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){ }elseif(strpos($name, "blockdust_") === 0){
$d = explode("_", $name); $d = explode("_", $name);

View File

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

View File

@ -100,7 +100,7 @@ abstract class Projectile extends Entity{
break; 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()); $this->blockHit->position($this->getWorld(), $blockPos->getFloorX(), $blockPos->getFloorY(), $blockPos->getFloorZ());
}while(false); }while(false);
} }

View File

@ -50,7 +50,7 @@ class ItemBlock extends Item{
} }
public function getBlock() : Block{ 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{ public function getFuelTime() : int{

View File

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

View File

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

View File

@ -51,7 +51,7 @@ class SimpleChunkManager implements ChunkManager{
public function getBlockAt(int $x, int $y, int $z) : Block{ public function getBlockAt(int $x, int $y, int $z) : Block{
if($this->terrainPointer->moveTo($x, $y, $z, false)){ 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(); 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); $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()){ if(!isset($dontTickBlocks[$state->getId()]) and $state->ticksRandomly()){
$this->randomTickBlocks[$state->getFullId()] = true; $this->randomTickBlocks[$state->getFullId()] = true;
} }
@ -945,7 +945,7 @@ class World implements ChunkManager{
if(isset($this->randomTickBlocks[$state])){ if(isset($this->randomTickBlocks[$state])){
/** @var Block $block */ /** @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->position($this, $chunkX * 16 + $x, ($Y << 4) + $y, $chunkZ * 16 + $z);
$block->onRandomTick(); $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); $block->position($this, $x, $y, $z);
static $dynamicStateRead = false; static $dynamicStateRead = false;

View File

@ -23,7 +23,6 @@ declare(strict_types=1);
namespace pocketmine\world\generator; namespace pocketmine\world\generator;
use pocketmine\block\BlockFactory;
use pocketmine\scheduler\AsyncTask; use pocketmine\scheduler\AsyncTask;
use pocketmine\world\biome\Biome; use pocketmine\world\biome\Biome;
use pocketmine\world\World; use pocketmine\world\World;
@ -56,7 +55,6 @@ class GeneratorRegisterTask extends AsyncTask{
} }
public function onRun() : void{ public function onRun() : void{
BlockFactory::init();
Biome::init(); Biome::init();
$manager = new GeneratorChunkManager($this->worldHeight); $manager = new GeneratorChunkManager($this->worldHeight);
$this->worker->saveToThreadStore("generation.world{$this->worldId}.manager", $manager); $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 = $manager->getChunk($chunk->getX(), $chunk->getZ());
$chunk->setPopulated(); $chunk->setPopulated();
$chunk->recalculateHeightMap(BlockFactory::$lightFilter, BlockFactory::$diffusesSkyLight); $blockFactory = BlockFactory::getInstance();
$chunk->populateSkyLight(BlockFactory::$lightFilter); $chunk->recalculateHeightMap($blockFactory->lightFilter, $blockFactory->diffusesSkyLight);
$chunk->populateSkyLight($blockFactory->lightFilter);
$chunk->setLightPopulated(); $chunk->setLightPopulated();
$this->chunk = FastChunkSerializer::serialize($chunk); $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{ public function populate(ChunkManager $world, int $chunkX, int $chunkZ, Random $random) : void{
$chunk = $world->getChunk($chunkX, $chunkZ); $chunk = $world->getChunk($chunkX, $chunkZ);
$factory = BlockFactory::getInstance();
for($x = 0; $x < 16; ++$x){ for($x = 0; $x < 16; ++$x){
for($z = 0; $z < 16; ++$z){ for($z = 0; $z < 16; ++$z){
$biome = Biome::getBiome($chunk->getBiomeId($x, $z)); $biome = Biome::getBiome($chunk->getBiomeId($x, $z));
@ -48,7 +49,7 @@ class GroundCover extends Populator{
$startY = 127; $startY = 127;
for(; $startY > 0; --$startY){ for(; $startY > 0; --$startY){
if(!BlockFactory::fromFullBlock($chunk->getFullBlock($x, $startY, $z))->isTransparent()){ if(!$factory->fromFullBlock($chunk->getFullBlock($x, $startY, $z))->isTransparent()){
break; break;
} }
} }
@ -56,7 +57,7 @@ class GroundCover extends Populator{
$endY = $startY - count($cover); $endY = $startY - count($cover);
for($y = $startY; $y > $endY and $y >= 0; --$y){ for($y = $startY; $y > $endY and $y >= 0; --$y){
$b = $cover[$startY - $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()){ if($id->getId() === BlockLegacyIds::AIR and $b->isSolid()){
break; break;
} }

View File

@ -33,6 +33,6 @@ class BlockLightUpdate extends LightUpdate{
public function recalculateNode(int $x, int $y, int $z) : void{ public function recalculateNode(int $x, int $y, int $z) : void{
$block = $this->world->getBlockAt($x, $y, $z); $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{ public function onRun() : void{
if(!BlockFactory::isInit()){
BlockFactory::init();
}
/** @var Chunk $chunk */ /** @var Chunk $chunk */
$chunk = FastChunkSerializer::deserialize($this->chunk); $chunk = FastChunkSerializer::deserialize($this->chunk);
$chunk->recalculateHeightMap(BlockFactory::$lightFilter, BlockFactory::$diffusesSkyLight); $blockFactory = BlockFactory::getInstance();
$chunk->populateSkyLight(BlockFactory::$lightFilter); $chunk->recalculateHeightMap($blockFactory->lightFilter, $blockFactory->diffusesSkyLight);
$chunk->populateSkyLight($blockFactory->lightFilter);
$chunk->setLightPopulated(); $chunk->setLightPopulated();
$this->resultHeightMap = igbinary_serialize($chunk->getHeightMapArray()); $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{ protected function computeSpreadLight(int $x, int $y, int $z, int $newAdjacentLevel) : void{
$current = $this->currentLightArray->get($x & 0xf, $y & 0xf, $z & 0xf); $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){ if($current < $potentialLight){
$this->currentLightArray->set($x & 0xf, $y & 0xf, $z & 0xf, $potentialLight); $this->currentLightArray->set($x & 0xf, $y & 0xf, $z & 0xf, $potentialLight);

View File

@ -51,7 +51,7 @@ class SkyLightUpdate extends LightUpdate{
$yPlusOne = $y + 1; $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. 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. }elseif($yPlusOne > $oldHeightMap){ //Block changed above the heightmap.
if($source->getLightFilter() > 0 or $source->diffusesSkyLight()){ if($source->getLightFilter() > 0 or $source->diffusesSkyLight()){
$chunk->setHeightMap($x & 0xf, $z & 0xf, $yPlusOne); $chunk->setHeightMap($x & 0xf, $z & 0xf, $yPlusOne);
@ -72,7 +72,7 @@ class SkyLightUpdate extends LightUpdate{
$this->setAndUpdateLight($x, $i, $z, 15); $this->setAndUpdateLight($x, $i, $z, 15);
} }
}else{ //No heightmap change, block changed "underground" }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{ class BlockTest extends TestCase{
/** @var BlockFactory */
private $blockFactory;
public function setUp() : void{ public function setUp() : void{
BlockFactory::init(); $this->blockFactory = new BlockFactory();
} }
/** /**
@ -39,7 +42,7 @@ class BlockTest extends TestCase{
public function testAccidentalOverrideBlock() : void{ public function testAccidentalOverrideBlock() : void{
$block = new MyCustomBlock(new BlockIdentifier(BlockLegacyIds::COBBLESTONE), "Cobblestone", BlockBreakInfo::instant()); $block = new MyCustomBlock(new BlockIdentifier(BlockLegacyIds::COBBLESTONE), "Cobblestone", BlockBreakInfo::instant());
$this->expectException(\InvalidArgumentException::class); $this->expectException(\InvalidArgumentException::class);
BlockFactory::register($block); $this->blockFactory->register($block);
} }
/** /**
@ -47,8 +50,8 @@ class BlockTest extends TestCase{
*/ */
public function testDeliberateOverrideBlock() : void{ public function testDeliberateOverrideBlock() : void{
$block = new MyCustomBlock(new BlockIdentifier(BlockLegacyIds::COBBLESTONE), "Cobblestone", BlockBreakInfo::instant()); $block = new MyCustomBlock(new BlockIdentifier(BlockLegacyIds::COBBLESTONE), "Cobblestone", BlockBreakInfo::instant());
BlockFactory::register($block, true); $this->blockFactory->register($block, true);
self::assertInstanceOf(MyCustomBlock::class, BlockFactory::get($block->getId())); self::assertInstanceOf(MyCustomBlock::class, $this->blockFactory->get($block->getId()));
} }
/** /**
@ -56,10 +59,10 @@ class BlockTest extends TestCase{
*/ */
public function testRegisterNewBlock() : void{ public function testRegisterNewBlock() : void{
for($i = 0; $i < 256; ++$i){ 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()); $b = new StrangeNewBlock(new BlockIdentifier($i), "Strange New Block", BlockBreakInfo::instant());
BlockFactory::register($b); $this->blockFactory->register($b);
self::assertInstanceOf(StrangeNewBlock::class, BlockFactory::get($b->getId())); self::assertInstanceOf(StrangeNewBlock::class, $this->blockFactory->get($b->getId()));
return; return;
} }
} }
@ -72,7 +75,7 @@ class BlockTest extends TestCase{
*/ */
public function testRegisterIdTooLarge() : void{ public function testRegisterIdTooLarge() : void{
self::expectException(\RuntimeException::class); 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{ public function testRegisterIdTooSmall() : void{
self::expectException(\RuntimeException::class); 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{ public function testBlockFactoryClone() : void{
for($i = 0; $i < 256; ++$i){ for($i = 0; $i < 256; ++$i){
$b1 = BlockFactory::get($i); $b1 = $this->blockFactory->get($i);
$b2 = BlockFactory::get($i); $b2 = $this->blockFactory->get($i);
self::assertNotSame($b1, $b2); self::assertNotSame($b1, $b2);
} }
} }
@ -117,7 +120,7 @@ class BlockTest extends TestCase{
* @param int $meta * @param int $meta
*/ */
public function testBlockGet(int $id, int $meta) : void{ 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($id, $block->getId());
self::assertEquals($meta, $block->getMeta()); self::assertEquals($meta, $block->getMeta());
@ -125,7 +128,7 @@ class BlockTest extends TestCase{
public function testBlockIds() : void{ public function testBlockIds() : void{
for($i = 0; $i < 256; ++$i){ for($i = 0; $i < 256; ++$i){
$b = BlockFactory::get($i); $b = $this->blockFactory->get($i);
self::assertContains($i, $b->getIdInfo()->getAllBlockIds()); self::assertContains($i, $b->getIdInfo()->getAllBlockIds());
} }
} }
@ -135,7 +138,7 @@ class BlockTest extends TestCase{
* (like freezes) when doing light population. * (like freezes) when doing light population.
*/ */
public function testLightFiltersValid() : void{ 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::assertNotNull($value, "Light filter value missing for $id");
self::assertLessThanOrEqual(15, $value, "Light filter value for $id is larger than the expected 15"); 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"); 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{ public function testConsistency() : void{
$list = json_decode(file_get_contents(__DIR__ . '/block_factory_consistency_check.json'), true); $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){ foreach($states as $k => $state){
self::assertArrayHasKey($k, $list, "New block state $k (" . $state->getName() . ") - consistency check may need regenerating"); self::assertArrayHasKey($k, $list, "New block state $k (" . $state->getName() . ") - consistency check may need regenerating");
self::assertSame($list[$k], $state->getName()); 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). */ /* 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); $old = json_decode(file_get_contents(__DIR__ . '/block_factory_consistency_check.json'), true);
$new = array_map( $new = array_map(
function(\pocketmine\block\Block $block) : string{ function(\pocketmine\block\Block $block) : string{
return $block->getName(); return $block->getName();
}, },
\pocketmine\block\BlockFactory::getAllKnownStates() $factory->getAllKnownStates()
); );
foreach($old as $k => $name){ foreach($old as $k => $name){
if(!isset($new[$k])){ if(!isset($new[$k])){

View File

@ -29,7 +29,6 @@ use pocketmine\block\BlockFactory;
class ItemFactoryTest extends TestCase{ class ItemFactoryTest extends TestCase{
public function setUp() : void{ public function setUp() : void{
BlockFactory::init();
ItemFactory::init(); ItemFactory::init();
} }
@ -38,7 +37,7 @@ class ItemFactoryTest extends TestCase{
*/ */
public function testItemBlockRegistered() : void{ public function testItemBlockRegistered() : void{
for($id = 0; $id < 256; ++$id){ 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; namespace pocketmine\item;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use pocketmine\block\BlockFactory;
use pocketmine\item\enchantment\Enchantment; use pocketmine\item\enchantment\Enchantment;
use pocketmine\item\enchantment\EnchantmentInstance; use pocketmine\item\enchantment\EnchantmentInstance;
class ItemTest extends TestCase{ class ItemTest extends TestCase{
public static function setUpBeforeClass() : void{ public static function setUpBeforeClass() : void{
BlockFactory::init();
ItemFactory::init(); ItemFactory::init();
Enchantment::init(); Enchantment::init();
} }