Separate consumable item interfaces from general consumable interfaces (#3595)

I wonder if there's a way to generalise item consuming beyond just eating/drinking. Stuff like lava bucket in a furnace needs the same kind of "leftover" logic.
This commit is contained in:
Dylan T 2020-06-28 17:53:03 +01:00 committed by GitHub
parent 01d221b794
commit d585081c22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 83 additions and 36 deletions

View File

@ -25,8 +25,8 @@ namespace pocketmine\block;
use pocketmine\block\utils\BlockDataSerializer; use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\entity\effect\EffectInstance; use pocketmine\entity\effect\EffectInstance;
use pocketmine\entity\FoodSource;
use pocketmine\entity\Living; use pocketmine\entity\Living;
use pocketmine\item\FoodSource;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB; use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing; use pocketmine\math\Facing;

View File

@ -21,25 +21,14 @@
declare(strict_types=1); declare(strict_types=1);
namespace pocketmine\item; namespace pocketmine\entity;
use pocketmine\block\Block;
use pocketmine\entity\effect\EffectInstance; use pocketmine\entity\effect\EffectInstance;
use pocketmine\entity\Living;
/** /**
* Interface implemented by objects that can be consumed by mobs. * Interface implemented by objects that can be consumed by mobs.
*/ */
interface Consumable{ interface Consumable{
/**
* Returns the leftover that this Consumable produces when it is consumed. For Items, this is usually air, but could
* be an Item to add to a Player's inventory afterwards (such as a bowl).
*
* @return Item|Block|mixed
*/
public function getResidue();
/** /**
* @return EffectInstance[] * @return EffectInstance[]
*/ */

View File

@ -21,7 +21,7 @@
declare(strict_types=1); declare(strict_types=1);
namespace pocketmine\item; namespace pocketmine\entity;
/** /**
* Interface implemented by objects that can be consumed by players, giving them food and saturation. * Interface implemented by objects that can be consumed by players, giving them food and saturation.

View File

@ -32,9 +32,7 @@ use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\event\player\PlayerExhaustEvent; use pocketmine\event\player\PlayerExhaustEvent;
use pocketmine\inventory\InventoryHolder; use pocketmine\inventory\InventoryHolder;
use pocketmine\inventory\PlayerInventory; use pocketmine\inventory\PlayerInventory;
use pocketmine\item\Consumable;
use pocketmine\item\enchantment\Enchantment; use pocketmine\item\enchantment\Enchantment;
use pocketmine\item\FoodSource;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\item\Totem; use pocketmine\item\Totem;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;

View File

@ -39,7 +39,6 @@ use pocketmine\inventory\ArmorInventory;
use pocketmine\inventory\CallbackInventoryListener; use pocketmine\inventory\CallbackInventoryListener;
use pocketmine\inventory\Inventory; use pocketmine\inventory\Inventory;
use pocketmine\item\Armor; use pocketmine\item\Armor;
use pocketmine\item\Consumable;
use pocketmine\item\Durable; use pocketmine\item\Durable;
use pocketmine\item\enchantment\Enchantment; use pocketmine\item\enchantment\Enchantment;
use pocketmine\item\Item; use pocketmine\item\Item;

View File

@ -37,7 +37,7 @@ class BeetrootSoup extends Food{
return 7.2; return 7.2;
} }
public function getResidue(){ public function getResidue() : Item{
return VanillaItems::BOWL(); return VanillaItems::BOWL();
} }
} }

View File

@ -0,0 +1,38 @@
<?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\item;
use pocketmine\entity\Consumable;
/**
* Interface implemented by objects that can be consumed by mobs.
*/
interface ConsumableItem extends Consumable{
/**
* Returns the leftover that this Consumable produces when it is consumed. For Items, this is usually air, but could
* be an Item to add to a Player's inventory afterwards (such as a bowl).
*/
public function getResidue() : Item;
}

View File

@ -23,17 +23,15 @@ declare(strict_types=1);
namespace pocketmine\item; namespace pocketmine\item;
use pocketmine\entity\FoodSource;
use pocketmine\entity\Living; use pocketmine\entity\Living;
abstract class Food extends Item implements FoodSource{ abstract class Food extends Item implements FoodSourceItem{
public function requiresHunger() : bool{ public function requiresHunger() : bool{
return true; return true;
} }
/** public function getResidue() : Item{
* @return Item
*/
public function getResidue(){
return ItemFactory::air(); return ItemFactory::air();
} }

View File

@ -0,0 +1,30 @@
<?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\item;
use pocketmine\entity\FoodSource;
interface FoodSourceItem extends ConsumableItem, FoodSource{
}

View File

@ -25,13 +25,13 @@ namespace pocketmine\item;
use pocketmine\entity\Living; use pocketmine\entity\Living;
class MilkBucket extends Item implements Consumable{ class MilkBucket extends Item implements ConsumableItem{
public function getMaxStackSize() : int{ public function getMaxStackSize() : int{
return 1; return 1;
} }
public function getResidue(){ public function getResidue() : Item{
return VanillaItems::BUCKET(); return VanillaItems::BUCKET();
} }

View File

@ -37,7 +37,7 @@ class MushroomStew extends Food{
return 7.2; return 7.2;
} }
public function getResidue(){ public function getResidue() : Item{
return VanillaItems::BOWL(); return VanillaItems::BOWL();
} }
} }

View File

@ -27,7 +27,7 @@ use pocketmine\entity\effect\EffectInstance;
use pocketmine\entity\effect\VanillaEffects; use pocketmine\entity\effect\VanillaEffects;
use pocketmine\entity\Living; use pocketmine\entity\Living;
class Potion extends Item implements Consumable{ class Potion extends Item implements ConsumableItem{
public const WATER = 0; public const WATER = 0;
public const MUNDANE = 1; public const MUNDANE = 1;
@ -274,7 +274,7 @@ class Potion extends Item implements Consumable{
return self::getPotionEffectsById($this->potionId); return self::getPotionEffectsById($this->potionId);
} }
public function getResidue(){ public function getResidue() : Item{
return VanillaItems::GLASS_BOTTLE(); return VanillaItems::GLASS_BOTTLE();
} }
} }

View File

@ -37,7 +37,7 @@ class RabbitStew extends Food{
return 12; return 12;
} }
public function getResidue(){ public function getResidue() : Item{
return VanillaItems::BOWL(); return VanillaItems::BOWL();
} }
} }

