-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
ChatScriptEvent.java
200 lines (182 loc) · 6.89 KB
/
ChatScriptEvent.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
package com.denizenscript.denizen.events.player;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.scripts.containers.core.FormatScriptContainer;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.BukkitScriptEntryData;
import com.denizenscript.denizen.Settings;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.scripts.ScriptRegistry;
import com.denizenscript.denizencore.utilities.CoreUtilities;
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.List;
import java.util.Set;
public class ChatScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// player chats
//
// @Regex ^on player chats$
//
// @Switch in <area>
//
// @Cancellable true
//
// @Warning Using this will forcibly sync the chat thread.
//
// @Triggers when a player chats.
// @Context
// <context.message> returns the player's message as an Element.
// <context.format> returns the chat message's raw format.
// <context.recipients> returns a list of all players that will receive the chat.
//
// @Determine
// ElementTag to change the message.
// "FORMAT:" + ScriptTag to set the format script the message should use.
// "RAW_FORMAT:" + ElementTag to set the format directly (without a format script). (Use with caution, avoid if possible).
// "RECIPIENTS:" + ListTag(PlayerTag) to set the list of players that will receive the message.
//
// -->
public ChatScriptEvent() {
instance = this;
}
public static ChatScriptEvent instance;
public PlayerChatEvent pcEvent;
public AsyncPlayerChatEvent apcEvent;
public PlayerTag player;
public SyncChatHandler sch = new SyncChatHandler();
public AsyncChatHandler asch = new AsyncChatHandler();
@Override
public boolean couldMatch(ScriptPath path) {
return path.eventLower.startsWith("player chats");
}
@Override
public boolean matches(ScriptPath path) {
return runInCheck(path, player.getLocation());
}
@Override
public String getName() {
return "Chat";
}
boolean async = false;
@Override
public void init() {
async = Settings.worldScriptChatEventAsynchronous();
if (async) {
initListener(asch);
}
else {
initListener(sch);
}
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag) {
String determination = determinationObj.toString();
String lower = CoreUtilities.toLowerCase(determination);
if (lower.startsWith("format:")) {
String name = determination.substring("format:".length());
FormatScriptContainer formatscr = ScriptRegistry.getScriptContainer(name);
if (formatscr == null) {
Debug.echoError("Could not find format script matching '" + name + '\'');
}
else {
String formatstr = formatscr.getFormatText(null, player);
if (com.denizenscript.denizencore.utilities.debugging.Debug.verbose) {
Debug.log("Setting format to " + formatstr);
}
if (pcEvent != null) {
pcEvent.setFormat(formatstr);
}
else {
apcEvent.setFormat(formatstr);
}
}
return true;
}
else if (lower.startsWith("raw_format:")) {
String form = determination.substring("raw_format:".length());
if (pcEvent != null) {
pcEvent.setFormat(form);
}
else {
apcEvent.setFormat(form);
}
return true;
}
else if (lower.startsWith("recipients:")) {
String rec_new = determination.substring("recipients:".length());
ListTag recs = ListTag.valueOf(rec_new);
List<PlayerTag> players = recs.filter(PlayerTag.class, path.container, true);
Set<Player> recipients;
if (pcEvent != null) {
recipients = pcEvent.getRecipients();
}
else {
recipients = apcEvent.getRecipients();
}
recipients.clear();
for (PlayerTag player : players) {
recipients.add(player.getPlayerEntity());
}
return true;
}
else if (!isDefaultDetermination(determinationObj)) {
if (pcEvent != null) {
pcEvent.setMessage(determination);
}
else {
apcEvent.setMessage(determination);
}
return true;
}
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(player, null);
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("message")) {
return new ElementTag(pcEvent != null ? pcEvent.getMessage() : apcEvent.getMessage());
}
else if (name.equals("format")) {
return new ElementTag(pcEvent != null ? pcEvent.getFormat() : apcEvent.getFormat());
}
if (name.equals("recipients")) {
ListTag list = new ListTag();
for (Player tplayer : pcEvent != null ? pcEvent.getRecipients() : apcEvent.getRecipients()) {
list.add(PlayerTag.mirrorBukkitPlayer(tplayer).identify());
}
return list;
}
return super.getContext(name);
}
class SyncChatHandler implements Listener {
@EventHandler
public void onSyncChat(PlayerChatEvent event) {
pcEvent = event;
apcEvent = null;
player = new PlayerTag(event.getPlayer());
fire(event);
}
}
class AsyncChatHandler implements Listener {
@EventHandler
public void onAsyncChat(AsyncPlayerChatEvent event) {
pcEvent = null;
apcEvent = event;
player = new PlayerTag(event.getPlayer());
fire(event);
}
}
}