/
ChatTrigger.java
455 lines (397 loc) · 17.9 KB
/
ChatTrigger.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package net.aufdemrand.denizen.scripts.triggers.core;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.npc.traits.ChatbotTrait;
import net.aufdemrand.denizen.npc.traits.TriggerTrait;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.scripts.containers.core.InteractScriptContainer;
import net.aufdemrand.denizen.scripts.containers.core.InteractScriptHelper;
import net.aufdemrand.denizen.scripts.triggers.AbstractTrigger;
import net.aufdemrand.denizen.tags.BukkitTagContext;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.scripts.commands.core.DetermineCommand;
import net.aufdemrand.denizencore.tags.TagManager;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerChatEvent;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@SuppressWarnings("deprecation")
public class ChatTrigger extends AbstractTrigger implements Listener {
final static Pattern triggerPattern = Pattern.compile("/([^/]*)/");
final static boolean HyperDebug = false; // TODO: Replace with core->dB.verbose!
@Override
public void onEnable() {
Bukkit.getServer().getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance());
}
// Technically defined in TriggerTrait, but placing here instead.
// <--[action]
// @Actions
// chat
//
// @Triggers when a player chats to the NPC.
//
// @Context
// <context.message> returns the triggering message
// <context.keyword> returns the keyword matched by a RegEx trigger
//
// @Determine
// "CANCELLED" to stop the player from chatting.
// Element to change the message.
//
// -->
public ChatContext process(Player player, String message) {
// Check if there is an NPC within range of a player to chat to.
dNPC npc = Utilities.getClosestNPC_ChatTrigger(player.getLocation(), 25);
dPlayer denizenPlayer = dPlayer.mirrorBukkitPlayer(player);
if (HyperDebug) {
dB.log("Processing chat trigger: valid npc? " + (npc != null));
}
// No NPC? Nothing else to do here.
if (npc == null) {
return new ChatContext(false);
}
if (HyperDebug) {
dB.log("Has trait? " + npc.getCitizen().hasTrait(TriggerTrait.class));
}
// If the NPC doesn't have triggers, or the triggers are not enabled, then
// just return false.
if (!npc.getCitizen().hasTrait(TriggerTrait.class)) {
return new ChatContext(false);
}
if (HyperDebug) {
dB.log("enabled? " + npc.getCitizen().getTrait(TriggerTrait.class).isEnabled(name));
}
if (!npc.getCitizen().getTrait(TriggerTrait.class).isEnabled(name)) {
return new ChatContext(false);
}
// Check range
if (npc.getTriggerTrait().getRadius(name) < npc.getLocation().distance(player.getLocation())) {
if (HyperDebug) {
dB.log("Not in range");
}
return new ChatContext(false);
}
// The Denizen config can require some other criteria for a successful chat-with-npc.
// Should we check 'line of sight'? Players cannot talk to NPCs through walls
// if enabled. Should the Player chat only when looking at the NPC? This may
// reduce accidental chats with NPCs.
if (Settings.chatMustSeeNPC()) {
if (!player.hasLineOfSight(npc.getEntity())) {
if (HyperDebug) {
dB.log("no LOS");
}
return new ChatContext(false);
}
}
if (Settings.chatMustLookAtNPC()) {
if (!NMSHandler.getInstance().getEntityHelper().isFacingEntity(player, npc.getEntity(), 45)) {
if (HyperDebug) {
dB.log("Not facing");
}
return new ChatContext(false);
}
}
Boolean ret = false;
// Denizen should be good to interact with. Let's get the script.
InteractScriptContainer script = npc.getInteractScript(denizenPlayer, ChatTrigger.class);
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("message", new Element(message));
//
// Fire the Actions!
//
// If engaged or not cool, calls On Unavailable, if cool, calls On Chat
// If available (not engaged, and cool) sets cool down and returns true.
TriggerTrait.TriggerContext trigger = npc.getTriggerTrait()
.trigger(ChatTrigger.this, denizenPlayer, context);
// Return false if determine cancelled
if (trigger.hasDetermination()) {
if (trigger.getDetermination().equalsIgnoreCase("cancelled")) {
if (HyperDebug) {
dB.log("Cancelled");
}
// Mark as handled, the event will cancel.
return new ChatContext(true);
}
}
// Return false if trigger was unable to fire
if (!trigger.wasTriggered()) {
// If the NPC is not interact-able, Settings may allow the chat to filter
// through. Check the Settings if this is enabled.
if (Settings.chatGloballyIfUninteractable()) {
dB.echoDebug(script, ChatColor.YELLOW + "Resuming. " + ChatColor.WHITE
+ "The NPC is currently cooling down or engaged.");
return new ChatContext(false);
}
else {
ret = true;
}
}
// Debugger
dB.report(script, name, aH.debugObj("Player", player.getName())
+ aH.debugObj("NPC", npc.toString())
+ aH.debugObj("Radius(Max)", npc.getLocation().distance(player.getLocation())
+ "(" + npc.getTriggerTrait().getRadius(name) + ")")
+ aH.debugObj("Trigger text", message)
+ aH.debugObj("LOS", String.valueOf(player.hasLineOfSight(npc.getEntity())))
+ aH.debugObj("Facing", String.valueOf(NMSHandler.getInstance().getEntityHelper().isFacingEntity(player, npc.getEntity(), 45))));
// Change the text if it's in the determination
if (trigger.hasDetermination()) {
message = trigger.getDetermination();
}
if (script == null) {
if (HyperDebug) {
dB.log("null script");
}
return new ChatContext(message, false);
}
// Check if the NPC has Chat Triggers for this step.
if (!script.containsTriggerInStep(
InteractScriptHelper.getCurrentStep(denizenPlayer,
script.getName()), ChatTrigger.class)) {
// If this is a Chatbot, make it chat anything it wants if
// it has no chat triggers for this step
if (npc.getCitizen().hasTrait(ChatbotTrait.class)) {
Utilities.talkToNPC(message, denizenPlayer, npc, Settings.chatToNpcOverhearingRange());
npc.getCitizen().getTrait(ChatbotTrait.class).chatTo(player, message);
if (HyperDebug) {
dB.log("chatbot");
}
return new ChatContext(false);
}
// No chat trigger for this step.. do we chat globally, or to the NPC?
else if (!Settings.chatGloballyIfNoChatTriggers()) {
dB.echoDebug(script, player.getName() + " says to "
+ npc.getNicknameTrait().getNickname() + ", " + message);
return new ChatContext(false);
}
else {
if (HyperDebug) {
dB.log("No trigger in step, chatting globally");
}
return new ChatContext(message, ret);
}
}
// Parse the script and match Triggers.. if found, cancel the text! The
// parser will take care of everything else.
String id = null;
boolean matched = false;
String replacementText = null;
String regexId = null;
String regexMessage = null;
// Use TreeMap to sort chat triggers alphabetically
TreeMap<String, String> idMap = new TreeMap<String, String>();
idMap.putAll(script.getIdMapFor(ChatTrigger.class, denizenPlayer));
if (!idMap.isEmpty()) {
// Iterate through the different id entries in the step's chat trigger
List<Map.Entry<String, String>> entries = new ArrayList<Map.Entry<String, String>>(idMap.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<String, String>>() {
@Override
public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
if (o1 == null || o2 == null) {
return 0;
}
return o1.getKey().compareToIgnoreCase(o2.getKey());
}
});
for (Map.Entry<String, String> entry : entries) {
// Check if the chat trigger specified in the specified id's 'trigger:' key
// matches the text the player has said
// TODO: script arg?
String triggerText = TagManager.tag(entry.getValue(), new BukkitTagContext
(denizenPlayer, npc, false, null, false, null));
Matcher matcher = triggerPattern.matcher(triggerText);
while (matcher.find()) {
if (!script.checkSpecificTriggerScriptRequirementsFor(ChatTrigger.class,
denizenPlayer, npc, entry.getKey())) {
continue;
}
// TODO: script arg?
String keyword = TagManager.tag(matcher.group().replace("/", ""), new BukkitTagContext
(denizenPlayer, npc, false, null, false, null));
String[] split = keyword.split("\\\\\\+REPLACE:", 2);
String replace = null;
if (split.length == 2) {
keyword = split[0];
replace = split[1];
}
// Check if the trigger is REGEX, but only if we don't have a REGEX
// match already (thus using alphabetical priority for triggers)
if (regexId == null && isKeywordRegex(keyword)) {
Pattern pattern = Pattern.compile(keyword.substring(6));
Matcher m = pattern.matcher(message);
if (m.find()) {
// REGEX matches are left for last, so save it in case non-REGEX
// matches don't exist
regexId = entry.getKey();
regexMessage = triggerText.replace(matcher.group(), m.group());
dB.log("entry value: " + triggerText + " keyword: " + keyword + " m.group: " + m.group() + " matcher.group: " + matcher.group());
context.put("keyword", new Element(m.group()));
if (replace != null) {
regexMessage = replace;
}
}
}
else if (isKeywordStrict(keyword)) {
if (message.toUpperCase().equalsIgnoreCase(keyword.toUpperCase())) {
// Trigger matches
id = entry.getKey();
replacementText = triggerText.replace("/", "");
matched = true;
if (replace != null) {
replacementText = replace;
}
}
}
else if (message.toUpperCase().contains(keyword.toUpperCase())) {
// Trigger matches
id = entry.getKey();
replacementText = triggerText.replace("/", "");
matched = true;
if (replace != null) {
replacementText = replace;
}
}
}
if (matched) {
break;
}
}
}
if (!matched && regexId != null) {
id = regexId;
replacementText = regexMessage;
}
// If there was a match, the id of the match should have been returned.
if (id != null) {
Utilities.talkToNPC(replacementText, denizenPlayer, npc, Settings.chatToNpcOverhearingRange());
parse(npc, denizenPlayer, script, id, context);
if (HyperDebug) {
dB.log("chat to NPC");
}
return new ChatContext(true);
}
else {
// If this is a Chatbot, make it chat anything it wants if
// none of its chat triggers worked
if (npc.getCitizen().hasTrait(ChatbotTrait.class)) {
Utilities.talkToNPC(message, denizenPlayer, npc, Settings.chatToNpcOverhearingRange());
npc.getCitizen().getTrait(ChatbotTrait.class).chatTo(player, message);
if (HyperDebug) {
dB.log("Chatbot");
}
return new ChatContext(true);
}
else if (!Settings.chatGloballyIfFailedChatTriggers()) {
Utilities.talkToNPC(message, denizenPlayer, npc, Settings.chatToNpcOverhearingRange());
if (HyperDebug) {
dB.log("Chat globally");
}
return new ChatContext(true);
}
// No matching chat triggers, and the config.yml says we
// should just ignore the interaction...
}
if (HyperDebug) {
dB.log("Finished calculating");
}
return new ChatContext(message, ret);
}
@EventHandler
public void asyncChatTrigger(final AsyncPlayerChatEvent event) {
if (HyperDebug) {
dB.log("Chat trigger seen, cancelled: " + event.isCancelled()
+ ", chatasync: " + Settings.chatAsynchronous());
}
if (event.isCancelled()) {
return;
}
// Return if "Use asynchronous event" is false in config file
if (!Settings.chatAsynchronous()) {
return;
}
if (!event.isAsynchronous()) {
syncChatTrigger(new PlayerChatEvent(event.getPlayer(), event.getMessage(), event.getFormat(), event.getRecipients()));
return;
}
FutureTask<ChatContext> futureTask = new FutureTask<ChatContext>(new Callable<ChatContext>() {
@Override
public ChatContext call() {
return process(event.getPlayer(), event.getMessage());
}
});
Bukkit.getScheduler().runTask(DenizenAPI.getCurrentInstance(), futureTask);
try {
ChatContext context = futureTask.get();
if (context.wasTriggered()) {
event.setCancelled(true);
}
if (context.hasChanges()) {
event.setMessage(context.getChanges());
}
}
catch (Exception e) {
dB.echoError(e);
}
}
@EventHandler
public void syncChatTrigger(final PlayerChatEvent event) {
if (event.isCancelled()) {
return;
}
// Return if "Use asynchronous event" is true in config file
if (Settings.chatAsynchronous()) {
return;
}
ChatContext chat = process(event.getPlayer(), event.getMessage());
if (chat.wasTriggered()) {
event.setCancelled(true);
}
if (chat.hasChanges()) {
event.setMessage(chat.getChanges());
}
}
private boolean isKeywordRegex(String keyWord) {
return keyWord.toUpperCase().startsWith("REGEX:");
}
private boolean isKeywordStrict(String keyWord) {
return keyWord.toUpperCase().startsWith("STRICT:");
}
/**
* Contains whether the chat trigger successfully 'triggered' and any context that was
* available while triggering or attempting to trigger.
*/
public class ChatContext {
public ChatContext(boolean triggered) {
this.triggered = triggered;
}
public ChatContext(String changed_text, boolean triggered) {
this.changed_text = changed_text;
this.triggered = triggered;
}
String changed_text;
boolean triggered;
public boolean hasChanges() {
return changed_text != null;
}
public String getChanges() {
return changed_text != null ? changed_text : DetermineCommand.DETERMINE_NONE;
}
public Boolean wasTriggered() {
return triggered;
}
}
}