Skip to content

Commit 198a249

Browse files
committed
fix: Call "SpellApplyDamageEvent"
1 parent 17fde47 commit 198a249

File tree

9 files changed

+83
-51
lines changed

9 files changed

+83
-51
lines changed

core/src/main/java/com/nisovin/magicspells/events/SpellApplyDamageEvent.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public SpellApplyDamageEvent(Spell spell, LivingEntity caster, LivingEntity targ
2222
this(spell, caster, target, damage, DamageType.GENERIC, cause, spellDamageType);
2323
}
2424

25+
public SpellApplyDamageEvent(Spell spell, LivingEntity caster, LivingEntity target, double damage, String spellDamageType) {
26+
this(spell, caster, target, damage, DamageType.GENERIC, spellDamageType);
27+
}
28+
2529
public SpellApplyDamageEvent(Spell spell, LivingEntity caster, LivingEntity target, double damage, DamageType damageType, String spellDamageType) {
2630
this(spell, caster, target, damage, damageType, DamageCause.ENTITY_ATTACK, spellDamageType);
2731
}

core/src/main/java/com/nisovin/magicspells/spells/instant/ThrowBlockSpell.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.nisovin.magicspells.util.config.FunctionData;
3131
import com.nisovin.magicspells.spelleffects.EffectPosition;
3232
import com.nisovin.magicspells.spells.TargetedLocationSpell;
33+
import com.nisovin.magicspells.events.SpellApplyDamageEvent;
3334

3435
public class ThrowBlockSpell extends InstantSpell implements TargetedLocationSpell {
3536

@@ -347,7 +348,6 @@ private void onDamage(EntityDamageByEntityEvent event) {
347348
}
348349

349350
subData = targetEvent.getSpellData();
350-
target = subData.target();
351351
}
352352

353353
double damage = event.getDamage();
@@ -358,7 +358,9 @@ private void onDamage(EntityDamageByEntityEvent event) {
358358
return;
359359
}
360360

361-
event.setDamage(damage);
361+
SpellApplyDamageEvent spellEvent = new SpellApplyDamageEvent(info.getSpell(), info.data.caster(), info.data.target(), damage, DamageCause.ENTITY_ATTACK, "");
362+
spellEvent.callEvent();
363+
event.setDamage(spellEvent.getFinalDamage());
362364

363365
if (spell.spellOnLand != null && !info.spellActivated) {
364366
spell.spellOnLand.subcast(subData.retarget(null, block.getLocation()));

core/src/main/java/com/nisovin/magicspells/spells/targeted/CombustSpell.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import com.nisovin.magicspells.util.*;
1414
import com.nisovin.magicspells.MagicSpells;
1515
import com.nisovin.magicspells.spells.TargetedSpell;
16-
import com.nisovin.magicspells.util.compat.EventUtil;
1716
import com.nisovin.magicspells.util.config.ConfigData;
1817
import com.nisovin.magicspells.spells.TargetedEntitySpell;
1918
import com.nisovin.magicspells.events.SpellApplyDamageEvent;
@@ -95,8 +94,9 @@ public void onEntityDamage(EntityDamageEvent event) {
9594
if (powerAffectsFireTickDamage.get(data.spellData)) fireTickDamage *= data.spellData.power();
9695
}
9796

98-
EventUtil.call(new SpellApplyDamageEvent(this, data.spellData.caster(), target, fireTickDamage, DamageCause.FIRE_TICK, ""));
99-
event.setDamage(fireTickDamage);
97+
SpellApplyDamageEvent spellEvent = new SpellApplyDamageEvent(this, data.spellData.caster(), target, fireTickDamage, DamageCause.FIRE_TICK, "");
98+
spellEvent.callEvent();
99+
event.setDamage(spellEvent.getFinalDamage());
100100

101101
if (data.preventImmunity) MagicSpells.scheduleDelayedTask(() -> target.setNoDamageTicks(0), 0);
102102
}

core/src/main/java/com/nisovin/magicspells/spells/targeted/DotSpell.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import com.nisovin.magicspells.util.*;
1515
import com.nisovin.magicspells.MagicSpells;
1616
import com.nisovin.magicspells.spells.TargetedSpell;
17-
import com.nisovin.magicspells.util.compat.EventUtil;
1817
import com.nisovin.magicspells.util.config.ConfigData;
1918
import com.nisovin.magicspells.spells.TargetedEntitySpell;
2019
import com.nisovin.magicspells.spelleffects.EffectPosition;
@@ -180,18 +179,18 @@ public void run() {
180179
data.target().setLastDamageCause(event);
181180
}
182181

