mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-07 10:22:56 +00:00
convert TileFactory to singleton
This commit is contained in:
@ -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");
|
||||
}
|
||||
|
Reference in New Issue
Block a user