-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
HealthTrait.java
310 lines (251 loc) · 9.56 KB
/
HealthTrait.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package net.aufdemrand.denizen.npc.traits;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.arguments.Duration;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.NPCDeathEvent;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.minecraft.server.v1_5_R2.EntityHuman;
import org.bukkit.Bukkit;
import org.bukkit.EntityEffect;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_5_R2.entity.CraftLivingEntity;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByBlockEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
public class HealthTrait extends Trait implements Listener {
// Saved to the C2 saves.yml
@Persist("max")
private int maxhealth = 20;
@Persist("current")
private int currenthealth = 20;
@Persist("animatedeath")
private boolean animatedeath = Settings.HealthTraitAnimatedDeathEnabled();
@Persist("animatedeathdelayinseconds")
private String animationDelay = "3s";
@Persist("respawnondeath")
private boolean respawn = Settings.HealthTraitRespawnEnabled();
@Persist("respawndelayinseconds")
private String respawnDelay = Settings.HealthTraitRespawnDelay();;
@Persist("respawnlocation")
private String respawnLocation = "<npc.location>";
// internal
private Player player = null;
private boolean dying = false;
private Location loc;
public double getAnimationDelay() {
return Duration.valueOf(animationDelay).getSeconds();
}
public double getRespawnDelay() {
return (Duration.valueOf(respawnDelay).getSeconds());
}
public void setRespawnLocation(String string) {
if (aH.matchesLocation("location:" + string))
respawnLocation = string;
}
public void setRespawnDelay(int seconds) {
respawnDelay = String.valueOf(seconds);
}
public void setRespawnDelay(String string) {
if (aH.matchesDuration("duration:" + string))
respawnDelay = string;
}
public String getRespawnLocationAsString() {
return respawnLocation;
}
public Location getRespawnLocation() {
return aH.getLocationFrom(respawnLocation);
}
public void setDeathAnimationDelay(int seconds) {
animationDelay = String.valueOf(seconds);
}
public void setDeathAnimationDelay(String string) {
if (aH.matchesDuration("duration:" + string))
animationDelay = string;
}
public void setRespawnable(boolean respawnable) {
respawn = respawnable;
}
public boolean isRespawnable() {
return respawn;
}
public void animateOnDeath(boolean animate) {
animatedeath = animate;
}
public boolean animatesOnDeath() {
return animatedeath;
}
/**
* Listens for spawn of an NPC and updates its health with saved
* information from this Trait. If a respawn from death, sets health to maxHealth.
* If any other type of respawn, sets health to the last known currentHealth.
*
*/
@Override public void onSpawn() {
dying = false;
if (currenthealth > 0) setHealth(currenthealth);
else setHealth();
}
/**
* Listens for a despawn to note currentHealth as the time. Will be used
* to reset health on a respawn.
*
*/
@Override public void onDespawn() {
if (getHealth() > 0) currenthealth = getHealth();
else currenthealth = -1;
}
public HealthTrait() {
super("health");
}
/**
* Gets the current health of this NPC.
*
* @return current health points
*
*/
public int getHealth() {
if (!npc.isSpawned()) return currenthealth;
else return ((CraftLivingEntity) npc.getBukkitEntity()).getHandle().getHealth();
}
/**
* Sets the maximum health for this NPC. Default max is 20.
*
* @param newMax new maximum health
*
*/
public void setMaxhealth(int newMax) {
this.maxhealth = newMax;
}
/**
* Gets the maximum health for this NPC.
*
* @return maximum health
*/
public int getMaxhealth() {
return maxhealth;
}
/**
* Heals the NPC.
*
* @param health number of health points to heal
*/
public void heal(int health) {
setHealth(getHealth() + health);
}
/**
* Sets the NPCs health to maximum.
*
*/
public void setHealth() {
setHealth(maxhealth);
}
/**
* Sets the NPCs health to a specific amount.
*
* @param health total health points
*/
public void setHealth(int health) {
if (npc.getBukkitEntity() != null)
((CraftLivingEntity) npc.getBukkitEntity()).getHandle().setHealth(health);
currenthealth = health;
}
public void die()
{
// Set the player as the killer of the NPC, for listeners
if (player != null)
((CraftLivingEntity) npc.getBukkitEntity())
.getHandle().killer = (EntityHuman) ((CraftLivingEntity) player).getHandle();
setHealth();
EntityDeathEvent entityDeath = new EntityDeathEvent(npc.getBukkitEntity(), null);
NPCDeathEvent npcDeath = new NPCDeathEvent(npc, entityDeath);
DenizenAPI.getCurrentInstance().getServer()
.getPluginManager().callEvent(npcDeath);
DenizenAPI.getCurrentInstance().getServer()
.getPluginManager().callEvent(entityDeath);
npc.despawn(DespawnReason.DEATH);
}
@EventHandler(priority = EventPriority.MONITOR)
public void onDeath(EntityDamageEvent event) {
// Don't use NPCDamageEvent because it doesn't work well
// Check if the event pertains to this NPC
if (event.getEntity() != npc.getBukkitEntity() || dying) return;
// Make sure this is a killing blow
if (this.getHealth() - event.getDamage() > 0)
return;
dying = true;
player = null;
String deathCause = event.getCause().toString().toLowerCase().replace('_', ' ');
// Check if the entity has been killed by another entity
if (event instanceof EntityDamageByEntityEvent)
{
Entity killerEntity = ((EntityDamageByEntityEvent) event).getDamager();
// Check if the damager was a player and, if so, attach
// that player to the action's ScriptEntry
if (killerEntity instanceof Player)
player = (Player) killerEntity;
// If the damager was a projectile, take its shooter into
// account as well
else if (killerEntity instanceof Projectile)
{
LivingEntity shooter = ((Projectile) killerEntity).getShooter();
if (shooter instanceof Player)
player = (Player) shooter;
DenizenAPI.getDenizenNPC(npc).action("death by " +
shooter.getType().toString(), player);
}
DenizenAPI.getDenizenNPC(npc).action("death by entity", player);
DenizenAPI.getDenizenNPC(npc).action("death by " +
killerEntity.getType().toString(), player);
}
// If not, check if the entity has been killed by a block
else if (event instanceof EntityDamageByBlockEvent)
{
DenizenAPI.getDenizenNPC(npc).action("death by block", player);
// The line of code below should work, but a Bukkit bug makes the damager
// return null. Uncomment it once the bug is fixed.
//DenizenAPI.getDenizenNPC(npc).action("death by " +
// ((EntityDamageByBlockEvent) event).getDamager().getType().name(), null);
}
DenizenAPI.getDenizenNPC(npc).action("death", player);
DenizenAPI.getDenizenNPC(npc).action("death by " + deathCause, player);
// One of the actions above may have removed the NPC, so check if the
// NPC's entity still exists before proceeding
if (npc.getBukkitEntity() == null)
return;
loc = aH.getLocationFrom(DenizenAPI.getCurrentInstance().tagManager()
.tag(null, DenizenAPI.getDenizenNPC(npc), respawnLocation, false));
if (loc == null) loc = npc.getBukkitEntity().getLocation();
if (animatedeath) {
setHealth();
npc.getBukkitEntity().playEffect(EntityEffect.DEATH);
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(),
new Runnable() {
public void run() {
die();
}
} , (long) ((Duration.valueOf(animationDelay).getSeconds() * 20)) );
} else {
die();
}
if (respawn) {
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(),
new Runnable() {
public void run() {
if (npc.isSpawned()) return;
npc.spawn(loc);
}
} , (long) ((Duration.valueOf(respawnDelay).getSeconds() * 20)) );
}
}
}