convert TileFactory to singleton

This commit is contained in:
Dylan K. Taylor 2020-04-26 01:11:30 +01:00
parent 2d55b2db1b
commit ac5cf2443e
6 changed files with 43 additions and 47 deletions

View File

@ -163,7 +163,7 @@ class Block{
} }
} }
if($oldTile === null and $tileType !== null){ if($oldTile === null and $tileType !== null){
$this->pos->getWorldNonNull()->addTile(TileFactory::create($tileType, $this->pos->getWorld(), $this->pos->asVector3())); $this->pos->getWorldNonNull()->addTile(TileFactory::getInstance()->create($tileType, $this->pos->getWorld(), $this->pos->asVector3()));
} }
} }

View File

@ -43,7 +43,6 @@ use pocketmine\block\tile\MonsterSpawner as TileMonsterSpawner;
use pocketmine\block\tile\Note as TileNote; use pocketmine\block\tile\Note as TileNote;
use pocketmine\block\tile\Sign as TileSign; use pocketmine\block\tile\Sign as TileSign;
use pocketmine\block\tile\Skull as TileSkull; use pocketmine\block\tile\Skull as TileSkull;
use pocketmine\block\tile\TileFactory;
use pocketmine\block\utils\DyeColor; use pocketmine\block\utils\DyeColor;
use pocketmine\block\utils\InvalidBlockStateException; use pocketmine\block\utils\InvalidBlockStateException;
use pocketmine\block\utils\PillarRotationTrait; use pocketmine\block\utils\PillarRotationTrait;
@ -86,8 +85,6 @@ class BlockFactory{
public $blastResistance; public $blastResistance;
public function __construct(){ public function __construct(){
TileFactory::init();
$this->fullList = new \SplFixedArray(8192); $this->fullList = new \SplFixedArray(8192);
$this->lightFilter = \SplFixedArray::fromArray(array_fill(0, 8192, 1)); $this->lightFilter = \SplFixedArray::fromArray(array_fill(0, 8192, 1));

View File

@ -71,7 +71,7 @@ abstract class Spawnable extends Tile{
final public function getSpawnCompound() : CompoundTag{ final public function getSpawnCompound() : CompoundTag{
$nbt = CompoundTag::create() $nbt = CompoundTag::create()
->setString(self::TAG_ID, TileFactory::getSaveId(get_class($this))) //TODO: disassociate network ID from save ID ->setString(self::TAG_ID, TileFactory::getInstance()->getSaveId(get_class($this))) //TODO: disassociate network ID from save ID
->setInt(self::TAG_X, $this->pos->x) ->setInt(self::TAG_X, $this->pos->x)
->setInt(self::TAG_Y, $this->pos->y) ->setInt(self::TAG_Y, $this->pos->y)
->setInt(self::TAG_Z, $this->pos->z); ->setInt(self::TAG_Z, $this->pos->z);

View File

@ -69,7 +69,7 @@ abstract class Tile{
public function saveNBT() : CompoundTag{ public function saveNBT() : CompoundTag{
$nbt = CompoundTag::create() $nbt = CompoundTag::create()
->setString(self::TAG_ID, TileFactory::getSaveId(get_class($this))) ->setString(self::TAG_ID, TileFactory::getInstance()->getSaveId(get_class($this)))
->setInt(self::TAG_X, $this->pos->getFloorX()) ->setInt(self::TAG_X, $this->pos->getFloorX())
->setInt(self::TAG_Y, $this->pos->getFloorY()) ->setInt(self::TAG_Y, $this->pos->getFloorY())
->setInt(self::TAG_Z, $this->pos->getFloorZ()); ->setInt(self::TAG_Z, $this->pos->getFloorZ());

View File

@ -25,6 +25,7 @@ namespace pocketmine\block\tile;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\utils\SingletonTrait;
use pocketmine\utils\Utils; use pocketmine\utils\Utils;
use pocketmine\world\World; use pocketmine\world\World;
use function assert; use function assert;
@ -33,44 +34,41 @@ use function is_a;
use function reset; use function reset;
final class TileFactory{ final class TileFactory{
use SingletonTrait;
/** /**
* @var string[] classes that extend Tile * @var string[] classes that extend Tile
* @phpstan-var array<string, class-string<Tile>> * @phpstan-var array<string, class-string<Tile>>
*/ */
private static $knownTiles = []; private $knownTiles = [];
/** /**
* @var string[][] * @var string[][]
* @phpstan-var array<class-string<Tile>, list<string>> * @phpstan-var array<class-string<Tile>, list<string>>
*/ */
private static $saveNames = []; private $saveNames = [];
/** /**
* @var string[] base class => overridden class * @var string[] base class => overridden class
* @phpstan-var array<class-string<Tile>, class-string<Tile>> * @phpstan-var array<class-string<Tile>, class-string<Tile>>
*/ */
private static $classMapping = []; private $classMapping = [];
private function __construct(){ public function __construct(){
//NOOP $this->register(Banner::class, ["Banner", "minecraft:banner"]);
} $this->register(Bed::class, ["Bed", "minecraft:bed"]);
$this->register(BrewingStand::class, ["BrewingStand", "minecraft:brewing_stand"]);
public static function init() : void{ $this->register(Chest::class, ["Chest", "minecraft:chest"]);
self::register(Banner::class, ["Banner", "minecraft:banner"]); $this->register(Comparator::class, ["Comparator", "minecraft:comparator"]);
self::register(Bed::class, ["Bed", "minecraft:bed"]); $this->register(DaylightSensor::class, ["DaylightDetector", "minecraft:daylight_detector"]);
self::register(BrewingStand::class, ["BrewingStand", "minecraft:brewing_stand"]); $this->register(EnchantTable::class, ["EnchantTable", "minecraft:enchanting_table"]);
self::register(Chest::class, ["Chest", "minecraft:chest"]); $this->register(EnderChest::class, ["EnderChest", "minecraft:ender_chest"]);
self::register(Comparator::class, ["Comparator", "minecraft:comparator"]); $this->register(FlowerPot::class, ["FlowerPot", "minecraft:flower_pot"]);
self::register(DaylightSensor::class, ["DaylightDetector", "minecraft:daylight_detector"]); $this->register(Furnace::class, ["Furnace", "minecraft:furnace"]);
self::register(EnchantTable::class, ["EnchantTable", "minecraft:enchanting_table"]); $this->register(Hopper::class, ["Hopper", "minecraft:hopper"]);
self::register(EnderChest::class, ["EnderChest", "minecraft:ender_chest"]); $this->register(ItemFrame::class, ["ItemFrame"]); //this is an entity in PC
self::register(FlowerPot::class, ["FlowerPot", "minecraft:flower_pot"]); $this->register(MonsterSpawner::class, ["MobSpawner", "minecraft:mob_spawner"]);
self::register(Furnace::class, ["Furnace", "minecraft:furnace"]); $this->register(Note::class, ["Music", "minecraft:noteblock"]);
self::register(Hopper::class, ["Hopper", "minecraft:hopper"]); $this->register(Sign::class, ["Sign", "minecraft:sign"]);
self::register(ItemFrame::class, ["ItemFrame"]); //this is an entity in PC $this->register(Skull::class, ["Skull", "minecraft:skull"]);
self::register(MonsterSpawner::class, ["MobSpawner", "minecraft:mob_spawner"]);
self::register(Note::class, ["Music", "minecraft:noteblock"]);
self::register(Sign::class, ["Sign", "minecraft:sign"]);
self::register(Skull::class, ["Skull", "minecraft:skull"]);
//TODO: Barrel //TODO: Barrel
//TODO: Beacon //TODO: Beacon
@ -101,10 +99,10 @@ final class TileFactory{
* @param string[] $saveNames * @param string[] $saveNames
* @phpstan-param class-string<Tile> $className * @phpstan-param class-string<Tile> $className
*/ */
public static function register(string $className, array $saveNames = []) : void{ public function register(string $className, array $saveNames = []) : void{
Utils::testValidInstance($className, Tile::class); Utils::testValidInstance($className, Tile::class);
self::$classMapping[$className] = $className; $this->classMapping[$className] = $className;
$shortName = (new \ReflectionClass($className))->getShortName(); $shortName = (new \ReflectionClass($className))->getShortName();
if(!in_array($shortName, $saveNames, true)){ if(!in_array($shortName, $saveNames, true)){
@ -112,10 +110,10 @@ final class TileFactory{
} }
foreach($saveNames as $name){ foreach($saveNames as $name){
self::$knownTiles[$name] = $className; $this->knownTiles[$name] = $className;
} }
self::$saveNames[$className] = $saveNames; $this->saveNames[$className] = $saveNames;
} }
/** /**
@ -128,13 +126,13 @@ final class TileFactory{
* *
* @throws \InvalidArgumentException if the base class is not a registered tile * @throws \InvalidArgumentException if the base class is not a registered tile
*/ */
public static function override(string $baseClass, string $newClass) : void{ public function override(string $baseClass, string $newClass) : void{
if(!isset(self::$classMapping[$baseClass])){ if(!isset($this->classMapping[$baseClass])){
throw new \InvalidArgumentException("Class $baseClass is not a registered tile"); throw new \InvalidArgumentException("Class $baseClass is not a registered tile");
} }
Utils::testValidInstance($newClass, $baseClass); Utils::testValidInstance($newClass, $baseClass);
self::$classMapping[$baseClass] = $newClass; $this->classMapping[$baseClass] = $newClass;
} }
/** /**
@ -146,9 +144,9 @@ final class TileFactory{
* *
* @throws \InvalidArgumentException if the specified class is not a registered tile * @throws \InvalidArgumentException if the specified class is not a registered tile
*/ */
public static function create(string $baseClass, World $world, Vector3 $pos) : Tile{ public function create(string $baseClass, World $world, Vector3 $pos) : Tile{
if(isset(self::$classMapping[$baseClass])){ if(isset($this->classMapping[$baseClass])){
$class = self::$classMapping[$baseClass]; $class = $this->classMapping[$baseClass];
assert(is_a($class, $baseClass, true)); assert(is_a($class, $baseClass, true));
/** /**
* @var Tile $tile * @var Tile $tile
@ -166,12 +164,12 @@ final class TileFactory{
/** /**
* @internal * @internal
*/ */
public static function createFromData(World $world, CompoundTag $nbt) : ?Tile{ public function createFromData(World $world, CompoundTag $nbt) : ?Tile{
$type = $nbt->getString(Tile::TAG_ID, ""); $type = $nbt->getString(Tile::TAG_ID, "");
if(!isset(self::$knownTiles[$type])){ if(!isset($this->knownTiles[$type])){
return null; return null;
} }
$class = self::$knownTiles[$type]; $class = $this->knownTiles[$type];
assert(is_a($class, Tile::class, true)); assert(is_a($class, Tile::class, true));
/** /**
* @var Tile $tile * @var Tile $tile
@ -186,9 +184,9 @@ final class TileFactory{
/** /**
* @phpstan-param class-string<Tile> $class * @phpstan-param class-string<Tile> $class
*/ */
public static function getSaveId(string $class) : string{ public function getSaveId(string $class) : string{
if(isset(self::$saveNames[$class])){ if(isset($this->saveNames[$class])){
return reset(self::$saveNames[$class]); return reset($this->saveNames[$class]);
} }
throw new \InvalidArgumentException("Tile $class is not registered"); throw new \InvalidArgumentException("Tile $class is not registered");
} }

View File

@ -534,8 +534,9 @@ class Chunk{
if($this->NBTtiles !== null){ if($this->NBTtiles !== null){
$this->dirtyFlags |= self::DIRTY_FLAG_TILES; $this->dirtyFlags |= self::DIRTY_FLAG_TILES;
$world->timings->syncChunkLoadTileEntitiesTimer->startTiming(); $world->timings->syncChunkLoadTileEntitiesTimer->startTiming();
$tileFactory = TileFactory::getInstance();
foreach($this->NBTtiles as $nbt){ foreach($this->NBTtiles as $nbt){
if(($tile = TileFactory::createFromData($world, $nbt)) !== null){ if(($tile = $tileFactory->createFromData($world, $nbt)) !== null){
$world->addTile($tile); $world->addTile($tile);
}else{ }else{
$world->getLogger()->warning("Chunk $this->x $this->z: Deleted unknown tile entity type " . $nbt->getString("id", "<unknown>")); $world->getLogger()->warning("Chunk $this->x $this->z: Deleted unknown tile entity type " . $nbt->getString("id", "<unknown>"));