PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
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
58use pocketmine\utils\SingletonTrait;
61use function count;
62use function reset;
63
68final class EntityFactory{
69 use SingletonTrait;
70
71 public const TAG_IDENTIFIER = "identifier"; //TAG_String
72 public const TAG_LEGACY_ID = "id"; //TAG_Int
73
78 private array $creationFuncs = [];
83 private array $saveNames = [];
84
85 public function __construct(){
86 //define legacy save IDs first - use them for saving for maximum compatibility with Minecraft PC
87 //TODO: index them by version to allow proper multi-save compatibility
88
89 $this->register(Arrow::class, function(World $world, CompoundTag $nbt) : Arrow{
90 return new Arrow(Helper::parseLocation($nbt, $world), null, $nbt->getByte(Arrow::TAG_CRIT, 0) === 1, $nbt);
91 }, ['Arrow', 'minecraft:arrow']);
92
93 $this->register(Egg::class, function(World $world, CompoundTag $nbt) : Egg{
94 return new Egg(Helper::parseLocation($nbt, $world), null, $nbt);
95 }, ['Egg', 'minecraft:egg']);
96
97 $this->register(EndCrystal::class, function(World $world, CompoundTag $nbt) : EndCrystal{
98 return new EndCrystal(Helper::parseLocation($nbt, $world), $nbt);
99 }, ['EnderCrystal', 'minecraft:ender_crystal']);
100
101 $this->register(EnderPearl::class, function(World $world, CompoundTag $nbt) : EnderPearl{
102 return new EnderPearl(Helper::parseLocation($nbt, $world), null, $nbt);
103 }, ['ThrownEnderpearl', 'minecraft:ender_pearl']);
104
105 $this->register(ExperienceBottle::class, function(World $world, CompoundTag $nbt) : ExperienceBottle{
106 return new ExperienceBottle(Helper::parseLocation($nbt, $world), null, $nbt);
107 }, ['ThrownExpBottle', 'minecraft:xp_bottle']);
108
109 $this->register(ExperienceOrb::class, function(World $world, CompoundTag $nbt) : ExperienceOrb{
110 $value = 1;
111 if(($valuePcTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PC)) instanceof ShortTag){ //PC
112 $value = $valuePcTag->getValue();
113 }elseif(($valuePeTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PE)) instanceof IntTag){ //PE save format
114 $value = $valuePeTag->getValue();
115 }
116
117 return new ExperienceOrb(Helper::parseLocation($nbt, $world), $value, $nbt);
118 }, ['XPOrb', 'minecraft:xp_orb']);
119
120 $this->register(FallingBlock::class, function(World $world, CompoundTag $nbt) : FallingBlock{
121 return new FallingBlock(Helper::parseLocation($nbt, $world), FallingBlock::parseBlockNBT(RuntimeBlockStateRegistry::getInstance(), $nbt), $nbt);
122 }, ['FallingSand', 'minecraft:falling_block']);
123
124 $this->register(IceBomb::class, function(World $world, CompoundTag $nbt) : IceBomb{
125 return new IceBomb(Helper::parseLocation($nbt, $world), null, $nbt);
126 }, ['minecraft:ice_bomb']);
127
128 $this->register(ItemEntity::class, function(World $world, CompoundTag $nbt) : ItemEntity{
129 $itemTag = $nbt->getCompoundTag(ItemEntity::TAG_ITEM);
130 if($itemTag === null){
131 throw new SavedDataLoadingException("Expected \"" . ItemEntity::TAG_ITEM . "\" NBT tag not found");
132 }
133
134 $item = Item::nbtDeserialize($itemTag);
135 if($item->isNull()){
136 throw new SavedDataLoadingException("Item is invalid");
137 }
138 return new ItemEntity(Helper::parseLocation($nbt, $world), $item, $nbt);
139 }, ['Item', 'minecraft:item']);
140
141 $this->register(Painting::class, function(World $world, CompoundTag $nbt) : Painting{
142 $motive = PaintingMotive::getMotiveByName($nbt->getString(Painting::TAG_MOTIVE));
143 if($motive === null){
144 throw new SavedDataLoadingException("Unknown painting motive");
145 }
146 $blockIn = new Vector3($nbt->getInt(Painting::TAG_TILE_X), $nbt->getInt(Painting::TAG_TILE_Y), $nbt->getInt(Painting::TAG_TILE_Z));
147 if(($directionTag = $nbt->getTag(Painting::TAG_DIRECTION_BE)) instanceof ByteTag){
148 $facing = Painting::DATA_TO_FACING[$directionTag->getValue()] ?? Facing::NORTH;
149 }elseif(($facingTag = $nbt->getTag(Painting::TAG_FACING_JE)) instanceof ByteTag){
150 $facing = Painting::DATA_TO_FACING[$facingTag->getValue()] ?? Facing::NORTH;
151 }else{
152 throw new SavedDataLoadingException("Missing facing info");
153 }
154
155 return new Painting(Helper::parseLocation($nbt, $world), $blockIn, $facing, $motive, $nbt);
156 }, ['Painting', 'minecraft:painting']);
157
158 $this->register(PrimedTNT::class, function(World $world, CompoundTag $nbt) : PrimedTNT{
159 return new PrimedTNT(Helper::parseLocation($nbt, $world), $nbt);
160 }, ['PrimedTnt', 'PrimedTNT', 'minecraft:tnt']);
161
162 $this->register(Snowball::class, function(World $world, CompoundTag $nbt) : Snowball{
163 return new Snowball(Helper::parseLocation($nbt, $world), null, $nbt);
164 }, ['Snowball', 'minecraft:snowball']);
165
166 $this->register(SplashPotion::class, function(World $world, CompoundTag $nbt) : SplashPotion{
167 $potionType = PotionTypeIdMap::getInstance()->fromId($nbt->getShort(SplashPotion::TAG_POTION_ID, PotionTypeIds::WATER));
168 if($potionType === null){
169 throw new SavedDataLoadingException("No such potion type");
170 }
171 return new SplashPotion(Helper::parseLocation($nbt, $world), null, $potionType, $nbt);
172 }, ['ThrownPotion', 'minecraft:potion', 'thrownpotion']);
173
174 $this->register(Squid::class, function(World $world, CompoundTag $nbt) : Squid{
175 return new Squid(Helper::parseLocation($nbt, $world), $nbt);
176 }, ['Squid', 'minecraft:squid']);
177
178 $this->register(Villager::class, function(World $world, CompoundTag $nbt) : Villager{
179 return new Villager(Helper::parseLocation($nbt, $world), $nbt);
180 }, ['Villager', 'minecraft:villager']);
181
182 $this->register(Zombie::class, function(World $world, CompoundTag $nbt) : Zombie{
183 return new Zombie(Helper::parseLocation($nbt, $world), $nbt);
184 }, ['Zombie', 'minecraft:zombie']);
185
186 $this->register(Human::class, function(World $world, CompoundTag $nbt) : Human{
187 return new Human(Helper::parseLocation($nbt, $world), Human::parseSkinNBT($nbt), $nbt);
188 }, ['Human']);
189 }
190
204 public function register(string $className, \Closure $creationFunc, array $saveNames) : void{
205 if(count($saveNames) === 0){
206 throw new \InvalidArgumentException("At least one save name must be provided");
207 }
208 Utils::testValidInstance($className, Entity::class);
209 Utils::validateCallableSignature(new CallbackType(
210 new ReturnType(Entity::class),
211 new ParameterType("world", World::class),
212 new ParameterType("nbt", CompoundTag::class)
213 ), $creationFunc);
214
215 foreach($saveNames as $name){
216 $this->creationFuncs[$name] = $creationFunc;
217 }
218
219 $this->saveNames[$className] = reset($saveNames);
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