Skip to content

Commit

Permalink
Add temporary hologram lines
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Sep 3, 2022
1 parent 6270985 commit 61b71cb
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 35 deletions.
2 changes: 1 addition & 1 deletion main/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
<relocations>
<relocation>
<pattern>gnu.trove</pattern>
<shadedPattern>lib.trove</shadedPattern>
<shadedPattern>clib.trove</shadedPattern>
</relocation>
<relocation>
<pattern>org.bstats</pattern>
Expand Down
71 changes: 60 additions & 11 deletions main/src/main/java/net/citizensnpcs/trait/HologramTrait.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.citizensnpcs.trait;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -26,6 +25,7 @@
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.Colorizer;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.Placeholders;
import net.citizensnpcs.api.util.SpigotUtil;
Expand All @@ -44,8 +44,7 @@ public class HologramTrait extends Trait {
@Persist
private double lineHeight = -1;
private final List<NPC> lineHolograms = Lists.newArrayList();
@Persist
private final List<String> lines = Lists.newArrayList();
private final List<HologramLine> lines = Lists.newArrayList();
private NPC nameNPC;
private final NPCRegistry registry = CitizensAPI.createCitizensBackedNPCRegistry(new MemoryNPCDataStore());

Expand All @@ -60,10 +59,23 @@ public HologramTrait() {
* The new line to add
*/
public void addLine(String text) {
lines.add(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));
}

/**
* Clears all hologram lines
*/
Expand Down Expand Up @@ -133,7 +145,7 @@ public double getLineHeight() {
* @return the hologram lines, in bottom-up order
*/
public List<String> getLines() {
return Collections.unmodifiableList(lines);
return Lists.transform(lines, (l) -> l.text);
}

private double getMaxHeight() {
Expand All @@ -147,6 +159,14 @@ public ArmorStand getNameEntity() {
return nameNPC != null && nameNPC.isSpawned() ? ((ArmorStand) nameNPC.getEntity()) : null;
}

@Override
public void load(DataKey root) {
lines.clear();
for (DataKey key : root.getRelative("lines").getIntegerSubKeys()) {
lines.add(new HologramLine(key.getString(""), true));
}
}

@Override
public void onDespawn() {
if (nameNPC != null) {
Expand Down Expand Up @@ -176,8 +196,7 @@ public void onSpawn() {
}

for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
lineHolograms.add(createHologram(Placeholders.replace(line, null, npc), getHeight(i)));
lineHolograms.add(createHologram(Placeholders.replace(lines.get(i).text, null, npc), getHeight(i)));
}
}

Expand All @@ -189,9 +208,9 @@ private void reloadLineHolograms() {

if (!npc.isSpawned())
return;

for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
lineHolograms.add(createHologram(Placeholders.replace(line, null, npc), getHeight(i)));
lineHolograms.add(createHologram(Placeholders.replace(lines.get(i).text, null, npc), getHeight(i)));
}
}

Expand Down Expand Up @@ -264,7 +283,12 @@ public void run() {
break;
}

String text = lines.get(i);
HologramLine line = lines.get(i);
if (line.ticks > 0 && --line.ticks == 0) {
lines.remove(i--);
continue;
}
String text = line.text;
if (ITEM_MATCHER.matcher(text).matches()) {
text = null;
}
Expand All @@ -279,6 +303,18 @@ public void run() {
}
}

@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
Expand All @@ -304,7 +340,7 @@ public void setLine(int idx, String text) {
return;
}

lines.set(idx, text);
lines.get(idx).text = text;
if (idx < lineHolograms.size()) {
lineHolograms.get(idx).setName(Placeholders.replace(text, null, npc));
return;
Expand All @@ -331,5 +367,18 @@ public enum HologramDirection {
TOP_DOWN;
}

private class HologramLine {
boolean persist;
String text;
public int ticks;

public HologramLine(String text, boolean persist) {
this(text, persist, -1);
}

public HologramLine(String text, boolean persist, int ticks) {
}
}

private static final Pattern ITEM_MATCHER = Pattern.compile("<item:(.*?)>");
}
27 changes: 4 additions & 23 deletions main/src/main/java/net/citizensnpcs/trait/text/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class Text extends Trait implements Runnable, Listener, ConversationAband
private double range = Setting.DEFAULT_TALK_CLOSE_RANGE.asDouble();
private boolean realisticLooker = Setting.DEFAULT_REALISTIC_LOOKING.asBoolean();
private boolean speechBubbles;
private int speechIndex = -1;
private final int speechIndex = -1;
private boolean talkClose = Setting.DEFAULT_TALK_CLOSE.asBoolean();
private final List<String> text = new ArrayList<String>();

Expand All @@ -71,14 +71,6 @@ public void add(String string) {
text.add(string);
}

private void clearBubble() {
if (speechIndex < npc.getOrAddTrait(HologramTrait.class).getLines().size()) {
npc.getOrAddTrait(HologramTrait.class).removeLine(speechIndex);
speechIndex = -1;
}
bubbleTicks = 0;
}

@Override
public void conversationAbandoned(ConversationAbandonedEvent event) {
}
Expand Down Expand Up @@ -193,12 +185,9 @@ public void run() {
if (!npc.isSpawned())
return;

if (bubbleTicks > 0 && --bubbleTicks == 0) {
clearBubble();
}

if (!talkClose)
return;

List<Entity> nearby = npc.getEntity().getNearbyEntities(range, range, range);
for (Entity search : nearby) {
if (!(search instanceof Player) || ((Player) search).getGameMode() == GameMode.SPECTATOR)
Expand Down Expand Up @@ -264,13 +253,8 @@ private boolean sendText(Player player) {

if (speechBubbles) {
HologramTrait trait = npc.getOrAddTrait(HologramTrait.class);
if (speechIndex == -1) {
speechIndex = trait.getLines().size();
trait.addLine(Placeholders.replace(text.get(index), player));
bubbleTicks = Setting.DEFAULT_TEXT_SPEECH_BUBBLE_TICKS.asInt();
} else if (speechIndex < trait.getLines().size()) {
trait.setLine(speechIndex, Placeholders.replace(text.get(index), player));
}
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));
}
Expand Down Expand Up @@ -336,9 +320,6 @@ public boolean toggleRealisticLooking() {
* Toggles using speech bubbles instead of messages.
*/
public boolean toggleSpeechBubbles() {
if (speechBubbles) {
clearBubble();
}
return (speechBubbles = !speechBubbles);
}

Expand Down

0 comments on commit 61b71cb

Please sign in to comment.