diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 040fa06c3..51b9d4bfb 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1058,6 +1058,13 @@ abstract class Entity{ } + /** + * Called when interacted or tapped by a Player. Returns whether something happened as a result of the interaction. + */ + public function onInteract(Player $player, Vector3 $clickPos) : bool{ + return false; + } + public function isUnderwater() : bool{ $block = $this->getWorld()->getBlockAt((int) floor($this->location->x), $blockY = (int) floor($y = ($this->location->y + $this->getEyeHeight())), (int) floor($this->location->z)); diff --git a/src/event/player/PlayerEntityInteractEvent.php b/src/event/player/PlayerEntityInteractEvent.php new file mode 100644 index 000000000..262d5dbc1 --- /dev/null +++ b/src/event/player/PlayerEntityInteractEvent.php @@ -0,0 +1,56 @@ +player = $player; + } + + public function getEntity() : Entity{ + return $this->entity; + } + + /** + * Returns the absolute coordinates of the click. This is usually on the surface of the entity's hitbox. + */ + public function getClickPosition() : Vector3{ + return $this->clickPos; + } +} diff --git a/src/player/Player.php b/src/player/Player.php index a76d1b52a..608ee63c9 100644 --- a/src/player/Player.php +++ b/src/player/Player.php @@ -53,6 +53,7 @@ use pocketmine\event\player\PlayerChatEvent; use pocketmine\event\player\PlayerCommandPreprocessEvent; use pocketmine\event\player\PlayerDeathEvent; use pocketmine\event\player\PlayerDisplayNameChangeEvent; +use pocketmine\event\player\PlayerEntityInteractEvent; use pocketmine\event\player\PlayerExhaustEvent; use pocketmine\event\player\PlayerGameModeChangeEvent; use pocketmine\event\player\PlayerInteractEvent; @@ -1684,7 +1685,17 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{ * Interacts with the given entity using the currently-held item. */ public function interactEntity(Entity $entity, Vector3 $clickPos) : bool{ - //TODO + $ev = new PlayerEntityInteractEvent($this, $entity, $clickPos); + + if(!$this->canInteract($entity->getLocation(), 8)){ + $ev->cancel(); + } + + $ev->call(); + + if(!$ev->isCancelled()){ + return $entity->onInteract($this, $clickPos); + } return false; }