diff --git a/src/item/Item.php b/src/item/Item.php index 2a4312539..de23f1e61 100644 --- a/src/item/Item.php +++ b/src/item/Item.php @@ -554,6 +554,13 @@ class Item implements \JsonSerializable{ return false; } + /** + * Called when a player uses the item to interact with entity, for example by using a name tag. + */ + public function onInteractEntity(Player $player, Entity $entity, Vector3 $directionVector) : bool{ + return false; + } + /** * Returns the number of ticks a player must wait before activating this item again. */ diff --git a/src/player/Player.php b/src/player/Player.php index 0eb4fe5a5..19840ae22 100644 --- a/src/player/Player.php +++ b/src/player/Player.php @@ -1818,7 +1818,17 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{ $ev->call(); + $item = $this->inventory->getItemInHand(); + $oldItem = clone $item; if(!$ev->isCancelled()){ + if($item->onInteractEntity($this, $entity, $clickPos)){ + if($this->hasFiniteResources() && !$item->equalsExact($oldItem) && $oldItem->equalsExact($this->inventory->getItemInHand())){ + if($item instanceof Durable && $item->isBroken()){ + $this->broadcastSound(new ItemBreakSound()); + } + $this->inventory->setItemInHand($item); + } + } return $entity->onInteract($this, $clickPos); } return false;