/
HologramTrait.java
409 lines (350 loc) · 12.2 KB
/
HologramTrait.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
package net.citizensnpcs.trait;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.EntityType;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.inventory.ItemStack;
import com.google.common.collect.Lists;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.MemoryNPCDataStore;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
import net.citizensnpcs.api.persistence.Persist;
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.Placeholders;
import net.citizensnpcs.api.util.SpigotUtil;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;
/**
* Persists a hologram attached to the NPC.
*/
@TraitName("hologramtrait")
public class HologramTrait extends Trait {
private Location currentLoc;
@Persist
private HologramDirection direction = HologramDirection.BOTTOM_UP;
private double lastEntityHeight = 0;
private boolean lastNameplateVisible;
@Persist
private double lineHeight = -1;
private final List<HologramLine> lines = Lists.newArrayList();
private NPC nameNPC;
private final NPCRegistry registry = CitizensAPI.createCitizensBackedNPCRegistry(new MemoryNPCDataStore());
public HologramTrait() {
super("hologramtrait");
}
/**
* Adds a new hologram line which will displayed over an NPC's head.
*
* @param text
* The new line to add
*/
public void addLine(String text) {
lines.add(new HologramLine(text, true));
reloadLineHolograms();
}
/**
* Adds a new hologram line which will displayed over an NPC's head. It will not persist to disk and will last for
* the specified amount of ticks.
*
* @param text
* The new line to add
* @param ticks
* The number of ticks to last for
*/
public void addTemporaryLine(String text, int ticks) {
lines.add(new HologramLine(text, false, ticks));
reloadLineHolograms();
}
/**
* Clears all hologram lines
*/
public void clear() {
for (HologramLine line : lines) {
line.removeNPC();
}
lines.clear();
}
private NPC createHologram(String line, double heightOffset) {
NPC hologramNPC = registry.createNPC(EntityType.ARMOR_STAND, line);
hologramNPC.getOrAddTrait(ArmorStandTrait.class).setAsHelperEntityWithName(npc);
hologramNPC.spawn(currentLoc.clone().add(0,
getEntityHeight()
+ (direction == HologramDirection.BOTTOM_UP ? heightOffset : getMaxHeight() - heightOffset),
0));
Matcher itemMatcher = ITEM_MATCHER.matcher(line);
if (itemMatcher.matches()) {
Material item = SpigotUtil.isUsing1_13API() ? Material.matchMaterial(itemMatcher.group(1), false)
: Material.matchMaterial(itemMatcher.group(1));
NPC itemNPC = registry.createNPCUsingItem(EntityType.DROPPED_ITEM, "", new ItemStack(item, 1));
if (itemMatcher.groupCount() > 1) {
itemNPC.getOrAddTrait(ScoreboardTrait.class)
.setColor(Util.matchEnum(ChatColor.values(), itemMatcher.group(2)));
}
itemNPC.spawn(currentLoc);
((ArmorStand) hologramNPC.getEntity()).addPassenger(itemNPC.getEntity());
itemNPC.addRunnable(new Runnable() {
@Override
public void run() {
if (!itemNPC.isSpawned() || !itemNPC.getEntity().isInsideVehicle()) {
itemNPC.destroy();
}
}
});
}
lastEntityHeight = getEntityHeight();
return hologramNPC;
}
/**
* @return The direction that hologram lines are displayed in
*/
public HologramDirection getDirection() {
return direction;
}
private double getEntityHeight() {
return NMS.getHeight(npc.getEntity());
}
private double getHeight(int lineNumber) {
return getLineHeight() * (lastNameplateVisible ? lineNumber + 1 : lineNumber);
}
/**
* Note: this is implementation-specific and may be removed at a later date.
*/
public Collection<ArmorStand> getHologramEntities() {
return lines.stream().filter(l -> l.hologram != null && l.hologram.getEntity() != null)
.map(l -> (ArmorStand) l.hologram.getEntity()).collect(Collectors.toList());
}
/**
* @return The line height between each hologram line, in blocks
*/
public double getLineHeight() {
return lineHeight == -1 ? Setting.DEFAULT_NPC_HOLOGRAM_LINE_HEIGHT.asDouble() : lineHeight;
}
/**
* @return the hologram lines, in bottom-up order
*/
public List<String> getLines() {
return Lists.transform(lines, (l) -> l.text);
}
private double getMaxHeight() {
return getLineHeight() * (lines.size() + (lastNameplateVisible ? 1 : -1));
}
/**
* Note: this is implementation-specific and may be removed at a later date.
*/
public ArmorStand getNameEntity() {
return nameNPC != null && nameNPC.isSpawned() ? ((ArmorStand) nameNPC.getEntity()) : null;
}
@Override
public void load(DataKey root) {
clear();
for (DataKey key : root.getRelative("lines").getIntegerSubKeys()) {
lines.add(new HologramLine(key.getString(""), true));
}
}
@Override
public void onDespawn() {
if (nameNPC != null) {
nameNPC.destroy();
nameNPC = null;
}
for (HologramLine line : lines) {
line.removeNPC();
}
}
@Override
public void onRemove() {
onDespawn();
}
@Override
public void onSpawn() {
if (!npc.isSpawned())
return;
lastNameplateVisible = Boolean
.parseBoolean(npc.data().<Object> get(NPC.Metadata.NAMEPLATE_VISIBLE, true).toString());
currentLoc = npc.getStoredLocation();
if (npc.requiresNameHologram() && lastNameplateVisible) {
nameNPC = createHologram(Placeholders.replace(npc.getRawName(), null, npc), 0);
}
for (int i = 0; i < lines.size(); i++) {
lines.get(i).spawnNPC(getHeight(i));
}
}
private void reloadLineHolograms() {
for (HologramLine line : lines) {
line.removeNPC();
}
if (!npc.isSpawned())
return;
for (int i = 0; i < lines.size(); i++) {
lines.get(i).spawnNPC(getHeight(i));
}
}
/**
* Removes the line at the specified index
*
* @param idx
*/
public void removeLine(int idx) {
if (idx < 0 || idx >= lines.size())
return;
lines.remove(idx).removeNPC();
reloadLineHolograms();
}
@Override
public void run() {
if (!npc.isSpawned()) {
onDespawn();
return;
}
if (currentLoc == null) {
currentLoc = npc.getStoredLocation();
}
boolean nameplateVisible = Boolean
.parseBoolean(npc.data().<Object> get(NPC.Metadata.NAMEPLATE_VISIBLE, true).toString());
if (npc.requiresNameHologram()) {
if (nameNPC != null && !nameplateVisible) {
nameNPC.destroy();
nameNPC = null;
} else if (nameNPC == null && nameplateVisible) {
nameNPC = createHologram(Placeholders.replace(npc.getRawName(), null, npc), 0);
}
}
boolean updatePosition = currentLoc.getWorld() != npc.getStoredLocation().getWorld()
|| currentLoc.distance(npc.getStoredLocation()) >= 0.001 || lastNameplateVisible != nameplateVisible
|| Math.abs(lastEntityHeight - getEntityHeight()) >= 0.05;
lastNameplateVisible = nameplateVisible;
if (updatePosition) {
currentLoc = npc.getStoredLocation();
lastEntityHeight = getEntityHeight();
}
if (nameNPC != null && nameNPC.isSpawned()) {
if (updatePosition) {
nameNPC.teleport(currentLoc.clone().add(0, getEntityHeight(), 0), TeleportCause.PLUGIN);
}
nameNPC.setName(Placeholders.replace(npc.getRawName(), null, npc));
}
for (int i = 0; i < lines.size(); i++) {
HologramLine line = lines.get(i);
NPC hologramNPC = line.hologram;
if (hologramNPC == null || !hologramNPC.isSpawned())
continue;
if (updatePosition) {
hologramNPC.teleport(currentLoc.clone().add(0, lastEntityHeight
+ (direction == HologramDirection.BOTTOM_UP ? getHeight(i) : getMaxHeight() - getHeight(i)), 0),
TeleportCause.PLUGIN);
}
if (line.ticks > 0 && --line.ticks == 0) {
line.removeNPC();
lines.remove(i--);
continue;
}
String text = line.text;
if (ITEM_MATCHER.matcher(text).matches()) {
text = null;
}
if (text != null && !ChatColor.stripColor(Messaging.parseComponents(text)).isEmpty()) {
hologramNPC.setName(Placeholders.replace(text, null, npc));
hologramNPC.data().set(NPC.Metadata.NAMEPLATE_VISIBLE, true);
} else {
hologramNPC.setName("");
hologramNPC.data().set(NPC.Metadata.NAMEPLATE_VISIBLE, "hover");
}
}
}
@Override
public void save(DataKey root) {
root.removeKey("lines");
int i = 0;
for (HologramLine line : lines) {
if (!line.persist)
continue;
root.setString("lines." + i, line.text);
i++;
}
}
/**
* @see #getDirection()
* @param direction
* The new direction
*/
public void setDirection(HologramDirection direction) {
this.direction = direction;
reloadLineHolograms();
}
/**
* Sets the hologram line at a specific index
*
* @param idx
* The index
* @param text
* The new line
*/
public void setLine(int idx, String text) {
if (idx == lines.size()) {
addLine(text);
return;
}
HologramLine line = lines.get(idx);
line.setText(text);
if (line.hologram == null) {
reloadLineHolograms();
}
}
/**
* Sets the line height
*
* @see #getLineHeight()
* @param height
* The line height in blocks
*/
public void setLineHeight(double height) {
lineHeight = height;
reloadLineHolograms();
}
public enum HologramDirection {
BOTTOM_UP,
TOP_DOWN;
}
private class HologramLine {
NPC hologram;
boolean persist;
String text;
int ticks;
public HologramLine(String text, boolean persist) {
this(text, persist, -1);
}
public HologramLine(String text, boolean persist, int ticks) {
this.text = text;
this.persist = persist;
this.ticks = ticks;
}
public void removeNPC() {
if (hologram == null)
return;
hologram.destroy();
hologram = null;
}
public void setText(String text) {
this.text = text;
if (hologram != null) {
hologram.setName(Placeholders.replace(text, null, npc));
}
}
public void spawnNPC(double height) {
this.hologram = createHologram(Placeholders.replace(text, null, npc), height);
}
}
private static final Pattern ITEM_MATCHER = Pattern.compile("<item:(.*?)>|<item:(.*?):(.*?)>");
}