Added PlayerEntityInteractEvent (#4374)

This commit is contained in:
marshall 2021-08-24 18:56:10 +08:00 committed by GitHub
parent 224d71f272
commit 6e68e99ec0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 1 deletions

View File

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

View File

@ -0,0 +1,56 @@
<?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\event\player;
use pocketmine\entity\Entity;
use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait;
use pocketmine\math\Vector3;
use pocketmine\player\Player;
/**
* Called when a player interacts with an entity (e.g. shearing a sheep, naming a mob using a nametag).
*/
class PlayerEntityInteractEvent extends PlayerEvent implements Cancellable{
use CancellableTrait;
public function __construct(
Player $player,
private Entity $entity,
private Vector3 $clickPos
){
$this->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;
}
}

View File

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