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(){
89 return new Arrow(Helper::parseLocation($nbt, $world),
null, $nbt->getByte(Arrow::TAG_CRIT, 0) === 1, $nbt);
90 }, [
'Arrow',
'minecraft:arrow']);
93 return new Egg(Helper::parseLocation($nbt, $world),
null, $nbt);
94 }, [
'Egg',
'minecraft:egg']);
97 return new EndCrystal(Helper::parseLocation($nbt, $world), $nbt);
98 }, [
'EnderCrystal',
'minecraft:ender_crystal']);
101 return new EnderPearl(Helper::parseLocation($nbt, $world),
null, $nbt);
102 }, [
'ThrownEnderpearl',
'minecraft:ender_pearl']);
105 return new ExperienceBottle(Helper::parseLocation($nbt, $world),
null, $nbt);
106 }, [
'ThrownExpBottle',
'minecraft:xp_bottle']);
110 if(($valuePcTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PC)) instanceof
ShortTag){
111 $value = $valuePcTag->getValue();
112 }elseif(($valuePeTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PE)) instanceof
IntTag){
113 $value = $valuePeTag->getValue();
116 return new ExperienceOrb(Helper::parseLocation($nbt, $world), $value, $nbt);
117 }, [
'XPOrb',
'minecraft:xp_orb']);
120 return new FallingBlock(Helper::parseLocation($nbt, $world), FallingBlock::parseBlockNBT(RuntimeBlockStateRegistry::getInstance(), $nbt), $nbt);
121 }, [
'FallingSand',
'minecraft:falling_block']);
124 $itemTag = $nbt->getCompoundTag(ItemEntity::TAG_ITEM);
125 if($itemTag ===
null){
129 $item = Item::nbtDeserialize($itemTag);
133 return new ItemEntity(Helper::parseLocation($nbt, $world), $item, $nbt);
134 }, [
'Item',
'minecraft:item']);
137 $motive = PaintingMotive::getMotiveByName($nbt->getString(Painting::TAG_MOTIVE));
138 if($motive ===
null){
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;
150 return new Painting(Helper::parseLocation($nbt, $world), $blockIn, $facing, $motive, $nbt);
151 }, [
'Painting',
'minecraft:painting']);
154 return new PrimedTNT(Helper::parseLocation($nbt, $world), $nbt);
155 }, [
'PrimedTnt',
'PrimedTNT',
'minecraft:tnt']);
158 return new Snowball(Helper::parseLocation($nbt, $world),
null, $nbt);
159 }, [
'Snowball',
'minecraft:snowball']);
162 $potionType = PotionTypeIdMap::getInstance()->fromId($nbt->getShort(SplashPotion::TAG_POTION_ID, PotionTypeIds::WATER));
163 if($potionType ===
null){
166 return new SplashPotion(Helper::parseLocation($nbt, $world),
null, $potionType, $nbt);
167 }, [
'ThrownPotion',
'minecraft:potion',
'thrownpotion']);
170 return new Squid(Helper::parseLocation($nbt, $world), $nbt);
171 }, [
'Squid',
'minecraft:squid']);
174 return new Villager(Helper::parseLocation($nbt, $world), $nbt);
175 }, [
'Villager',
'minecraft:villager']);
178 return new Zombie(Helper::parseLocation($nbt, $world), $nbt);
179 }, [
'Zombie',
'minecraft:zombie']);
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");
203 Utils::testValidInstance($className, Entity::class);
210 foreach($saveNames as $name){
211 $this->creationFuncs[$name] = $creationFunc;
214 $this->saveNames[$className] = reset($saveNames);
225 $saveId = $nbt->getTag(self::TAG_IDENTIFIER) ?? $nbt->getTag(self::TAG_LEGACY_ID);
228 $func = $this->creationFuncs[$saveId->getValue()] ??
null;
229 }elseif($saveId instanceof
IntTag){
230 $stringId = LegacyEntityIdToStringIdMap::getInstance()->legacyToString($saveId->getValue() & 0xff);
231 $func = $stringId !==
null ? $this->creationFuncs[$stringId] ?? null :
null;
237 $entity = $func($world, $nbt);
240 }
catch(NbtException $e){
241 throw new SavedDataLoadingException($e->getMessage(), 0, $e);
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]));
249 throw new \InvalidArgumentException(
"Entity $class is not registered");
257 if(isset($this->saveNames[$class])){
258 return $this->saveNames[$class];
260 throw new \InvalidArgumentException(
"Entity $class is not registered");