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;
255 $this->uuid = Uuid::uuid3(Uuid::NIL, ((string) $this->getId()) . $this->skin->getSkinData() . $this->getNameTag());
262 private static function populateInventoryFromListTag(
Inventory $inventory, array $items) : void{
263 $listeners = $inventory->getListeners()->toArray();
271 protected function initEntity(
CompoundTag $nbt) : void{
272 parent::initEntity($nbt);
274 $this->hungerManager =
new HungerManager($this);
275 $this->xpManager =
new ExperienceManager($this);
277 $this->inventory =
new PlayerInventory($this);
278 $syncHeldItem = fn() => NetworkBroadcastUtils::broadcastEntityEvent(
282 $this->inventory->getListeners()->add(
new CallbackInventoryListener(
283 function(Inventory $unused,
int $slot, Item $unused2) use ($syncHeldItem) :
void{
284 if($slot === $this->inventory->getHeldItemIndex()){
288 function(Inventory $unused, array $oldItems) use ($syncHeldItem) : void{
289 if(array_key_exists($this->inventory->getHeldItemIndex(), $oldItems)){
294 $this->offHandInventory =
new PlayerOffHandInventory($this);
295 $this->enderInventory =
new PlayerEnderInventory($this);
296 $this->initHumanData($nbt);
298 $inventoryTag = $nbt->
getListTag(self::TAG_INVENTORY);
299 if($inventoryTag !==
null){
300 $inventoryItems = [];
301 $armorInventoryItems = [];
304 foreach($inventoryTag as $i => $item){
305 $slot = $item->getByte(SavedItemStackData::TAG_SLOT);
306 if($slot >= 0 && $slot < 9){
308 }elseif($slot >= 100 && $slot < 104){
309 $armorInventoryItems[$slot - 100] = Item::nbtDeserialize($item);
310 }elseif($slot >= 9 && $slot < $this->inventory->getSize() + 9){
311 $inventoryItems[$slot - 9] = Item::nbtDeserialize($item);
315 self::populateInventoryFromListTag($this->inventory, $inventoryItems);
316 self::populateInventoryFromListTag($this->armorInventory, $armorInventoryItems);
319 if($offHand !==
null){
320 $this->offHandInventory->setItem(0, Item::nbtDeserialize($offHand));
322 $this->offHandInventory->getListeners()->add(CallbackInventoryListener::onAnyChange(fn() => NetworkBroadcastUtils::broadcastEntityEvent(
324 fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onMobOffHandItemChange($recipients, $this)
327 $enderChestInventoryTag = $nbt->
getListTag(self::TAG_ENDER_CHEST_INVENTORY);
328 if($enderChestInventoryTag !==
null){
329 $enderChestInventoryItems = [];
332 foreach($enderChestInventoryTag as $i => $item){
333 $enderChestInventoryItems[$item->getByte(SavedItemStackData::TAG_SLOT)] = Item::nbtDeserialize($item);
335 self::populateInventoryFromListTag($this->enderInventory, $enderChestInventoryItems);
338 $this->inventory->setHeldItemIndex($nbt->getInt(self::TAG_SELECTED_INVENTORY_SLOT, 0));
339 $this->inventory->getHeldItemIndexChangeListeners()->add(fn() => NetworkBroadcastUtils::broadcastEntityEvent(
341 fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onMobMainHandItemChange($recipients, $this)
344 $this->hungerManager->setFood((
float) $nbt->getInt(self::TAG_FOOD_LEVEL, (
int) $this->hungerManager->getFood()));
345 $this->hungerManager->setExhaustion($nbt->getFloat(self::TAG_FOOD_EXHAUSTION_LEVEL, $this->hungerManager->getExhaustion()));
346 $this->hungerManager->setSaturation($nbt->getFloat(self::TAG_FOOD_SATURATION_LEVEL, $this->hungerManager->getSaturation()));
347 $this->hungerManager->setFoodTickTimer($nbt->getInt(self::TAG_FOOD_TICK_TIMER, $this->hungerManager->getFoodTickTimer()));
349 $this->xpManager->setXpAndProgressNoEvent(
350 $nbt->getInt(self::TAG_XP_LEVEL, 0),
351 $nbt->getFloat(self::TAG_XP_PROGRESS, 0.0));
352 $this->xpManager->setLifetimeTotalXp($nbt->getInt(self::TAG_LIFETIME_XP_TOTAL, 0));
354 if(($xpSeedTag = $nbt->
getTag(self::TAG_XP_SEED)) instanceof IntTag){
355 $this->xpSeed = $xpSeedTag->getValue();
357 $this->xpSeed = EnchantingHelper::generateSeed();
361 protected function entityBaseTick(
int $tickDiff = 1) : bool{
362 $hasUpdate = parent::entityBaseTick($tickDiff);
364 $this->hungerManager->tick($tickDiff);
365 $this->xpManager->tick($tickDiff);
370 public function getName() : string{
371 return $this->getNameTag();
375 parent::applyDamageModifiers($source);
377 $type = $source->getCause();
378 if($type !== EntityDamageEvent::CAUSE_SUICIDE && $type !== EntityDamageEvent::CAUSE_VOID
379 && ($this->inventory->getItemInHand() instanceof
Totem || $this->offHandInventory->getItem(0) instanceof
Totem)){
381 $compensation = $this->getHealth() - $source->getFinalDamage() - 1;
382 if($compensation <= -1){
383 $source->setModifier($compensation, EntityDamageEvent::MODIFIER_TOTEM);
389 parent::applyPostDamageEffects($source);
390 $totemModifier = $source->getModifier(EntityDamageEvent::MODIFIER_TOTEM);
391 if($totemModifier < 0){
392 $this->effectManager->clear();
394 $this->effectManager->add(
new EffectInstance(VanillaEffects::REGENERATION(), 40 * 20, 1));
395 $this->effectManager->add(
new EffectInstance(VanillaEffects::FIRE_RESISTANCE(), 40 * 20, 1));
396 $this->effectManager->add(
new EffectInstance(VanillaEffects::ABSORPTION(), 5 * 20, 1));
401 $hand = $this->inventory->getItemInHand();
402 if($hand instanceof
Totem){
404 $this->inventory->setItemInHand($hand);
405 }elseif(($offHand = $this->offHandInventory->getItem(0)) instanceof
Totem){
407 $this->offHandInventory->setItem(0, $offHand);
413 return array_filter(array_merge(
414 array_values($this->inventory->getContents()),
415 array_values($this->armorInventory->getContents()),
416 array_values($this->offHandInventory->getContents()),
417 ), function(
Item $item) : bool{ return !$item->hasEnchantment(
VanillaEnchantments::VANISHING()) && !$item->keepOnDeath(); });
421 $nbt = parent::saveNBT();
423 $nbt->
setInt(self::TAG_FOOD_LEVEL, (
int) $this->hungerManager->getFood());
424 $nbt->
setFloat(self::TAG_FOOD_EXHAUSTION_LEVEL, $this->hungerManager->getExhaustion());
425 $nbt->
setFloat(self::TAG_FOOD_SATURATION_LEVEL, $this->hungerManager->getSaturation());
426 $nbt->
setInt(self::TAG_FOOD_TICK_TIMER, $this->hungerManager->getFoodTickTimer());
428 $nbt->
setInt(self::TAG_XP_LEVEL, $this->xpManager->getXpLevel());
429 $nbt->
setFloat(self::TAG_XP_PROGRESS, $this->xpManager->getXpProgress());
430 $nbt->
setInt(self::TAG_LIFETIME_XP_TOTAL, $this->xpManager->getLifetimeTotalXp());
431 $nbt->
setInt(self::TAG_XP_SEED, $this->xpSeed);
433 $inventoryTag =
new ListTag([], NBT::TAG_Compound);
434 $nbt->
setTag(self::TAG_INVENTORY, $inventoryTag);
437 $slotCount = $this->inventory->getSize() + $this->inventory->getHotbarSize();
438 for($slot = $this->inventory->getHotbarSize(); $slot < $slotCount; ++$slot){
439 $item = $this->inventory->getItem($slot - 9);
440 if(!$item->isNull()){
441 $inventoryTag->push($item->nbtSerialize($slot));
446 for($slot = 100; $slot < 104; ++$slot){
447 $item = $this->armorInventory->getItem($slot - 100);
448 if(!$item->isNull()){
449 $inventoryTag->push($item->nbtSerialize($slot));
453 $nbt->
setInt(self::TAG_SELECTED_INVENTORY_SLOT, $this->inventory->getHeldItemIndex());
455 $offHandItem = $this->offHandInventory->getItem(0);
456 if(!$offHandItem->isNull()){
457 $nbt->setTag(self::TAG_OFF_HAND_ITEM, $offHandItem->nbtSerialize());
463 $slotCount = $this->enderInventory->getSize();
464 for($slot = 0; $slot < $slotCount; ++$slot){
465 $item = $this->enderInventory->getItem($slot);
466 if(!$item->isNull()){
467 $items[] = $item->nbtSerialize($slot);
471 $nbt->
setTag(self::TAG_ENDER_CHEST_INVENTORY,
new ListTag($items, NBT::TAG_Compound));
473 $nbt->
setTag(self::TAG_SKIN, CompoundTag::create()
474 ->setString(self::TAG_SKIN_NAME, $this->skin->getSkinId())
475 ->setByteArray(self::TAG_SKIN_DATA, $this->skin->getSkinData())
476 ->setByteArray(self::TAG_SKIN_CAPE_DATA, $this->skin->getCapeData())
477 ->setString(self::TAG_SKIN_GEOMETRY_NAME, $this->skin->getGeometryName())
478 ->setByteArray(self::TAG_SKIN_GEOMETRY_DATA, $this->skin->getGeometryData())
484 public function spawnTo(Player $player) : void{
485 if($player !== $this){
486 parent::spawnTo($player);
491 $networkSession = $player->getNetworkSession();
492 $typeConverter = $networkSession->getTypeConverter();
493 if(!($this instanceof
Player)){
494 $networkSession->sendDataPacket(PlayerListPacket::add([PlayerListEntry::createAdditionEntry($this->uuid, $this->
id, $this->getName(), $typeConverter->getSkinAdapter()->toSkinData($this->skin))]));
497 $networkSession->sendDataPacket(AddPlayerPacket::create(
498 $this->getUniqueId(),
502 $this->location->asVector3(),
504 $this->location->pitch,
505 $this->location->yaw,
506 $this->location->yaw,
507 ItemStackWrapper::legacy($typeConverter->coreItemStackToNet($this->getInventory()->getItemInHand())),
509 $this->getAllNetworkData(),
511 UpdateAbilitiesPacket::create(
new AbilitiesData(CommandPermissions::NORMAL, PlayerPermissions::VISITOR, $this->getId() , [
513 AbilitiesLayer::LAYER_BASE,
514 array_fill(0, AbilitiesLayer::NUMBER_OF_ABILITIES,
false),
525 $this->sendData([$player], [EntityMetadataProperties::NAMETAG =>
new StringMetadataProperty($this->getNameTag())]);
527 $entityEventBroadcaster = $networkSession->getEntityEventBroadcaster();
528 $entityEventBroadcaster->onMobArmorChange([$networkSession], $this);
529 $entityEventBroadcaster->onMobOffHandItemChange([$networkSession], $this);
531 if(!($this instanceof
Player)){
532 $networkSession->sendDataPacket(PlayerListPacket::remove([PlayerListEntry::createRemovalEntry($this->uuid)]));
536 public function getOffsetPosition(Vector3 $vector3) : Vector3{
537 return $vector3->add(0, 1.621, 0);
541 $this->inventory->removeAllViewers();
542 $this->inventory->getHeldItemIndexChangeListeners()->clear();
543 $this->offHandInventory->removeAllViewers();
544 $this->enderInventory->removeAllViewers();
551 $this->offHandInventory,
552 $this->enderInventory,
553 $this->hungerManager,
556 parent::destroyCycles();