PocketMine-MP 5.33.2 git-1133d49c924b4358c79d44eeb97dcbf56cb4d1eb
Loading...
Searching...
No Matches
Human.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\entity;
25
71use Ramsey\Uuid\Uuid;
72use Ramsey\Uuid\UuidInterface;
73use function array_fill;
74use function array_filter;
75use function array_key_exists;
76use function array_merge;
77use function array_values;
78use function min;
79
80class Human extends Living implements ProjectileSource, InventoryHolder{
81
82 private const TAG_INVENTORY = "Inventory"; //TAG_List<TAG_Compound>
83 private const TAG_OFF_HAND_ITEM = "OffHandItem"; //TAG_Compound
84 private const TAG_ENDER_CHEST_INVENTORY = "EnderChestInventory"; //TAG_List<TAG_Compound>
85 private const TAG_SELECTED_INVENTORY_SLOT = "SelectedInventorySlot"; //TAG_Int
86 private const TAG_FOOD_LEVEL = "foodLevel"; //TAG_Int
87 private const TAG_FOOD_EXHAUSTION_LEVEL = "foodExhaustionLevel"; //TAG_Float
88 private const TAG_FOOD_SATURATION_LEVEL = "foodSaturationLevel"; //TAG_Float
89 private const TAG_FOOD_TICK_TIMER = "foodTickTimer"; //TAG_Int
90 private const TAG_XP_LEVEL = "XpLevel"; //TAG_Int
91 private const TAG_XP_PROGRESS = "XpP"; //TAG_Float
92 private const TAG_LIFETIME_XP_TOTAL = "XpTotal"; //TAG_Int
93 private const TAG_XP_SEED = "XpSeed"; //TAG_Int
94 private const TAG_SKIN = "Skin"; //TAG_Compound
95 private const TAG_SKIN_NAME = "Name"; //TAG_String
96 private const TAG_SKIN_DATA = "Data"; //TAG_ByteArray
97 private const TAG_SKIN_CAPE_DATA = "CapeData"; //TAG_ByteArray
98 private const TAG_SKIN_GEOMETRY_NAME = "GeometryName"; //TAG_String
99 private const TAG_SKIN_GEOMETRY_DATA = "GeometryData"; //TAG_ByteArray
100
101 public function getNetworkTypeId() : string{ return EntityIds::PLAYER; }
102
103 protected Hotbar $hotbar;
104 protected Inventory $inventory;
105 protected Inventory $offHandInventory;
106 protected Inventory $enderInventory;
107
108 protected UuidInterface $uuid;
109
110 protected Skin $skin;
111
112 protected HungerManager $hungerManager;
113 protected ExperienceManager $xpManager;
114
115 protected int $xpSeed;
116
117 public function __construct(Location $location, Skin $skin, ?CompoundTag $nbt = null){
118 $this->skin = $skin;
119 parent::__construct($location, $nbt);
120 }
121
122 protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(1.8, 0.6, 1.62); }
123
128 public static function parseSkinNBT(CompoundTag $nbt) : Skin{
129 $skinTag = $nbt->getCompoundTag(self::TAG_SKIN);
130 if($skinTag === null){
131 throw new SavedDataLoadingException("Missing skin data");
132 }
133 return new Skin( //this throws if the skin is invalid
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), //old data (this used to be saved as a StringTag in older versions of PM)
136 $skinTag->getByteArray(self::TAG_SKIN_CAPE_DATA, ""),
137 $skinTag->getString(self::TAG_SKIN_GEOMETRY_NAME, ""),
138 $skinTag->getByteArray(self::TAG_SKIN_GEOMETRY_DATA, "")
139 );
140 }
141
142 public function getUniqueId() : UuidInterface{
143 return $this->uuid;
144 }
145
149 public function getSkin() : Skin{
150 return $this->skin;
151 }
152
157 public function setSkin(Skin $skin) : void{
158 $this->skin = $skin;
159 }
160
167 public function sendSkin(?array $targets = null) : void{
168 NetworkBroadcastUtils::broadcastPackets($targets ?? $this->hasSpawned, [
169 PlayerSkinPacket::create($this->getUniqueId(), "", "", TypeConverter::getInstance()->getSkinAdapter()->toSkinData($this->skin))
170 ]);
171 }
172
173 public function jump() : void{
174 parent::jump();
175 if($this->isSprinting()){
176 $this->hungerManager->exhaust(0.2, EntityExhaustEvent::CAUSE_SPRINT_JUMPING);
177 }else{
178 $this->hungerManager->exhaust(0.05, EntityExhaustEvent::CAUSE_JUMPING);
179 }
180 }
181
182 public function emote(string $emoteId) : void{
183 NetworkBroadcastUtils::broadcastEntityEvent(
184 $this->getViewers(),
185 fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onEmote($recipients, $this, $emoteId)
186 );
187 }
188
189 public function getHungerManager() : HungerManager{
190 return $this->hungerManager;
191 }
192
197 public function canEat() : bool{
198 return $this->hungerManager->isHungry() || $this->getWorld()->getDifficulty() === World::DIFFICULTY_PEACEFUL;
199 }
200
201 public function consumeObject(Consumable $consumable) : bool{
202 if($consumable instanceof FoodSource && $consumable->requiresHunger() && !$this->canEat()){
203 return false;
204 }
205
206 return parent::consumeObject($consumable);
207 }
208
209 protected function applyConsumptionResults(Consumable $consumable) : void{
210 if($consumable instanceof FoodSource){
211 $this->hungerManager->addFood($consumable->getFoodRestore());
212 $this->hungerManager->addSaturation($consumable->getSaturationRestore());
213 }
214
215 parent::applyConsumptionResults($consumable);
216 }
217
218 public function getXpManager() : ExperienceManager{
219 return $this->xpManager;
220 }
221
222 public function getEnchantmentSeed() : int{
223 return $this->xpSeed;
224 }
225
226 public function setEnchantmentSeed(int $seed) : void{
227 $this->xpSeed = $seed;
228 }
229
230 public function regenerateEnchantmentSeed() : void{
231 $this->xpSeed = EnchantingHelper::generateSeed();
232 }
233
234 public function getXpDropAmount() : int{
235 //this causes some XP to be lost on death when above level 1 (by design), dropping at most enough points for
236 //about 7.5 levels of XP.
237 return min(100, 7 * $this->xpManager->getXpLevel());
238 }
239
240 public function getHotbar() : Hotbar{
241 return $this->hotbar;
242 }
243
244 public function getInventory() : Inventory{
245 return $this->inventory;
246 }
247
248 public function getMainHandItem() : Item{
249 return $this->inventory->getItem($this->hotbar->getSelectedIndex());
250 }
251
252 public function setMainHandItem(Item $item) : void{
253 $this->inventory->setItem($this->hotbar->getSelectedIndex(), $item);
254 }
255
256 public function getOffHandItem() : Item{
257 return $this->offHandInventory->getItem(0);
258 }
259
260 public function setOffHandItem(Item $item) : void{
261 $this->offHandInventory->setItem(0, $item);
262 }
263
264 public function getOffHandInventory() : Inventory{ return $this->offHandInventory; }
265
266 public function getEnderInventory() : Inventory{
267 return $this->enderInventory;
268 }
269
270 public function getSneakOffset() : float{
271 return 0.31;
272 }
273
277 protected function initHumanData(CompoundTag $nbt) : void{
278 //TODO: use of NIL UUID for namespace is a hack; we should provide a proper UUID for the namespace
279 $this->uuid = Uuid::uuid3(Uuid::NIL, ((string) $this->getId()) . $this->skin->getSkinData() . $this->getNameTag());
280 }
281
286 private static function populateInventoryFromListTag(Inventory $inventory, array $items) : void{
287 $listeners = $inventory->getListeners()->toArray();
288 $inventory->getListeners()->clear();
289
290 $inventory->setContents($items);
291
292 $inventory->getListeners()->add(...$listeners);
293 }
294
295 protected function initEntity(CompoundTag $nbt) : void{
296 parent::initEntity($nbt);
297
298 $this->hungerManager = new HungerManager($this);
299 $this->xpManager = new ExperienceManager($this);
300
301 $this->inventory = new SimpleInventory(36);
302 $this->hotbar = new Hotbar();
303
304 $syncHeldItem = fn() => NetworkBroadcastUtils::broadcastEntityEvent(
305 $this->getViewers(),
306 fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onMobMainHandItemChange($recipients, $this)
307 );
308 $this->inventory->getListeners()->add(new CallbackInventoryListener(
309 function(Inventory $unused, int $slot, Item $unused2) use ($syncHeldItem) : void{
310 if($slot === $this->hotbar->getSelectedIndex()){
311 $syncHeldItem();
312 }
313 },
314 function(Inventory $unused, array $oldItems) use ($syncHeldItem) : void{
315 if(array_key_exists($this->hotbar->getSelectedIndex(), $oldItems)){
316 $syncHeldItem();
317 }
318 }
319 ));
320 $this->offHandInventory = new SimpleInventory(1);
321 $this->enderInventory = new SimpleInventory(27);
322 $this->initHumanData($nbt);
323
324 $inventoryTag = $nbt->getListTag(self::TAG_INVENTORY);
325 if($inventoryTag !== null){
326 $inventoryItems = [];
327 $armorInventoryItems = [];
328
330 foreach($inventoryTag as $i => $item){
331 $slot = $item->getByte(SavedItemStackData::TAG_SLOT);
332 if($slot >= 0 && $slot < 9){ //Hotbar
333 //Old hotbar saving stuff, ignore it
334 }elseif($slot >= 100 && $slot < 104){ //Armor
335 $armorInventoryItems[$slot - 100] = Item::nbtDeserialize($item);
336 }elseif($slot >= 9 && $slot < $this->inventory->getSize() + 9){
337 $inventoryItems[$slot - 9] = Item::nbtDeserialize($item);
338 }
339 }
340
341 self::populateInventoryFromListTag($this->inventory, $inventoryItems);
342 self::populateInventoryFromListTag($this->armorInventory, $armorInventoryItems);
343 }
344 $offHand = $nbt->getCompoundTag(self::TAG_OFF_HAND_ITEM);
345 if($offHand !== null){
346 $this->setOffHandItem(Item::nbtDeserialize($offHand));
347 }
348 $this->offHandInventory->getListeners()->add(CallbackInventoryListener::onAnyChange(fn() => NetworkBroadcastUtils::broadcastEntityEvent(
349 $this->getViewers(),
350 fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onMobOffHandItemChange($recipients, $this)
351 )));
352
353 $enderChestInventoryTag = $nbt->getListTag(self::TAG_ENDER_CHEST_INVENTORY);
354 if($enderChestInventoryTag !== null){
355 $enderChestInventoryItems = [];
356
358 foreach($enderChestInventoryTag as $i => $item){
359 $enderChestInventoryItems[$item->getByte(SavedItemStackData::TAG_SLOT)] = Item::nbtDeserialize($item);
360 }
361 self::populateInventoryFromListTag($this->enderInventory, $enderChestInventoryItems);
362 }
363
364 $this->hotbar->setSelectedIndex($nbt->getInt(self::TAG_SELECTED_INVENTORY_SLOT, 0));
365 //TODO: cyclic reference
366 $this->hotbar->getSelectedIndexChangeListeners()->add(fn() => NetworkBroadcastUtils::broadcastEntityEvent(
367 $this->getViewers(),
368 fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onMobMainHandItemChange($recipients, $this)
369 ));
370
371 $this->hungerManager->setFood((float) $nbt->getInt(self::TAG_FOOD_LEVEL, (int) $this->hungerManager->getFood()));
372 $this->hungerManager->setExhaustion($nbt->getFloat(self::TAG_FOOD_EXHAUSTION_LEVEL, $this->hungerManager->getExhaustion()));
373 $this->hungerManager->setSaturation($nbt->getFloat(self::TAG_FOOD_SATURATION_LEVEL, $this->hungerManager->getSaturation()));
374 $this->hungerManager->setFoodTickTimer($nbt->getInt(self::TAG_FOOD_TICK_TIMER, $this->hungerManager->getFoodTickTimer()));
375
376 $this->xpManager->setXpAndProgressNoEvent(
377 $nbt->getInt(self::TAG_XP_LEVEL, 0),
378 $nbt->getFloat(self::TAG_XP_PROGRESS, 0.0));
379 $this->xpManager->setLifetimeTotalXp($nbt->getInt(self::TAG_LIFETIME_XP_TOTAL, 0));
380
381 if(($xpSeedTag = $nbt->getTag(self::TAG_XP_SEED)) instanceof IntTag){
382 $this->xpSeed = $xpSeedTag->getValue();
383 }else{
384 $this->xpSeed = EnchantingHelper::generateSeed();
385 }
386 }
387
388 protected function entityBaseTick(int $tickDiff = 1) : bool{
389 $hasUpdate = parent::entityBaseTick($tickDiff);
390
391 $this->hungerManager->tick($tickDiff);
392 $this->xpManager->tick($tickDiff);
393
394 return $hasUpdate;
395 }
396
397 public function getName() : string{
398 return $this->getNameTag();
399 }
400
401 public function applyDamageModifiers(EntityDamageEvent $source) : void{
402 parent::applyDamageModifiers($source);
403
404 $type = $source->getCause();
405 if($type !== EntityDamageEvent::CAUSE_SUICIDE && $type !== EntityDamageEvent::CAUSE_VOID
406 && ($this->getMainHandItem() instanceof Totem || $this->getOffHandItem() instanceof Totem)){
407
408 $compensation = $this->getHealth() - $source->getFinalDamage() - 1;
409 if($compensation <= -1){
410 $source->setModifier($compensation, EntityDamageEvent::MODIFIER_TOTEM);
411 }
412 }
413 }
414
415 protected function applyPostDamageEffects(EntityDamageEvent $source) : void{
416 parent::applyPostDamageEffects($source);
417 $totemModifier = $source->getModifier(EntityDamageEvent::MODIFIER_TOTEM);
418 if($totemModifier < 0){ //Totem prevented death
419 $this->effectManager->clear();
420
421 $this->effectManager->add(new EffectInstance(VanillaEffects::REGENERATION(), 40 * 20, 1));
422 $this->effectManager->add(new EffectInstance(VanillaEffects::FIRE_RESISTANCE(), 40 * 20, 1));
423 $this->effectManager->add(new EffectInstance(VanillaEffects::ABSORPTION(), 5 * 20, 1));
424
425 $this->broadcastAnimation(new TotemUseAnimation($this));
426 $this->broadcastSound(new TotemUseSound());
427
428 $hand = $this->getMainHandItem();
429 if($hand instanceof Totem){
430 $hand->pop(); //Plugins could alter max stack size
431 $this->setMainHandItem($hand);
432 }elseif(($offHand = $this->getOffHandItem()) instanceof Totem){
433 $offHand->pop();
434 $this->setOffHandItem($offHand);
435 }
436 }
437 }
438
439 public function getDrops() : array{
440 return array_filter(array_merge(
441 array_values($this->inventory->getContents()),
442 array_values($this->armorInventory->getContents()),
443 array_values($this->offHandInventory->getContents()),
444 ), function(Item $item) : bool{ return !$item->hasEnchantment(VanillaEnchantments::VANISHING()) && !$item->keepOnDeath(); });
445 }
446
447 public function saveNBT() : CompoundTag{
448 $nbt = parent::saveNBT();
449
450 $nbt->setInt(self::TAG_FOOD_LEVEL, (int) $this->hungerManager->getFood());
451 $nbt->setFloat(self::TAG_FOOD_EXHAUSTION_LEVEL, $this->hungerManager->getExhaustion());
452 $nbt->setFloat(self::TAG_FOOD_SATURATION_LEVEL, $this->hungerManager->getSaturation());
453 $nbt->setInt(self::TAG_FOOD_TICK_TIMER, $this->hungerManager->getFoodTickTimer());
454
455 $nbt->setInt(self::TAG_XP_LEVEL, $this->xpManager->getXpLevel());
456 $nbt->setFloat(self::TAG_XP_PROGRESS, $this->xpManager->getXpProgress());
457 $nbt->setInt(self::TAG_LIFETIME_XP_TOTAL, $this->xpManager->getLifetimeTotalXp());
458 $nbt->setInt(self::TAG_XP_SEED, $this->xpSeed);
459
460 $inventoryTag = new ListTag([], NBT::TAG_Compound);
461 $nbt->setTag(self::TAG_INVENTORY, $inventoryTag);
462
463 //Normal inventory
464 $slotCount = $this->inventory->getSize() + $this->hotbar->getSize();
465 for($slot = $this->hotbar->getSize(); $slot < $slotCount; ++$slot){
466 $item = $this->inventory->getItem($slot - 9);
467 if(!$item->isNull()){
468 $inventoryTag->push($item->nbtSerialize($slot));
469 }
470 }
471
472 //Armor
473 for($slot = 100; $slot < 104; ++$slot){
474 $item = $this->armorInventory->getItem($slot - 100);
475 if(!$item->isNull()){
476 $inventoryTag->push($item->nbtSerialize($slot));
477 }
478 }
479
480 $nbt->setInt(self::TAG_SELECTED_INVENTORY_SLOT, $this->hotbar->getSelectedIndex());
481
482 $offHandItem = $this->getOffHandItem();
483 if(!$offHandItem->isNull()){
484 $nbt->setTag(self::TAG_OFF_HAND_ITEM, $offHandItem->nbtSerialize());
485 }
486
488 $items = [];
489
490 $slotCount = $this->enderInventory->getSize();
491 for($slot = 0; $slot < $slotCount; ++$slot){
492 $item = $this->enderInventory->getItem($slot);
493 if(!$item->isNull()){
494 $items[] = $item->nbtSerialize($slot);
495 }
496 }
497
498 $nbt->setTag(self::TAG_ENDER_CHEST_INVENTORY, new ListTag($items, NBT::TAG_Compound));
499
500 $nbt->setTag(self::TAG_SKIN, CompoundTag::create()
501 ->setString(self::TAG_SKIN_NAME, $this->skin->getSkinId())
502 ->setByteArray(self::TAG_SKIN_DATA, $this->skin->getSkinData())
503 ->setByteArray(self::TAG_SKIN_CAPE_DATA, $this->skin->getCapeData())
504 ->setString(self::TAG_SKIN_GEOMETRY_NAME, $this->skin->getGeometryName())
505 ->setByteArray(self::TAG_SKIN_GEOMETRY_DATA, $this->skin->getGeometryData())
506 );
507
508 return $nbt;
509 }
510
511 public function spawnTo(Player $player) : void{
512 if($player !== $this){
513 parent::spawnTo($player);
514 }
515 }
516
517 protected function sendSpawnPacket(Player $player) : void{
518 $networkSession = $player->getNetworkSession();
519 $typeConverter = $networkSession->getTypeConverter();
520 if(!($this instanceof Player)){
521 $networkSession->sendDataPacket(PlayerListPacket::add([PlayerListEntry::createAdditionEntry($this->uuid, $this->id, $this->getName(), $typeConverter->getSkinAdapter()->toSkinData($this->skin))]));
522 }
523
524 $networkSession->sendDataPacket(AddPlayerPacket::create(
525 $this->getUniqueId(),
526 $this->getName(),
527 $this->getId(),
528 "",
529 $this->location->asVector3(),
530 $this->getMotion(),
531 $this->location->pitch,
532 $this->location->yaw,
533 $this->location->yaw, //TODO: head yaw
534 ItemStackWrapper::legacy($typeConverter->coreItemStackToNet($this->getMainHandItem())),
535 GameMode::SURVIVAL,
536 $this->getAllNetworkData(),
537 new PropertySyncData([], []),
538 UpdateAbilitiesPacket::create(new AbilitiesData(CommandPermissions::NORMAL, PlayerPermissions::VISITOR, $this->getId() /* TODO: this should be unique ID */, [
539 new AbilitiesLayer(
540 AbilitiesLayer::LAYER_BASE,
541 array_fill(0, AbilitiesLayer::NUMBER_OF_ABILITIES, false),
542 0.0,
543 0.0,
544 0.0
545 )
546 ])),
547 [], //TODO: entity links
548 "", //device ID (we intentionally don't send this - secvuln)
549 DeviceOS::UNKNOWN //we intentionally don't send this (secvuln)
550 ));
551
552 //TODO: Hack for MCPE 1.2.13: DATA_NAMETAG is useless in AddPlayerPacket, so it has to be sent separately
553 $this->sendData([$player], [EntityMetadataProperties::NAMETAG => new StringMetadataProperty($this->getNameTag())]);
554
555 $entityEventBroadcaster = $networkSession->getEntityEventBroadcaster();
556 $entityEventBroadcaster->onMobArmorChange([$networkSession], $this);
557 $entityEventBroadcaster->onMobOffHandItemChange([$networkSession], $this);
558
559 if(!($this instanceof Player)){
560 $networkSession->sendDataPacket(PlayerListPacket::remove([PlayerListEntry::createRemovalEntry($this->uuid)]));
561 }
562 }
563
564 public function getOffsetPosition(Vector3 $vector3) : Vector3{
565 return $vector3->add(0, 1.621, 0); //TODO: +0.001 hack for MCPE falling underground
566 }
567
568 protected function onDispose() : void{
569 $this->hotbar->getSelectedIndexChangeListeners()->clear();
570 $this->inventory->removeAllWindows();
571 $this->offHandInventory->removeAllWindows();
572 $this->enderInventory->removeAllWindows();
573 parent::onDispose();
574 }
575
576 protected function destroyCycles() : void{
577 unset(
578 $this->hungerManager,
579 $this->xpManager
580 );
581 parent::destroyCycles();
582 }
583}
applyPostDamageEffects(EntityDamageEvent $source)
Definition Human.php:415
applyConsumptionResults(Consumable $consumable)
Definition Human.php:209
static parseSkinNBT(CompoundTag $nbt)
Definition Human.php:128
sendSkin(?array $targets=null)
Definition Human.php:167
sendSpawnPacket(Player $player)
Definition Human.php:517
initHumanData(CompoundTag $nbt)
Definition Human.php:277
setSkin(Skin $skin)
Definition Human.php:157
applyDamageModifiers(EntityDamageEvent $source)
Definition Human.php:401
consumeObject(Consumable $consumable)
Definition Human.php:201
setInt(string $name, int $value)
setTag(string $name, Tag $tag)
setFloat(string $name, float $value)
onMobMainHandItemChange(array $recipients, Human $mob)