PocketMine-MP 5.33.2 git-1133d49c924b4358c79d44eeb97dcbf56cb4d1eb
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 $this->register(HangingSign::class, ["HangingSign", "minecraft:hanging_sign"]);
83
84 //TODO: ChalkboardBlock
85 //TODO: ChemistryTable
86 //TODO: CommandBlock
87 //TODO: Conduit
88 //TODO: Dispenser
89 //TODO: Dropper
90 //TODO: EndGateway
91 //TODO: EndPortal
92 //TODO: JigsawBlock
93 //TODO: MovingBlock
94 //TODO: NetherReactor
95 //TODO: PistonArm
96 //TODO: StructureBlock
97 }
98
103 public function register(string $className, array $saveNames = []) : void{
104 Utils::testValidInstance($className, Tile::class);
105
106 $shortName = (new \ReflectionClass($className))->getShortName();
107 if(!in_array($shortName, $saveNames, true)){
108 $saveNames[] = $shortName;
109 }
110
111 foreach($saveNames as $name){
112 $this->knownTiles[$name] = $className;
113 }
114
115 $this->saveNames[$className] = reset($saveNames);
116 }
117
121 public function isRegistered(string $class) : bool{
122 return isset($this->saveNames[$class]);
123 }
124
129 public function createFromData(World $world, CompoundTag $nbt) : ?Tile{
130 try{
131 $type = $nbt->getString(Tile::TAG_ID, "");
132 if(!isset($this->knownTiles[$type])){
133 return null;
134 }
135 $class = $this->knownTiles[$type];
136 assert(is_a($class, Tile::class, true));
141 $tile = new $class($world, new Vector3($nbt->getInt(Tile::TAG_X), $nbt->getInt(Tile::TAG_Y), $nbt->getInt(Tile::TAG_Z)));
142 $tile->readSaveData($nbt);
143 }catch(NbtException $e){
144 throw new SavedDataLoadingException($e->getMessage(), 0, $e);
145 }
146
147 return $tile;
148 }
149
153 public function getSaveId(string $class) : string{
154 if(isset($this->saveNames[$class])){
155 return $this->saveNames[$class];
156 }
157 throw new \InvalidArgumentException("Tile $class is not registered");
158 }
159}