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