View File

@ -73,7 +73,7 @@ use pocketmine\form\Form;
use pocketmine\form\FormValidationException; use pocketmine\form\FormValidationException;
use pocketmine\inventory\Inventory; use pocketmine\inventory\Inventory;
use pocketmine\inventory\PlayerCursorInventory; use pocketmine\inventory\PlayerCursorInventory;
use pocketmine\item\Consumable; use pocketmine\item\ConsumableItem;
use pocketmine\item\enchantment\EnchantmentInstance; use pocketmine\item\enchantment\EnchantmentInstance;
use pocketmine\item\enchantment\MeleeWeaponEnchantment; use pocketmine\item\enchantment\MeleeWeaponEnchantment;
use pocketmine\item\Item; use pocketmine\item\Item;
@ -1489,7 +1489,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
*/ */
public function consumeHeldItem() : bool{ public function consumeHeldItem() : bool{
$slot = $this->inventory->getItemInHand(); $slot = $this->inventory->getItemInHand();
if($slot instanceof Consumable){ if($slot instanceof ConsumableItem){
$ev = new PlayerItemConsumeEvent($this, $slot); $ev = new PlayerItemConsumeEvent($this, $slot);
if($this->hasItemCooldown($slot)){ if($this->hasItemCooldown($slot)){
$ev->setCancelled(); $ev->setCancelled();

View File

@ -180,11 +180,6 @@ parameters:
count: 1 count: 1
path: ../../../src/player/Player.php path: ../../../src/player/Player.php
-
message: "#^Parameter \\#1 \\.\\.\\.\\$slots of method pocketmine\\\\inventory\\\\BaseInventory\\:\\:addItem\\(\\) expects pocketmine\\\\item\\\\Item, mixed given\\.$#"
count: 1
path: ../../../src/player/Player.php
- -
message: "#^Parameter \\#1 \\$description of method pocketmine\\\\command\\\\Command\\:\\:setDescription\\(\\) expects string, mixed given\\.$#" message: "#^Parameter \\#1 \\$description of method pocketmine\\\\command\\\\Command\\:\\:setDescription\\(\\) expects string, mixed given\\.$#"
count: 1 count: 1