-
-
Notifications
You must be signed in to change notification settings - Fork 294
/
Text.java
328 lines (285 loc) · 10.2 KB
/
Text.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
package net.citizensnpcs.trait.text;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import org.bukkit.conversations.Conversation;
import org.bukkit.conversations.ConversationFactory;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import com.google.common.collect.Maps;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.ai.speech.SpeechContext;
import net.citizensnpcs.api.event.NPCRightClickEvent;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.Paginator;
import net.citizensnpcs.api.util.Placeholders;
import net.citizensnpcs.editor.Editor;
import net.citizensnpcs.trait.HologramTrait;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Util;
/**
* Persists text metadata, i.e. text that will be said by an NPC on certain triggers.
*/
@TraitName("text")
public class Text extends Trait implements Runnable, Listener {
private final Map<UUID, Long> cooldowns = Maps.newHashMap();
private int currentIndex;
private int delay = -1;
private String itemInHandPattern = "default";
private final Plugin plugin;
private boolean randomTalker = Setting.DEFAULT_RANDOM_TALKER.asBoolean();
private double range = Setting.DEFAULT_TALK_CLOSE_RANGE.asDouble();
private boolean realisticLooker = Setting.DEFAULT_REALISTIC_LOOKING.asBoolean();
private boolean speechBubbles;
private boolean talkClose = Setting.DEFAULT_TALK_CLOSE.asBoolean();
private final List<String> text = new ArrayList<String>();
public Text() {
super("text");
this.plugin = CitizensAPI.getPlugin();
}
/**
* Adds a piece of text that will be said by the NPC.
*
* @param string
* the text to say
*/
public void add(String string) {
text.add(string);
}
/**
* Edit the text at a given index to a new text.
*
* @param index
* the text's index
* @param newText
* the new text to use
*/
public void edit(int index, String newText) {
text.set(index, newText);
}
/**
* Builds a text editor in game for the supplied {@link Player}.
*/
public Editor getEditor(final Player player) {
final Conversation conversation = new ConversationFactory(plugin).withLocalEcho(false)
.withEscapeSequence("/npc text").withEscapeSequence("exit").withModality(false)
.withFirstPrompt(new TextBasePrompt(this)).buildConversation(player);
return new Editor() {
@Override
public void begin() {
Messaging.sendTr(player, Messages.TEXT_EDITOR_BEGIN);
conversation.begin();
}
@Override
public void end() {
Messaging.sendTr(player, Messages.TEXT_EDITOR_END);
conversation.abandon();
}
};
}
String getPageText(int page) {
Paginator paginator = new Paginator().header("Current Texts");
for (int i = 0; i < text.size(); i++) {
paginator.addLine("<green>" + i + " <gray>- <yellow>" + text.get(i));
}
return paginator.getPageText(page);
}
/**
* @return The list of all texts
*/
public List<String> getTexts() {
return text;
}
/**
* @return whether there is text at a certain index
*/
public boolean hasIndex(int index) {
return index >= 0 && text.size() > index;
}
boolean hasPage(int page) {
return new Paginator(text.size()).hasPage(page);
}
public boolean isRandomTalker() {
return randomTalker;
}
@Override
public void load(DataKey key) throws NPCLoadException {
text.clear();
for (DataKey sub : key.getRelative("text").getIntegerSubKeys()) {
text.add(sub.getString(""));
}
if (text.isEmpty()) {
populateDefaultText();
}
talkClose = key.getBoolean("talk-close", talkClose);
realisticLooker = key.getBoolean("realistic-looking", realisticLooker);
randomTalker = key.getBoolean("random-talker", randomTalker);
range = key.getDouble("range", range);
delay = key.getInt("delay", delay);
speechBubbles = key.getBoolean("speech-bubbles", speechBubbles);
itemInHandPattern = key.getString("talkitem", itemInHandPattern);
}
@EventHandler
private void onRightClick(NPCRightClickEvent event) {
if (!event.getNPC().equals(npc) || text.size() == 0)
return;
String localPattern = itemInHandPattern.equals("default") ? Setting.TALK_ITEM.asString() : itemInHandPattern;
if (Util.matchesItemInHand(event.getClicker(), localPattern) && !shouldTalkClose()) {
talk(event.getClicker());
event.setDelayedCancellation(true);
}
}
private void populateDefaultText() {
text.addAll(Setting.DEFAULT_TEXT.asList());
}
/**
* Remove text at a given index.
*/
public void remove(int index) {
text.remove(index);
}
@Override
public void run() {
if (!npc.isSpawned() || !talkClose || text.size() == 0)
return;
for (Player player : CitizensAPI.getLocationLookup().getNearbyPlayers(npc.getEntity().getLocation(), range)) {
if (player.getGameMode() == GameMode.SPECTATOR)
continue;
talk(player);
}
}
@Override
public void save(DataKey key) {
key.setInt("delay", delay);
key.setBoolean("talk-close", talkClose);
key.setBoolean("random-talker", randomTalker);
key.setBoolean("realistic-looking", realisticLooker);
key.setDouble("range", range);
key.setString("talkitem", itemInHandPattern);
key.setBoolean("speech-bubbles", speechBubbles);
key.removeKey("text");
for (int i = 0; i < text.size(); i++) {
key.setString("text." + String.valueOf(i), text.get(i));
}
}
boolean sendPage(CommandSender player, int page) {
Paginator paginator = new Paginator().header("Current Texts").enablePageSwitcher("/npc text page $page");
for (int i = 0; i < text.size(); i++) {
paginator.addLine(text.get(i) + " <green>(<click:suggest_command:edit " + i
+ " ><yellow>edit</click>) (<hover:show_text:Remove this text><click:run_command:/npc text remove "
+ i + "><red>-</click></hover>)");
}
return paginator.sendPage(player, page);
}
private boolean sendText(Player player) {
if (text.size() == 0)
return false;
int index = 0;
if (randomTalker) {
index = RANDOM.nextInt(text.size());
} else {
if (currentIndex > text.size() - 1) {
currentIndex = 0;
}
index = currentIndex++;
}
if (speechBubbles) {
HologramTrait trait = npc.getOrAddTrait(HologramTrait.class);
trait.addTemporaryLine(Placeholders.replace(text.get(index), player),
Setting.DEFAULT_TEXT_SPEECH_BUBBLE_TICKS.asInt());
} else {
npc.getDefaultSpeechController().speak(new SpeechContext(text.get(index), player));
}
return true;
}
/**
* Set the text delay between messages.
*
* @param delay
* the delay in ticks
*/
public void setDelay(int delay) {
this.delay = delay;
}
/**
* Sets the item in hand pattern required to talk to NPCs, if enabled.
*
* @param pattern
* The new pattern
*/
public void setItemInHandPattern(String pattern) {
itemInHandPattern = pattern;
}
/**
* Set the range in blocks before text will be sent.
*
* @param range
*/
public void setRange(double range) {
this.range = range;
}
/**
* @return Whether talking close is enabled.
*/
public boolean shouldTalkClose() {
return talkClose;
}
private void talk(Player player) {
Long cooldown = cooldowns.get(player.getUniqueId());
if (cooldown != null) {
if (System.currentTimeMillis() < cooldown)
return;
cooldowns.remove(player.getUniqueId());
}
sendText(player);
int delay = this.delay == -1
? Setting.DEFAULT_TEXT_DELAY_MIN.asInt() + Util.getFastRandom()
.nextInt(Setting.DEFAULT_TEXT_DELAY_MAX.asInt() - Setting.DEFAULT_TEXT_DELAY_MIN.asInt())
: this.delay;
if (delay <= 0)
return;
cooldowns.put(player.getUniqueId(), System.currentTimeMillis() + (delay * 50));
}
/**
* Toggles talking at random intervals.
*/
public boolean toggleRandomTalker() {
return (randomTalker = !randomTalker);
}
/**
* Toggles requiring line of sight before talking.
*/
public boolean toggleRealisticLooking() {
return (realisticLooker = !realisticLooker);
}
/**
* Toggles using speech bubbles instead of messages.
*/
public boolean toggleSpeechBubbles() {
return (speechBubbles = !speechBubbles);
}
/**
* Toggles talking to nearby Players.
*/
public boolean toggleTalkClose() {
return (talkClose = !talkClose);
}
public boolean useRealisticLooking() {
return realisticLooker;
}
public boolean useSpeechBubbles() {
return speechBubbles;
}
private static Random RANDOM = Util.getFastRandom();
}