/
AssignmentTrait.java
370 lines (332 loc) · 13.2 KB
/
AssignmentTrait.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package com.denizenscript.denizen.npc.traits;
import com.denizenscript.denizen.objects.dEntity;
import com.denizenscript.denizen.objects.dPlayer;
import com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.Settings;
import com.denizenscript.denizen.npc.DenizenNPCHelper;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptRegistry;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import net.citizensnpcs.api.command.exception.CommandException;
import net.citizensnpcs.api.event.NPCSpawnEvent;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.Paginator;
import net.citizensnpcs.util.Messages;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
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.entity.EntityDamageByBlockEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.projectiles.ProjectileSource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class AssignmentTrait extends Trait {
// Saved to the C2 saves.yml
@Persist
private String assignment = "";
public AssignmentTrait() {
super("assignment");
}
/**
* Checks to see if the NPCs assignment is still a valid script on load of NPC.
*/
@Override
public void load(DataKey key) throws NPCLoadException {
// Check to make sure assignment is still valid. Throw a dB error if not.
if (hasAssignment()) {
Debug.echoError("Missing assignment '" + assignment + "' for NPC '"
+ npc.getName() + "/" + npc.getId() + "! Perhaps the script has been removed?");
}
npc.getTrait(ConstantsTrait.class).rebuildAssignmentConstants();
}
// <--[action]
// @Actions
// assignment
//
// @Triggers when the NPC receives an assignment via '/npc assign --set Name'.
//
// @Context
// None
//
// -->
/**
* Sets the NPCs Assignment Script and fires an 'On Assignment:' action. Can specify a player for
* context with the action.
*
* @param assignment the name of the Assignment Script, case in-sensitive
* @param player the player adding the assignment, can be null
* @return false if the assignment is invalid
*/
public boolean setAssignment(String assignment, dPlayer player) {
if (ScriptRegistry.containsScript(assignment, AssignmentScriptContainer.class)) {
this.assignment = assignment.toUpperCase();
// Add Constants/Trigger trait if not already added to the NPC.
if (!npc.hasTrait(ConstantsTrait.class)) {
npc.addTrait(ConstantsTrait.class);
}
if (!npc.hasTrait(TriggerTrait.class)) {
npc.addTrait(TriggerTrait.class);
}
if (Settings.healthTraitEnabledByDefault()) {
if (!npc.hasTrait(HealthTrait.class)) {
npc.addTrait(HealthTrait.class);
}
}
// Reset Constants
npc.getTrait(ConstantsTrait.class).rebuildAssignmentConstants();
// 'On Assignment' action.
DenizenNPCHelper.getDenizen(npc).action("assignment", player);
return true;
}
else {
return false;
}
}
/**
* Gets the name of the current Assignment Script assigned to this NPC.
*
* @return assignment script name, null if not set or assignment is invalid
*/
public AssignmentScriptContainer getAssignment() {
if (hasAssignment() && ScriptRegistry.containsScript(assignment, AssignmentScriptContainer.class)) {
return ScriptRegistry.getScriptContainer(assignment);
}
else {
return null;
}
}
/**
* Checks to see if this NPC currently has an assignment.
*
* @return true if NPC has an assignment and it is valid
*/
public boolean hasAssignment() {
if (assignment == null || assignment.equals("")) {
return false;
}
return ScriptRegistry.containsScript(assignment);
}
// <--[action]
// @Actions
// remove assignment
//
// @Triggers when the NPC loses its assignment.
//
// @Context
// None
//
// -->
/**
* Removes the current assignment and fires an 'On Remove Assignment:' action. Can specify a player for
* context with the action.
*
* @param player the player removing the assignment, can be null
*/
public void removeAssignment(dPlayer player) {
DenizenNPCHelper.getDenizen(npc).action("remove assignment", player);
assignment = "";
}
public void describe(CommandSender sender, int page) throws CommandException {
AssignmentScriptContainer assignmentScript = ScriptRegistry.getScriptContainer(assignment);
Paginator paginator = new Paginator().header("Assignment");
paginator.addLine("<e>Current assignment: " + (hasAssignment() ? this.assignment : "None.") + "");
paginator.addLine("");
if (!hasAssignment()) {
paginator.sendPage(sender, page);
return;
}
// Interact Scripts
boolean entriesPresent = false;
paginator.addLine(ChatColor.GRAY + "Interact Scripts:");
paginator.addLine("<e>Key: <a>Priority <b>Name");
if (assignmentScript.contains("INTERACT SCRIPTS")) {
entriesPresent = true;
for (String scriptEntry : assignmentScript.getStringList("INTERACT SCRIPTS")) {
String[] split = scriptEntry.split(" ", 2);
if (split.length == 2 && ArgumentHelper.matchesInteger(split[0])) {
paginator.addLine("<a>" + split[0] + "<b> " + split[1]);
}
else {
paginator.addLine("<b>" + scriptEntry);
}
}
}
if (!entriesPresent) {
paginator.addLine("<c>No Interact Scripts assigned.");
}
paginator.addLine("");
if (!entriesPresent) {
if (!paginator.sendPage(sender, page)) {
throw new CommandException(Messages.COMMAND_PAGE_MISSING);
}
return;
}
// Scheduled Activities
entriesPresent = false;
paginator.addLine(ChatColor.GRAY + "Scheduled Scripts:");
paginator.addLine("<e>Key: <a>Time <b>Name");
if (assignmentScript.contains("SCHEDULED ACTIVITIES")) {
entriesPresent = true;
for (String scriptEntry : assignmentScript.getStringList("SCHEDULED ACTIVITIES")) {
paginator.addLine("<a>" + scriptEntry.split(" ")[0] + "<b> " + scriptEntry.split(" ", 2)[1]);
}
}
if (!entriesPresent) {
paginator.addLine("<c>No scheduled scripts activities.");
}
paginator.addLine("");
// Actions
entriesPresent = false;
paginator.addLine(ChatColor.GRAY + "Actions:");
paginator.addLine("<e>Key: <a>Action name <b>Script Size");
if (assignmentScript.contains("ACTIONS")) {
entriesPresent = true;
}
if (entriesPresent) {
for (StringHolder action : assignmentScript.getConfigurationSection("ACTIONS").getKeys(false)) {
paginator.addLine("<a>" + action.str + " <b>" + assignmentScript.getStringList("ACTIONS." + action.str).size());
}
}
else {
paginator.addLine("<c>No actions defined in the assignment.");
}
paginator.addLine("");
if (!paginator.sendPage(sender, page)) {
throw new CommandException(Messages.COMMAND_PAGE_MISSING, page);
}
}
public void onSpawn(NPCSpawnEvent event) {
if (event.getNPC().getId() == npc.getId()) {
entityId = npc.getEntity().getUniqueId();
}
}
// <--[action]
// @Actions
// death
// death by entity
// death by <entity>
// death by block
// death by <cause>
//
// @Triggers when the NPC dies.
//
// @Context
// <context.killer> returns the entity that killed the NPC (if any)
// <context.shooter> returns the shooter of the killing projectile (if any)
// <context.damage> returns the last amount of damage applied (if any)
// <context.death_cause> returns the last damage cause (if any)
//
// -->
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onDeath(EntityDeathEvent deathEvent) {
if (!deathEvent.getEntity().getUniqueId().equals(entityId)) {
return;
}
EntityDamageEvent event = deathEvent.getEntity().getLastDamageCause();
String deathCause = event == null ? "unknown" : CoreUtilities.toLowerCase(event.getCause().toString()).replace('_', ' ');
Map<String, ObjectTag> context = new HashMap<>();
context.put("damage", new ElementTag(event == null ? 0 : event.getDamage()));
context.put("death_cause", new ElementTag(deathCause));
dPlayer player = null;
if (event instanceof EntityDamageByEntityEvent) {
Entity killerEntity = ((EntityDamageByEntityEvent) event).getDamager();
context.put("killer", new dEntity(killerEntity));
if (killerEntity instanceof Player) {
player = dPlayer.mirrorBukkitPlayer((Player) killerEntity);
}
else if (killerEntity instanceof Projectile) {
ProjectileSource shooter = ((Projectile) killerEntity).getShooter();
if (shooter != null && shooter instanceof LivingEntity) {
context.put("shooter", new dEntity((LivingEntity) shooter));
if (shooter instanceof Player) {
player = dPlayer.mirrorBukkitPlayer((Player) shooter);
}
DenizenAPI.getDenizenNPC(npc).action("death by " +
((LivingEntity) shooter).getType().toString(), player, context);
}
}
DenizenAPI.getDenizenNPC(npc).action("death by entity", player, context);
DenizenAPI.getDenizenNPC(npc).action("death by " +
killerEntity.getType().toString(), player, context);
}
else if (event instanceof EntityDamageByBlockEvent) {
DenizenAPI.getDenizenNPC(npc).action("death by block", player, context);
}
DenizenAPI.getDenizenNPC(npc).action("death", player, context);
DenizenAPI.getDenizenNPC(npc).action("death by " + deathCause, player, context);
}
private UUID entityId;
// <--[action]
// @Actions
// hit
// hit on <entity>
//
// @Triggers when the NPC hits an enemy.
//
// @Context
// None
//
// -->
// <--[action]
// @Actions
// kill
// kill of <entity>
//
// @Triggers when the NPC kills an enemy.
//
// @Context
// None
//
// -->
// Listen for this NPC's hits on entities
@EventHandler(priority = EventPriority.MONITOR)
public void onHit(EntityDamageByEntityEvent event) {
if (!npc.isSpawned()) {
return;
}
// Check if the damager is this NPC
if (event.getDamager() != npc.getEntity()) {
// If the damager is not this NPC, the damager could still be a
// projectile shot by this NPC, in which case we want to continue
if (event.getDamager() instanceof Projectile) {
if (((Projectile) event.getDamager()).getShooter() != npc.getEntity()) {
return;
}
}
else {
return;
}
}
dPlayer player = null;
// Check if the entity hit by this NPC is a player
if (event.getEntity() instanceof Player) {
player = dPlayer.mirrorBukkitPlayer((Player) event.getEntity());
}
// TODO: Context containing the entity hit
DenizenAPI.getDenizenNPC(npc).action("hit", player);
DenizenAPI.getDenizenNPC(npc).action("hit on " + event.getEntityType().name(), player);
if (event.getEntity() instanceof LivingEntity) {
if (((LivingEntity) event.getEntity()).getHealth() - event.getFinalDamage() <= 0) {
DenizenAPI.getDenizenNPC(npc).action("kill", player);
DenizenAPI.getDenizenNPC(npc).action("kill of " + event.getEntityType().name(), player);
}
}
// All done!
}
}