PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
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(Arrow::class, function(World $world, CompoundTag $nbt) : Arrow{
89 return new Arrow(Helper::parseLocation($nbt, $world), null, $nbt->getByte(Arrow::TAG_CRIT, 0) === 1, $nbt);
90 }, ['Arrow', 'minecraft:arrow']);
91
92 $this->register(Egg::class, function(World $world, CompoundTag $nbt) : Egg{
93 return new Egg(Helper::parseLocation($nbt, $world), null, $nbt);
94 }, ['Egg', 'minecraft:egg']);
95
96 $this->register(EndCrystal::class, function(World $world, CompoundTag $nbt) : EndCrystal{
97 return new EndCrystal(Helper::parseLocation($nbt, $world), $nbt);
98 }, ['EnderCrystal', 'minecraft:ender_crystal']);
99
100 $this->register(EnderPearl::class, function(World $world, CompoundTag $nbt) : EnderPearl{
101 return new EnderPearl(Helper::parseLocation($nbt, $world), null, $nbt);
102 }, ['ThrownEnderpearl', 'minecraft:ender_pearl']);
103
104 $this->register(ExperienceBottle::class, function(World $world, CompoundTag $nbt) : ExperienceBottle{
105 return new ExperienceBottle(Helper::parseLocation($nbt, $world), null, $nbt);
106 }, ['ThrownExpBottle', 'minecraft:xp_bottle']);
107
108 $this->register(ExperienceOrb::class, function(World $world, CompoundTag $nbt) : ExperienceOrb{
109 $value = 1;
110 if(($valuePcTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PC)) instanceof ShortTag){ //PC
111 $value = $valuePcTag->getValue();
112 }elseif(($valuePeTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PE)) instanceof IntTag){ //PE save format
113 $value = $valuePeTag->getValue();
114 }
115
116 return new ExperienceOrb(Helper::parseLocation($nbt, $world), $value, $nbt);
117 }, ['XPOrb', 'minecraft:xp_orb']);
118
119 $this->register(FallingBlock::class, function(World $world, CompoundTag $nbt) : FallingBlock{
120 return new FallingBlock(Helper::parseLocation($nbt, $world), FallingBlock::parseBlockNBT(RuntimeBlockStateRegistry::getInstance(), $nbt), $nbt);
121 }, ['FallingSand', 'minecraft:falling_block']);
122
123 $this->register(ItemEntity::class, function(World $world, CompoundTag $nbt) : ItemEntity{
124 $itemTag = $nbt->getCompoundTag(ItemEntity::TAG_ITEM);
125 if($itemTag === null){
126 throw new SavedDataLoadingException("Expected \"" . ItemEntity::TAG_ITEM . "\" NBT tag not found");
127 }
128
129 $item = Item::nbtDeserialize($itemTag);
130 if($item->isNull()){
131 throw new SavedDataLoadingException("Item is invalid");
132 }
133 return new ItemEntity(Helper::parseLocation($nbt, $world), $item, $nbt);
134 }, ['Item', 'minecraft:item']);
135
136 $this->register(Painting::class, function(World $world, CompoundTag $nbt) : Painting{
137 $motive = PaintingMotive::getMotiveByName($nbt->getString(Painting::TAG_MOTIVE));
138 if($motive === null){
139 throw new SavedDataLoadingException("Unknown painting motive");
140 }
141 $blockIn = new Vector3($nbt->getInt(Painting::TAG_TILE_X), $nbt->getInt(Painting::TAG_TILE_Y), $nbt->getInt(Painting::TAG_TILE_Z));
142 if(($directionTag = $nbt->getTag(Painting::TAG_DIRECTION_BE)) instanceof ByteTag){
143 $facing = Painting::DATA_TO_FACING[$directionTag->getValue()] ?? Facing::NORTH;
144 }elseif(($facingTag = $nbt->getTag(Painting::TAG_FACING_JE)) instanceof ByteTag){
145 $facing = Painting::DATA_TO_FACING[$facingTag->getValue()] ?? Facing::NORTH;
146 }else{
147 throw new SavedDataLoadingException("Missing facing info");
148 }
149
150 return new Painting(Helper::parseLocation($nbt, $world), $blockIn, $facing, $motive, $nbt);
151 }, ['Painting', 'minecraft:painting']);
152
153 $this->register(PrimedTNT::class, function(World $world, CompoundTag $nbt) : PrimedTNT{
154 return new PrimedTNT(Helper::parseLocation($nbt, $world), $nbt);
155 }, ['PrimedTnt', 'PrimedTNT', 'minecraft:tnt']);
156
157 $this->register(Snowball::class, function(World $world, CompoundTag $nbt) : Snowball{
158 return new Snowball(Helper::parseLocation($nbt, $world), null, $nbt);
159 }, ['Snowball', 'minecraft:snowball']);
160
161 $this->register(SplashPotion::class, function(World $world, CompoundTag $nbt) : SplashPotion{
162 $potionType = PotionTypeIdMap::getInstance()->fromId($nbt->getShort(SplashPotion::TAG_POTION_ID, PotionTypeIds::WATER));
163 if($potionType === null){
164 throw new SavedDataLoadingException("No such potion type");
165 }
166 return new SplashPotion(Helper::parseLocation($nbt, $world), null, $potionType, $nbt);
167 }, ['ThrownPotion', 'minecraft:potion', 'thrownpotion']);
168
169 $this->register(Squid::class, function(World $world, CompoundTag $nbt) : Squid{
170 return new Squid(Helper::parseLocation($nbt, $world), $nbt);
171 }, ['Squid', 'minecraft:squid']);
172
173 $this->register(Villager::class, function(World $world, CompoundTag $nbt) : Villager{
174 return new Villager(Helper::parseLocation($nbt, $world), $nbt);
175 }, ['Villager', 'minecraft:villager']);
176
177 $this->register(Zombie::class, function(World $world, CompoundTag $nbt) : Zombie{
178 return new Zombie(Helper::parseLocation($nbt, $world), $nbt);
179 }, ['Zombie', 'minecraft:zombie']);
180
181 $this->register(Human::class, function(World $world, CompoundTag $nbt) : Human{
182 return new Human(Helper::parseLocation($nbt, $world), Human::parseSkinNBT($nbt), $nbt);
183 }, ['Human']);
184 }
185
199 public function register(string $className, \Closure $creationFunc, array $saveNames) : void{
200 if(count($saveNames) === 0){
201 throw new \InvalidArgumentException("At least one save name must be provided");
202 }
203 Utils::testValidInstance($className, Entity::class);
204 Utils::validateCallableSignature(new CallbackType(
205 new ReturnType(Entity::class),
206 new ParameterType("world", World::class),
207 new ParameterType("nbt", CompoundTag::class)
208 ), $creationFunc);
209
210 foreach($saveNames as $name){
211 $this->creationFuncs[$name] = $creationFunc;
212 }
213
214 $this->saveNames[$className] = reset($saveNames);
215 }
216
223 public function createFromData(World $world, CompoundTag $nbt) : ?Entity{
224 try{
225 $saveId = $nbt->getTag(self::TAG_IDENTIFIER) ?? $nbt->getTag(self::TAG_LEGACY_ID);
226 $func = null;
227 if($saveId instanceof StringTag){
228 $func = $this->creationFuncs[$saveId->getValue()] ?? null;
229 }elseif($saveId instanceof IntTag){ //legacy MCPE format
230 $stringId = LegacyEntityIdToStringIdMap::getInstance()->legacyToString($saveId->getValue() & 0xff);
231 $func = $stringId !== null ? $this->creationFuncs[$stringId] ?? null : null;
232 }
233 if($func === null){
234 return null;
235 }
237 $entity = $func($world, $nbt);
238
239 return $entity;
240 }catch(NbtException $e){
241 throw new SavedDataLoadingException($e->getMessage(), 0, $e);
242 }
243 }
244
245 public function injectSaveId(string $class, CompoundTag $saveData) : void{
246 if(isset($this->saveNames[$class])){
247 $saveData->setTag(self::TAG_IDENTIFIER, new StringTag($this->saveNames[$class]));
248 }else{
249 throw new \InvalidArgumentException("Entity $class is not registered");
250 }
251 }
252
256 public function getSaveId(string $class) : string{
257 if(isset($this->saveNames[$class])){
258 return $this->saveNames[$class];
259 }
260 throw new \InvalidArgumentException("Entity $class is not registered");
261 }
262}
createFromData(World $world, CompoundTag $nbt)
static parseSkinNBT(CompoundTag $nbt)
Definition Human.php:127