-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
EntityDamagedScriptEvent.java
206 lines (187 loc) · 7.51 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
package net.aufdemrand.denizen.events.entity;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.events.BukkitScriptEvent;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.scripts.ScriptEntryData;
import net.aufdemrand.denizencore.scripts.containers.ScriptContainer;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
public class EntityDamagedScriptEvent extends BukkitScriptEvent implements Listener {
// <--[language]
// @name Damage Cause
// @group Events
// @description
// Possible damage causes
// See list in Spigot source here: <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityDamageEvent.DamageCause.html>
// -->
// <--[event]
// @Events
// entity damaged (by <cause>)
// <entity> damaged (by <cause>)
// entity damages entity
// entity damages <entity>
// entity damaged by entity
// entity damaged by <entity>
// <entity> damages entity
// <entity> damaged by entity
// <entity> damaged by <entity>
// <entity> damages <entity>
//
// @Regex ^on [^\s]+ ((damages [^\s]+)|damaged( by [^\s]+)?)$
// @Switch in <area>
//
// @Switch with <item>
//
// @Cancellable true
//
// @Triggers when an entity is damaged.
//
// @Context
// <context.entity> returns the dEntity that was damaged.
// <context.damager> returns the dEntity damaging the other entity, if any.
// <context.cause> returns the an Element of reason the entity was damaged - see <@link language damage cause> for causes.
// <context.damage> returns an Element(Decimal) of the amount of damage dealt.
// <context.final_damage> returns an Element(Decimal) of the amount of damage dealt, after armor is calculated.
// <context.projectile> returns a dEntity of the projectile, if one caused the event.
// <context.damage_TYPE> returns the damage dealt by a specific damage type where TYPE can be any of: BASE, HARD_HAT, BLOCKING, ARMOR, RESISTANCE, MAGIC, ABSORPTION.
//
// @Determine
// Element(Decimal) to set the amount of damage the entity receives.
//
// @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;
}
public static EntityDamagedScriptEvent instance;
public dEntity entity;
public Element cause;
public Element damage;
public Element final_damage;
public dEntity damager;
public dEntity projectile;
public dItem held;
public EntityDamageEvent event;
@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String cmd = CoreUtilities.getXthArg(1, lower);
return cmd.equals("damaged") || cmd.equals("damages");
}
@Override
public boolean matches(ScriptPath path) {
String lower = path.eventLower;
String cmd = CoreUtilities.getXthArg(1, lower);
String attacker = cmd.equals("damages") ? CoreUtilities.getXthArg(0, lower) :
CoreUtilities.getXthArg(2, lower).equals("by") ? CoreUtilities.getXthArg(3, lower) : "";
String target = cmd.equals("damages") ? CoreUtilities.getXthArg(2, lower) : CoreUtilities.getXthArg(0, lower);
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 true;
}
@Override
public String getName() {
return "EntityDamaged";
}
@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
if (aH.matchesDouble(determination)) {
damage = new Element(aH.getDoubleFrom(determination));
return true;
}
return super.applyDetermination(container, determination);
}
@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() ? dEntity.getNPCFrom(event.getEntity()) : null);
}
@Override
public dObject getContext(String name) {
if (name.equals("entity")) {
return entity.getDenizenObject();
}
else if (name.equals("damage")) {
return damage;
}
else if (name.equals("final_damage")) {
return final_damage;
}
else if (name.equals("cause")) {
return cause;
}
else if (name.equals("damager") && damager != null) {
return damager.getDenizenObject();
}
else if (name.equals("projectile") && projectile != null) {
return projectile.getDenizenObject();
}
else if (name.startsWith("damage_")) {
for (EntityDamageEvent.DamageModifier dm : EntityDamageEvent.DamageModifier.values()) {
if (name.equals("damage_" + CoreUtilities.toLowerCase(dm.name()))) {
return new Element(event.getDamage(dm));
}
}
}
return super.getContext(name);
}
@EventHandler
public void onEntityDamaged(EntityDamageEvent event) {
entity = new dEntity(event.getEntity());
damage = new Element(event.getDamage());
final_damage = new Element(event.getFinalDamage());
cause = new Element(CoreUtilities.toLowerCase(event.getCause().name()));
damager = null;
projectile = null;
held = null;
if (event instanceof EntityDamageByEntityEvent) {
damager = new dEntity(((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);
}
}
}
cancelled = event.isCancelled();
this.event = event;
fire();
event.setCancelled(cancelled);
event.setDamage(damage.asDouble());
}
}