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