diff --git a/src/item/Bow.php b/src/item/Bow.php index fd6364e84..41777e0a6 100644 --- a/src/item/Bow.php +++ b/src/item/Bow.php @@ -123,4 +123,8 @@ class Bow extends Tool implements Releasable{ return ItemUseResult::SUCCESS(); } + + public function canStartUsingItem(Player $player) : bool{ + return !$player->hasFiniteResources() || $player->getOffHandInventory()->contains($arrow = VanillaItems::ARROW()) || $player->getInventory()->contains($arrow); + } } diff --git a/src/item/Food.php b/src/item/Food.php index ff737e329..ddaa77da6 100644 --- a/src/item/Food.php +++ b/src/item/Food.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\item; use pocketmine\entity\Living; +use pocketmine\player\Player; abstract class Food extends Item implements FoodSourceItem{ public function requiresHunger() : bool{ @@ -41,4 +42,8 @@ abstract class Food extends Item implements FoodSourceItem{ public function onConsume(Living $consumer) : void{ } + + public function canStartUsingItem(Player $player) : bool{ + return !$this->requiresHunger() || $player->getHungerManager()->isHungry(); + } } diff --git a/src/item/MilkBucket.php b/src/item/MilkBucket.php index 4419a510f..f8c8b3ddf 100644 --- a/src/item/MilkBucket.php +++ b/src/item/MilkBucket.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\item; use pocketmine\entity\Living; +use pocketmine\player\Player; class MilkBucket extends Item implements ConsumableItem{ @@ -42,4 +43,8 @@ class MilkBucket extends Item implements ConsumableItem{ public function onConsume(Living $consumer) : void{ $consumer->getEffects()->clear(); } + + public function canStartUsingItem(Player $player) : bool{ + return true; + } } diff --git a/src/item/Potion.php b/src/item/Potion.php index 5b2bbc17e..5be216e96 100644 --- a/src/item/Potion.php +++ b/src/item/Potion.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\item; use pocketmine\entity\Living; +use pocketmine\player\Player; class Potion extends Item implements ConsumableItem{ @@ -52,4 +53,8 @@ class Potion extends Item implements ConsumableItem{ public function getResidue() : Item{ return VanillaItems::GLASS_BOTTLE(); } + + public function canStartUsingItem(Player $player) : bool{ + return true; + } } diff --git a/src/item/Releasable.php b/src/item/Releasable.php index 07e77cf28..acb9d65c8 100644 --- a/src/item/Releasable.php +++ b/src/item/Releasable.php @@ -23,9 +23,13 @@ declare(strict_types=1); namespace pocketmine\item; +use pocketmine\player\Player; + /** * Interface implemented by objects that can be used. */ interface Releasable{ + public function canStartUsingItem(Player $player) : bool; + } diff --git a/src/player/Player.php b/src/player/Player.php index 3b74b27c7..a568ba644 100644 --- a/src/player/Player.php +++ b/src/player/Player.php @@ -1374,7 +1374,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{ $this->inventory->setItemInHand($item); } - $this->setUsingItem($item instanceof Releasable); + $this->setUsingItem($item instanceof Releasable && $item->canStartUsingItem($this)); return true; }