-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathEntityFactory.php
More file actions
302 lines (262 loc) · 12 KB
/
EntityFactory.php
File metadata and controls
302 lines (262 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\entity;
use DaveRandom\CallbackValidator\CallbackType;
use DaveRandom\CallbackValidator\ParameterType;
use DaveRandom\CallbackValidator\ReturnType;
use pocketmine\block\RuntimeBlockStateRegistry;
use pocketmine\data\bedrock\LegacyEntityIdToStringIdMap;
use pocketmine\data\bedrock\PotionTypeIdMap;
use pocketmine\data\bedrock\PotionTypeIds;
use pocketmine\data\SavedDataLoadingException;
use pocketmine\entity\EntityDataHelper as Helper;
use pocketmine\entity\object\AreaEffectCloud;
use pocketmine\entity\object\EndCrystal;
use pocketmine\entity\object\ExperienceOrb;
use pocketmine\entity\object\FallingBlock;
use pocketmine\entity\object\ItemEntity;
use pocketmine\entity\object\Painting;
use pocketmine\entity\object\PaintingMotive;
use pocketmine\entity\object\PrimedTNT;
use pocketmine\entity\projectile\Arrow;
use pocketmine\entity\projectile\Egg;
use pocketmine\entity\projectile\EnderPearl;
use pocketmine\entity\projectile\ExperienceBottle;
use pocketmine\entity\projectile\IceBomb;
use pocketmine\entity\projectile\Snowball;
use pocketmine\entity\projectile\SplashPotion;
use pocketmine\entity\projectile\Trident;
use pocketmine\item\Item;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
use pocketmine\nbt\NbtException;
use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ShortTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\utils\SingletonTrait;
use pocketmine\utils\Utils;
use pocketmine\world\World;
use function count;
use function reset;
/**
* This class manages the creation of entities loaded from disk.
* You need to register your entity into this factory if you want to load/save your entity on disk (saving with chunks).
*/
final class EntityFactory{
use SingletonTrait;
public const TAG_IDENTIFIER = "identifier"; //TAG_String
public const TAG_LEGACY_ID = "id"; //TAG_Int
/**
* @var \Closure[] save ID => creator function
* @phpstan-var array<int|string, \Closure(World, CompoundTag) : Entity>
*/
private array $creationFuncs = [];
/**
* @var string[]
* @phpstan-var array<class-string<Entity>, string>
*/
private array $saveNames = [];
public function __construct(){
//define legacy save IDs first - use them for saving for maximum compatibility with Minecraft PC
//TODO: index them by version to allow proper multi-save compatibility
$this->register(AreaEffectCloud::class, function(World $world, CompoundTag $nbt) : AreaEffectCloud{
return new AreaEffectCloud(Helper::parseLocation($nbt, $world), $nbt);
}, ['AreaEffectCloud', 'minecraft:area_effect_cloud']);
$this->register(Arrow::class, function(World $world, CompoundTag $nbt) : Arrow{
return new Arrow(Helper::parseLocation($nbt, $world), null, $nbt->getByte(Arrow::TAG_CRIT, 0) === 1, $nbt);
}, ['Arrow', 'minecraft:arrow']);
$this->register(Egg::class, function(World $world, CompoundTag $nbt) : Egg{
return new Egg(Helper::parseLocation($nbt, $world), null, $nbt);
}, ['Egg', 'minecraft:egg']);
$this->register(EndCrystal::class, function(World $world, CompoundTag $nbt) : EndCrystal{
return new EndCrystal(Helper::parseLocation($nbt, $world), $nbt);
}, ['EnderCrystal', 'minecraft:ender_crystal']);
$this->register(EnderPearl::class, function(World $world, CompoundTag $nbt) : EnderPearl{
return new EnderPearl(Helper::parseLocation($nbt, $world), null, $nbt);
}, ['ThrownEnderpearl', 'minecraft:ender_pearl']);
$this->register(ExperienceBottle::class, function(World $world, CompoundTag $nbt) : ExperienceBottle{
return new ExperienceBottle(Helper::parseLocation($nbt, $world), null, $nbt);
}, ['ThrownExpBottle', 'minecraft:xp_bottle']);
$this->register(ExperienceOrb::class, function(World $world, CompoundTag $nbt) : ExperienceOrb{
$value = 1;
if(($valuePcTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PC)) instanceof ShortTag){ //PC
$value = $valuePcTag->getValue();
}elseif(($valuePeTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PE)) instanceof IntTag){ //PE save format
$value = $valuePeTag->getValue();
}
return new ExperienceOrb(Helper::parseLocation($nbt, $world), $value, $nbt);
}, ['XPOrb', 'minecraft:xp_orb']);
$this->register(FallingBlock::class, function(World $world, CompoundTag $nbt) : FallingBlock{
return new FallingBlock(Helper::parseLocation($nbt, $world), FallingBlock::parseBlockNBT(RuntimeBlockStateRegistry::getInstance(), $nbt), $nbt);
}, ['FallingSand', 'minecraft:falling_block']);
$this->register(IceBomb::class, function(World $world, CompoundTag $nbt) : IceBomb{
return new IceBomb(Helper::parseLocation($nbt, $world), null, $nbt);
}, ['minecraft:ice_bomb']);
$this->register(ItemEntity::class, function(World $world, CompoundTag $nbt) : ItemEntity{
$itemTag = $nbt->getCompoundTag(ItemEntity::TAG_ITEM);
if($itemTag === null){
throw new SavedDataLoadingException("Expected \"" . ItemEntity::TAG_ITEM . "\" NBT tag not found");
}
$item = Item::nbtDeserialize($itemTag);
if($item->isNull()){
throw new SavedDataLoadingException("Item is invalid");
}
return new ItemEntity(Helper::parseLocation($nbt, $world), $item, $nbt);
}, ['Item', 'minecraft:item']);
$this->register(Painting::class, function(World $world, CompoundTag $nbt) : Painting{
$motive = PaintingMotive::getMotiveByName($nbt->getString(Painting::TAG_MOTIVE));
if($motive === null){
throw new SavedDataLoadingException("Unknown painting motive");
}
$blockIn = new Vector3($nbt->getInt(Painting::TAG_TILE_X), $nbt->getInt(Painting::TAG_TILE_Y), $nbt->getInt(Painting::TAG_TILE_Z));
if(($directionTag = $nbt->getTag(Painting::TAG_DIRECTION_BE)) instanceof ByteTag){
$facing = Painting::DATA_TO_FACING[$directionTag->getValue()] ?? Facing::NORTH;
}elseif(($facingTag = $nbt->getTag(Painting::TAG_FACING_JE)) instanceof ByteTag){
$facing = Painting::DATA_TO_FACING[$facingTag->getValue()] ?? Facing::NORTH;
}else{
throw new SavedDataLoadingException("Missing facing info");
}
return new Painting(Helper::parseLocation($nbt, $world), $blockIn, $facing, $motive, $nbt);
}, ['Painting', 'minecraft:painting']);
$this->register(PrimedTNT::class, function(World $world, CompoundTag $nbt) : PrimedTNT{
return new PrimedTNT(Helper::parseLocation($nbt, $world), $nbt);
}, ['PrimedTnt', 'PrimedTNT', 'minecraft:tnt']);
$this->register(Snowball::class, function(World $world, CompoundTag $nbt) : Snowball{
return new Snowball(Helper::parseLocation($nbt, $world), null, $nbt);
}, ['Snowball', 'minecraft:snowball']);
$this->register(SplashPotion::class, function(World $world, CompoundTag $nbt) : SplashPotion{
$potionType = PotionTypeIdMap::getInstance()->fromId($nbt->getShort(SplashPotion::TAG_POTION_ID, PotionTypeIds::WATER));
if($potionType === null){
throw new SavedDataLoadingException("No such potion type");
}
return new SplashPotion(Helper::parseLocation($nbt, $world), null, $potionType, $nbt);
}, ['ThrownPotion', 'minecraft:potion', 'thrownpotion']);
$this->register(Trident::class, function(World $world, CompoundTag $nbt) : Trident{
$itemTag = $nbt->getCompoundTag(Trident::TAG_ITEM);
if($itemTag === null){
throw new SavedDataLoadingException("Expected \"" . Trident::TAG_ITEM . "\" NBT tag not found");
}
$item = Item::nbtDeserialize($itemTag);
if($item->isNull()){
throw new SavedDataLoadingException("Trident item is invalid");
}
return new Trident(Helper::parseLocation($nbt, $world), $item, null, $nbt);
}, [
'minecraft:trident', //java
'minecraft:thrown_trident', //bedrock
'Trident', //backwards compat for people who used #4547 before it was merged, since it was sitting around for 4 years...
'ThrownTrident' //as above
]);
$this->register(LightningBolt::class, function(World $world, CompoundTag $nbt) : LightningBolt{
return new LightningBolt(Helper::parseLocation($nbt, $world), $nbt);
}, ['lightning_bolt', 'minecraft:lightning_bolt']);
$this->register(Squid::class, function(World $world, CompoundTag $nbt) : Squid{
return new Squid(Helper::parseLocation($nbt, $world), $nbt);
}, ['Squid', 'minecraft:squid']);
$this->register(Villager::class, function(World $world, CompoundTag $nbt) : Villager{
return new Villager(Helper::parseLocation($nbt, $world), $nbt);
}, ['Villager', 'minecraft:villager']);
$this->register(Zombie::class, function(World $world, CompoundTag $nbt) : Zombie{
return new Zombie(Helper::parseLocation($nbt, $world), $nbt);
}, ['Zombie', 'minecraft:zombie']);
$this->register(Human::class, function(World $world, CompoundTag $nbt) : Human{
return new Human(Helper::parseLocation($nbt, $world), Human::parseSkinNBT($nbt), $nbt);
}, ['Human']);
}
/**
* Registers an entity type into the index.
*
* @param string $className Class that extends Entity
* @param string[] $saveNames An array of save names which this entity might be saved under.
* @phpstan-param class-string<Entity> $className
* @phpstan-param list<string> $saveNames
* @phpstan-param \Closure(World $world, CompoundTag $nbt) : Entity $creationFunc
*
* NOTE: The first save name in the $saveNames array will be used when saving the entity to disk.
*
* @throws \InvalidArgumentException
*/
public function register(string $className, \Closure $creationFunc, array $saveNames) : void{
if(count($saveNames) === 0){
throw new \InvalidArgumentException("At least one save name must be provided");
}
Utils::testValidInstance($className, Entity::class);
Utils::validateCallableSignature(new CallbackType(
new ReturnType(Entity::class),
new ParameterType("world", World::class),
new ParameterType("nbt", CompoundTag::class)
), $creationFunc);
foreach($saveNames as $name){
$this->creationFuncs[$name] = $creationFunc;
}
$this->saveNames[$className] = reset($saveNames);
}
/**
* @phpstan-param class-string<Entity> $class
*/
public function isRegistered(string $class) : bool{
return isset($this->saveNames[$class]);
}
/**
* Creates an entity from data stored on a chunk.
*
* @throws SavedDataLoadingException
* @internal
*/
public function createFromData(World $world, CompoundTag $nbt) : ?Entity{
try{
$saveId = $nbt->getTag(self::TAG_IDENTIFIER) ?? $nbt->getTag(self::TAG_LEGACY_ID);
$func = null;
if($saveId instanceof StringTag){
$func = $this->creationFuncs[$saveId->getValue()] ?? null;
}elseif($saveId instanceof IntTag){ //legacy MCPE format
$stringId = LegacyEntityIdToStringIdMap::getInstance()->legacyToString($saveId->getValue() & 0xff);
$func = $stringId !== null ? $this->creationFuncs[$stringId] ?? null : null;
}
if($func === null){
return null;
}
/** @var Entity $entity */
$entity = $func($world, $nbt);
return $entity;
}catch(NbtException $e){
throw new SavedDataLoadingException($e->getMessage(), 0, $e);
}
}
public function injectSaveId(string $class, CompoundTag $saveData) : void{
if(isset($this->saveNames[$class])){
$saveData->setTag(self::TAG_IDENTIFIER, new StringTag($this->saveNames[$class]));
}else{
throw new \InvalidArgumentException("Entity $class is not registered");
}
}
/**
* @phpstan-param class-string<Entity> $class
*/
public function getSaveId(string $class) : string{
if(isset($this->saveNames[$class])){
return $this->saveNames[$class];
}
throw new \InvalidArgumentException("Entity $class is not registered");
}
}