Skip to content

Commit

Permalink
Enable text display by default
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Mar 20, 2023
1 parent eac2d33 commit bdc2264
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions main/src/main/java/net/citizensnpcs/trait/HologramTrait.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Display.Billboard;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.TextDisplay;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.inventory.ItemStack;

Expand Down Expand Up @@ -46,7 +48,7 @@ public class HologramTrait extends Trait {
private NPC nameNPC;
private final NPCRegistry registry = CitizensAPI.createCitizensBackedNPCRegistry(new MemoryNPCDataStore());
private int t;
private boolean useTextDisplay = false;
private boolean useTextDisplay = SUPPORTS_TEXT_DISPLAY;

public HologramTrait() {
super("hologramtrait");
Expand Down Expand Up @@ -91,6 +93,7 @@ private NPC createHologram(String line, double heightOffset) {
NPC hologramNPC = null;
if (useTextDisplay) {
hologramNPC = registry.createNPC(EntityType.TEXT_DISPLAY, line);
hologramNPC.addTrait(new ClickRedirectTrait(npc));
} else {
hologramNPC = registry.createNPC(EntityType.ARMOR_STAND, line);
hologramNPC.getOrAddTrait(ArmorStandTrait.class).setAsHelperEntityWithName(npc);
Expand All @@ -104,6 +107,10 @@ private NPC createHologram(String line, double heightOffset) {
+ (direction == HologramDirection.BOTTOM_UP ? heightOffset : getMaxHeight() - heightOffset),
0));

if (useTextDisplay) {
((TextDisplay) hologramNPC.getEntity()).setBillboard(Billboard.CENTER);
}

Matcher itemMatcher = ITEM_MATCHER.matcher(line);
if (itemMatcher.matches()) {
Material item = SpigotUtil.isUsing1_13API() ? Material.matchMaterial(itemMatcher.group(1), false)
Expand Down Expand Up @@ -141,7 +148,7 @@ private double getEntityHeight() {
private double getHeight(int lineNumber) {
double base = (lastNameplateVisible ? 0 : -getLineHeight());
if (useTextDisplay) {
base += 0.15;
base += 0.27;
}
for (int i = 0; i <= lineNumber; i++) {
HologramLine line = lines.get(i);
Expand Down Expand Up @@ -186,11 +193,6 @@ public Entity getNameEntity() {
return nameNPC != null && nameNPC.isSpawned() ? nameNPC.getEntity() : null;
}

private double getRotationDistance(Location loc) {
return Math.abs(loc.getYaw() - npc.getStoredLocation().getYaw())
+ Math.abs(loc.getPitch() - npc.getStoredLocation().getPitch());
}

@Override
public void load(DataKey root) {
clear();
Expand Down Expand Up @@ -284,8 +286,7 @@ public void run() {

boolean updatePosition = currentLoc.getWorld() != npc.getStoredLocation().getWorld()
|| currentLoc.distance(npc.getStoredLocation()) >= 0.001 || lastNameplateVisible != nameplateVisible
|| Math.abs(lastEntityHeight - getEntityHeight()) >= 0.05
|| (useTextDisplay && getRotationDistance(currentLoc) >= 0.001);
|| Math.abs(lastEntityHeight - getEntityHeight()) >= 0.05;
boolean updateName = false;
if (t++ >= Setting.HOLOGRAM_UPDATE_RATE.asTicks() + Util.getFastRandom().nextInt(3) /* add some jitter */) {
t = 0;
Expand Down Expand Up @@ -396,8 +397,9 @@ public void setLineHeight(double height) {
reloadLineHolograms();
}

public void setUseTextDisplay(boolean useTextDisplay) {
this.useTextDisplay = useTextDisplay;
public void setUseTextDisplay(boolean use) {
this.useTextDisplay = use;
reloadLineHolograms();
}

public enum HologramDirection {
Expand Down Expand Up @@ -448,4 +450,12 @@ public void spawnNPC(double height) {
}

private static final Pattern ITEM_MATCHER = Pattern.compile("<item:(.*?)([:].*?)?>");
private static boolean SUPPORTS_TEXT_DISPLAY = false;
static {
try {
EntityType.valueOf("TEXT_DISPLAY");
SUPPORTS_TEXT_DISPLAY = true;
} catch (IllegalArgumentException iae) {
}
}
}

0 comments on commit bdc2264

Please sign in to comment.