-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
EntityDeathScriptEvent.java
293 lines (261 loc) · 10.3 KB
/
EntityDeathScriptEvent.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
package com.denizenscript.denizen.events.entity;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.InventoryTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Material;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
public class EntityDeathScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// entity death
// entity dies
// <entity> dies
// <entity> death
//
// @Cancellable true
//
// @Regex ^on [^\s]+ (death|dies)$
//
// @Switch in <area>
// @Switch by <entity type>
//
// @Triggers when an entity dies. Note that this fires *after* the entity dies, and thus some data may be lost from the entity.
// The death cannot be cancelled, only the death message (for players).
//
// @Context
// <context.entity> returns the EntityTag that died.
// <context.damager> returns the EntityTag damaging the other entity, if any.
// <context.message> returns an ElementTag of a player's death message.
// <context.inventory> returns the InventoryTag of the entity if it was a player.
// <context.cause> returns an ElementTag of the cause of the death. See <@link language damage cause> for a list of possible damage causes.
// <context.drops> returns a ListTag of all pending item drops.
// <context.xp> returns an ElementTag of the amount of experience to be dropped.
//
// @Determine
// ElementTag to change the death message.
// "NO_DROPS" to specify that any drops should be removed.
// "NO_DROPS_OR_XP" to specify that any drops or XP orbs should be removed.
// "NO_XP" to specify that any XP orbs should be removed.
// ListTag(ItemTag) to specify new items to be dropped.
// Element(Number) to specify the new amount of XP to be dropped.
// "KEEP_INV" to specify (if a player death) that the inventory should be kept.
// "KEEP_LEVEL" to specify (if a player death) that the XP level should be kept.
// Note that the event can be cancelled to hide a player death message.
//
// @Player when the entity that died is a player.
//
// @NPC when the entity that died is an NPC.
//
// -->
public EntityDeathScriptEvent() {
instance = this;
}
public static EntityDeathScriptEvent instance;
public EntityTag entity;
public EntityTag damager;
public ElementTag message;
public InventoryTag inventory;
public ElementTag cause;
public ListTag drops;
public List<ItemTag> dropItems;
public Integer xp;
public boolean keep_inv;
public boolean keep_level;
public EntityDeathEvent event;
@Override
public boolean couldMatch(ScriptPath path) {
String cmd = path.eventArgLowerAt(1);
return cmd.equals("dies") || cmd.equals("death");
}
@Override
public boolean matches(ScriptPath path) {
String target = path.eventArgLowerAt(0);
if (!tryEntity(entity, target)) {
return false;
}
if (!runInCheck(path, entity.getLocation())) {
return false;
}
if (path.switches.containsKey("by") && (damager == null || !tryEntity(damager, path.switches.get("by")))) {
return false;
}
return true;
}
@Override
public String getName() {
return "EntityDies";
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
String determination = determinationObj.toString();
// finish this
String lower = CoreUtilities.toLowerCase(determination);
// Deprecated
if (lower.startsWith("drops ")) {
lower = lower.substring(6);
determination = determination.substring(6);
}
//Handle no_drops and no_drops_or_xp and just no_xp
if (lower.startsWith("no_drops")) {
drops.clear();
dropItems = new ArrayList<>();
if (lower.endsWith("_or_xp")) {
xp = 0;
}
}
else if (lower.equals("no_xp")) {
xp = 0;
}
else if (lower.equals("keep_inv")) {
keep_inv = true;
}
else if (lower.equals("keep_level")) {
keep_level = true;
}
// Change xp value only
else if (ArgumentHelper.matchesInteger(determination)) {
xp = Argument.valueOf(lower).asElement().asInt();
}
// Change dropped items if ListTag detected
else if (Argument.valueOf(lower).matchesArgumentList(ItemTag.class)) {
drops.clear();
dropItems = new ArrayList<>();
ListTag drops_list = ListTag.valueOf(determination);
drops_list.filter(ItemTag.class, path.container);
for (String drop : drops_list) {
ItemTag item = ItemTag.valueOf(drop, path.container);
if (item != null) {
dropItems.add(item);
drops.add(item.identify());
}
}
}
// String containing new Death Message
else if (event instanceof PlayerDeathEvent && !isDefaultDetermination(determinationObj)) {
message = new ElementTag(determination);
}
else {
return super.applyDetermination(path, determinationObj);
}
return true;
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(entity.isPlayer() ? EntityTag.getPlayerFrom(event.getEntity()) : null,
entity.isCitizensNPC() ? EntityTag.getNPCFrom(event.getEntity()) : null);
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("entity")) {
return entity.getDenizenObject();
}
else if (name.equals("damager") && damager != null) {
return damager.getDenizenObject();
}
else if (name.equals("message") && message != null) {
return message;
}
else if (name.equals("inventory") && inventory != null) {
return inventory;
}
else if (name.equals("cause") && cause != null) {
return cause;
}
else if (name.equals("drops") && drops != null) {
return drops;
}
else if (name.equals("xp") && xp != null) {
return new ElementTag(xp);
}
return super.getContext(name);
}
@EventHandler(priority = EventPriority.LOW)
public void onEntityDeath(EntityDeathEvent event) {
LivingEntity livingEntity = event.getEntity();
EntityTag.rememberEntity(livingEntity);
entity = new EntityTag(livingEntity);
PlayerTag player = null;
if (entity.isPlayer()) {
player = entity.getDenizenPlayer();
}
cause = null;
damager = null;
EntityDamageEvent lastDamage = entity.getBukkitEntity().getLastDamageCause();
if (lastDamage != null) {
cause = new ElementTag(event.getEntity().getLastDamageCause().getCause().toString());
if (lastDamage instanceof EntityDamageByEntityEvent) {
EntityTag damageEntity = new EntityTag(((EntityDamageByEntityEvent) lastDamage).getDamager());
EntityTag shooter = damageEntity.getShooter();
if (shooter != null) {
damager = shooter;
}
else {
damager = damageEntity;
}
}
else if (livingEntity.getKiller() != null) {
damager = new EntityTag(livingEntity.getKiller());
}
}
message = null;
inventory = null;
PlayerDeathEvent subEvent = null;
if (event instanceof PlayerDeathEvent) {
subEvent = (PlayerDeathEvent) event;
message = new ElementTag(subEvent.getDeathMessage());
// Null check to prevent NPCs from causing an NPE
if (player != null) {
inventory = player.getInventory();
}
keep_inv = subEvent.getKeepInventory();
keep_level = subEvent.getKeepLevel();
}
drops = new ListTag();
for (ItemStack stack : event.getDrops()) {
drops.addObject(new ItemTag(stack == null ? new ItemStack(Material.AIR) : stack));
}
cancelled = false;
dropItems = null;
xp = event.getDroppedExp();
this.event = event;
fire(event);
event.setDroppedExp(xp);
if (dropItems != null) {
event.getDrops().clear();
for (ItemTag drop : dropItems) {
if (drop != null) {
event.getDrops().add(drop.getItemStack());
}
}
}
if (subEvent != null) {
subEvent.setKeepInventory(keep_inv);
subEvent.setKeepLevel(keep_level);
if (message != null) {
subEvent.setDeathMessage(message.asString());
}
if (cancelled) { // Hacked-in player-only cancellation tool to cancel messages
subEvent.setDeathMessage(null);
}
}
EntityTag.forgetEntity(livingEntity);
}
}