mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-22 08:44:01 +00:00
Improved EntityRegainHealthEvent
This commit is contained in:
parent
4346773e25
commit
7b09edf048
@ -31,6 +31,7 @@ use pocketmine\entity\Living;
|
||||
use pocketmine\event\block\SignChangeEvent;
|
||||
use pocketmine\event\entity\EntityDamageByEntityEvent;
|
||||
use pocketmine\event\entity\EntityDamageEvent;
|
||||
use pocketmine\event\entity\EntityRegainHealthEvent;
|
||||
use pocketmine\event\entity\EntityShootBowEvent;
|
||||
use pocketmine\event\inventory\InventoryCloseEvent;
|
||||
use pocketmine\event\inventory\InventoryPickupItemEvent;
|
||||
@ -1950,7 +1951,12 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
|
||||
$pk->eid = $this->getID();
|
||||
Server::broadcastPacket($this->getViewers(), $pk);
|
||||
|
||||
$this->heal($items[$slot->getID()]);
|
||||
$amount = $items[$slot->getID()];
|
||||
$this->server->getPluginManager()->callEvent($ev = new EntityRegainHealthEvent($this, $amount, EntityRegainHealthEvent::CAUSE_EATING));
|
||||
if(!$ev->isCancelled()){
|
||||
$this->heal($ev->getAmount(), $ev);
|
||||
}
|
||||
|
||||
--$slot->count;
|
||||
$this->inventory->setItemInHand($slot, $this);
|
||||
if($slot->getID() === Item::MUSHROOM_STEW or $slot->getID() === Item::BEETROOT_SOUP){
|
||||
|
@ -21,10 +21,12 @@
|
||||
|
||||
namespace pocketmine\block;
|
||||
|
||||
use pocketmine\event\entity\EntityRegainHealthEvent;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\level\Level;
|
||||
use pocketmine\math\AxisAlignedBB;
|
||||
use pocketmine\Player;
|
||||
use pocketmine\Server;
|
||||
|
||||
class Cake extends Transparent{
|
||||
public function __construct($meta = 0){
|
||||
@ -78,7 +80,10 @@ class Cake extends Transparent{
|
||||
public function onActivate(Item $item, Player $player = null){
|
||||
if($player instanceof Player and $player->getHealth() < 20){
|
||||
++$this->meta;
|
||||
$player->heal(3, "cake");
|
||||
Server::getInstance()->getPluginManager()->callEvent($ev = new EntityRegainHealthEvent($player, 3, EntityRegainHealthEvent::CAUSE_EATING));
|
||||
if(!$ev->isCancelled()){
|
||||
$player->heal($ev->getAmount(), $ev);
|
||||
}
|
||||
if($this->meta >= 0x06){
|
||||
$this->getLevel()->setBlock($this, new Air(), true);
|
||||
}else{
|
||||
|
@ -31,6 +31,7 @@ use pocketmine\event\entity\EntityDespawnEvent;
|
||||
use pocketmine\event\entity\EntityLevelChangeEvent;
|
||||
use pocketmine\event\entity\EntityMotionEvent;
|
||||
use pocketmine\event\entity\EntityMoveEvent;
|
||||
use pocketmine\event\entity\EntityRegainHealthEvent;
|
||||
use pocketmine\event\entity\EntitySpawnEvent;
|
||||
use pocketmine\event\entity\EntityTeleportEvent;
|
||||
use pocketmine\event\Timings;
|
||||
@ -307,11 +308,15 @@ abstract class Entity extends Position implements Metadatable{
|
||||
* @param float $damage
|
||||
* @param int|EntityDamageEvent $source
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract function attack($damage, $source = EntityDamageEvent::CAUSE_MAGIC);
|
||||
|
||||
abstract function heal($amount);
|
||||
/**
|
||||
* @param float $amount
|
||||
* @param int|EntityRegainHealthEvent $source
|
||||
*
|
||||
*/
|
||||
abstract function heal($amount, $source = EntityRegainHealthEvent::CAUSE_MAGIC);
|
||||
|
||||
/**
|
||||
* @return int
|
||||
|
@ -113,11 +113,7 @@ abstract class Living extends Entity implements Damageable{
|
||||
$this->setMotion($motion);
|
||||
}
|
||||
|
||||
public function heal($amount){
|
||||
$this->server->getPluginManager()->callEvent($ev = new EntityRegainHealthEvent($this, $amount));
|
||||
if($ev->isCancelled()){
|
||||
return;
|
||||
}
|
||||
public function heal($amount, $source = EntityRegainHealthEvent::CAUSE_MAGIC){
|
||||
$this->setHealth($this->getHealth() + $amount);
|
||||
}
|
||||
|
||||
|
@ -27,18 +27,24 @@ use pocketmine\event\Cancellable;
|
||||
class EntityRegainHealthEvent extends EntityEvent implements Cancellable{
|
||||
public static $handlerList = null;
|
||||
|
||||
//TODO: add cause
|
||||
const CAUSE_REGEN = 0;
|
||||
const CAUSE_EATING = 1;
|
||||
const CAUSE_MAGIC = 2;
|
||||
const CAUSE_CUSTOM = 3;
|
||||
|
||||
private $amount;
|
||||
private $reason;
|
||||
|
||||
|
||||
/**
|
||||
* @param Entity $entity
|
||||
* @param float $amount
|
||||
* @param int $regainReason
|
||||
*/
|
||||
public function __construct(Entity $entity, $amount){
|
||||
public function __construct(Entity $entity, $amount, $regainReason){
|
||||
$this->entity = $entity;
|
||||
$this->amount = $amount;
|
||||
$this->reason = (int) $regainReason;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,4 +61,8 @@ class EntityRegainHealthEvent extends EntityEvent implements Cancellable{
|
||||
$this->amount = $amount;
|
||||
}
|
||||
|
||||
public function getRegainReason(){
|
||||
return $this->reason;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user