Fixed eating

This commit is contained in:
PEMapModder 2016-02-11 15:42:07 +08:00
parent 52e8781d36
commit 28967ca495
5 changed files with 41 additions and 70 deletions

View File

@ -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;

View File

@ -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
*/

View File

@ -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;
}
}

View File

@ -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),

View File

@ -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());
}
}