Implemented hook method Item::onInteractEntity() (#5432)

this is called when the player right-clicks on an entity to do some action, such as shearing, naming etc.
This commit is contained in:
! Bryan 2022-12-15 13:30:52 -06:00 committed by GitHub
parent 4357c110c8
commit 1308cda5c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

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

View File

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