68 public const TAG_IDENTIFIER =
"identifier";
69 public const TAG_LEGACY_ID =
"id";
75 private array $creationFuncs = [];
80 private array $saveNames = [];
82 public function __construct(){
87 return new Arrow(Helper::parseLocation($nbt, $world),
null, $nbt->getByte(Arrow::TAG_CRIT, 0) === 1, $nbt);
88 }, [
'Arrow',
'minecraft:arrow']);
91 return new Egg(Helper::parseLocation($nbt, $world),
null, $nbt);
92 }, [
'Egg',
'minecraft:egg']);
95 return new EndCrystal(Helper::parseLocation($nbt, $world), $nbt);
96 }, [
'EnderCrystal',
'minecraft:ender_crystal']);
99 return new EnderPearl(Helper::parseLocation($nbt, $world),
null, $nbt);
100 }, [
'ThrownEnderpearl',
'minecraft:ender_pearl']);
103 return new ExperienceBottle(Helper::parseLocation($nbt, $world),
null, $nbt);
104 }, [
'ThrownExpBottle',
'minecraft:xp_bottle']);
108 if(($valuePcTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PC)) instanceof
ShortTag){
109 $value = $valuePcTag->getValue();
110 }elseif(($valuePeTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PE)) instanceof
IntTag){
111 $value = $valuePeTag->getValue();
114 return new ExperienceOrb(Helper::parseLocation($nbt, $world), $value, $nbt);
115 }, [
'XPOrb',
'minecraft:xp_orb']);
118 return new FallingBlock(Helper::parseLocation($nbt, $world), FallingBlock::parseBlockNBT(RuntimeBlockStateRegistry::getInstance(), $nbt), $nbt);
119 }, [
'FallingSand',
'minecraft:falling_block']);
122 return new IceBomb(Helper::parseLocation($nbt, $world),
null, $nbt);
123 }, [
'minecraft:ice_bomb']);
126 $itemTag = $nbt->getCompoundTag(ItemEntity::TAG_ITEM);
127 if($itemTag ===
null){
131 $item = Item::nbtDeserialize($itemTag);
135 return new ItemEntity(Helper::parseLocation($nbt, $world), $item, $nbt);
136 }, [
'Item',
'minecraft:item']);
139 $motive = PaintingMotive::getMotiveByName($nbt->getString(Painting::TAG_MOTIVE));
140 if($motive ===
null){
143 $blockIn =
new Vector3($nbt->getInt(Painting::TAG_TILE_X), $nbt->getInt(Painting::TAG_TILE_Y), $nbt->getInt(Painting::TAG_TILE_Z));
144 if(($directionTag = $nbt->getTag(Painting::TAG_DIRECTION_BE)) instanceof
ByteTag){
145 $facing = Painting::DATA_TO_FACING[$directionTag->getValue()] ?? Facing::NORTH;
146 }elseif(($facingTag = $nbt->getTag(Painting::TAG_FACING_JE)) instanceof
ByteTag){
147 $facing = Painting::DATA_TO_FACING[$facingTag->getValue()] ?? Facing::NORTH;
152 return new Painting(Helper::parseLocation($nbt, $world), $blockIn, $facing, $motive, $nbt);
153 }, [
'Painting',
'minecraft:painting']);
156 return new PrimedTNT(Helper::parseLocation($nbt, $world), $nbt);
157 }, [
'PrimedTnt',
'PrimedTNT',
'minecraft:tnt']);
160 return new Snowball(Helper::parseLocation($nbt, $world),
null, $nbt);
161 }, [
'Snowball',
'minecraft:snowball']);
164 $potionType = PotionTypeIdMap::getInstance()->fromId($nbt->getShort(SplashPotion::TAG_POTION_ID, PotionTypeIds::WATER));
165 if($potionType ===
null){
168 return new SplashPotion(Helper::parseLocation($nbt, $world),
null, $potionType, $nbt);
169 }, [
'ThrownPotion',
'minecraft:potion',
'thrownpotion']);
172 return new Squid(Helper::parseLocation($nbt, $world), $nbt);
173 }, [
'Squid',
'minecraft:squid']);
176 return new Villager(Helper::parseLocation($nbt, $world), $nbt);
177 }, [
'Villager',
'minecraft:villager']);
180 return new Zombie(Helper::parseLocation($nbt, $world), $nbt);
181 }, [
'Zombie',
'minecraft:zombie']);
201 public function register(
string $className, \Closure $creationFunc, array $saveNames) : void{
202 if(count($saveNames) === 0){
203 throw new \InvalidArgumentException(
"At least one save name must be provided");
205 Utils::testValidInstance($className, Entity::class);
208 foreach($saveNames as $name){
209 $this->creationFuncs[$name] = $creationFunc;
212 $this->saveNames[$className] = reset($saveNames);
219 return isset($this->saveNames[$class]);
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");