PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
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
56use pocketmine\utils\SingletonTrait;
59use function count;
60use function reset;
61
66final class EntityFactory{
67 use SingletonTrait;
68
69 public const TAG_IDENTIFIER = "identifier"; //TAG_String
70 public const TAG_LEGACY_ID = "id"; //TAG_Int
71
76 private array $creationFuncs = [];
81 private array $saveNames = [];
82
83 public function __construct(){
84 //define legacy save IDs first - use them for saving for maximum compatibility with Minecraft PC
85 //TODO: index them by version to allow proper multi-save compatibility
86
87 $this->register(Arrow::class, function(World $world, CompoundTag $nbt) : Arrow{
88 return new Arrow(Helper::parseLocation($nbt, $world), null, $nbt->getByte(Arrow::TAG_CRIT, 0) === 1, $nbt);
89 }, ['Arrow', 'minecraft:arrow']);
90
91 $this->register(Egg::class, function(World $world, CompoundTag $nbt) : Egg{
92 return new Egg(Helper::parseLocation($nbt, $world), null, $nbt);
93 }, ['Egg', 'minecraft:egg']);
94
95 $this->register(EnderPearl::class, function(World $world, CompoundTag $nbt) : EnderPearl{
96 return new EnderPearl(Helper::parseLocation($nbt, $world), null, $nbt);
97 }, ['ThrownEnderpearl', 'minecraft:ender_pearl']);
98
99 $this->register(ExperienceBottle::class, function(World $world, CompoundTag $nbt) : ExperienceBottle{
100 return new ExperienceBottle(Helper::parseLocation($nbt, $world), null, $nbt);
101 }, ['ThrownExpBottle', 'minecraft:xp_bottle']);
102
103 $this->register(ExperienceOrb::class, function(World $world, CompoundTag $nbt) : ExperienceOrb{
104 $value = 1;
105 if(($valuePcTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PC)) instanceof ShortTag){ //PC
106 $value = $valuePcTag->getValue();
107 }elseif(($valuePeTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PE)) instanceof IntTag){ //PE save format
108 $value = $valuePeTag->getValue();
109 }
110
111 return new ExperienceOrb(Helper::parseLocation($nbt, $world), $value, $nbt);
112 }, ['XPOrb', 'minecraft:xp_orb']);
113
114 $this->register(FallingBlock::class, function(World $world, CompoundTag $nbt) : FallingBlock{
115 return new FallingBlock(Helper::parseLocation($nbt, $world), FallingBlock::parseBlockNBT(RuntimeBlockStateRegistry::getInstance(), $nbt), $nbt);
116 }, ['FallingSand', 'minecraft:falling_block']);
117
118 $this->register(ItemEntity::class, function(World $world, CompoundTag $nbt) : ItemEntity{
119 $itemTag = $nbt->getCompoundTag(ItemEntity::TAG_ITEM);
120 if($itemTag === null){
121 throw new SavedDataLoadingException("Expected \"" . ItemEntity::TAG_ITEM . "\" NBT tag not found");
122 }
123
124 $item = Item::nbtDeserialize($itemTag);
125 if($item->isNull()){
126 throw new SavedDataLoadingException("Item is invalid");
127 }
128 return new ItemEntity(Helper::parseLocation($nbt, $world), $item, $nbt);
129 }, ['Item', 'minecraft:item']);
130
131 $this->register(Painting::class, function(World $world, CompoundTag $nbt) : Painting{
132 $motive = PaintingMotive::getMotiveByName($nbt->getString(Painting::TAG_MOTIVE));
133 if($motive === null){
134 throw new SavedDataLoadingException("Unknown painting motive");
135 }
136 $blockIn = new Vector3($nbt->getInt(Painting::TAG_TILE_X), $nbt->getInt(Painting::TAG_TILE_Y), $nbt->getInt(Painting::TAG_TILE_Z));
137 if(($directionTag = $nbt->getTag(Painting::TAG_DIRECTION_BE)) instanceof ByteTag){
138 $facing = Painting::DATA_TO_FACING[$directionTag->getValue()] ?? Facing::NORTH;
139 }elseif(($facingTag = $nbt->getTag(Painting::TAG_FACING_JE)) instanceof ByteTag){
140 $facing = Painting::DATA_TO_FACING[$facingTag->getValue()] ?? Facing::NORTH;
141 }else{
142 throw new SavedDataLoadingException("Missing facing info");
143 }
144
145 return new Painting(Helper::parseLocation($nbt, $world), $blockIn, $facing, $motive, $nbt);
146 }, ['Painting', 'minecraft:painting']);
147
148 $this->register(PrimedTNT::class, function(World $world, CompoundTag $nbt) : PrimedTNT{
149 return new PrimedTNT(Helper::parseLocation($nbt, $world), $nbt);
150 }, ['PrimedTnt', 'PrimedTNT', 'minecraft:tnt']);
151
152 $this->register(Snowball::class, function(World $world, CompoundTag $nbt) : Snowball{
153 return new Snowball(Helper::parseLocation($nbt, $world), null, $nbt);
154 }, ['Snowball', 'minecraft:snowball']);
155
156 $this->register(SplashPotion::class, function(World $world, CompoundTag $nbt) : SplashPotion{
157 $potionType = PotionTypeIdMap::getInstance()->fromId($nbt->getShort(SplashPotion::TAG_POTION_ID, PotionTypeIds::WATER));
158 if($potionType === null){
159 throw new SavedDataLoadingException("No such potion type");
160 }
161 return new SplashPotion(Helper::parseLocation($nbt, $world), null, $potionType, $nbt);
162 }, ['ThrownPotion', 'minecraft:potion', 'thrownpotion']);
163
164 $this->register(Squid::class, function(World $world, CompoundTag $nbt) : Squid{
165 return new Squid(Helper::parseLocation($nbt, $world), $nbt);
166 }, ['Squid', 'minecraft:squid']);
167
168 $this->register(Villager::class, function(World $world, CompoundTag $nbt) : Villager{
169 return new Villager(Helper::parseLocation($nbt, $world), $nbt);
170 }, ['Villager', 'minecraft:villager']);
171
172 $this->register(Zombie::class, function(World $world, CompoundTag $nbt) : Zombie{
173 return new Zombie(Helper::parseLocation($nbt, $world), $nbt);
174 }, ['Zombie', 'minecraft:zombie']);
175
176 $this->register(Human::class, function(World $world, CompoundTag $nbt) : Human{
177 return new Human(Helper::parseLocation($nbt, $world), Human::parseSkinNBT($nbt), $nbt);
178 }, ['Human']);
179 }
180
194 public function register(string $className, \Closure $creationFunc, array $saveNames) : void{
195 if(count($saveNames) === 0){
196 throw new \InvalidArgumentException("At least one save name must be provided");
197 }
198 Utils::testValidInstance($className, Entity::class);
200 new ReturnType(Entity::class),
201 new ParameterType("world", World::class),
202 new ParameterType("nbt", CompoundTag::class)
203 ), $creationFunc);
204
205 foreach($saveNames as $name){
206 $this->creationFuncs[$name] = $creationFunc;
207 }
208
209 $this->saveNames[$className] = reset($saveNames);
210 }
211
218 public function createFromData(World $world, CompoundTag $nbt) : ?Entity{
219 try{
220 $saveId = $nbt->getTag(self::TAG_IDENTIFIER) ?? $nbt->getTag(self::TAG_LEGACY_ID);
221 $func = null;
222 if($saveId instanceof StringTag){
223 $func = $this->creationFuncs[$saveId->getValue()] ?? null;
224 }elseif($saveId instanceof IntTag){ //legacy MCPE format
225 $stringId = LegacyEntityIdToStringIdMap::getInstance()->legacyToString($saveId->getValue() & 0xff);
226 $func = $stringId !== null ? $this->creationFuncs[$stringId] ?? null : null;
227 }
228 if($func === null){
229 return null;
230 }
232 $entity = $func($world, $nbt);
233
234 return $entity;
235 }catch(NbtException $e){
236 throw new SavedDataLoadingException($e->getMessage(), 0, $e);
237 }
238 }
239
240 public function injectSaveId(string $class, CompoundTag $saveData) : void{
241 if(isset($this->saveNames[$class])){
242 $saveData->setTag(self::TAG_IDENTIFIER, new StringTag($this->saveNames[$class]));
243 }else{
244 throw new \InvalidArgumentException("Entity $class is not registered");
245 }
246 }
247
251 public function getSaveId(string $class) : string{
252 if(isset($this->saveNames[$class])){
253 return $this->saveNames[$class];
254 }
255 throw new \InvalidArgumentException("Entity $class is not registered");
256 }
257}
createFromData(World $world, CompoundTag $nbt)
static parseSkinNBT(CompoundTag $nbt)
Definition: Human.php:127
static nbtDeserialize(CompoundTag $tag)
Definition: Item.php:740
static validateCallableSignature(callable|CallbackType $signature, callable $subject)
Definition: Utils.php:571
static testValidInstance(string $className, string $baseName)
Definition: Utils.php:539