PocketMine-MP 5.14.2 git-50e2c469a547a16a23b2dc691e70a51d34e29395
EntityDamageEvent.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
25
29use function array_sum;
30use function max;
31
36class EntityDamageEvent extends EntityEvent implements Cancellable{
38
39 public const MODIFIER_ARMOR = 1;
40 public const MODIFIER_STRENGTH = 2;
41 public const MODIFIER_WEAKNESS = 3;
42 public const MODIFIER_RESISTANCE = 4;
43 public const MODIFIER_ABSORPTION = 5;
44 public const MODIFIER_ARMOR_ENCHANTMENTS = 6;
45 public const MODIFIER_CRITICAL = 7;
46 public const MODIFIER_TOTEM = 8;
47 public const MODIFIER_WEAPON_ENCHANTMENTS = 9;
48 public const MODIFIER_PREVIOUS_DAMAGE_COOLDOWN = 10;
49 public const MODIFIER_ARMOR_HELMET = 11;
50
51 public const CAUSE_CONTACT = 0;
52 public const CAUSE_ENTITY_ATTACK = 1;
53 public const CAUSE_PROJECTILE = 2;
54 public const CAUSE_SUFFOCATION = 3;
55 public const CAUSE_FALL = 4;
56 public const CAUSE_FIRE = 5;
57 public const CAUSE_FIRE_TICK = 6;
58 public const CAUSE_LAVA = 7;
59 public const CAUSE_DROWNING = 8;
60 public const CAUSE_BLOCK_EXPLOSION = 9;
61 public const CAUSE_ENTITY_EXPLOSION = 10;
62 public const CAUSE_VOID = 11;
63 public const CAUSE_SUICIDE = 12;
64 public const CAUSE_MAGIC = 13;
65 public const CAUSE_CUSTOM = 14;
66 public const CAUSE_STARVATION = 15;
67 public const CAUSE_FALLING_BLOCK = 16;
68
69 private float $baseDamage;
70 private float $originalBase;
71
73 private array $originals;
74 private int $attackCooldown = 10;
75
79 public function __construct(
81 private int $cause,
82 float $damage,
83 private array $modifiers = []
84 ){
85 $this->entity = $entity;
86 $this->baseDamage = $this->originalBase = $damage;
87 $this->originals = $modifiers;
88 }
89
90 public function getCause() : int{
91 return $this->cause;
92 }
93
97 public function getBaseDamage() : float{
98 return $this->baseDamage;
99 }
100
106 public function setBaseDamage(float $damage) : void{
107 $this->baseDamage = $damage;
108 }
109
113 public function getOriginalBaseDamage() : float{
114 return $this->originalBase;
115 }
116
120 public function getOriginalModifiers() : array{
121 return $this->originals;
122 }
123
124 public function getOriginalModifier(int $type) : float{
125 return $this->originals[$type] ?? 0.0;
126 }
127
131 public function getModifiers() : array{
132 return $this->modifiers;
133 }
134
135 public function getModifier(int $type) : float{
136 return $this->modifiers[$type] ?? 0.0;
137 }
138
139 public function setModifier(float $damage, int $type) : void{
140 $this->modifiers[$type] = $damage;
141 }
142
143 public function isApplicable(int $type) : bool{
144 return isset($this->modifiers[$type]);
145 }
146
147 public function getFinalDamage() : float{
148 return max(0, $this->baseDamage + array_sum($this->modifiers));
149 }
150
154 public function canBeReducedByArmor() : bool{
155 switch($this->cause){
156 case self::CAUSE_FIRE_TICK:
157 case self::CAUSE_SUFFOCATION:
158 case self::CAUSE_DROWNING:
159 case self::CAUSE_STARVATION:
160 case self::CAUSE_FALL:
161 case self::CAUSE_VOID:
162 case self::CAUSE_MAGIC:
163 case self::CAUSE_SUICIDE:
164 return false;
165
166 }
167
168 return true;
169 }
170
174 public function getAttackCooldown() : int{
175 return $this->attackCooldown;
176 }
177
183 public function setAttackCooldown(int $attackCooldown) : void{
184 $this->attackCooldown = $attackCooldown;
185 }
186}
__construct(Entity $entity, private int $cause, float $damage, private array $modifiers=[])