Files
PocketMine-MP/src/pocketmine/event/entity/EntityDamageEvent.php
2017-06-07 12:53:16 +01:00

159 lines
3.4 KiB
PHP

<?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\entity;
use pocketmine\entity\Effect;
use pocketmine\entity\Entity;
use pocketmine\event\Cancellable;
/**
* Called when an entity takes damage.
*/
class EntityDamageEvent extends EntityEvent implements Cancellable{
public static $handlerList = null;
const MODIFIER_BASE = 0;
const MODIFIER_ARMOR = 1;
const MODIFIER_STRENGTH = 2;
const MODIFIER_WEAKNESS = 3;
const MODIFIER_RESISTANCE = 4;
const CAUSE_CONTACT = 0;
const CAUSE_ENTITY_ATTACK = 1;
const CAUSE_PROJECTILE = 2;
const CAUSE_SUFFOCATION = 3;
const CAUSE_FALL = 4;
const CAUSE_FIRE = 5;
const CAUSE_FIRE_TICK = 6;
const CAUSE_LAVA = 7;
const CAUSE_DROWNING = 8;
const CAUSE_BLOCK_EXPLOSION = 9;
const CAUSE_ENTITY_EXPLOSION = 10;
const CAUSE_VOID = 11;
const CAUSE_SUICIDE = 12;
const CAUSE_MAGIC = 13;
const CAUSE_CUSTOM = 14;
const CAUSE_STARVATION = 15;
private $cause;
/** @var array */
private $modifiers;
private $originals;
/**
* @param Entity $entity
* @param int $cause
* @param int|int[] $damage
*
* @throws \Exception
*/
public function __construct(Entity $entity, $cause, $damage){
$this->entity = $entity;
$this->cause = $cause;
if(is_array($damage)){
$this->modifiers = $damage;
}else{
$this->modifiers = [
self::MODIFIER_BASE => $damage
];
}
$this->originals = $this->modifiers;
if(!isset($this->modifiers[self::MODIFIER_BASE])){
throw new \InvalidArgumentException("BASE Damage modifier missing");
}
if($entity->hasEffect(Effect::DAMAGE_RESISTANCE)){
$this->setDamage(-($this->getDamage(self::MODIFIER_BASE) * 0.20 * $entity->getEffect(Effect::DAMAGE_RESISTANCE)->getEffectLevel()), self::MODIFIER_RESISTANCE);
}
}
/**
* @return int
*/
public function getCause(){
return $this->cause;
}
/**
* @param int $type
*
* @return int
*/
public function getOriginalDamage($type = self::MODIFIER_BASE){
if(isset($this->originals[$type])){
return $this->originals[$type];
}
return 0;
}
/**
* @param int $type
*
* @return int
*/
public function getDamage($type = self::MODIFIER_BASE){
if(isset($this->modifiers[$type])){
return $this->modifiers[$type];
}
return 0;
}
/**
* @param float $damage
* @param int $type
*
* @throws \UnexpectedValueException
*/
public function setDamage($damage, $type = self::MODIFIER_BASE){
$this->modifiers[$type] = $damage;
}
/**
* @param int $type
*
* @return bool
*/
public function isApplicable($type){
return isset($this->modifiers[$type]);
}
/**
* @return int
*/
public function getFinalDamage(){
$damage = 0;
foreach($this->modifiers as $type => $d){
$damage += $d;
}
return $damage;
}
}