PocketMine-MP 5.35.1 git-05a71d8cc5185aa9e46ef5f9754bb862464c13e0
Loading...
Searching...
No Matches
EntityFactory.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\entity;
25
59use pocketmine\utils\SingletonTrait;
62use function count;
63use function reset;
64
69final class EntityFactory{
70 use SingletonTrait;
71
72 public const TAG_IDENTIFIER = "identifier"; //TAG_String
73 public const TAG_LEGACY_ID = "id"; //TAG_Int
74
79 private array $creationFuncs = [];
84 private array $saveNames = [];
85
86 public function __construct(){
87 //define legacy save IDs first - use them for saving for maximum compatibility with Minecraft PC
88 //TODO: index them by version to allow proper multi-save compatibility
89
90 $this->register(Arrow::class, function(World $world, CompoundTag $nbt) : Arrow{
91 return new Arrow(Helper::parseLocation($nbt, $world), null, $nbt->getByte(Arrow::TAG_CRIT, 0) === 1, $nbt);
92 }, ['Arrow', 'minecraft:arrow']);
93
94 $this->register(Egg::class, function(World $world, CompoundTag $nbt) : Egg{
95 return new Egg(Helper::parseLocation($nbt, $world), null, $nbt);
96 }, ['Egg', 'minecraft:egg']);
97
98 $this->register(EndCrystal::class, function(World $world, CompoundTag $nbt) : EndCrystal{
99 return new EndCrystal(Helper::parseLocation($nbt, $world), $nbt);
100 }, ['EnderCrystal', 'minecraft:ender_crystal']);
101
102 $this->register(EnderPearl::class, function(World $world, CompoundTag $nbt) : EnderPearl{
103 return new EnderPearl(Helper::parseLocation($nbt, $world), null, $nbt);
104 }, ['ThrownEnderpearl', 'minecraft:ender_pearl']);
105
106 $this->register(ExperienceBottle::class, function(World $world, CompoundTag $nbt) : ExperienceBottle{
107 return new ExperienceBottle(Helper::parseLocation($nbt, $world), null, $nbt);
108 }, ['ThrownExpBottle', 'minecraft:xp_bottle']);
109
110 $this->register(ExperienceOrb::class, function(World $world, CompoundTag $nbt) : ExperienceOrb{
111 $value = 1;
112 if(($valuePcTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PC)) instanceof ShortTag){ //PC
113 $value = $valuePcTag->getValue();
114 }elseif(($valuePeTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PE)) instanceof IntTag){ //PE save format
115 $value = $valuePeTag->getValue();
116 }
117
118 return new ExperienceOrb(Helper::parseLocation($nbt, $world), $value, $nbt);
119 }, ['XPOrb', 'minecraft:xp_orb']);
120
121 $this->register(FallingBlock::class, function(World $world, CompoundTag $nbt) : FallingBlock{
122 return new FallingBlock(Helper::parseLocation($nbt, $world), FallingBlock::parseBlockNBT(RuntimeBlockStateRegistry::getInstance(), $nbt), $nbt);
123 }, ['FallingSand', 'minecraft:falling_block']);
124
125 $this->register(IceBomb::class, function(World $world, CompoundTag $nbt) : IceBomb{
126 return new IceBomb(Helper::parseLocation($nbt, $world), null, $nbt);
127 }, ['minecraft:ice_bomb']);
128
129 $this->register(ItemEntity::class, function(World $world, CompoundTag $nbt) : ItemEntity{
130 $itemTag = $nbt->getCompoundTag(ItemEntity::TAG_ITEM);
131 if($itemTag === null){
132 throw new SavedDataLoadingException("Expected \"" . ItemEntity::TAG_ITEM . "\" NBT tag not found");
133 }
134
135 $item = Item::nbtDeserialize($itemTag);
136 if($item->isNull()){
137 throw new SavedDataLoadingException("Item is invalid");
138 }
139 return new ItemEntity(Helper::parseLocation($nbt, $world), $item, $nbt);
140 }, ['Item', 'minecraft:item']);
141
142 $this->register(Painting::class, function(World $world, CompoundTag $nbt) : Painting{
143 $motive = PaintingMotive::getMotiveByName($nbt->getString(Painting::TAG_MOTIVE));
144 if($motive === null){
145 throw new SavedDataLoadingException("Unknown painting motive");
146 }
147 $blockIn = new Vector3($nbt->getInt(Painting::TAG_TILE_X), $nbt->getInt(Painting::TAG_TILE_Y), $nbt->getInt(Painting::TAG_TILE_Z));
148 if(($directionTag = $nbt->getTag(Painting::TAG_DIRECTION_BE)) instanceof ByteTag){
149 $facing = Painting::DATA_TO_FACING[$directionTag->getValue()] ?? Facing::NORTH;
150 }elseif(($facingTag = $nbt->getTag(Painting::TAG_FACING_JE)) instanceof ByteTag){
151 $facing = Painting::DATA_TO_FACING[$facingTag->getValue()] ?? Facing::NORTH;
152 }else{
153 throw new SavedDataLoadingException("Missing facing info");
154 }
155
156 return new Painting(Helper::parseLocation($nbt, $world), $blockIn, $facing, $motive, $nbt);
157 }, ['Painting', 'minecraft:painting']);
158
159 $this->register(PrimedTNT::class, function(World $world, CompoundTag $nbt) : PrimedTNT{
160 return new PrimedTNT(Helper::parseLocation($nbt, $world), $nbt);
161 }, ['PrimedTnt', 'PrimedTNT', 'minecraft:tnt']);
162
163 $this->register(Snowball::class, function(World $world, CompoundTag $nbt) : Snowball{
164 return new Snowball(Helper::parseLocation($nbt, $world), null, $nbt);
165 }, ['Snowball', 'minecraft:snowball']);
166
167 $this->register(SplashPotion::class, function(World $world, CompoundTag $nbt) : SplashPotion{
168 $potionType = PotionTypeIdMap::getInstance()->fromId($nbt->getShort(SplashPotion::TAG_POTION_ID, PotionTypeIds::WATER));
169 if($potionType === null){
170 throw new SavedDataLoadingException("No such potion type");
171 }
172 return new SplashPotion(Helper::parseLocation($nbt, $world), null, $potionType, $nbt);
173 }, ['ThrownPotion', 'minecraft:potion', 'thrownpotion']);
174
175 $this->register(Trident::class, function(World $world, CompoundTag $nbt) : Trident{
176 $itemTag = $nbt->getCompoundTag(Trident::TAG_ITEM);
177 if($itemTag === null){
178 throw new SavedDataLoadingException("Expected \"" . Trident::TAG_ITEM . "\" NBT tag not found");
179 }
180
181 $item = Item::nbtDeserialize($itemTag);
182 if($item->isNull()){
183 throw new SavedDataLoadingException("Trident item is invalid");
184 }
185 return new Trident(Helper::parseLocation($nbt, $world), $item, null, $nbt);
186 }, [
187 'minecraft:trident', //java
188 'minecraft:thrown_trident', //bedrock
189 'Trident', //backwards compat for people who used #4547 before it was merged, since it was sitting around for 4 years...
190 'ThrownTrident' //as above
191 ]);
192
193 $this->register(Squid::class, function(World $world, CompoundTag $nbt) : Squid{
194 return new Squid(Helper::parseLocation($nbt, $world), $nbt);
195 }, ['Squid', 'minecraft:squid']);
196
197 $this->register(Villager::class, function(World $world, CompoundTag $nbt) : Villager{
198 return new Villager(Helper::parseLocation($nbt, $world), $nbt);
199 }, ['Villager', 'minecraft:villager']);
200
201 $this->register(Zombie::class, function(World $world, CompoundTag $nbt) : Zombie{
202 return new Zombie(Helper::parseLocation($nbt, $world), $nbt);
203 }, ['Zombie', 'minecraft:zombie']);
204
205 $this->register(Human::class, function(World $world, CompoundTag $nbt) : Human{
206 return new Human(Helper::parseLocation($nbt, $world), Human::parseSkinNBT($nbt), $nbt);
207 }, ['Human']);
208 }
209
223 public function register(string $className, \Closure $creationFunc, array $saveNames) : void{
224 if(count($saveNames) === 0){
225 throw new \InvalidArgumentException("At least one save name must be provided");
226 }
227 Utils::testValidInstance($className, Entity::class);
228 Utils::validateCallableSignature(new CallbackType(
229 new ReturnType(Entity::class),
230 new ParameterType("world", World::class),
231 new ParameterType("nbt", CompoundTag::class)
232 ), $creationFunc);
233
234 foreach($saveNames as $name){
235 $this->creationFuncs[$name] = $creationFunc;
236 }
237
238 $this->saveNames[$className] = reset($saveNames);
239 }
240
244 public function isRegistered(string $class) : bool{
245 return isset($this->saveNames[$class]);
246 }
247
254 public function createFromData(World $world, CompoundTag $nbt) : ?Entity{
255 try{
256 $saveId = $nbt->getTag(self::TAG_IDENTIFIER) ?? $nbt->getTag(self::TAG_LEGACY_ID);
257 $func = null;
258 if($saveId instanceof StringTag){
259 $func = $this->creationFuncs[$saveId->getValue()] ?? null;
260 }elseif($saveId instanceof IntTag){ //legacy MCPE format
261 $stringId = LegacyEntityIdToStringIdMap::getInstance()->legacyToString($saveId->getValue() & 0xff);
262 $func = $stringId !== null ? $this->creationFuncs[$stringId] ?? null : null;
263 }
264 if($func === null){
265 return null;
266 }
268 $entity = $func($world, $nbt);
269
270 return $entity;
271 }catch(NbtException $e){
272 throw new SavedDataLoadingException($e->getMessage(), 0, $e);
273 }
274 }
275
276 public function injectSaveId(string $class, CompoundTag $saveData) : void{
277 if(isset($this->saveNames[$class])){
278 $saveData->setTag(self::TAG_IDENTIFIER, new StringTag($this->saveNames[$class]));
279 }else{
280 throw new \InvalidArgumentException("Entity $class is not registered");
281 }
282 }
283
287 public function getSaveId(string $class) : string{
288 if(isset($this->saveNames[$class])){
289 return $this->saveNames[$class];
290 }
291 throw new \InvalidArgumentException("Entity $class is not registered");
292 }
293}
createFromData(World $world, CompoundTag $nbt)
static parseSkinNBT(CompoundTag $nbt)
Definition Human.php:128