diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index bfc346e83..c657cfd0e 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -76,6 +76,7 @@ use pocketmine\inventory\PlayerInventory; use pocketmine\inventory\ShapedRecipe; use pocketmine\inventory\ShapelessRecipe; use pocketmine\inventory\SimpleTransactionGroup; +use pocketmine\item\Food; use pocketmine\item\Item; use pocketmine\level\ChunkLoader; use pocketmine\level\format\FullChunk; @@ -2433,65 +2434,19 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade $this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ACTION, false); //TODO: check if this should be true switch($packet->event){ - case 9: //Eating - $items = [ //TODO: change to hunger; add RabbitStew and RawRabbit and RottenFlesh - Item::APPLE => 4, - Item::MUSHROOM_STEW => 10, - Item::BEETROOT_SOUP => 10, - Item::BREAD => 5, - Item::RAW_PORKCHOP => 3, - Item::COOKED_PORKCHOP => 8, - Item::RAW_BEEF => 3, - Item::STEAK => 8, - Item::COOKED_CHICKEN => 6, - Item::RAW_CHICKEN => 2, - Item::MELON_SLICE => 2, - Item::GOLDEN_APPLE => 10, - Item::PUMPKIN_PIE => 8, - Item::CARROT => 4, - Item::POTATO => 1, - Item::BAKED_POTATO => 6, - Item::COOKIE => 2, - Item::COOKED_FISH => [ - 0 => 5, - 1 => 6 - ], - Item::RAW_FISH => [ - 0 => 2, - 1 => 2, - 2 => 1, - 3 => 1 - ], - ]; + case EntityEventPacket::USE_ITEM: //Eating $slot = $this->inventory->getItemInHand(); - if($this->getHealth() < $this->getMaxHealth() and isset($items[$slot->getId()])){ - $this->server->getPluginManager()->callEvent($ev = new PlayerItemConsumeEvent($this, $slot)); - if($ev->isCancelled()){ + + if($slot instanceof Food){ + $ev = new PlayerItemConsumeEvent($this, $slot); + if($this->getFood() >= $this->getMaxFood()){ + $ev->setCancelled(); + } + $this->server->getPluginManager()->callEvent($ev); + if(!$ev->isCancelled()){ + $slot->onEat($this); + }else{ $this->inventory->sendContents($this); - break; - } - - $pk = new EntityEventPacket(); - $pk->eid = $this->getId(); - $pk->event = EntityEventPacket::USE_ITEM; - $this->dataPacket($pk); - Server::broadcastPacket($this->getViewers(), $pk); - - $amount = $items[$slot->getId()]; - if(is_array($amount)){ - $amount = isset($amount[$slot->getDamage()]) ? $amount[$slot->getDamage()] : 0; - } - $ev = new EntityRegainHealthEvent($this, $amount, EntityRegainHealthEvent::CAUSE_EATING); - $this->heal($ev->getAmount(), $ev); - - --$slot->count; - $this->inventory->setItemInHand($slot); - if($slot->getId() === Item::MUSHROOM_STEW or $slot->getId() === Item::BEETROOT_SOUP){ - $this->inventory->addItem(Item::get(Item::BOWL, 0, 1)); - }elseif($slot->getId() === Item::RAW_FISH and $slot->getDamage() === 3){ //Pufferfish - //$this->addEffect(Effect::getEffect(Effect::HUNGER)->setAmplifier(2)->setDuration(15 * 20)); - $this->addEffect(Effect::getEffect(Effect::NAUSEA)->setAmplifier(1)->setDuration(15 * 20)); - $this->addEffect(Effect::getEffect(Effect::POISON)->setAmplifier(3)->setDuration(60 * 20)); } } break; @@ -3264,7 +3219,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade $pk->eid = 0; $pk->event = EntityEventPacket::HURT_ANIMATION; $this->dataPacket($pk); - + if($this->isSurvival()){ $this->exhaust(0.3); } diff --git a/src/pocketmine/entity/Human.php b/src/pocketmine/entity/Human.php index c8e91909c..312bf2968 100644 --- a/src/pocketmine/entity/Human.php +++ b/src/pocketmine/entity/Human.php @@ -94,7 +94,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ } public function getFood() : float{ - return (float) $this->attributeMap->getAttribute(Attribute::HUNGER)->getValue(); + return $this->attributeMap->getAttribute(Attribute::HUNGER)->getValue(); } /** @@ -120,6 +120,10 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ } } + public function getMaxFood() : float{ + return $this->attributeMap->getAttribute(Attribute::HUNGER)->getMaxValue(); + } + public function addFood(float $amount){ $attr = $this->attributeMap->getAttribute(Attribute::HUNGER); $amount = max(min($amount, $attr->getMaxValue()), $attr->getMinValue()); @@ -162,12 +166,6 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ /** * Increases a human's exhaustion level. - * TODO walk per meter: 0.01 - * TODO sneak per meter: 0.005 - * TODO swim per meter: 0.015 - * TODO sprint per meter: 0.1 - * TODO jump: 0.2 - * TODO regen per halfheart | food >= 18: 4.0 * * @param float $amount */ diff --git a/src/pocketmine/item/CookedFish.php b/src/pocketmine/item/CookedFish.php index 62111645a..bc8dceb4c 100644 --- a/src/pocketmine/item/CookedFish.php +++ b/src/pocketmine/item/CookedFish.php @@ -30,7 +30,7 @@ class CookedFish extends Fish{ return $this->meta === self::FISH_SALMON ? 6 : 5; } - public function getSaturationRestore() : int{ + public function getSaturationRestore() : float{ return $this->meta === self::FISH_SALMON ? 9.6 : 6; } } diff --git a/src/pocketmine/item/Fish.php b/src/pocketmine/item/Fish.php index 7a477f5b6..aa6c9e5fc 100644 --- a/src/pocketmine/item/Fish.php +++ b/src/pocketmine/item/Fish.php @@ -67,7 +67,7 @@ class Fish extends Food{ return 0; } - public function getAdditionEffects(){ + public function getAdditionEffects() : array{ return $this->meta === self::FISH_PUFFERFISH ? [ Effect::getEffect(Effect::HUNGER)->setDuration(300)->setAmplifier(2), Effect::getEffect(Effect::NAUSEA)->setDuration(300)->setAmplifier(1), diff --git a/src/pocketmine/item/Food.php b/src/pocketmine/item/Food.php index a10c85689..52ba6bbb1 100644 --- a/src/pocketmine/item/Food.php +++ b/src/pocketmine/item/Food.php @@ -22,7 +22,10 @@ namespace pocketmine\item; use pocketmine\entity\Effect; +use pocketmine\entity\Human; +use pocketmine\network\protocol\EntityEventPacket; use pocketmine\Player; +use pocketmine\Server; abstract class Food extends Item{ public abstract function getFoodRestore() : int; @@ -33,8 +36,9 @@ abstract class Food extends Item{ if($this->getCount() === 1){ return Item::get(0); }else{ - $this->count--; - return $this; + $new = clone $this; + $new->count--; + return $new; } } @@ -45,6 +49,20 @@ abstract class Food extends Item{ return []; } - public function onEat(Player $player){ + public function onEat(Human $human){ + $pk = new EntityEventPacket(); + $pk->eid = $human->getId(); + $pk->event = EntityEventPacket::USE_ITEM; + if($human instanceof Player){ + $human->dataPacket($pk); + } + Server::broadcastPacket($human->getViewers(), $pk); + + $human->addSaturation($this->getSaturationRestore()); + $human->addFood($this->getFoodRestore()); + foreach($this->getAdditionEffects() as $effect){ + $human->addEffect($effect); + } + $human->getInventory()->setItemInHand($this->getResidue()); } }