mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-22 08:44:01 +00:00
convert TileFactory to singleton
This commit is contained in:
parent
2d55b2db1b
commit
ac5cf2443e
@ -163,7 +163,7 @@ class Block{
|
||||
}
|
||||
}
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,6 @@ use pocketmine\block\tile\MonsterSpawner as TileMonsterSpawner;
|
||||
use pocketmine\block\tile\Note as TileNote;
|
||||
use pocketmine\block\tile\Sign as TileSign;
|
||||
use pocketmine\block\tile\Skull as TileSkull;
|
||||
use pocketmine\block\tile\TileFactory;
|
||||
use pocketmine\block\utils\DyeColor;
|
||||
use pocketmine\block\utils\InvalidBlockStateException;
|
||||
use pocketmine\block\utils\PillarRotationTrait;
|
||||
@ -86,8 +85,6 @@ class BlockFactory{
|
||||
public $blastResistance;
|
||||
|
||||
public function __construct(){
|
||||
TileFactory::init();
|
||||
|
||||
$this->fullList = new \SplFixedArray(8192);
|
||||
|
||||
$this->lightFilter = \SplFixedArray::fromArray(array_fill(0, 8192, 1));
|
||||
|
@ -71,7 +71,7 @@ abstract class Spawnable extends Tile{
|
||||
|
||||
final public function getSpawnCompound() : CompoundTag{
|
||||
$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_Y, $this->pos->y)
|
||||
->setInt(self::TAG_Z, $this->pos->z);
|
||||
|
@ -69,7 +69,7 @@ abstract class Tile{
|
||||
|
||||
public function saveNBT() : CompoundTag{
|
||||
$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_Y, $this->pos->getFloorY())
|
||||
->setInt(self::TAG_Z, $this->pos->getFloorZ());
|
||||
|
@ -25,6 +25,7 @@ namespace pocketmine\block\tile;
|
||||
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
use pocketmine\utils\SingletonTrait;
|
||||
use pocketmine\utils\Utils;
|
||||
use pocketmine\world\World;
|
||||
use function assert;
|
||||
@ -33,44 +34,41 @@ use function is_a;
|
||||
use function reset;
|
||||
|
||||
final class TileFactory{
|
||||
use SingletonTrait;
|
||||
|
||||
/**
|
||||
* @var string[] classes that extend Tile
|
||||
* @phpstan-var array<string, class-string<Tile>>
|
||||
*/
|
||||
private static $knownTiles = [];
|
||||
private $knownTiles = [];
|
||||
/**
|
||||
* @var string[][]
|
||||
* @phpstan-var array<class-string<Tile>, list<string>>
|
||||
*/
|
||||
private static $saveNames = [];
|
||||
private $saveNames = [];
|
||||
/**
|
||||
* @var string[] base class => overridden class
|
||||
* @phpstan-var array<class-string<Tile>, class-string<Tile>>
|
||||
*/
|
||||
private static $classMapping = [];
|
||||
private $classMapping = [];
|
||||
|
||||
private function __construct(){
|
||||
//NOOP
|
||||
}
|
||||
|
||||
public static function init() : void{
|
||||
self::register(Banner::class, ["Banner", "minecraft:banner"]);
|
||||
self::register(Bed::class, ["Bed", "minecraft:bed"]);
|
||||
self::register(BrewingStand::class, ["BrewingStand", "minecraft:brewing_stand"]);
|
||||
self::register(Chest::class, ["Chest", "minecraft:chest"]);
|
||||
self::register(Comparator::class, ["Comparator", "minecraft:comparator"]);
|
||||
self::register(DaylightSensor::class, ["DaylightDetector", "minecraft:daylight_detector"]);
|
||||
self::register(EnchantTable::class, ["EnchantTable", "minecraft:enchanting_table"]);
|
||||
self::register(EnderChest::class, ["EnderChest", "minecraft:ender_chest"]);
|
||||
self::register(FlowerPot::class, ["FlowerPot", "minecraft:flower_pot"]);
|
||||
self::register(Furnace::class, ["Furnace", "minecraft:furnace"]);
|
||||
self::register(Hopper::class, ["Hopper", "minecraft:hopper"]);
|
||||
self::register(ItemFrame::class, ["ItemFrame"]); //this is an entity in PC
|
||||
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"]);
|
||||
public function __construct(){
|
||||
$this->register(Banner::class, ["Banner", "minecraft:banner"]);
|
||||
$this->register(Bed::class, ["Bed", "minecraft:bed"]);
|
||||
$this->register(BrewingStand::class, ["BrewingStand", "minecraft:brewing_stand"]);
|
||||
$this->register(Chest::class, ["Chest", "minecraft:chest"]);
|
||||
$this->register(Comparator::class, ["Comparator", "minecraft:comparator"]);
|
||||
$this->register(DaylightSensor::class, ["DaylightDetector", "minecraft:daylight_detector"]);
|
||||
$this->register(EnchantTable::class, ["EnchantTable", "minecraft:enchanting_table"]);
|
||||
$this->register(EnderChest::class, ["EnderChest", "minecraft:ender_chest"]);
|
||||
$this->register(FlowerPot::class, ["FlowerPot", "minecraft:flower_pot"]);
|
||||
$this->register(Furnace::class, ["Furnace", "minecraft:furnace"]);
|
||||
$this->register(Hopper::class, ["Hopper", "minecraft:hopper"]);
|
||||
$this->register(ItemFrame::class, ["ItemFrame"]); //this is an entity in PC
|
||||
$this->register(MonsterSpawner::class, ["MobSpawner", "minecraft:mob_spawner"]);
|
||||
$this->register(Note::class, ["Music", "minecraft:noteblock"]);
|
||||
$this->register(Sign::class, ["Sign", "minecraft:sign"]);
|
||||
$this->register(Skull::class, ["Skull", "minecraft:skull"]);
|
||||
|
||||
//TODO: Barrel
|
||||
//TODO: Beacon
|
||||
@ -101,10 +99,10 @@ final class TileFactory{
|
||||
* @param string[] $saveNames
|
||||
* @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);
|
||||
|
||||
self::$classMapping[$className] = $className;
|
||||
$this->classMapping[$className] = $className;
|
||||
|
||||
$shortName = (new \ReflectionClass($className))->getShortName();
|
||||
if(!in_array($shortName, $saveNames, true)){
|
||||
@ -112,10 +110,10 @@ final class TileFactory{
|
||||
}
|
||||
|
||||
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
|
||||
*/
|
||||
public static function override(string $baseClass, string $newClass) : void{
|
||||
if(!isset(self::$classMapping[$baseClass])){
|
||||
public function override(string $baseClass, string $newClass) : void{
|
||||
if(!isset($this->classMapping[$baseClass])){
|
||||
throw new \InvalidArgumentException("Class $baseClass is not a registered tile");
|
||||
}
|
||||
|
||||
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
|
||||
*/
|
||||
public static function create(string $baseClass, World $world, Vector3 $pos) : Tile{
|
||||
if(isset(self::$classMapping[$baseClass])){
|
||||
$class = self::$classMapping[$baseClass];
|
||||
public function create(string $baseClass, World $world, Vector3 $pos) : Tile{
|
||||
if(isset($this->classMapping[$baseClass])){
|
||||
$class = $this->classMapping[$baseClass];
|
||||
assert(is_a($class, $baseClass, true));
|
||||
/**
|
||||
* @var Tile $tile
|
||||
@ -166,12 +164,12 @@ final class TileFactory{
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public static function createFromData(World $world, CompoundTag $nbt) : ?Tile{
|
||||
public function createFromData(World $world, CompoundTag $nbt) : ?Tile{
|
||||
$type = $nbt->getString(Tile::TAG_ID, "");
|
||||
if(!isset(self::$knownTiles[$type])){
|
||||
if(!isset($this->knownTiles[$type])){
|
||||
return null;
|
||||
}
|
||||
$class = self::$knownTiles[$type];
|
||||
$class = $this->knownTiles[$type];
|
||||
assert(is_a($class, Tile::class, true));
|
||||
/**
|
||||
* @var Tile $tile
|
||||
@ -186,9 +184,9 @@ final class TileFactory{
|
||||
/**
|
||||
* @phpstan-param class-string<Tile> $class
|
||||
*/
|
||||
public static function getSaveId(string $class) : string{
|
||||
if(isset(self::$saveNames[$class])){
|
||||
return reset(self::$saveNames[$class]);
|
||||
public function getSaveId(string $class) : string{
|
||||
if(isset($this->saveNames[$class])){
|
||||
return reset($this->saveNames[$class]);
|
||||
}
|
||||
throw new \InvalidArgumentException("Tile $class is not registered");
|
||||
}
|
||||
|
@ -534,8 +534,9 @@ class Chunk{
|
||||
if($this->NBTtiles !== null){
|
||||
$this->dirtyFlags |= self::DIRTY_FLAG_TILES;
|
||||
$world->timings->syncChunkLoadTileEntitiesTimer->startTiming();
|
||||
$tileFactory = TileFactory::getInstance();
|
||||
foreach($this->NBTtiles as $nbt){
|
||||
if(($tile = TileFactory::createFromData($world, $nbt)) !== null){
|
||||
if(($tile = $tileFactory->createFromData($world, $nbt)) !== null){
|
||||
$world->addTile($tile);
|
||||
}else{
|
||||
$world->getLogger()->warning("Chunk $this->x $this->z: Deleted unknown tile entity type " . $nbt->getString("id", "<unknown>"));
|
||||
|
Loading…
x
Reference in New Issue
Block a user