/
EntityDamagedScriptEvent.java
262 lines (247 loc) · 10.5 KB
/
EntityDamagedScriptEvent.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
package com.denizenscript.denizen.events.entity;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.Deprecations;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.potion.PotionEffectType;
public class EntityDamagedScriptEvent extends BukkitScriptEvent implements Listener {
// <--[language]
// @name Damage Cause
// @group Useful Lists
// @description
// Possible damage causes: <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityDamageEvent.DamageCause.html>
// These are used in <@link event entity damage>, <@link tag server.damage_causes>, <@link tag EntityTag.last_damage.cause>, ...
// -->
// <--[event]
// @Events
// <entity> damaged by <entity>
// <entity> damaged (by <'cause'>)
// <entity> damages <entity>
//
// @Group Entity
//
// @Location true
//
// @Switch with:<item> to only process the event when the item used to cause damage (in the damager's hand) is a specified item.
//
// @Cancellable true
//
// @Triggers when an entity is damaged.
//
// @Context
// <context.entity> returns the EntityTag that was damaged.
// <context.damager> returns the EntityTag damaging the other entity, if any.
// <context.cause> returns the an ElementTag of reason the entity was damaged - see <@link language damage cause> for causes.
// <context.damage> returns an ElementTag(Decimal) of the amount of damage dealt.
// <context.final_damage> returns an ElementTag(Decimal) of the amount of damage dealt, after armor is calculated.
// <context.projectile> returns a EntityTag of the projectile, if one caused the event.
// <context.damage_type_map> returns a MapTag the damage dealt by a specific damage type with keys: BASE, HARD_HAT, BLOCKING, ARMOR, RESISTANCE, MAGIC, ABSORPTION.
// <context.was_critical> returns 'true' if the damage was a critical hit. (Warning: this value is calculated and not guaranteed to be correct if the event is altered).
//
// @Determine
// ElementTag(Decimal) to set the amount of damage the entity receives.
// "CLEAR_MODIFIERS" to zero out all damage modifiers other than "BASE", effectively making damage == final_damage.
//
// @Player when the damager or damaged entity is a player. Cannot be both.
//
// @NPC when the damager or damaged entity is an NPC. Cannot be both.
//
// -->
public EntityDamagedScriptEvent() {
instance = this;
registerCouldMatcher("<entity> damaged (by <'cause'>)");
registerCouldMatcher("<entity> damaged by <entity>");
registerCouldMatcher("<entity> damages <entity>");
registerSwitches("with");
}
public static EntityDamagedScriptEvent instance;
public EntityTag entity;
public ElementTag cause;
public EntityTag damager;
public EntityTag projectile;
public ItemTag held;
public EntityDamageEvent event;
@Override
public boolean couldMatch(ScriptPath path) {
if (!super.couldMatch(path)) {
return false;
}
String cmd = path.eventArgLowerAt(1);
if (cmd.equals("damaged")) {
if (path.eventArgLowerAt(0).equals("vehicle")) {
return false;
}
}
else if (cmd.equals("damages")) {
if (path.eventArgLowerAt(2).equals("vehicle")) {
return false;
}
}
else {
return false;
}
return true;
}
@Override
public boolean matches(ScriptPath path) {
String cmd = path.eventArgLowerAt(1);
String attacker = cmd.equals("damages") ? path.eventArgLowerAt(0) :
path.eventArgLowerAt(2).equals("by") ? path.eventArgLowerAt(3) : "";
String target = cmd.equals("damages") ? path.eventArgLowerAt(2) : path.eventArgLowerAt(0);
if (!attacker.isEmpty()) {
if (damager != null) {
if (!cause.asString().equals(attacker) && !tryEntity(projectile, attacker) && !tryEntity(damager, attacker)) {
return false;
}
}
else {
if (!cause.asString().equals(attacker)) {
return false;
}
}
}
if (!tryEntity(entity, target)) {
return false;
}
if (!runInCheck(path, entity.getLocation())) {
return false;
}
if (!runWithCheck(path, held)) {
return false;
}
return super.matches(path);
}
@Override
public String getName() {
return "EntityDamaged";
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag) {
if (CoreUtilities.equalsIgnoreCase(determinationObj.toString(), "clear_modifiers")) {
for (EntityDamageEvent.DamageModifier modifier : EntityDamageEvent.DamageModifier.values()) {
if (modifier != EntityDamageEvent.DamageModifier.BASE && event.isApplicable(modifier)) {
event.setDamage(modifier, 0);
}
}
return true;
}
else if (((ElementTag) determinationObj).isDouble()) {
event.setDamage(((ElementTag) determinationObj).asDouble());
return true;
}
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(damager != null && damager.isPlayer() ? damager.getDenizenPlayer() : entity.isPlayer() ? entity.getDenizenPlayer() : null,
damager != null && damager.isCitizensNPC() ? damager.getDenizenNPC() : entity.isCitizensNPC() ? entity.getDenizenNPC() : null);
}
public boolean calculateWasCritical() {
if (!(event instanceof EntityDamageByEntityEvent)) {
return false;
}
if (!damager.isPlayer()) {
return false;
}
// This based on the source of NMS EntityHuman#attack(Entity entity)
// boolean flag = f2 > 0.9F;
// boolean flag2 = flag && this.fallDistance > 0.0F && !this.onGround && !this.isClimbing() && !this.isInWater() && !this.hasEffect(MobEffects.BLINDNESS) && !this.isPassenger() && entity instanceof EntityLiving;
if (event.getDamage() <= 0.9) {
return false;
}
if (!(event.getEntity() instanceof LivingEntity)) {
return false;
}
Player player = damager.getPlayer();
if (NMSHandler.getPlayerHelper().getAttackCooldownPercent(player) < 0.999) { // attack cooldown is also checked in that method earlier
return false;
}
if (player.getFallDistance() <= 0 || player.isOnGround() || player.isClimbing() || player.isInWater()) {
return false;
}
if (player.hasPotionEffect(PotionEffectType.BLINDNESS)) {
return false;
}
if (player.isInsideVehicle()) {
return false;
}
return true;
}
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "entity": return entity.getDenizenObject();
case "damage": return new ElementTag(event.getDamage());
case "final_damage": return new ElementTag(event.getFinalDamage());
case "cause": return cause;
case "damager":
if (damager != null) {
return damager.getDenizenObject();
}
break;
case "projectile":
if (projectile != null) {
return projectile.getDenizenObject();
}
break;
case "damage_type_map": {
MapTag map = new MapTag();
for (EntityDamageEvent.DamageModifier dm : EntityDamageEvent.DamageModifier.values()) {
map.putObject(dm.name(), new ElementTag(event.getDamage(dm)));
}
return map;
}
case "was_critical":
return new ElementTag(calculateWasCritical());
}
if (name.startsWith("damage_")) {
Deprecations.damageEventTypeMap.warn();
for (EntityDamageEvent.DamageModifier dm : EntityDamageEvent.DamageModifier.values()) {
if (name.equals("damage_" + CoreUtilities.toLowerCase(dm.name()))) {
return new ElementTag(event.getDamage(dm));
}
}
}
return super.getContext(name);
}
@EventHandler
public void onEntityDamaged(EntityDamageEvent event) {
entity = new EntityTag(event.getEntity());
cause = new ElementTag(CoreUtilities.toLowerCase(event.getCause().name()));
damager = null;
projectile = null;
held = null;
if (event instanceof EntityDamageByEntityEvent) {
damager = new EntityTag(((EntityDamageByEntityEvent) event).getDamager());
if (damager.isProjectile()) {
projectile = damager;
if (damager.hasShooter()) {
damager = damager.getShooter();
}
}
if (damager != null) {
held = damager.getItemInHand();
if (held != null) {
held.setAmount(1);
}
}
}
this.event = event;
fire(event);
}
}