-
Notifications
You must be signed in to change notification settings - Fork 2
/
DamageEffect.java
189 lines (153 loc) · 5.63 KB
/
DamageEffect.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* QuestManager: An RPG plugin for the Bukkit API.
* Copyright (C) 2015-2016 Github Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.skyisland.questmanager.magic.spell.effect;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import com.skyisland.questmanager.QuestManagerPlugin;
import com.skyisland.questmanager.configuration.utils.YamlWriter;
import com.skyisland.questmanager.magic.MagicUser;
import com.skyisland.questmanager.npc.NPC;
import com.skyisland.questmanager.player.PlayerOptions;
import com.skyisland.questmanager.player.QuestPlayer;
import com.skyisland.questmanager.player.skill.event.MagicApplyEvent;
public class DamageEffect extends ImbuementEffect {
public static final String DAMAGE_META_KEY = "QM_magic_damage";
/**
* Registers this class as configuration serializable with all defined
* {@link aliases aliases}
*/
public static void registerWithAliases() {
for (aliases alias : aliases.values()) {
ConfigurationSerialization.registerClass(DamageEffect.class, alias.getAlias());
}
}
/**
* Registers this class as configuration serializable with only the default alias
*/
public static void registerWithoutAliases() {
ConfigurationSerialization.registerClass(DamageEffect.class);
}
private enum aliases {
DEFAULT(DamageEffect.class.getName()),
OLD("com.SkyIsland.QuestManager.Magic.Spell.Effect." + DamageEffect.class.getSimpleName()),
LONGI("SpellDamage"),
LONG("DamageSpell"),
SHORT("SDamage");
private String alias;
aliases(String alias) {
this.alias = alias;
}
public String getAlias() {
return alias;
}
}
public static DamageEffect valueOf(Map<String, Object> map) {
return new DamageEffect((double) map.get("damage"));
}
@Override
public Map<String, Object> serialize() {
Map<String, Object> map = new HashMap<>();
map.put("damage", damage);
return map;
}
private double damage;
public DamageEffect(double damage) {
this.damage = damage;
}
@Override
public void apply(Entity e, MagicUser cause) {
if (e instanceof LivingEntity) {
double curDamage = damage;
if (cause instanceof QuestPlayer) {
QuestPlayer qp = (QuestPlayer) cause;
MagicApplyEvent aEvent = new MagicApplyEvent(qp, curDamage);
Bukkit.getPluginManager().callEvent(aEvent);
curDamage = aEvent.getFinalDamage();
}
LivingEntity targ = (LivingEntity) e;
targ.setMetadata(DAMAGE_META_KEY, new FixedMetadataValue
(QuestManagerPlugin.questManagerPlugin, true));
boolean invi = false;
double snapshot = targ.getHealth();
if (!(targ instanceof Player && (((Player) targ).getGameMode() == GameMode.CREATIVE || ((Player) targ).getGameMode() == GameMode.SPECTATOR))) {
List<MetadataValue> meta = targ.getMetadata(NPC.NPC_META_KEY);
if (meta == null || meta.isEmpty() || !meta.get(0).asBoolean()) {
targ.setInvulnerable(false);
invi = true;
}
}
targ.damage(curDamage, cause.getEntity());
// targ.damage(0.0, cause.getEntity());
// targ.setHealth(Math.max(0.0, Math.min(targ.getMaxHealth(), targ.getHealth() - curDamage)));
//make sure they didn't invincible their way out damage
if (invi && Math.abs(snapshot - targ.getHealth()) < .0001) {
//targ.setHealth(targ.getHealth() - curDamage);
targ.setHealth(Math.max(0.0, Math.min(targ.getMaxHealth(), targ.getHealth() - curDamage)));
}
targ.setMetadata(DAMAGE_META_KEY, new FixedMetadataValue
(QuestManagerPlugin.questManagerPlugin, false));
if (cause instanceof QuestPlayer) {
QuestPlayer qp = (QuestPlayer) cause;
if (qp.getOptions().getOption(PlayerOptions.Key.CHAT_COMBAT_DAMAGE)
&& qp.getPlayer().isOnline()) {
Player p = qp.getPlayer().getPlayer();
String msg;
String name = e.getCustomName();
if (e instanceof Player)
name = ((Player) e).getName();
if (name == null) {
name = YamlWriter.toStandardFormat(cause.getEntity().getType().toString());
}
msg = ChatColor.DARK_GRAY + "You damaged " + ChatColor.GRAY + name + ChatColor.DARK_GRAY
+ " for " + ChatColor.DARK_RED + "%.2f" + ChatColor.DARK_GRAY + " damage"
+ ChatColor.RESET;
p.sendMessage(String.format(msg, curDamage));
}
}
}
}
@Override
public void apply(Location loc, MagicUser cause) {
//can't damage a location
//do nothing
;
}
@Override
public ImbuementEffect getCopyAtPotency(double potency) {
DamageEffect effect = new DamageEffect(damage * potency);
effect.setDisplayName(getDisplayName());
return effect;
}
public double getDamage() {
return damage;
}
public void setDamage(double damage) {
this.damage = damage;
}
}