70 public const TAG_IDENTIFIER =
"identifier";
71 public const TAG_LEGACY_ID =
"id";
77 private array $creationFuncs = [];
82 private array $saveNames = [];
84 public function __construct(){
90 }, [
'AreaEffectCloud',
'minecraft:area_effect_cloud']);
93 return new Arrow(Helper::parseLocation($nbt, $world),
null, $nbt->getByte(Arrow::TAG_CRIT, 0) === 1, $nbt);
94 }, [
'Arrow',
'minecraft:arrow']);
97 return new Egg(Helper::parseLocation($nbt, $world),
null, $nbt);
98 }, [
'Egg',
'minecraft:egg']);
101 return new EndCrystal(Helper::parseLocation($nbt, $world), $nbt);
102 }, [
'EnderCrystal',
'minecraft:ender_crystal']);
105 return new EnderPearl(Helper::parseLocation($nbt, $world),
null, $nbt);
106 }, [
'ThrownEnderpearl',
'minecraft:ender_pearl']);
109 return new ExperienceBottle(Helper::parseLocation($nbt, $world),
null, $nbt);
110 }, [
'ThrownExpBottle',
'minecraft:xp_bottle']);
114 if(($valuePcTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PC)) instanceof
ShortTag){
115 $value = $valuePcTag->getValue();
116 }elseif(($valuePeTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PE)) instanceof
IntTag){
117 $value = $valuePeTag->getValue();
120 return new ExperienceOrb(Helper::parseLocation($nbt, $world), $value, $nbt);
121 }, [
'XPOrb',
'minecraft:xp_orb']);
124 return new FallingBlock(Helper::parseLocation($nbt, $world), FallingBlock::parseBlockNBT(RuntimeBlockStateRegistry::getInstance(), $nbt), $nbt);
125 }, [
'FallingSand',
'minecraft:falling_block']);
128 return new IceBomb(Helper::parseLocation($nbt, $world),
null, $nbt);
129 }, [
'minecraft:ice_bomb']);
132 $itemTag = $nbt->getCompoundTag(ItemEntity::TAG_ITEM);
133 if($itemTag ===
null){
137 $item = Item::nbtDeserialize($itemTag);
141 return new ItemEntity(Helper::parseLocation($nbt, $world), $item, $nbt);
142 }, [
'Item',
'minecraft:item']);
145 $motive = PaintingMotive::getMotiveByName($nbt->getString(Painting::TAG_MOTIVE));
146 if($motive ===
null){
149 $blockIn =
new Vector3($nbt->getInt(Painting::TAG_TILE_X), $nbt->getInt(Painting::TAG_TILE_Y), $nbt->getInt(Painting::TAG_TILE_Z));
150 if(($directionTag = $nbt->getTag(Painting::TAG_DIRECTION_BE)) instanceof
ByteTag){
151 $facing = Painting::DATA_TO_FACING[$directionTag->getValue()] ?? Facing::NORTH;
152 }elseif(($facingTag = $nbt->getTag(Painting::TAG_FACING_JE)) instanceof
ByteTag){
153 $facing = Painting::DATA_TO_FACING[$facingTag->getValue()] ?? Facing::NORTH;
158 return new Painting(Helper::parseLocation($nbt, $world), $blockIn, $facing, $motive, $nbt);
159 }, [
'Painting',
'minecraft:painting']);
162 return new PrimedTNT(Helper::parseLocation($nbt, $world), $nbt);
163 }, [
'PrimedTnt',
'PrimedTNT',
'minecraft:tnt']);
166 return new Snowball(Helper::parseLocation($nbt, $world),
null, $nbt);
167 }, [
'Snowball',
'minecraft:snowball']);
170 $potionType = PotionTypeIdMap::getInstance()->fromId($nbt->getShort(SplashPotion::TAG_POTION_ID, PotionTypeIds::WATER));
171 if($potionType ===
null){
174 return new SplashPotion(Helper::parseLocation($nbt, $world),
null, $potionType, $nbt);
175 }, [
'ThrownPotion',
'minecraft:potion',
'thrownpotion']);
178 $itemTag = $nbt->getCompoundTag(Trident::TAG_ITEM);
179 if($itemTag ===
null){
183 $item = Item::nbtDeserialize($itemTag);
187 return new Trident(Helper::parseLocation($nbt, $world), $item,
null, $nbt);
190 'minecraft:thrown_trident',
196 return new Squid(Helper::parseLocation($nbt, $world), $nbt);
197 }, [
'Squid',
'minecraft:squid']);
200 return new Villager(Helper::parseLocation($nbt, $world), $nbt);
201 }, [
'Villager',
'minecraft:villager']);
204 return new Zombie(Helper::parseLocation($nbt, $world), $nbt);
205 }, [
'Zombie',
'minecraft:zombie']);
225 public function register(
string $className, \Closure $creationFunc, array $saveNames) : void{
226 if(count($saveNames) === 0){
227 throw new \InvalidArgumentException(
"At least one save name must be provided");
229 Utils::testValidInstance($className, Entity::class);
232 foreach($saveNames as $name){
233 $this->creationFuncs[$name] = $creationFunc;
236 $this->saveNames[$className] = reset($saveNames);
243 return isset($this->saveNames[$class]);
254 $saveId = $nbt->getTag(self::TAG_IDENTIFIER) ?? $nbt->getTag(self::TAG_LEGACY_ID);
257 $func = $this->creationFuncs[$saveId->getValue()] ??
null;
258 }elseif($saveId instanceof
IntTag){
259 $stringId = LegacyEntityIdToStringIdMap::getInstance()->legacyToString($saveId->getValue() & 0xff);
260 $func = $stringId !==
null ? $this->creationFuncs[$stringId] ?? null :
null;
266 $entity = $func($world, $nbt);
269 }
catch(NbtException $e){
270 throw new SavedDataLoadingException($e->getMessage(), 0, $e);
274 public function injectSaveId(
string $class, CompoundTag $saveData) : void{
275 if(isset($this->saveNames[$class])){
276 $saveData->setTag(self::TAG_IDENTIFIER,
new StringTag($this->saveNames[$class]));
278 throw new \InvalidArgumentException(
"Entity $class is not registered");
286 if(isset($this->saveNames[$class])){
287 return $this->saveNames[$class];
289 throw new \InvalidArgumentException(
"Entity $class is not registered");