Consumables refactor (#1796)

* Removed broken EntityEatEvents - these don't fit the pattern since they only apply to Human entities anyway. PlayerItemConsumeEvent and PlayerInteractEvent can be used for cancellation purposes, and plugins can do custom stuff without mess.

* Restrict item consuming to Living entities only

* Added FoodSource->requiresHunger()

* Only items implementing the Consumable interface can now be consumed.

* The effects from consuming items are now generic-ized by way of the Living->consume() function. This is overridden in Human to allow applying food and hunger.

* Fixed the hardcoded mess for buckets
This commit is contained in:
Dylan K. Taylor
2017-12-23 13:03:41 +00:00
committed by GitHub
parent 329fe7d844
commit 6e1df36188
14 changed files with 113 additions and 306 deletions

View File

@ -1,54 +0,0 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\event\entity;
use pocketmine\block\Block;
use pocketmine\entity\Entity;
use pocketmine\item\FoodSource;
class EntityEatBlockEvent extends EntityEatEvent{
public function __construct(Entity $entity, FoodSource $foodSource){
if(!($foodSource instanceof Block)){
throw new \InvalidArgumentException("Food source must be a block");
}
parent::__construct($entity, $foodSource);
}
/**
* @return Block
*/
public function getResidue(){
return parent::getResidue();
}
/**
* @param Block $residue
*/
public function setResidue($residue){
if(!($residue instanceof Block)){
throw new \InvalidArgumentException("Eating a Block can only result in a Block residue");
}
parent::setResidue($residue);
}
}

View File

@ -1,109 +0,0 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\event\entity;
use pocketmine\entity\Effect;
use pocketmine\entity\Entity;
use pocketmine\event\Cancellable;
use pocketmine\item\FoodSource;
class EntityEatEvent extends EntityEvent implements Cancellable{
public static $handlerList = null;
/** @var FoodSource */
private $foodSource;
/** @var int */
private $foodRestore;
/** @var float */
private $saturationRestore;
/** @var mixed */
private $residue;
/** @var Effect[] */
private $additionalEffects;
public function __construct(Entity $entity, FoodSource $foodSource){
$this->entity = $entity;
$this->foodSource = $foodSource;
$this->foodRestore = $foodSource->getFoodRestore();
$this->saturationRestore = $foodSource->getSaturationRestore();
$this->residue = $foodSource->getResidue();
$this->additionalEffects = $foodSource->getAdditionalEffects();
}
public function getFoodSource() : FoodSource{
return $this->foodSource;
}
public function getFoodRestore() : int{
return $this->foodRestore;
}
public function setFoodRestore(int $foodRestore){
$this->foodRestore = $foodRestore;
}
public function getSaturationRestore() : float{
return $this->saturationRestore;
}
public function setSaturationRestore(float $saturationRestore){
$this->saturationRestore = $saturationRestore;
}
/**
* Returns the result of eating the food source.
* @return mixed
*/
public function getResidue(){
return $this->residue;
}
/**
* @param mixed $residue
*/
public function setResidue($residue){
$this->residue = $residue;
}
/**
* @return Effect[]
*/
public function getAdditionalEffects() : array{
return $this->additionalEffects;
}
/**
* @param Effect[] $additionalEffects
*
* @throws \TypeError
*/
public function setAdditionalEffects(array $additionalEffects){
foreach($additionalEffects as $effect){
if(!($effect instanceof Effect)){
throw new \TypeError("Argument 1 passed to EntityEatEvent::setAdditionalEffects() must be an Effect array");
}
}
$this->additionalEffects = $additionalEffects;
}
}

View File

@ -1,51 +0,0 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\event\entity;
use pocketmine\entity\Entity;
use pocketmine\item\Food;
use pocketmine\item\Item;
class EntityEatItemEvent extends EntityEatEvent{
public function __construct(Entity $entity, Food $foodSource){
parent::__construct($entity, $foodSource);
}
/**
* @return Item
*/
public function getResidue(){
return parent::getResidue();
}
/**
* @param Item $residue
*/
public function setResidue($residue){
if(!($residue instanceof Item)){
throw new \InvalidArgumentException("Eating an Item can only result in an Item residue");
}
parent::setResidue($residue);
}
}