69 public const TAG_IDENTIFIER =
"identifier";
70 public const TAG_LEGACY_ID =
"id";
76 private array $creationFuncs = [];
81 private array $saveNames = [];
83 public function __construct(){
88 return new Arrow(Helper::parseLocation($nbt, $world),
null, $nbt->getByte(Arrow::TAG_CRIT, 0) === 1, $nbt);
89 }, [
'Arrow',
'minecraft:arrow']);
92 return new Egg(Helper::parseLocation($nbt, $world),
null, $nbt);
93 }, [
'Egg',
'minecraft:egg']);
96 return new EnderPearl(Helper::parseLocation($nbt, $world),
null, $nbt);
97 }, [
'ThrownEnderpearl',
'minecraft:ender_pearl']);
100 return new ExperienceBottle(Helper::parseLocation($nbt, $world),
null, $nbt);
101 }, [
'ThrownExpBottle',
'minecraft:xp_bottle']);
105 if(($valuePcTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PC)) instanceof
ShortTag){
106 $value = $valuePcTag->getValue();
107 }elseif(($valuePeTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PE)) instanceof
IntTag){
108 $value = $valuePeTag->getValue();
111 return new ExperienceOrb(Helper::parseLocation($nbt, $world), $value, $nbt);
112 }, [
'XPOrb',
'minecraft:xp_orb']);
115 return new FallingBlock(Helper::parseLocation($nbt, $world), FallingBlock::parseBlockNBT(RuntimeBlockStateRegistry::getInstance(), $nbt), $nbt);
116 }, [
'FallingSand',
'minecraft:falling_block']);
119 $itemTag = $nbt->getCompoundTag(ItemEntity::TAG_ITEM);
120 if($itemTag ===
null){
124 $item = Item::nbtDeserialize($itemTag);
128 return new ItemEntity(Helper::parseLocation($nbt, $world), $item, $nbt);
129 }, [
'Item',
'minecraft:item']);
132 $motive = PaintingMotive::getMotiveByName($nbt->getString(Painting::TAG_MOTIVE));
133 if($motive ===
null){
136 $blockIn =
new Vector3($nbt->getInt(Painting::TAG_TILE_X), $nbt->getInt(Painting::TAG_TILE_Y), $nbt->getInt(Painting::TAG_TILE_Z));
137 if(($directionTag = $nbt->getTag(Painting::TAG_DIRECTION_BE)) instanceof
ByteTag){
138 $facing = Painting::DATA_TO_FACING[$directionTag->getValue()] ?? Facing::NORTH;
139 }elseif(($facingTag = $nbt->getTag(Painting::TAG_FACING_JE)) instanceof
ByteTag){
140 $facing = Painting::DATA_TO_FACING[$facingTag->getValue()] ?? Facing::NORTH;
145 return new Painting(Helper::parseLocation($nbt, $world), $blockIn, $facing, $motive, $nbt);
146 }, [
'Painting',
'minecraft:painting']);
149 return new PrimedTNT(Helper::parseLocation($nbt, $world), $nbt);
150 }, [
'PrimedTnt',
'PrimedTNT',
'minecraft:tnt']);
153 return new Snowball(Helper::parseLocation($nbt, $world),
null, $nbt);
154 }, [
'Snowball',
'minecraft:snowball']);
157 $potionType = PotionTypeIdMap::getInstance()->fromId($nbt->getShort(SplashPotion::TAG_POTION_ID, PotionTypeIds::WATER));
158 if($potionType ===
null){
161 return new SplashPotion(Helper::parseLocation($nbt, $world),
null, $potionType, $nbt);
162 }, [
'ThrownPotion',
'minecraft:potion',
'thrownpotion']);
165 return new Squid(Helper::parseLocation($nbt, $world), $nbt);
166 }, [
'Squid',
'minecraft:squid']);
169 return new Villager(Helper::parseLocation($nbt, $world), $nbt);
170 }, [
'Villager',
'minecraft:villager']);
173 return new Zombie(Helper::parseLocation($nbt, $world), $nbt);
174 }, [
'Zombie',
'minecraft:zombie']);
194 public function register(
string $className, \Closure $creationFunc, array $saveNames) : void{
195 if(count($saveNames) === 0){
196 throw new \InvalidArgumentException(
"At least one save name must be provided");
198 Utils::testValidInstance($className, Entity::class);
205 foreach($saveNames as $name){
206 $this->creationFuncs[$name] = $creationFunc;
209 $this->saveNames[$className] = reset($saveNames);
220 $saveId = $nbt->getTag(self::TAG_IDENTIFIER) ?? $nbt->getTag(self::TAG_LEGACY_ID);
223 $func = $this->creationFuncs[$saveId->getValue()] ??
null;
224 }elseif($saveId instanceof
IntTag){
225 $stringId = LegacyEntityIdToStringIdMap::getInstance()->legacyToString($saveId->getValue() & 0xff);
226 $func = $stringId !==
null ? $this->creationFuncs[$stringId] ?? null :
null;
232 $entity = $func($world, $nbt);
235 }
catch(NbtException $e){
236 throw new SavedDataLoadingException($e->getMessage(), 0, $e);
240 public function injectSaveId(
string $class, CompoundTag $saveData) : void{
241 if(isset($this->saveNames[$class])){
242 $saveData->setTag(self::TAG_IDENTIFIER,
new StringTag($this->saveNames[$class]));
244 throw new \InvalidArgumentException(
"Entity $class is not registered");
252 if(isset($this->saveNames[$class])){
253 return $this->saveNames[$class];
255 throw new \InvalidArgumentException(
"Entity $class is not registered");