/
AssignmentTrait.java
248 lines (217 loc) · 8.94 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
package net.aufdemrand.denizen.npc.traits;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.containers.core.AssignmentScriptContainer;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
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.command.exception.CommandException;
import net.citizensnpcs.api.util.Paginator;
import net.citizensnpcs.util.Messages;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
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.EntityDamageByEntityEvent;
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())
dB.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.
DenizenAPI.getCurrentInstance().getNPCRegistry().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.getScriptContainerAs(assignment, AssignmentScriptContainer.class);
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) {
assignment = "";
DenizenAPI.getCurrentInstance().getNPCRegistry().getDenizen(npc).action("remove assignment", player);
}
public void describe(CommandSender sender, int page) throws CommandException {
AssignmentScriptContainer assignmentScript = ScriptRegistry
.getScriptContainerAs(assignment, AssignmentScriptContainer.class);
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"))
paginator.addLine("<a>" + scriptEntry.split(" ")[0] + "<b> " + scriptEntry.split(" ", 2)[1]);
} 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 (String action : assignmentScript.getConfigurationSection("ACTIONS").getKeys(false))
paginator.addLine("<a>" + action + " <b>" + assignmentScript.getStringList("ACTIONS." + action).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);
}
// <--[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) {
// Check if the damager is this NPC
if (event.getDamager() != npc.getBukkitEntity()) {
// 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.getBukkitEntity()) 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.getDamage() <= 0) {
DenizenAPI.getDenizenNPC(npc).action("kill", player);
DenizenAPI.getDenizenNPC(npc).action("kill of " + event.getEntityType().name(), player);
}
}
// All done!
}
}