-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
VehicleDamagedScriptEvent.java
132 lines (120 loc) · 4.3 KB
/
VehicleDamagedScriptEvent.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
package com.denizenscript.denizen.events.vehicle;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.vehicle.VehicleDamageEvent;
public class VehicleDamagedScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// vehicle damaged
// <vehicle> damaged
// entity damages vehicle
// <entity> damages vehicle
// entity damages <vehicle>
// <entity> damages <vehicle>
//
// @Group Vehicle
//
// @Regex ^on [^\s]+ damages [^\s]+$
//
// @Switch in:<area> to only process the event if it occurred within a specified area.
//
// @Cancellable true
//
// @Triggers when a vehicle is damaged.
//
// @Context
// <context.vehicle> returns the EntityTag of the vehicle.
// <context.entity> returns the EntityTag of the attacking entity.
// <context.damage> returns the amount of damage to be received.
//
// @Determine
// ElementTag(Decimal) to set the value of the damage received by the vehicle.
//
// @NPC when the entity that damaged the vehicle is a player.
//
// @NPC when the entity that damaged the vehicle is an NPC.
//
// -->
public VehicleDamagedScriptEvent() {
instance = this;
}
public static VehicleDamagedScriptEvent instance;
public EntityTag vehicle;
public EntityTag entity;
public VehicleDamageEvent event;
@Override
public boolean couldMatch(ScriptPath path) {
String cmd = path.eventArgAt(1);
if (!cmd.equals("damaged") && !cmd.equals("damages")) {
return false;
}
if (path.eventArgLowerAt(3).equals("by")) {
return false;
}
if (!couldMatchVehicle(path.eventArgLowerAt(0)) && !couldMatchVehicle(path.eventArgLowerAt(2))) {
return false;
}
if (!couldMatchEntity(path.eventArgLowerAt(0))) {
return false;
}
return true;
}
@Override
public boolean matches(ScriptPath path) {
String cmd = path.eventArgLowerAt(1);
String veh = cmd.equals("damaged") ? path.eventArgLowerAt(0) : path.eventArgLowerAt(2);
String ent = cmd.equals("damages") ? path.eventArgLowerAt(0) : "";
if (!tryEntity(vehicle, veh)) {
return false;
}
if (!ent.isEmpty() && entity != null && !tryEntity(entity, ent)) {
return false;
}
if (!runInCheck(path, vehicle.getLocation())) {
return false;
}
return super.matches(path);
}
@Override
public String getName() {
return "VehicleDamaged";
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag && ((ElementTag) determinationObj).isDouble()) {
event.setDamage(((ElementTag) determinationObj).asDouble());
return true;
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(entity);
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("vehicle")) {
return vehicle.getDenizenObject();
}
else if (name.equals("entity") && entity != null) {
return entity.getDenizenObject();
}
else if (name.equals("damage")) {
return new ElementTag(event.getDamage());
}
return super.getContext(name);
}
@EventHandler
public void onVehicleDestroyed(VehicleDamageEvent event) {
vehicle = new EntityTag(event.getVehicle());
entity = event.getAttacker() != null ? new EntityTag(event.getAttacker()) : null;
this.event = event;
fire(event);
}
}