forked from nisovin/MagicSpells
-
Notifications
You must be signed in to change notification settings - Fork 66
/
PainSpell.java
99 lines (72 loc) · 3.71 KB
/
PainSpell.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.nisovin.magicspells.spells.targeted;
import org.bukkit.entity.Player;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import com.nisovin.magicspells.util.*;
import com.nisovin.magicspells.spells.TargetedSpell;
import com.nisovin.magicspells.util.config.ConfigData;
import com.nisovin.magicspells.util.compat.CompatBasics;
import com.nisovin.magicspells.spells.TargetedEntitySpell;
import com.nisovin.magicspells.events.SpellApplyDamageEvent;
import com.nisovin.magicspells.events.MagicSpellsEntityDamageByEntityEvent;
public class PainSpell extends TargetedSpell implements TargetedEntitySpell {
private final ConfigData<String> spellDamageType;
private final ConfigData<DamageCause> damageType;
private final ConfigData<Double> damage;
private final ConfigData<Boolean> ignoreArmor;
private final ConfigData<Boolean> checkPlugins;
private final ConfigData<Boolean> powerAffectsDamage;
private final ConfigData<Boolean> avoidDamageModification;
private final ConfigData<Boolean> tryAvoidingAntiCheatPlugins;
public PainSpell(MagicConfig config, String spellName) {
super(config, spellName);
spellDamageType = getConfigDataString("spell-damage-type", "");
damageType = getConfigDataEnum("damage-type", DamageCause.class, DamageCause.ENTITY_ATTACK);
damage = getConfigDataDouble("damage", 4);
ignoreArmor = getConfigDataBoolean("ignore-armor", false);
checkPlugins = getConfigDataBoolean("check-plugins", true);
powerAffectsDamage = getConfigDataBoolean("power-affects-damage", true);
avoidDamageModification = getConfigDataBoolean("avoid-damage-modification", true);
tryAvoidingAntiCheatPlugins = getConfigDataBoolean("try-avoiding-anticheat-plugins", false);
}
@Override
public CastResult cast(SpellData data) {
TargetInfo<LivingEntity> info = getTargetedEntity(data);
if (info.noTarget()) return noTarget(info);
if (data.caster() instanceof Player caster)
return CompatBasics.exemptAction(() -> castAtEntity(info.spellData()), caster, CompatBasics.activeExemptionAssistant.getPainExemptions());
return castAtEntity(info.spellData());
}
@Override
public CastResult castAtEntity(SpellData data) {
if (!data.target().isValid()) return noTarget(data);
double damage = this.damage.get(data);
if (powerAffectsDamage.get(data)) damage *= data.power();
DamageCause damageType = this.damageType.get(data);
String spellDamageType = this.spellDamageType.get(data);
if (checkPlugins.get(data)) {
MagicSpellsEntityDamageByEntityEvent event = new MagicSpellsEntityDamageByEntityEvent(data.caster(), data.target(), damageType, damage, this);
if (!event.callEvent()) return noTarget(data);
if (!avoidDamageModification.get(data)) damage = event.getDamage();
data.target().setLastDamageCause(event);
}
SpellApplyDamageEvent event = new SpellApplyDamageEvent(this, data.caster(), data.target(), damage, damageType, spellDamageType);
event.callEvent();
damage = event.getFinalDamage();
if (ignoreArmor.get(data)) {
double maxHealth = Util.getMaxHealth(data.target());
double health = Math.min(data.target().getHealth(), maxHealth);
health = Math.max(Math.min(health - damage, maxHealth), 0);
if (health == 0 && data.caster() instanceof Player player) data.target().setKiller(player);
data.target().setHealth(health);
data.target().setLastDamage(damage);
Util.playHurtEffect(data.target(), data.caster());
playSpellEffects(data);
return new CastResult(PostCastAction.HANDLE_NORMALLY, data);
}
if (tryAvoidingAntiCheatPlugins.get(data)) data.target().damage(damage);
else data.target().damage(damage, data.caster());
playSpellEffects(data);
return new CastResult(PostCastAction.HANDLE_NORMALLY, data);
}
}