PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
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(Campfire::class, ["Campfire", "minecraft:campfire"]);
61 $this->register(Cauldron::class, ["Cauldron", "minecraft:cauldron"]);
62 $this->register(Chest::class, ["Chest", "minecraft:chest"]);
63 $this->register(ChiseledBookshelf::class, ["ChiseledBookshelf", "minecraft:chiseled_bookshelf"]);
64 $this->register(Comparator::class, ["Comparator", "minecraft:comparator"]);
65 $this->register(DaylightSensor::class, ["DaylightDetector", "minecraft:daylight_detector"]);
66 $this->register(EnchantTable::class, ["EnchantTable", "minecraft:enchanting_table"]);
67 $this->register(EnderChest::class, ["EnderChest", "minecraft:ender_chest"]);
68 $this->register(FlowerPot::class, ["FlowerPot", "minecraft:flower_pot"]);
69 $this->register(NormalFurnace::class, ["Furnace", "minecraft:furnace"]);
70 $this->register(Hopper::class, ["Hopper", "minecraft:hopper"]);
71 $this->register(ItemFrame::class, ["ItemFrame"]); //this is an entity in PC
72 $this->register(Jukebox::class, ["Jukebox", "RecordPlayer", "minecraft:jukebox"]);
73 $this->register(Lectern::class, ["Lectern", "minecraft:lectern"]);
74 $this->register(MonsterSpawner::class, ["MobSpawner", "minecraft:mob_spawner"]);
75 $this->register(Note::class, ["Music", "minecraft:noteblock"]);
76 $this->register(ShulkerBox::class, ["ShulkerBox", "minecraft:shulker_box"]);
77 $this->register(Sign::class, ["Sign", "minecraft:sign"]);
78 $this->register(Smoker::class, ["Smoker", "minecraft:smoker"]);
79 $this->register(SporeBlossom::class, ["SporeBlossom", "minecraft:spore_blossom"]);
80 $this->register(MobHead::class, ["Skull", "minecraft:skull"]);
81 $this->register(GlowingItemFrame::class, ["GlowItemFrame"]);
82
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}