182+
if (ignoreArmor && checkPlugins && data.hasCaster()) {
183+
EntityDamageEvent damageEvent = createFakeDamageEvent(data.caster(), data.target(), DamageCause.ENTITY_ATTACK, localDamage);
184+
if (!damageEvent.callEvent()) return;
185+
186+
if (!avoidDamageModification) localDamage = damageEvent.getDamage();
187+
}
188+
183189
SpellApplyDamageEvent event = new SpellApplyDamageEvent(DotSpell.this, data.caster(), data.target(), localDamage, damageType, spellDamageType);
184-
EventUtil.call(event);
190+
event.callEvent();
185191
localDamage = event.getFinalDamage();
186192

187193
if (ignoreArmor) {
188-
if (checkPlugins && data.hasCaster()) {
189-
EntityDamageEvent damageEvent = createFakeDamageEvent(data.caster(), data.target(), DamageCause.ENTITY_ATTACK, localDamage);
190-
if (!damageEvent.callEvent()) return;
191-
192-
if (!avoidDamageModification) localDamage = event.getDamage();
193-
}
194-
195194
double maxHealth = Util.getMaxHealth(data.target());
196195
double health = data.target().getHealth();
197196

core/src/main/java/com/nisovin/magicspells/spells/targeted/FireballSpell.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.nisovin.magicspells.spells.TargetedSpell;
2828
import com.nisovin.magicspells.util.config.ConfigData;
2929
import com.nisovin.magicspells.spelleffects.EffectPosition;
30+
import com.nisovin.magicspells.events.SpellApplyDamageEvent;
3031
import com.nisovin.magicspells.spells.TargetedEntityFromLocationSpell;
3132

3233
public class FireballSpell extends TargetedSpell implements TargetedEntityFromLocationSpell {
@@ -201,6 +202,10 @@ public void onExplosionPrime(ExplosionPrimeEvent event) {
201202
double noExplosionDamage = this.noExplosionDamage.get(data);
202203
if (powerAffectsNoExplosionDamage.get(data)) noExplosionDamage *= data.power();
203204

205+
SpellApplyDamageEvent spellEvent = new SpellApplyDamageEvent(this, data.caster(), data.target(), noExplosionDamage, "");
206+
spellEvent.callEvent();
207+
noExplosionDamage = spellEvent.getFinalDamage();
208+
204209
target.damage(noExplosionDamage, caster);
205210
}
206211

core/src/main/java/com/nisovin/magicspells/spells/targeted/ForcetossSpell.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77
import com.nisovin.magicspells.spells.TargetedSpell;
88
import com.nisovin.magicspells.util.config.ConfigData;
99
import com.nisovin.magicspells.spells.TargetedEntitySpell;
10+
import com.nisovin.magicspells.events.SpellApplyDamageEvent;
1011

1112
public class ForcetossSpell extends TargetedSpell implements TargetedEntitySpell {
1213

13-
private ConfigData<Double> damage;
14+
private final ConfigData<Double> damage;
1415

15-
private ConfigData<Double> vForce;
16-
private ConfigData<Double> hForce;
17-
private ConfigData<Double> rotation;
16+
private final ConfigData<Double> vForce;
17+
private final ConfigData<Double> hForce;
18+
private final ConfigData<Double> rotation;
1819

19-
private ConfigData<Boolean> powerAffectsForce;
20-
private ConfigData<Boolean> powerAffectsDamage;
21-
private ConfigData<Boolean> addVelocityInstead;
20+
private final ConfigData<Boolean> powerAffectsForce;
21+
private final ConfigData<Boolean> powerAffectsDamage;
22+
private final ConfigData<Boolean> addVelocityInstead;
2223

2324
public ForcetossSpell(MagicConfig config, String spellName) {
2425
super(config, spellName);
@@ -45,14 +46,18 @@ public CastResult cast(SpellData data) {
4546
@Override
4647
public CastResult castAtEntity(SpellData data) {
4748
if (!data.hasCaster()) return new CastResult(PostCastAction.ALREADY_HANDLED, data);
48-
if (!data.caster().getWorld().equals(data.target().getWorld())) return noTarget(data);
4949

5050
LivingEntity caster = data.caster();
5151
LivingEntity target = data.target();
52+
if (!caster.getWorld().equals(target.getWorld())) return noTarget(data);
5253

5354
double damage = this.damage.get(data);
5455
if (powerAffectsDamage.get(data)) damage *= data.power();
5556

57+
SpellApplyDamageEvent event = new SpellApplyDamageEvent(this, caster, target, damage, "");
58+
event.callEvent();
59+
damage = event.getFinalDamage();
60+
5661
if (damage > 0) target.damage(damage, caster);
5762

5863
Vector v;

core/src/main/java/com/nisovin/magicspells/spells/targeted/GeyserSpell.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
import com.nisovin.magicspells.spells.TargetedSpell;
1919
import com.nisovin.magicspells.util.config.ConfigData;
2020
import com.nisovin.magicspells.spells.TargetedEntitySpell;
21+
import com.nisovin.magicspells.events.SpellApplyDamageEvent;
2122

2223
public class GeyserSpell extends TargetedSpell implements TargetedEntitySpell {
2324

24-
private ConfigData<BlockData> geyserType;
25+
private final ConfigData<BlockData> geyserType;
2526

2627
private final ConfigData<Double> damage;
2728
private final ConfigData<Double> velocity;
@@ -63,24 +64,31 @@ public CastResult cast(SpellData data) {
6364

6465
@Override
6566
public CastResult castAtEntity(SpellData data) {
67+
LivingEntity caster = data.caster();
6668
LivingEntity target = data.target();
6769

6870
double damage = this.damage.get(data);
6971
if (powerAffectsDamage.get(data)) damage *= data.power();
7072

7173
if (damage > 0) {
72-
if (ignoreArmor.get(data)) {
73-
if (data.hasCaster() && checkPlugins.get(data)) {
74-
EntityDamageEvent event = createFakeDamageEvent(data.caster(), target, DamageCause.ENTITY_ATTACK, damage);
75-
if (!event.callEvent()) return noTarget(data);
74+
boolean ignoreArmor = this.ignoreArmor.get(data);
7675

77-
if (!avoidDamageModification.get(data)) damage = event.getDamage();
78-
}
76+
if (ignoreArmor && data.hasCaster() && checkPlugins.get(data)) {
77+
EntityDamageEvent event = createFakeDamageEvent(caster, target, DamageCause.ENTITY_ATTACK, damage);
78+
if (!event.callEvent()) return noTarget(data);
7979

80+
if (!avoidDamageModification.get(data)) damage = event.getDamage();
81+
}
82+
83+
SpellApplyDamageEvent spellEvent = new SpellApplyDamageEvent(this, caster, target, damage, "");
84+
spellEvent.callEvent();
85+
damage = spellEvent.getFinalDamage();
86+
87+
if (ignoreArmor) {
8088
target.setHealth(Math.clamp(target.getHealth() - damage, 0, Util.getMaxHealth(target)));
81-
Util.playHurtEffect(data.target(), data.caster());
89+
Util.playHurtEffect(target, caster);
8290
} else {
83-
if (data.hasCaster()) target.damage(damage, data.caster());
91+
if (data.hasCaster()) target.damage(damage, caster);
8492
else target.damage(damage);
8593
}
8694
}

core/src/main/java/com/nisovin/magicspells/spells/targeted/LightningSpell.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.nisovin.magicspells.spells.TargetedSpell;
2424
import com.nisovin.magicspells.util.config.ConfigData;
2525
import com.nisovin.magicspells.spells.TargetedLocationSpell;
26+
import com.nisovin.magicspells.events.SpellApplyDamageEvent;
2627

2728
public class LightningSpell extends TargetedSpell implements TargetedLocationSpell {
2829

@@ -82,8 +83,8 @@ public CastResult cast(SpellData data) {
8283
if (checkPlugins.get(data) && checkFakeDamageEvent(data.caster(), data.target()))
8384
return noTarget(data);
8485

85-
ChargeOption option = new ChargeOption(additionalDamage, chargeCreepers.get(data), zapPigs.get(data), transformEntities.get(data));
8686
LightningStrike strike = data.target().getWorld().strikeLightning(data.target().getLocation());
87+
ChargeOption option = new ChargeOption(this, data.caster(), additionalDamage, chargeCreepers.get(data), zapPigs.get(data), transformEntities.get(data));
8788
lightningListener.striking.put(strike.getUniqueId(), option);
8889

8990
return new CastResult(PostCastAction.HANDLE_NORMALLY, data);
@@ -104,17 +105,16 @@ public CastResult castAtLocation(SpellData data) {
104105
double additionalDamage = this.additionalDamage.get(data);
105106
if (powerAffectsAdditionalDamage.get(data)) additionalDamage *= data.power();
106107

107-
ChargeOption option = new ChargeOption(additionalDamage, chargeCreepers.get(data), zapPigs.get(data), transformEntities.get(data));
108108
LightningStrike strike = target.getWorld().strikeLightning(target);
109+
ChargeOption option = new ChargeOption(this, data.caster(), additionalDamage, chargeCreepers.get(data), zapPigs.get(data), transformEntities.get(data));
109110
lightningListener.striking.put(strike.getUniqueId(), option);
110111
}
111112

112113
playSpellEffects(data);
113114
return new CastResult(PostCastAction.HANDLE_NORMALLY, data);
114115
}
115116

116-
private record ChargeOption(double additionalDamage, boolean chargeCreeper, boolean changePig, boolean transformEntities) {
117-
117+
private record ChargeOption(LightningSpell spell, LivingEntity caster, double additionalDamage, boolean chargeCreeper, boolean changePig, boolean transformEntities) {
118118
}
119119

120120
private static class LightningListener implements Listener {
@@ -130,9 +130,18 @@ public void onLightningDamage(EntityDamageByEntityEvent event) {
130130
if (source.getDamageType() != DamageType.LIGHTNING_BOLT) return;
131131

132132
ChargeOption option = striking.get(strike.getUniqueId());
133-
if (option == null || option.additionalDamage <= 0) return;
133+
if (option == null) return;
134+
135+
double damage = event.getDamage();
136+
if (option.additionalDamage > 0) damage += option.additionalDamage;
137+
138+
if (event.getEntity() instanceof LivingEntity target) {
139+
SpellApplyDamageEvent spellEvent = new SpellApplyDamageEvent(option.spell(), option.caster(), target, damage, source.getDamageType(), "");
140+
spellEvent.callEvent();
141+
damage = spellEvent.getFinalDamage();
142+
}
134143

135-
event.setDamage(event.getDamage() + option.additionalDamage);
144+
event.setDamage(damage);
136145
}
137146

138147
@EventHandler

core/src/main/java/com/nisovin/magicspells/spells/targeted/PainSpell.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,20 @@ public CastResult castAtEntity(SpellData data) {
8585
data.target().setLastDamageCause(event);
8686
}
8787

88+
boolean ignoreArmor = this.ignoreArmor.get(data);
89+
90+
if (ignoreArmor && checkPlugins && data.hasCaster()) {
91+
EntityDamageEvent damageEvent = createFakeDamageEvent(data.caster(), data.target(), DamageCause.ENTITY_ATTACK, damage);
92+
if (!damageEvent.callEvent()) return noTarget(data);
93+
94+
if (!avoidDamageModification) damage = damageEvent.getDamage();
95+
}
96+
8897
SpellApplyDamageEvent event = new SpellApplyDamageEvent(this, data.caster(), data.target(), damage, damageType, spellDamageType);
8998
event.callEvent();
9099
damage = event.getFinalDamage();
91100

92-
if (ignoreArmor.get(data)) {
93-
if (checkPlugins && data.hasCaster()) {
94-
EntityDamageEvent damageEvent = createFakeDamageEvent(data.caster(), data.target(), DamageCause.ENTITY_ATTACK, damage);
95-
if (!damageEvent.callEvent()) return noTarget(data);
96-
97-
if (!avoidDamageModification) damage = event.getDamage();
98-
}
99-
101+
if (ignoreArmor) {
100102
double maxHealth = Util.getMaxHealth(data.target());
101103

102104
double health = Math.min(data.target().getHealth(), maxHealth);
@@ -106,13 +108,11 @@ public CastResult castAtEntity(SpellData data) {
106108
data.target().setHealth(health);
107109
data.target().setLastDamage(damage);
108110
Util.playHurtEffect(data.target(), data.caster());
109-
110-
playSpellEffects(data);
111-
return new CastResult(PostCastAction.HANDLE_NORMALLY, data);
112111
}
113-
114-
if (tryAvoidingAntiCheatPlugins.get(data)) data.target().damage(damage);
115-
else data.target().damage(damage, data.caster());
112+
else {
113+
if (tryAvoidingAntiCheatPlugins.get(data)) data.target().damage(damage);
114+
else data.target().damage(damage, data.caster());
115+
}
116116

117117
playSpellEffects(data);
118118
return new CastResult(PostCastAction.HANDLE_NORMALLY, data);

0 commit comments

Comments
 (0)