71 public const TAG_IDENTIFIER =
"identifier";
72 public const TAG_LEGACY_ID =
"id";
78 private array $creationFuncs = [];
83 private array $saveNames = [];
85 public function __construct(){
90 return new Arrow(Helper::parseLocation($nbt, $world),
null, $nbt->getByte(Arrow::TAG_CRIT, 0) === 1, $nbt);
91 }, [
'Arrow',
'minecraft:arrow']);
94 return new Egg(Helper::parseLocation($nbt, $world),
null, $nbt);
95 }, [
'Egg',
'minecraft:egg']);
98 return new EndCrystal(Helper::parseLocation($nbt, $world), $nbt);
99 }, [
'EnderCrystal',
'minecraft:ender_crystal']);
102 return new EnderPearl(Helper::parseLocation($nbt, $world),
null, $nbt);
103 }, [
'ThrownEnderpearl',
'minecraft:ender_pearl']);
106 return new ExperienceBottle(Helper::parseLocation($nbt, $world),
null, $nbt);
107 }, [
'ThrownExpBottle',
'minecraft:xp_bottle']);
111 if(($valuePcTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PC)) instanceof
ShortTag){
112 $value = $valuePcTag->getValue();
113 }elseif(($valuePeTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PE)) instanceof
IntTag){
114 $value = $valuePeTag->getValue();
117 return new ExperienceOrb(Helper::parseLocation($nbt, $world), $value, $nbt);
118 }, [
'XPOrb',
'minecraft:xp_orb']);
121 return new FallingBlock(Helper::parseLocation($nbt, $world), FallingBlock::parseBlockNBT(RuntimeBlockStateRegistry::getInstance(), $nbt), $nbt);
122 }, [
'FallingSand',
'minecraft:falling_block']);
125 return new IceBomb(Helper::parseLocation($nbt, $world),
null, $nbt);
126 }, [
'minecraft:ice_bomb']);
129 $itemTag = $nbt->getCompoundTag(ItemEntity::TAG_ITEM);
130 if($itemTag ===
null){
134 $item = Item::nbtDeserialize($itemTag);
138 return new ItemEntity(Helper::parseLocation($nbt, $world), $item, $nbt);
139 }, [
'Item',
'minecraft:item']);
142 $motive = PaintingMotive::getMotiveByName($nbt->getString(Painting::TAG_MOTIVE));
143 if($motive ===
null){
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;
155 return new Painting(Helper::parseLocation($nbt, $world), $blockIn, $facing, $motive, $nbt);
156 }, [
'Painting',
'minecraft:painting']);
159 return new PrimedTNT(Helper::parseLocation($nbt, $world), $nbt);
160 }, [
'PrimedTnt',
'PrimedTNT',
'minecraft:tnt']);
163 return new Snowball(Helper::parseLocation($nbt, $world),
null, $nbt);
164 }, [
'Snowball',
'minecraft:snowball']);
167 $potionType = PotionTypeIdMap::getInstance()->fromId($nbt->getShort(SplashPotion::TAG_POTION_ID, PotionTypeIds::WATER));
168 if($potionType ===
null){
171 return new SplashPotion(Helper::parseLocation($nbt, $world),
null, $potionType, $nbt);
172 }, [
'ThrownPotion',
'minecraft:potion',
'thrownpotion']);
175 return new Squid(Helper::parseLocation($nbt, $world), $nbt);
176 }, [
'Squid',
'minecraft:squid']);
179 return new Villager(Helper::parseLocation($nbt, $world), $nbt);
180 }, [
'Villager',
'minecraft:villager']);
183 return new Zombie(Helper::parseLocation($nbt, $world), $nbt);
184 }, [
'Zombie',
'minecraft:zombie']);
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");
208 Utils::testValidInstance($className, Entity::class);
215 foreach($saveNames as $name){
216 $this->creationFuncs[$name] = $creationFunc;
219 $this->saveNames[$className] = reset($saveNames);
230 $saveId = $nbt->getTag(self::TAG_IDENTIFIER) ?? $nbt->getTag(self::TAG_LEGACY_ID);
233 $func = $this->creationFuncs[$saveId->getValue()] ??
null;
234 }elseif($saveId instanceof
IntTag){
235 $stringId = LegacyEntityIdToStringIdMap::getInstance()->legacyToString($saveId->getValue() & 0xff);
236 $func = $stringId !==
null ? $this->creationFuncs[$stringId] ?? null :
null;
242 $entity = $func($world, $nbt);
245 }
catch(NbtException $e){
246 throw new SavedDataLoadingException($e->getMessage(), 0, $e);
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]));
254 throw new \InvalidArgumentException(
"Entity $class is not registered");
262 if(isset($this->saveNames[$class])){
263 return $this->saveNames[$class];
265 throw new \InvalidArgumentException(
"Entity $class is not registered");