82 private const TAG_INVENTORY =
"Inventory";
83 private const TAG_OFF_HAND_ITEM =
"OffHandItem";
84 private const TAG_ENDER_CHEST_INVENTORY =
"EnderChestInventory";
85 private const TAG_SELECTED_INVENTORY_SLOT =
"SelectedInventorySlot";
86 private const TAG_FOOD_LEVEL =
"foodLevel";
87 private const TAG_FOOD_EXHAUSTION_LEVEL =
"foodExhaustionLevel";
88 private const TAG_FOOD_SATURATION_LEVEL =
"foodSaturationLevel";
89 private const TAG_FOOD_TICK_TIMER =
"foodTickTimer";
90 private const TAG_XP_LEVEL =
"XpLevel";
91 private const TAG_XP_PROGRESS =
"XpP";
92 private const TAG_LIFETIME_XP_TOTAL =
"XpTotal";
93 private const TAG_XP_SEED =
"XpSeed";
94 private const TAG_SKIN =
"Skin";
95 private const TAG_SKIN_NAME =
"Name";
96 private const TAG_SKIN_DATA =
"Data";
97 private const TAG_SKIN_CAPE_DATA =
"CapeData";
98 private const TAG_SKIN_GEOMETRY_NAME =
"GeometryName";
99 private const TAG_SKIN_GEOMETRY_DATA =
"GeometryData";
101 public function getNetworkTypeId() :
string{
return EntityIds::PLAYER; }
107 protected UuidInterface $uuid;
109 protected Skin $skin;
114 protected int $xpSeed;
118 parent::__construct($location, $nbt);
128 $skinTag = $nbt->getCompoundTag(self::TAG_SKIN);
129 if($skinTag ===
null){
133 $skinTag->getString(self::TAG_SKIN_NAME),
134 ($skinDataTag = $skinTag->getTag(self::TAG_SKIN_DATA)) instanceof
StringTag ? $skinDataTag->
getValue() : $skinTag->getByteArray(self::TAG_SKIN_DATA),
135 $skinTag->getByteArray(self::TAG_SKIN_CAPE_DATA,
""),
136 $skinTag->getString(self::TAG_SKIN_GEOMETRY_NAME,
""),
137 $skinTag->getByteArray(self::TAG_SKIN_GEOMETRY_DATA,
"")
141 public function getUniqueId() : UuidInterface{
166 public function sendSkin(?array $targets =
null) : void{
174 if($this->isSprinting()){
175 $this->hungerManager->exhaust(0.2, PlayerExhaustEvent::CAUSE_SPRINT_JUMPING);
177 $this->hungerManager->exhaust(0.05, PlayerExhaustEvent::CAUSE_JUMPING);
181 public function emote(
string $emoteId) : void{
182 NetworkBroadcastUtils::broadcastEntityEvent(
184 fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onEmote($recipients, $this, $emoteId)
188 public function getHungerManager() : HungerManager{
189 return $this->hungerManager;
193 if($consumable instanceof
FoodSource && $consumable->requiresHunger() && !$this->hungerManager->isHungry()){
197 return parent::consumeObject($consumable);
202 $this->hungerManager->addFood($consumable->getFoodRestore());
203 $this->hungerManager->addSaturation($consumable->getSaturationRestore());
206 parent::applyConsumptionResults($consumable);
209 public function getXpManager() : ExperienceManager{
210 return $this->xpManager;
213 public function getEnchantmentSeed() : int{
214 return $this->xpSeed;
217 public function setEnchantmentSeed(
int $seed) : void{
218 $this->xpSeed = $seed;
221 public function regenerateEnchantmentSeed() : void{
222 $this->xpSeed = EnchantingHelper::generateSeed();
228 return min(100, 7 * $this->xpManager->getXpLevel());
232 return $this->inventory;
235 public function getOffHandInventory() : PlayerOffHandInventory{ return $this->offHandInventory; }
237 public function getEnderInventory() : PlayerEnderInventory{
238 return $this->enderInventory;
246 $this->uuid = Uuid::uuid3(Uuid::NIL, ((string) $this->getId()) . $this->skin->getSkinData() . $this->getNameTag());
253 private static function populateInventoryFromListTag(
Inventory $inventory, array $items) : void{
254 $listeners = $inventory->getListeners()->toArray();
262 protected function initEntity(
CompoundTag $nbt) : void{
263 parent::initEntity($nbt);
265 $this->hungerManager =
new HungerManager($this);
266 $this->xpManager =
new ExperienceManager($this);
268 $this->inventory =
new PlayerInventory($this);
269 $syncHeldItem = fn() => NetworkBroadcastUtils::broadcastEntityEvent(
273 $this->inventory->getListeners()->add(
new CallbackInventoryListener(
274 function(Inventory $unused,
int $slot, Item $unused2) use ($syncHeldItem) :
void{
275 if($slot === $this->inventory->getHeldItemIndex()){
279 function(Inventory $unused, array $oldItems) use ($syncHeldItem) : void{
280 if(array_key_exists($this->inventory->getHeldItemIndex(), $oldItems)){
285 $this->offHandInventory =
new PlayerOffHandInventory($this);
286 $this->enderInventory =
new PlayerEnderInventory($this);
287 $this->initHumanData($nbt);
289 $inventoryTag = $nbt->
getListTag(self::TAG_INVENTORY);
290 if($inventoryTag !==
null){
291 $inventoryItems = [];
292 $armorInventoryItems = [];
295 foreach($inventoryTag as $i => $item){
296 $slot = $item->getByte(SavedItemStackData::TAG_SLOT);
297 if($slot >= 0 && $slot < 9){
299 }elseif($slot >= 100 && $slot < 104){
300 $armorInventoryItems[$slot - 100] = Item::nbtDeserialize($item);
301 }elseif($slot >= 9 && $slot < $this->inventory->getSize() + 9){
302 $inventoryItems[$slot - 9] = Item::nbtDeserialize($item);
306 self::populateInventoryFromListTag($this->inventory, $inventoryItems);
307 self::populateInventoryFromListTag($this->armorInventory, $armorInventoryItems);
310 if($offHand !==
null){
311 $this->offHandInventory->setItem(0, Item::nbtDeserialize($offHand));
313 $this->offHandInventory->getListeners()->add(CallbackInventoryListener::onAnyChange(fn() => NetworkBroadcastUtils::broadcastEntityEvent(
315 fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onMobOffHandItemChange($recipients, $this)
318 $enderChestInventoryTag = $nbt->
getListTag(self::TAG_ENDER_CHEST_INVENTORY);
319 if($enderChestInventoryTag !==
null){
320 $enderChestInventoryItems = [];
323 foreach($enderChestInventoryTag as $i => $item){
324 $enderChestInventoryItems[$item->getByte(SavedItemStackData::TAG_SLOT)] = Item::nbtDeserialize($item);
326 self::populateInventoryFromListTag($this->enderInventory, $enderChestInventoryItems);
329 $this->inventory->setHeldItemIndex($nbt->getInt(self::TAG_SELECTED_INVENTORY_SLOT, 0));
330 $this->inventory->getHeldItemIndexChangeListeners()->add(fn() => NetworkBroadcastUtils::broadcastEntityEvent(
332 fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onMobMainHandItemChange($recipients, $this)
335 $this->hungerManager->setFood((
float) $nbt->getInt(self::TAG_FOOD_LEVEL, (
int) $this->hungerManager->getFood()));
336 $this->hungerManager->setExhaustion($nbt->getFloat(self::TAG_FOOD_EXHAUSTION_LEVEL, $this->hungerManager->getExhaustion()));
337 $this->hungerManager->setSaturation($nbt->getFloat(self::TAG_FOOD_SATURATION_LEVEL, $this->hungerManager->getSaturation()));
338 $this->hungerManager->setFoodTickTimer($nbt->getInt(self::TAG_FOOD_TICK_TIMER, $this->hungerManager->getFoodTickTimer()));
340 $this->xpManager->setXpAndProgressNoEvent(
341 $nbt->getInt(self::TAG_XP_LEVEL, 0),
342 $nbt->getFloat(self::TAG_XP_PROGRESS, 0.0));
343 $this->xpManager->setLifetimeTotalXp($nbt->getInt(self::TAG_LIFETIME_XP_TOTAL, 0));
345 if(($xpSeedTag = $nbt->
getTag(self::TAG_XP_SEED)) instanceof IntTag){
346 $this->xpSeed = $xpSeedTag->getValue();
348 $this->xpSeed = EnchantingHelper::generateSeed();
352 protected function entityBaseTick(
int $tickDiff = 1) : bool{
353 $hasUpdate = parent::entityBaseTick($tickDiff);
355 $this->hungerManager->tick($tickDiff);
356 $this->xpManager->tick($tickDiff);
361 public function getName() : string{
362 return $this->getNameTag();
366 parent::applyDamageModifiers($source);
368 $type = $source->getCause();
369 if($type !== EntityDamageEvent::CAUSE_SUICIDE && $type !== EntityDamageEvent::CAUSE_VOID
370 && ($this->inventory->getItemInHand() instanceof
Totem || $this->offHandInventory->getItem(0) instanceof
Totem)){
372 $compensation = $this->getHealth() - $source->getFinalDamage() - 1;
373 if($compensation <= -1){
374 $source->setModifier($compensation, EntityDamageEvent::MODIFIER_TOTEM);
380 parent::applyPostDamageEffects($source);
381 $totemModifier = $source->getModifier(EntityDamageEvent::MODIFIER_TOTEM);
382 if($totemModifier < 0){
383 $this->effectManager->clear();
385 $this->effectManager->add(
new EffectInstance(VanillaEffects::REGENERATION(), 40 * 20, 1));
386 $this->effectManager->add(
new EffectInstance(VanillaEffects::FIRE_RESISTANCE(), 40 * 20, 1));
387 $this->effectManager->add(
new EffectInstance(VanillaEffects::ABSORPTION(), 5 * 20, 1));
392 $hand = $this->inventory->getItemInHand();
393 if($hand instanceof
Totem){
395 $this->inventory->setItemInHand($hand);
396 }elseif(($offHand = $this->offHandInventory->getItem(0)) instanceof
Totem){
398 $this->offHandInventory->setItem(0, $offHand);
404 return array_filter(array_merge(
405 array_values($this->inventory->getContents()),
406 array_values($this->armorInventory->getContents()),
407 array_values($this->offHandInventory->getContents()),
408 ), function(
Item $item) : bool{ return !$item->hasEnchantment(
VanillaEnchantments::VANISHING()) && !$item->keepOnDeath(); });
412 $nbt = parent::saveNBT();
414 $nbt->
setInt(self::TAG_FOOD_LEVEL, (
int) $this->hungerManager->getFood());
415 $nbt->
setFloat(self::TAG_FOOD_EXHAUSTION_LEVEL, $this->hungerManager->getExhaustion());
416 $nbt->
setFloat(self::TAG_FOOD_SATURATION_LEVEL, $this->hungerManager->getSaturation());
417 $nbt->
setInt(self::TAG_FOOD_TICK_TIMER, $this->hungerManager->getFoodTickTimer());
419 $nbt->
setInt(self::TAG_XP_LEVEL, $this->xpManager->getXpLevel());
420 $nbt->
setFloat(self::TAG_XP_PROGRESS, $this->xpManager->getXpProgress());
421 $nbt->
setInt(self::TAG_LIFETIME_XP_TOTAL, $this->xpManager->getLifetimeTotalXp());
422 $nbt->
setInt(self::TAG_XP_SEED, $this->xpSeed);
424 $inventoryTag =
new ListTag([], NBT::TAG_Compound);
425 $nbt->
setTag(self::TAG_INVENTORY, $inventoryTag);
428 $slotCount = $this->inventory->getSize() + $this->inventory->getHotbarSize();
429 for($slot = $this->inventory->getHotbarSize(); $slot < $slotCount; ++$slot){
430 $item = $this->inventory->getItem($slot - 9);
431 if(!$item->isNull()){
432 $inventoryTag->push($item->nbtSerialize($slot));
437 for($slot = 100; $slot < 104; ++$slot){
438 $item = $this->armorInventory->getItem($slot - 100);
439 if(!$item->isNull()){
440 $inventoryTag->push($item->nbtSerialize($slot));
444 $nbt->
setInt(self::TAG_SELECTED_INVENTORY_SLOT, $this->inventory->getHeldItemIndex());
446 $offHandItem = $this->offHandInventory->getItem(0);
447 if(!$offHandItem->isNull()){
448 $nbt->setTag(self::TAG_OFF_HAND_ITEM, $offHandItem->nbtSerialize());
454 $slotCount = $this->enderInventory->getSize();
455 for($slot = 0; $slot < $slotCount; ++$slot){
456 $item = $this->enderInventory->getItem($slot);
457 if(!$item->isNull()){
458 $items[] = $item->nbtSerialize($slot);
462 $nbt->
setTag(self::TAG_ENDER_CHEST_INVENTORY,
new ListTag($items, NBT::TAG_Compound));
464 $nbt->
setTag(self::TAG_SKIN, CompoundTag::create()
465 ->setString(self::TAG_SKIN_NAME, $this->skin->getSkinId())
466 ->setByteArray(self::TAG_SKIN_DATA, $this->skin->getSkinData())
467 ->setByteArray(self::TAG_SKIN_CAPE_DATA, $this->skin->getCapeData())
468 ->setString(self::TAG_SKIN_GEOMETRY_NAME, $this->skin->getGeometryName())
469 ->setByteArray(self::TAG_SKIN_GEOMETRY_DATA, $this->skin->getGeometryData())
475 public function spawnTo(Player $player) : void{
476 if($player !== $this){
477 parent::spawnTo($player);
482 $networkSession = $player->getNetworkSession();
483 $typeConverter = $networkSession->getTypeConverter();
484 if(!($this instanceof
Player)){
485 $networkSession->sendDataPacket(PlayerListPacket::add([PlayerListEntry::createAdditionEntry($this->uuid, $this->
id, $this->getName(), $typeConverter->getSkinAdapter()->toSkinData($this->skin))]));
488 $networkSession->sendDataPacket(AddPlayerPacket::create(
489 $this->getUniqueId(),
493 $this->location->asVector3(),
495 $this->location->pitch,
496 $this->location->yaw,
497 $this->location->yaw,
498 ItemStackWrapper::legacy($typeConverter->coreItemStackToNet($this->getInventory()->getItemInHand())),
500 $this->getAllNetworkData(),
502 UpdateAbilitiesPacket::create(
new AbilitiesData(CommandPermissions::NORMAL, PlayerPermissions::VISITOR, $this->getId() , [
504 AbilitiesLayer::LAYER_BASE,
505 array_fill(0, AbilitiesLayer::NUMBER_OF_ABILITIES,
false),
516 $this->sendData([$player], [EntityMetadataProperties::NAMETAG =>
new StringMetadataProperty($this->getNameTag())]);
518 $entityEventBroadcaster = $networkSession->getEntityEventBroadcaster();
519 $entityEventBroadcaster->onMobArmorChange([$networkSession], $this);
520 $entityEventBroadcaster->onMobOffHandItemChange([$networkSession], $this);
522 if(!($this instanceof
Player)){
523 $networkSession->sendDataPacket(PlayerListPacket::remove([PlayerListEntry::createRemovalEntry($this->uuid)]));
527 public function getOffsetPosition(Vector3 $vector3) : Vector3{
528 return $vector3->add(0, 1.621, 0);
532 $this->inventory->removeAllViewers();
533 $this->inventory->getHeldItemIndexChangeListeners()->clear();
534 $this->offHandInventory->removeAllViewers();
535 $this->enderInventory->removeAllViewers();
542 $this->offHandInventory,
543 $this->enderInventory,
544 $this->hungerManager,
547 parent::destroyCycles();