/
ChatTrigger.java
225 lines (188 loc) · 10.7 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
package net.aufdemrand.denizen.scripts.triggers.core;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.npc.traits.TriggerTrait;
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.TagManager;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.entity.Rotation;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ChatTrigger extends AbstractTrigger implements Listener {
final static Pattern triggerPattern = Pattern.compile("\\/([^/]*)\\/");
@Override
public void onEnable() {
Bukkit.getServer().getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance());
}
@EventHandler
public void chatTrigger(final AsyncPlayerChatEvent event) {
if (event.isCancelled()) return;
boolean wasInterrupted = Thread.interrupted();
Callable<Boolean> call = new Callable<Boolean>() {
public Boolean call() {
// Check if there is an NPC within range of a player to chat to.
dNPC npc = Utilities.getClosestNPC(event.getPlayer().getLocation(), 25);
dPlayer player = dPlayer.mirrorBukkitPlayer(event.getPlayer());
// No NPC? Nothing else to do here.
if (npc == null) return null;
// If the NPC doesn't have triggers, or the triggers are not enabled, then
// just return.
if (!npc.getCitizen().hasTrait(TriggerTrait.class)) return null;
if (!npc.getCitizen().getTrait(TriggerTrait.class).isEnabled(name)) return null;
// Check range
if (npc.getTriggerTrait().getRadius(name) < npc.getLocation().distance(event.getPlayer().getLocation()))
return null;
// Debugger
dB.report(name, aH.debugObj("Player", event.getPlayer().getName())
+ aH.debugObj("NPC", npc.toString())
+ aH.debugObj("Radius(Max)", npc.getLocation().distance(event.getPlayer().getLocation())
+ "(" + npc.getTriggerTrait().getRadius(name) + ")")
+ aH.debugObj("Trigger text", event.getMessage())
+ aH.debugObj("LOS", String.valueOf(npc.getEntity().hasLineOfSight(event.getPlayer())))
+ aH.debugObj("Facing", String.valueOf(Rotation.isFacingEntity(event.getPlayer(), npc.getEntity(), 45))));
// 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.
// TODO: CraftBukkit 1.6 broke the two lines below. Think of a workaround.
//if (Settings.ChatMustSeeNPC())
// if (!npc.getEntity().hasLineOfSight(event.getPlayer())) return null;
if (Settings.ChatMustLookAtNPC())
if (!Rotation.isFacingEntity(event.getPlayer(), npc.getEntity(), 45)) return null;
Boolean ret = null;
// 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.
if (!npc.getTriggerTrait().trigger(ChatTrigger.this, player)) {
// If the NPC is not interactable, Settings may allow the chat to filter
// through. Check the Settings if this is enabled.
if (Settings.ChatGloballyIfUninteractable()) {
dB.echoDebug (ChatColor.YELLOW + "Resuming. " + ChatColor.WHITE
+ "The NPC is currently cooling down or engaged.");
return null;
} else {
ret = true;
}
}
// Denizen should be good to interact with. Let's get the script.
InteractScriptContainer script = npc.getInteractScript(player, ChatTrigger.class);
if (script == null) return null;
// Check if the NPC has Chat Triggers for this step.
if (!script.containsTriggerInStep(
InteractScriptHelper.getCurrentStep(player,
script.getName()), ChatTrigger.class)) {
// No chat trigger for this step.. do we chat globally, or to the NPC?
if (!Settings.ChatGloballyIfNoChatTriggers()) {
dB.echoDebug(event.getPlayer().getName() + " says to "
+ npc.getNicknameTrait().getNickname() + ", " + event.getMessage());
return true;
}
else return 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;
Map<String, String> idMap = script.getIdMapFor(ChatTrigger.class, player);
if (!idMap.isEmpty()) {
// Iterate through the different id entries in the step's chat trigger
for (Map.Entry<String, String> entry : idMap.entrySet()) {
// Check if the chat trigger specified in the specified id's 'trigger:' key
// matches the text the player has said
Matcher matcher = triggerPattern.matcher(entry.getValue());
while (matcher.find ()) {
if (!script.checkSpecificTriggerScriptRequirementsFor(ChatTrigger.class,
player, npc, entry.getKey())) continue;
String keyword = TagManager.tag(player, npc, matcher.group().replace("/", ""));
// Check if the trigger is REGEX
if(isKeywordRegex(keyword)) {
Pattern pattern = Pattern.compile(keyword.substring(6));
Matcher m = pattern.matcher(event.getMessage());
if (m.find()) {
// REGEX matches are left for last, so save it in case non-REGEX
// matches don't exist
regexId = entry.getKey();
regexMessage = entry.getValue().replace(matcher.group(), m.group());
dB.log("entry value: " + entry.getValue() + " keyword: " + keyword + " m.group: " + m.group() + " matcher.group: " + matcher.group());
}
}
else if (isKeywordStrict(keyword)) {
if (event.getMessage().toUpperCase().equalsIgnoreCase(keyword.toUpperCase()))
{
// Trigger matches
id = entry.getKey();
replacementText = entry.getValue().replace("/", "");
matched = true;
}
}
else if (event.getMessage().toUpperCase().contains(keyword.toUpperCase()))
{
// Trigger matches
id = entry.getKey();
replacementText = entry.getValue().replace("/", "");
matched = true;
}
}
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, player, npc, Settings.ChatToNpcOverhearingRange());
parse(npc, player, script, id);
return true;
} else {
if (!Settings.ChatGloballyIfFailedChatTriggers ()) {
Utilities.talkToNPC(event.getMessage(), player, npc, Settings.ChatToNpcOverhearingRange());
return true;
}
// No matching chat triggers, and the config.yml says we
// should just ignore the interaction...
}
return ret;
}
};
Boolean cancelled = null;
try {
cancelled = event.isAsynchronous() ? Bukkit.getScheduler().callSyncMethod(DenizenAPI.getCurrentInstance(), call).get() : call.call();
} catch (InterruptedException e) {
// e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if (wasInterrupted) {
Thread.currentThread().interrupt();
}
if (cancelled == null)
return;
event.setCancelled(cancelled);
}
private boolean isKeywordRegex (String keyWord) {
return keyWord.toUpperCase().startsWith("REGEX:");
}
private boolean isKeywordStrict (String keyWord) {
return keyWord.toUpperCase().startsWith("STRICT:");
}
}