PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
TileFactory.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\block\tile;
25
30use pocketmine\utils\SingletonTrait;
33use function assert;
34use function in_array;
35use function is_a;
36use function reset;
37
38final class TileFactory{
39 use SingletonTrait;
40
45 private array $knownTiles = [];
50 private array $saveNames = [];
51
52 public function __construct(){
53 $this->register(Barrel::class, ["Barrel", "minecraft:barrel"]);
54 $this->register(Banner::class, ["Banner", "minecraft:banner"]);
55 $this->register(Beacon::class, ["Beacon", "minecraft:beacon"]);
56 $this->register(Bed::class, ["Bed", "minecraft:bed"]);
57 $this->register(Bell::class, ["Bell", "minecraft:bell"]);
58 $this->register(BlastFurnace::class, ["BlastFurnace", "minecraft:blast_furnace"]);
59 $this->register(BrewingStand::class, ["BrewingStand", "minecraft:brewing_stand"]);
60 $this->register(Cauldron::class, ["Cauldron", "minecraft:cauldron"]);
61 $this->register(Chest::class, ["Chest", "minecraft:chest"]);
62 $this->register(ChiseledBookshelf::class, ["ChiseledBookshelf", "minecraft:chiseled_bookshelf"]);
63 $this->register(Comparator::class, ["Comparator", "minecraft:comparator"]);
64 $this->register(DaylightSensor::class, ["DaylightDetector", "minecraft:daylight_detector"]);
65 $this->register(EnchantTable::class, ["EnchantTable", "minecraft:enchanting_table"]);
66 $this->register(EnderChest::class, ["EnderChest", "minecraft:ender_chest"]);
67 $this->register(FlowerPot::class, ["FlowerPot", "minecraft:flower_pot"]);
68 $this->register(NormalFurnace::class, ["Furnace", "minecraft:furnace"]);
69 $this->register(Hopper::class, ["Hopper", "minecraft:hopper"]);
70 $this->register(ItemFrame::class, ["ItemFrame"]); //this is an entity in PC
71 $this->register(Jukebox::class, ["Jukebox", "RecordPlayer", "minecraft:jukebox"]);
72 $this->register(Lectern::class, ["Lectern", "minecraft:lectern"]);
73 $this->register(MonsterSpawner::class, ["MobSpawner", "minecraft:mob_spawner"]);
74 $this->register(Note::class, ["Music", "minecraft:noteblock"]);
75 $this->register(ShulkerBox::class, ["ShulkerBox", "minecraft:shulker_box"]);
76 $this->register(Sign::class, ["Sign", "minecraft:sign"]);
77 $this->register(Smoker::class, ["Smoker", "minecraft:smoker"]);
78 $this->register(SporeBlossom::class, ["SporeBlossom", "minecraft:spore_blossom"]);
79 $this->register(MobHead::class, ["Skull", "minecraft:skull"]);
80 $this->register(GlowingItemFrame::class, ["GlowItemFrame"]);
81
82 //TODO: Campfire
83 //TODO: ChalkboardBlock
84 //TODO: ChemistryTable
85 //TODO: CommandBlock
86 //TODO: Conduit
87 //TODO: Dispenser
88 //TODO: Dropper
89 //TODO: EndGateway
90 //TODO: EndPortal
91 //TODO: JigsawBlock
92 //TODO: MovingBlock
93 //TODO: NetherReactor
94 //TODO: PistonArm
95 //TODO: StructureBlock
96 }
97
102 public function register(string $className, array $saveNames = []) : void{
103 Utils::testValidInstance($className, Tile::class);
104
105 $shortName = (new \ReflectionClass($className))->getShortName();
106 if(!in_array($shortName, $saveNames, true)){
107 $saveNames[] = $shortName;
108 }
109
110 foreach($saveNames as $name){
111 $this->knownTiles[$name] = $className;
112 }
113
114 $this->saveNames[$className] = reset($saveNames);
115 }
116
121 public function createFromData(World $world, CompoundTag $nbt) : ?Tile{
122 try{
123 $type = $nbt->getString(Tile::TAG_ID, "");
124 if(!isset($this->knownTiles[$type])){
125 return null;
126 }
127 $class = $this->knownTiles[$type];
128 assert(is_a($class, Tile::class, true));
133 $tile = new $class($world, new Vector3($nbt->getInt(Tile::TAG_X), $nbt->getInt(Tile::TAG_Y), $nbt->getInt(Tile::TAG_Z)));
134 $tile->readSaveData($nbt);
135 }catch(NbtException $e){
136 throw new SavedDataLoadingException($e->getMessage(), 0, $e);
137 }
138
139 return $tile;
140 }
141
145 public function getSaveId(string $class) : string{
146 if(isset($this->saveNames[$class])){
147 return $this->saveNames[$class];
148 }
149 throw new \InvalidArgumentException("Tile $class is not registered");
150 }
151}
static testValidInstance(string $className, string $baseName)
Definition: Utils.php:539