83 private const TAG_INVENTORY =
"Inventory";
84 private const TAG_OFF_HAND_ITEM =
"OffHandItem";
85 private const TAG_ENDER_CHEST_INVENTORY =
"EnderChestInventory";
86 private const TAG_SELECTED_INVENTORY_SLOT =
"SelectedInventorySlot";
87 private const TAG_FOOD_LEVEL =
"foodLevel";
88 private const TAG_FOOD_EXHAUSTION_LEVEL =
"foodExhaustionLevel";
89 private const TAG_FOOD_SATURATION_LEVEL =
"foodSaturationLevel";
90 private const TAG_FOOD_TICK_TIMER =
"foodTickTimer";
91 private const TAG_XP_LEVEL =
"XpLevel";
92 private const TAG_XP_PROGRESS =
"XpP";
93 private const TAG_LIFETIME_XP_TOTAL =
"XpTotal";
94 private const TAG_XP_SEED =
"XpSeed";
95 private const TAG_SKIN =
"Skin";
96 private const TAG_SKIN_NAME =
"Name";
97 private const TAG_SKIN_DATA =
"Data";
98 private const TAG_SKIN_CAPE_DATA =
"CapeData";
99 private const TAG_SKIN_GEOMETRY_NAME =
"GeometryName";
100 private const TAG_SKIN_GEOMETRY_DATA =
"GeometryData";
102 public static function getNetworkTypeId() :
string{
return EntityIds::PLAYER; }
108 protected UuidInterface $uuid;
110 protected Skin $skin;
115 protected int $xpSeed;
119 parent::__construct($location, $nbt);
129 $skinTag = $nbt->getCompoundTag(self::TAG_SKIN);
130 if($skinTag ===
null){
134 $skinTag->getString(self::TAG_SKIN_NAME),
135 ($skinDataTag = $skinTag->getTag(self::TAG_SKIN_DATA)) instanceof
StringTag ? $skinDataTag->
getValue() : $skinTag->getByteArray(self::TAG_SKIN_DATA),
136 $skinTag->getByteArray(self::TAG_SKIN_CAPE_DATA,
""),
137 $skinTag->getString(self::TAG_SKIN_GEOMETRY_NAME,
""),
138 $skinTag->getByteArray(self::TAG_SKIN_GEOMETRY_DATA,
"")
142 public function getUniqueId() : UuidInterface{
167 public function sendSkin(?array $targets =
null) : void{
175 if($this->isSprinting()){
176 $this->hungerManager->exhaust(0.2, PlayerExhaustEvent::CAUSE_SPRINT_JUMPING);
178 $this->hungerManager->exhaust(0.05, PlayerExhaustEvent::CAUSE_JUMPING);
182 public function emote(
string $emoteId) : void{
183 NetworkBroadcastUtils::broadcastEntityEvent(
185 fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onEmote($recipients, $this, $emoteId)
189 public function getHungerManager() : HungerManager{
190 return $this->hungerManager;
198 return $this->hungerManager->isHungry() || $this->getWorld()->getDifficulty() ===
World::DIFFICULTY_PEACEFUL;
202 if($consumable instanceof
FoodSource && $consumable->requiresHunger() && !$this->canEat()){
206 return parent::consumeObject($consumable);
211 $this->hungerManager->addFood($consumable->getFoodRestore());
212 $this->hungerManager->addSaturation($consumable->getSaturationRestore());
215 parent::applyConsumptionResults($consumable);
218 public function getXpManager() : ExperienceManager{
219 return $this->xpManager;
222 public function getEnchantmentSeed() : int{
223 return $this->xpSeed;
226 public function setEnchantmentSeed(
int $seed) : void{
227 $this->xpSeed = $seed;
230 public function regenerateEnchantmentSeed() : void{
231 $this->xpSeed = EnchantingHelper::generateSeed();
237 return min(100, 7 * $this->xpManager->getXpLevel());
241 return $this->inventory;
244 public function getOffHandInventory() : PlayerOffHandInventory{ return $this->offHandInventory; }
246 public function getEnderInventory() : PlayerEnderInventory{
247 return $this->enderInventory;
250 public function getSneakOffset() : float{
259 $this->uuid = Uuid::uuid3(Uuid::NIL, ((string) $this->getId()) . $this->skin->getSkinData() . $this->getNameTag());
266 private static function populateInventoryFromListTag(
Inventory $inventory, array $items) : void{
267 $listeners = $inventory->getListeners()->toArray();
275 protected function initEntity(
CompoundTag $nbt) : void{
276 parent::initEntity($nbt);
278 $this->hungerManager =
new HungerManager($this);
279 $this->xpManager =
new ExperienceManager($this);
281 $this->inventory =
new PlayerInventory($this);
282 $syncHeldItem = fn() => NetworkBroadcastUtils::broadcastEntityEvent(
286 $this->inventory->getListeners()->add(
new CallbackInventoryListener(
287 function(Inventory $unused,
int $slot, Item $unused2) use ($syncHeldItem) :
void{
288 if($slot === $this->inventory->getHeldItemIndex()){
292 function(Inventory $unused, array $oldItems) use ($syncHeldItem) : void{
293 if(array_key_exists($this->inventory->getHeldItemIndex(), $oldItems)){
298 $this->offHandInventory =
new PlayerOffHandInventory($this);
299 $this->enderInventory =
new PlayerEnderInventory($this);
300 $this->initHumanData($nbt);
302 $inventoryTag = $nbt->
getListTag(self::TAG_INVENTORY, CompoundTag::class);
303 if($inventoryTag !==
null){
304 $inventoryItems = [];
305 $armorInventoryItems = [];
307 foreach($inventoryTag as $i => $item){
308 $slot = $item->getByte(SavedItemStackData::TAG_SLOT);
309 if($slot >= 0 && $slot < 9){
311 }elseif($slot >= 100 && $slot < 104){
312 $armorInventoryItems[$slot - 100] = Item::nbtDeserialize($item);
313 }elseif($slot >= 9 && $slot < $this->inventory->getSize() + 9){
314 $inventoryItems[$slot - 9] = Item::nbtDeserialize($item);
318 self::populateInventoryFromListTag($this->inventory, $inventoryItems);
319 self::populateInventoryFromListTag($this->armorInventory, $armorInventoryItems);
322 if($offHand !==
null){
323 $this->offHandInventory->setItem(0, Item::nbtDeserialize($offHand));
325 $this->offHandInventory->getListeners()->add(CallbackInventoryListener::onAnyChange(fn() => NetworkBroadcastUtils::broadcastEntityEvent(
327 fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onMobOffHandItemChange($recipients, $this)
330 $enderChestInventoryTag = $nbt->
getListTag(self::TAG_ENDER_CHEST_INVENTORY, CompoundTag::class);
331 if($enderChestInventoryTag !==
null){
332 $enderChestInventoryItems = [];
334 foreach($enderChestInventoryTag as $i => $item){
335 $enderChestInventoryItems[$item->getByte(SavedItemStackData::TAG_SLOT)] = Item::nbtDeserialize($item);
337 self::populateInventoryFromListTag($this->enderInventory, $enderChestInventoryItems);
340 $this->inventory->setHeldItemIndex($nbt->getInt(self::TAG_SELECTED_INVENTORY_SLOT, 0));
341 $this->inventory->getHeldItemIndexChangeListeners()->add(fn() => NetworkBroadcastUtils::broadcastEntityEvent(
343 fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onMobMainHandItemChange($recipients, $this)
346 $this->hungerManager->setFood((
float) $nbt->getInt(self::TAG_FOOD_LEVEL, (
int) $this->hungerManager->getFood()));
347 $this->hungerManager->setExhaustion($nbt->getFloat(self::TAG_FOOD_EXHAUSTION_LEVEL, $this->hungerManager->getExhaustion()));
348 $this->hungerManager->setSaturation($nbt->getFloat(self::TAG_FOOD_SATURATION_LEVEL, $this->hungerManager->getSaturation()));
349 $this->hungerManager->setFoodTickTimer($nbt->getInt(self::TAG_FOOD_TICK_TIMER, $this->hungerManager->getFoodTickTimer()));
351 $this->xpManager->setXpAndProgressNoEvent(
352 $nbt->getInt(self::TAG_XP_LEVEL, 0),
353 $nbt->getFloat(self::TAG_XP_PROGRESS, 0.0));
354 $this->xpManager->setLifetimeTotalXp($nbt->getInt(self::TAG_LIFETIME_XP_TOTAL, 0));
356 if(($xpSeedTag = $nbt->
getTag(self::TAG_XP_SEED)) instanceof IntTag){
357 $this->xpSeed = $xpSeedTag->getValue();
359 $this->xpSeed = EnchantingHelper::generateSeed();
363 protected function entityBaseTick(
int $tickDiff = 1) : bool{
364 $hasUpdate = parent::entityBaseTick($tickDiff);
366 $this->hungerManager->tick($tickDiff);
367 $this->xpManager->tick($tickDiff);
372 public function getName() : string{
373 return $this->getNameTag();
377 parent::applyDamageModifiers($source);
379 $type = $source->getCause();
380 if($type !== EntityDamageEvent::CAUSE_SUICIDE && $type !== EntityDamageEvent::CAUSE_VOID
381 && ($this->inventory->getItemInHand() instanceof
Totem || $this->offHandInventory->getItem(0) instanceof
Totem)){
383 $compensation = $this->getHealth() - $source->getFinalDamage() - 1;
384 if($compensation <= -1){
385 $source->setModifier($compensation, EntityDamageEvent::MODIFIER_TOTEM);
391 parent::applyPostDamageEffects($source);
392 $totemModifier = $source->getModifier(EntityDamageEvent::MODIFIER_TOTEM);
393 if($totemModifier < 0){
394 $this->effectManager->clear();
396 $this->effectManager->add(
new EffectInstance(VanillaEffects::REGENERATION(), 40 * 20, 1));
397 $this->effectManager->add(
new EffectInstance(VanillaEffects::FIRE_RESISTANCE(), 40 * 20, 1));
398 $this->effectManager->add(
new EffectInstance(VanillaEffects::ABSORPTION(), 5 * 20, 1));
403 $hand = $this->inventory->getItemInHand();
404 if($hand instanceof
Totem){
406 $this->inventory->setItemInHand($hand);
407 }elseif(($offHand = $this->offHandInventory->getItem(0)) instanceof
Totem){
409 $this->offHandInventory->setItem(0, $offHand);
415 return array_filter(array_merge(
416 array_values($this->inventory->getContents()),
417 array_values($this->armorInventory->getContents()),
418 array_values($this->offHandInventory->getContents()),
419 ), function(
Item $item) : bool{ return !$item->hasEnchantment(
VanillaEnchantments::VANISHING()) && !$item->keepOnDeath(); });
423 $nbt = parent::saveNBT();
425 $nbt->
setInt(self::TAG_FOOD_LEVEL, (
int) $this->hungerManager->getFood());
426 $nbt->
setFloat(self::TAG_FOOD_EXHAUSTION_LEVEL, $this->hungerManager->getExhaustion());
427 $nbt->
setFloat(self::TAG_FOOD_SATURATION_LEVEL, $this->hungerManager->getSaturation());
428 $nbt->
setInt(self::TAG_FOOD_TICK_TIMER, $this->hungerManager->getFoodTickTimer());
430 $nbt->
setInt(self::TAG_XP_LEVEL, $this->xpManager->getXpLevel());
431 $nbt->
setFloat(self::TAG_XP_PROGRESS, $this->xpManager->getXpProgress());
432 $nbt->
setInt(self::TAG_LIFETIME_XP_TOTAL, $this->xpManager->getLifetimeTotalXp());
433 $nbt->
setInt(self::TAG_XP_SEED, $this->xpSeed);
435 $inventoryTag =
new ListTag([], NBT::TAG_Compound);
436 $nbt->
setTag(self::TAG_INVENTORY, $inventoryTag);
439 $slotCount = $this->inventory->getSize() + $this->inventory->getHotbarSize();
440 for($slot = $this->inventory->getHotbarSize(); $slot < $slotCount; ++$slot){
441 $item = $this->inventory->getItem($slot - 9);
442 if(!$item->isNull()){
443 $inventoryTag->push($item->nbtSerialize($slot));
448 for($slot = 100; $slot < 104; ++$slot){
449 $item = $this->armorInventory->getItem($slot - 100);
450 if(!$item->isNull()){
451 $inventoryTag->push($item->nbtSerialize($slot));
455 $nbt->
setInt(self::TAG_SELECTED_INVENTORY_SLOT, $this->inventory->getHeldItemIndex());
457 $offHandItem = $this->offHandInventory->getItem(0);
458 if(!$offHandItem->isNull()){
459 $nbt->setTag(self::TAG_OFF_HAND_ITEM, $offHandItem->nbtSerialize());
465 $slotCount = $this->enderInventory->getSize();
466 for($slot = 0; $slot < $slotCount; ++$slot){
467 $item = $this->enderInventory->getItem($slot);
468 if(!$item->isNull()){
469 $items[] = $item->nbtSerialize($slot);
473 $nbt->
setTag(self::TAG_ENDER_CHEST_INVENTORY,
new ListTag($items, NBT::TAG_Compound));
475 $nbt->
setTag(self::TAG_SKIN, CompoundTag::create()
476 ->setString(self::TAG_SKIN_NAME, $this->skin->getSkinId())
477 ->setByteArray(self::TAG_SKIN_DATA, $this->skin->getSkinData())
478 ->setByteArray(self::TAG_SKIN_CAPE_DATA, $this->skin->getCapeData())
479 ->setString(self::TAG_SKIN_GEOMETRY_NAME, $this->skin->getGeometryName())
480 ->setByteArray(self::TAG_SKIN_GEOMETRY_DATA, $this->skin->getGeometryData())
486 public function spawnTo(Player $player) : void{
487 if($player !== $this){
488 parent::spawnTo($player);
493 $networkSession = $player->getNetworkSession();
494 $typeConverter = $networkSession->getTypeConverter();
495 if(!($this instanceof
Player)){
496 $networkSession->sendDataPacket(PlayerListPacket::add([PlayerListEntry::createAdditionEntry($this->uuid, $this->
id, $this->getName(), $typeConverter->getSkinAdapter()->toSkinData($this->skin))]));
499 $networkSession->sendDataPacket(AddPlayerPacket::create(
500 $this->getUniqueId(),
504 $this->location->asVector3(),
506 $this->location->pitch,
507 $this->location->yaw,
508 $this->location->yaw,
509 ItemStackWrapper::legacy($typeConverter->coreItemStackToNet($this->getInventory()->getItemInHand())),
511 $this->getAllNetworkData(),
513 UpdateAbilitiesPacket::create(
new AbilitiesData(CommandPermissions::NORMAL, PlayerPermissions::VISITOR, $this->getId() , [
515 AbilitiesLayer::LAYER_BASE,
516 array_fill(0, AbilitiesLayer::NUMBER_OF_ABILITIES,
false),
528 $this->sendData([$player], [EntityMetadataProperties::NAMETAG =>
new StringMetadataProperty($this->getNameTag())]);
530 $entityEventBroadcaster = $networkSession->getEntityEventBroadcaster();
531 $entityEventBroadcaster->onMobArmorChange([$networkSession], $this);
532 $entityEventBroadcaster->onMobOffHandItemChange([$networkSession], $this);
534 if(!($this instanceof
Player)){
535 $networkSession->sendDataPacket(PlayerListPacket::remove([PlayerListEntry::createRemovalEntry($this->uuid)]));
539 public function getOffsetPosition(Vector3 $vector3) : Vector3{
540 return $vector3->add(0, 1.621, 0);
544 $this->inventory->removeAllViewers();
545 $this->inventory->getHeldItemIndexChangeListeners()->clear();
546 $this->offHandInventory->removeAllViewers();
547 $this->enderInventory->removeAllViewers();
554 $this->offHandInventory,
555 $this->enderInventory,
556 $this->hungerManager,
559 parent::destroyCycles();