72 public const TAG_IDENTIFIER =
"identifier";
73 public const TAG_LEGACY_ID =
"id";
79 private array $creationFuncs = [];
84 private array $saveNames = [];
86 public function __construct(){
91 return new Arrow(Helper::parseLocation($nbt, $world),
null, $nbt->getByte(Arrow::TAG_CRIT, 0) === 1, $nbt);
92 }, [
'Arrow',
'minecraft:arrow']);
95 return new Egg(Helper::parseLocation($nbt, $world),
null, $nbt);
96 }, [
'Egg',
'minecraft:egg']);
99 return new EndCrystal(Helper::parseLocation($nbt, $world), $nbt);
100 }, [
'EnderCrystal',
'minecraft:ender_crystal']);
103 return new EnderPearl(Helper::parseLocation($nbt, $world),
null, $nbt);
104 }, [
'ThrownEnderpearl',
'minecraft:ender_pearl']);
107 return new ExperienceBottle(Helper::parseLocation($nbt, $world),
null, $nbt);
108 }, [
'ThrownExpBottle',
'minecraft:xp_bottle']);
112 if(($valuePcTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PC)) instanceof
ShortTag){
113 $value = $valuePcTag->getValue();
114 }elseif(($valuePeTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PE)) instanceof
IntTag){
115 $value = $valuePeTag->getValue();
118 return new ExperienceOrb(Helper::parseLocation($nbt, $world), $value, $nbt);
119 }, [
'XPOrb',
'minecraft:xp_orb']);
122 return new FallingBlock(Helper::parseLocation($nbt, $world), FallingBlock::parseBlockNBT(RuntimeBlockStateRegistry::getInstance(), $nbt), $nbt);
123 }, [
'FallingSand',
'minecraft:falling_block']);
126 return new IceBomb(Helper::parseLocation($nbt, $world),
null, $nbt);
127 }, [
'minecraft:ice_bomb']);
130 $itemTag = $nbt->getCompoundTag(ItemEntity::TAG_ITEM);
131 if($itemTag ===
null){
135 $item = Item::nbtDeserialize($itemTag);
139 return new ItemEntity(Helper::parseLocation($nbt, $world), $item, $nbt);
140 }, [
'Item',
'minecraft:item']);
143 $motive = PaintingMotive::getMotiveByName($nbt->getString(Painting::TAG_MOTIVE));
144 if($motive ===
null){
147 $blockIn =
new Vector3($nbt->getInt(Painting::TAG_TILE_X), $nbt->getInt(Painting::TAG_TILE_Y), $nbt->getInt(Painting::TAG_TILE_Z));
148 if(($directionTag = $nbt->getTag(Painting::TAG_DIRECTION_BE)) instanceof
ByteTag){
149 $facing = Painting::DATA_TO_FACING[$directionTag->getValue()] ?? Facing::NORTH;
150 }elseif(($facingTag = $nbt->getTag(Painting::TAG_FACING_JE)) instanceof
ByteTag){
151 $facing = Painting::DATA_TO_FACING[$facingTag->getValue()] ?? Facing::NORTH;
156 return new Painting(Helper::parseLocation($nbt, $world), $blockIn, $facing, $motive, $nbt);
157 }, [
'Painting',
'minecraft:painting']);
160 return new PrimedTNT(Helper::parseLocation($nbt, $world), $nbt);
161 }, [
'PrimedTnt',
'PrimedTNT',
'minecraft:tnt']);
164 return new Snowball(Helper::parseLocation($nbt, $world),
null, $nbt);
165 }, [
'Snowball',
'minecraft:snowball']);
168 $potionType = PotionTypeIdMap::getInstance()->fromId($nbt->getShort(SplashPotion::TAG_POTION_ID, PotionTypeIds::WATER));
169 if($potionType ===
null){
172 return new SplashPotion(Helper::parseLocation($nbt, $world),
null, $potionType, $nbt);
173 }, [
'ThrownPotion',
'minecraft:potion',
'thrownpotion']);
176 $itemTag = $nbt->getCompoundTag(Trident::TAG_ITEM);
177 if($itemTag ===
null){
181 $item = Item::nbtDeserialize($itemTag);
185 return new Trident(Helper::parseLocation($nbt, $world), $item,
null, $nbt);
188 'minecraft:thrown_trident',
194 return new Squid(Helper::parseLocation($nbt, $world), $nbt);
195 }, [
'Squid',
'minecraft:squid']);
198 return new Villager(Helper::parseLocation($nbt, $world), $nbt);
199 }, [
'Villager',
'minecraft:villager']);
202 return new Zombie(Helper::parseLocation($nbt, $world), $nbt);
203 }, [
'Zombie',
'minecraft:zombie']);
223 public function register(
string $className, \Closure $creationFunc, array $saveNames) : void{
224 if(count($saveNames) === 0){
225 throw new \InvalidArgumentException(
"At least one save name must be provided");
227 Utils::testValidInstance($className, Entity::class);
234 foreach($saveNames as $name){
235 $this->creationFuncs[$name] = $creationFunc;
238 $this->saveNames[$className] = reset($saveNames);
245 return isset($this->saveNames[$class]);
256 $saveId = $nbt->getTag(self::TAG_IDENTIFIER) ?? $nbt->getTag(self::TAG_LEGACY_ID);
259 $func = $this->creationFuncs[$saveId->getValue()] ??
null;
260 }elseif($saveId instanceof
IntTag){
261 $stringId = LegacyEntityIdToStringIdMap::getInstance()->legacyToString($saveId->getValue() & 0xff);
262 $func = $stringId !==
null ? $this->creationFuncs[$stringId] ?? null :
null;
268 $entity = $func($world, $nbt);
271 }
catch(NbtException $e){
272 throw new SavedDataLoadingException($e->getMessage(), 0, $e);
276 public function injectSaveId(
string $class, CompoundTag $saveData) : void{
277 if(isset($this->saveNames[$class])){
278 $saveData->setTag(self::TAG_IDENTIFIER,
new StringTag($this->saveNames[$class]));
280 throw new \InvalidArgumentException(
"Entity $class is not registered");
288 if(isset($this->saveNames[$class])){
289 return $this->saveNames[$class];
291 throw new \InvalidArgumentException(
"Entity $class is not registered");