PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
EntityDamageEvent.php
1 <?php
2 
22 namespace pocketmine\event\entity;
23 
26 
27 class EntityDamageEvent extends EntityEvent implements Cancellable{
28  public static $handlerList = null;
29  public static $eventPool = [];
30  public static $nextEvent = 0;
31 
32  const MODIFIER_BASE = 0;
33  const MODIFIER_ARMOR = 1;
34 
35  const CAUSE_CONTACT = 0;
36  const CAUSE_ENTITY_ATTACK = 1;
37  const CAUSE_PROJECTILE = 2;
38  const CAUSE_SUFFOCATION = 3;
39  const CAUSE_FALL = 4;
40  const CAUSE_FIRE = 5;
41  const CAUSE_FIRE_TICK = 6;
42  const CAUSE_LAVA = 7;
43  const CAUSE_DROWNING = 8;
44  const CAUSE_BLOCK_EXPLOSION = 9;
45  const CAUSE_ENTITY_EXPLOSION = 10;
46  const CAUSE_VOID = 11;
47  const CAUSE_SUICIDE = 12;
48  const CAUSE_MAGIC = 13;
49  const CAUSE_CUSTOM = 14;
50 
51 
52  private $cause;
54  private $modifiers;
55  private $originals;
56 
57 
65  public function __construct(Entity $entity, $cause, $damage){
66  $this->entity = $entity;
67  $this->cause = $cause;
68  if(is_array($damage)){
69  $this->modifiers = $damage;
70  }else{
71  $this->modifiers = [
72  self::MODIFIER_BASE => $damage
73  ];
74  }
75 
76  $this->originals = $this->modifiers;
77 
78  if(!isset($this->modifiers[self::MODIFIER_BASE])){
79  throw new \InvalidArgumentException("BASE Damage modifier missing");
80  }
81  }
82 
86  public function getCause(){
87  return $this->cause;
88  }
89 
95  public function getOriginalDamage($type = self::MODIFIER_BASE){
96  if(isset($this->originals[$type])){
97  return $this->originals[$type];
98  }
99 
100  return 0;
101  }
102 
108  public function getDamage($type = self::MODIFIER_BASE){
109  if(isset($this->modifiers[$type])){
110  return $this->modifiers[$type];
111  }
112 
113  return 0;
114  }
115 
122  public function setDamage($damage, $type = self::MODIFIER_BASE){
123  if(!isset($this->modifiers[$type])){
124  throw new \UnexpectedValueException($type . " is not applicable to " . $this->getEntity());
125  }
126  $this->modifiers[$type] = $damage;
127  }
128 
134  public function isApplicable($type){
135  return isset($this->modifiers[$type]);
136  }
137 
141  public function getFinalDamage(){
142  $damage = 0;
143  foreach($this->modifiers as $type => $d){
144  $damage += $d;
145  }
146 
147  return $damage;
148  }
149 
150 }
setDamage($damage, $type=self::MODIFIER_BASE)
getOriginalDamage($type=self::MODIFIER_BASE)
__construct(Entity $entity, $cause, $damage)