Skip to content

Commit

Permalink
Use holograms instead of scoreboard for names
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jul 6, 2020
1 parent d333c98 commit 0a58215
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 122 deletions.
16 changes: 13 additions & 3 deletions main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java
Expand Up @@ -31,6 +31,7 @@
import net.citizensnpcs.api.event.SpawnReason;
import net.citizensnpcs.api.npc.AbstractNPC;
import net.citizensnpcs.api.npc.BlockBreaker;
import net.citizensnpcs.api.npc.MemoryNPCDataStore;
import net.citizensnpcs.api.npc.BlockBreaker.BlockBreakerConfiguration;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
Expand All @@ -43,6 +44,7 @@
import net.citizensnpcs.npc.skin.SkinnableEntity;
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.Gravity;
import net.citizensnpcs.trait.HologramTrait;
import net.citizensnpcs.trait.ScoreboardTrait;
import net.citizensnpcs.util.ChunkCoord;
import net.citizensnpcs.util.Messages;
Expand All @@ -53,6 +55,8 @@
public class CitizensNPC extends AbstractNPC {
private ChunkCoord cachedCoord;
private EntityController entityController;
private final NPC nameHologram = null;
private final NPCRegistry registry = CitizensAPI.createAnonymousNPCRegistry(new MemoryNPCDataStore());
private final CitizensNavigator navigator = new CitizensNavigator(this);
private int updateCounter = 0;

Expand Down Expand Up @@ -267,6 +271,12 @@ public boolean spawn(Location at, SpawnReason reason) {
NMS.setHeadYaw(getEntity(), at.getYaw());
NMS.setBodyYaw(getEntity(), at.getYaw());

if (requiresNameHologram() && !hasTrait(HologramTrait.class)) {
addTrait(HologramTrait.class);
}
String nameplateVisible = data().<Object> get(NPC.NAMEPLATE_VISIBLE_METADATA, true).toString();
getEntity().setCustomNameVisible(Boolean.parseBoolean(nameplateVisible));

// Set the spawned state
getTrait(CurrentLocation.class).setLocation(at);
getTrait(Spawned.class).setSpawned(true);
Expand Down Expand Up @@ -362,10 +372,10 @@ public void update() {
updateCounter = 0;
}

if (isLiving) {
String nameplateVisible = data().<Object> get(NPC.NAMEPLATE_VISIBLE_METADATA, true).toString();
((LivingEntity) getEntity()).setCustomNameVisible(Boolean.parseBoolean(nameplateVisible));
String nameplateVisible = data().<Object> get(NPC.NAMEPLATE_VISIBLE_METADATA, true).toString();
getEntity().setCustomNameVisible(Boolean.parseBoolean(nameplateVisible));

if (isLiving) {
NMS.setKnockbackResistance((LivingEntity) getEntity(),
data().get(NPC.DEFAULT_PROTECTED_METADATA, true) ? 1D : 0D);
}
Expand Down
46 changes: 31 additions & 15 deletions main/src/main/java/net/citizensnpcs/trait/HologramTrait.java
Expand Up @@ -3,7 +3,6 @@
import java.util.List;

import org.bukkit.Location;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.EntityType;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;

Expand Down Expand Up @@ -31,6 +30,7 @@ public class HologramTrait extends Trait {
private double lineHeight = -1;
@Persist
private final List<String> lines = Lists.newArrayList();
private NPC nameNPC;
private final NPCRegistry registry = CitizensAPI.createAnonymousNPCRegistry(new MemoryNPCDataStore());

public HologramTrait() {
Expand All @@ -43,6 +43,20 @@ public void addLine(String text) {
load();
}

private NPC createHologram(String line, double heightOffset) {
NPC hologramNPC = registry.createNPC(EntityType.ARMOR_STAND, line);
ArmorStandTrait trait = hologramNPC.getTrait(ArmorStandTrait.class);
trait.setVisible(false);
trait.setSmall(true);
trait.setMarker(true);
trait.setGravity(false);
trait.setHasArms(false);
trait.setHasBaseplate(false);
hologramNPC.spawn(currentLoc.clone().add(0, getEntityHeight() + heightOffset, 0));
hologramNPC.getEntity().setInvulnerable(true);
return hologramNPC;
}

private double getEntityHeight() {
if (SUPPORT_GET_HEIGHT) {
try {
Expand All @@ -63,20 +77,13 @@ public List<String> getLines() {
}

private void load() {
int i = 0;
currentLoc = npc.getStoredLocation();
int i = 0;
if (npc.requiresNameHologram()) {
nameNPC = createHologram(npc.getFullName(), 0);
}
for (String line : lines) {
NPC hologramNPC = registry.createNPC(EntityType.ARMOR_STAND, Placeholders.replace(line, null, npc));
ArmorStandTrait trait = hologramNPC.getTrait(ArmorStandTrait.class);
trait.setVisible(false);
trait.setSmall(true);
trait.setMarker(true);
trait.setGravity(false);
trait.setHasArms(false);
trait.setHasBaseplate(false);
hologramNPC.spawn(currentLoc.clone().add(0, getEntityHeight() + getHeight(i), 0));
hologramNPC.getEntity().setInvulnerable(true);
hologramNPCs.add(hologramNPC);
hologramNPCs.add(createHologram(Placeholders.replace(line, null, npc), getHeight(i)));
i++;
}
}
Expand Down Expand Up @@ -112,10 +119,15 @@ public void run() {
if (update) {
currentLoc = npc.getStoredLocation();
}
if (nameNPC != null && nameNPC.isSpawned()) {
if (update) {
nameNPC.teleport(currentLoc.clone().add(0, getEntityHeight(), 0), TeleportCause.PLUGIN);
}
nameNPC.setName(npc.getFullName());
}
for (int i = 0; i < hologramNPCs.size(); i++) {
NPC hologramNPC = hologramNPCs.get(i);
ArmorStand hologram = (ArmorStand) hologramNPC.getEntity();
if (hologram == null)
if (!hologramNPC.isSpawned())
continue;
if (update) {
hologramNPC.teleport(currentLoc.clone().add(0, getEntityHeight() + getHeight(i), 0),
Expand All @@ -138,6 +150,10 @@ public void setLineHeight(double height) {
}

private void unload() {
if (nameNPC != null) {
nameNPC.destroy();
nameNPC = null;
}
if (hologramNPCs.isEmpty())
return;
for (NPC npc : hologramNPCs) {
Expand Down
Expand Up @@ -2,7 +2,6 @@

import java.util.UUID;

import net.citizensnpcs.util.Util;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_10_R1.CraftWorld;
Expand All @@ -16,11 +15,11 @@
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Colorizer;
import net.citizensnpcs.npc.AbstractEntityController;
import net.citizensnpcs.npc.skin.Skin;
import net.citizensnpcs.npc.skin.SkinnableEntity;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;
import net.minecraft.server.v1_10_R1.PlayerInteractManager;
import net.minecraft.server.v1_10_R1.WorldServer;

Expand All @@ -32,12 +31,9 @@ public HumanController() {
@Override
protected Entity createEntity(final Location at, final NPC npc) {
final WorldServer nmsWorld = ((CraftWorld) at.getWorld()).getHandle();
String coloredName = Colorizer.parseColors(npc.getFullName());

String[] nameSplit = Util.splitPlayerName(coloredName);
String name = nameSplit[0];
String coloredName = npc.getFullName();

final String prefixCapture = nameSplit[1], suffixCapture = nameSplit[2];
String name = coloredName.length() > 16 ? coloredName.substring(0, 16) : coloredName;

UUID uuid = npc.getUniqueId();
if (uuid.version() == 4) { // clear version
Expand Down Expand Up @@ -76,12 +72,6 @@ public void run() {
team = scoreboard.registerNewTeam(teamName);
mode = 0;
}
if (prefixCapture != null) {
team.setPrefix(prefixCapture);
}
if (suffixCapture != null) {
team.setSuffix(suffixCapture);
}
team.addPlayer(handle.getBukkitEntity());

handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
Expand Down
Expand Up @@ -2,7 +2,6 @@

import java.util.UUID;

import net.citizensnpcs.util.Util;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_11_R1.CraftWorld;
Expand All @@ -16,11 +15,11 @@
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Colorizer;
import net.citizensnpcs.npc.AbstractEntityController;
import net.citizensnpcs.npc.skin.Skin;
import net.citizensnpcs.npc.skin.SkinnableEntity;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;
import net.minecraft.server.v1_11_R1.PlayerInteractManager;
import net.minecraft.server.v1_11_R1.WorldServer;

Expand All @@ -32,12 +31,9 @@ public HumanController() {
@Override
protected Entity createEntity(final Location at, final NPC npc) {
final WorldServer nmsWorld = ((CraftWorld) at.getWorld()).getHandle();
String coloredName = Colorizer.parseColors(npc.getFullName());

String[] nameSplit = Util.splitPlayerName(coloredName);
String name = nameSplit[0];
String coloredName = npc.getFullName();

final String prefixCapture = nameSplit[1], suffixCapture = nameSplit[2];
String name = coloredName.length() > 16 ? coloredName.substring(0, 16) : coloredName;

UUID uuid = npc.getUniqueId();
if (uuid.version() == 4) { // clear version
Expand Down Expand Up @@ -76,12 +72,6 @@ public void run() {
team = scoreboard.registerNewTeam(teamName);
mode = 0;
}
if (prefixCapture != null) {
team.setPrefix(prefixCapture);
}
if (suffixCapture != null) {
team.setSuffix(suffixCapture);
}
team.addPlayer(handle.getBukkitEntity());

handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
Expand Down
Expand Up @@ -15,7 +15,6 @@
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Colorizer;
import net.citizensnpcs.npc.AbstractEntityController;
import net.citizensnpcs.npc.skin.Skin;
import net.citizensnpcs.npc.skin.SkinnableEntity;
Expand All @@ -32,12 +31,9 @@ public HumanController() {
@Override
protected Entity createEntity(final Location at, final NPC npc) {
final WorldServer nmsWorld = ((CraftWorld) at.getWorld()).getHandle();
String coloredName = Colorizer.parseColors(npc.getFullName());
String coloredName = npc.getFullName();

String[] nameSplit = Util.splitPlayerName(coloredName);
String name = nameSplit[0];

final String prefixCapture = nameSplit[1], suffixCapture = nameSplit[2];
String name = coloredName.length() > 16 ? coloredName.substring(0, 16) : coloredName;

UUID uuid = npc.getUniqueId();
if (uuid.version() == 4) { // clear version
Expand Down Expand Up @@ -76,12 +72,6 @@ public void run() {
team = scoreboard.registerNewTeam(teamName);
mode = 0;
}
if (prefixCapture != null) {
team.setPrefix(prefixCapture);
}
if (suffixCapture != null) {
team.setSuffix(suffixCapture);
}
team.addPlayer(handle.getBukkitEntity());

handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
Expand Down
Expand Up @@ -15,7 +15,6 @@
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Colorizer;
import net.citizensnpcs.npc.AbstractEntityController;
import net.citizensnpcs.npc.skin.Skin;
import net.citizensnpcs.npc.skin.SkinnableEntity;
Expand All @@ -32,12 +31,8 @@ public HumanController() {
@Override
protected Entity createEntity(final Location at, final NPC npc) {
final WorldServer nmsWorld = ((CraftWorld) at.getWorld()).getHandle();
String coloredName = Colorizer.parseColors(npc.getFullName());

String[] nameSplit = Util.splitPlayerName(coloredName);
String name = nameSplit[0];

final String prefixCapture = nameSplit[1], suffixCapture = nameSplit[2];
String coloredName = npc.getFullName();
String name = coloredName.length() > 16 ? coloredName.substring(0, 16) : coloredName;

UUID uuid = npc.getUniqueId();
if (uuid.version() == 4) { // clear version
Expand Down Expand Up @@ -76,12 +71,6 @@ public void run() {
team = scoreboard.registerNewTeam(teamName);
mode = 0;
}
if (prefixCapture != null) {
team.setPrefix(prefixCapture);
}
if (suffixCapture != null) {
team.setSuffix(suffixCapture);
}
team.addPlayer(handle.getBukkitEntity());

handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
Expand Down
Expand Up @@ -15,7 +15,6 @@
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Colorizer;
import net.citizensnpcs.npc.AbstractEntityController;
import net.citizensnpcs.npc.skin.Skin;
import net.citizensnpcs.npc.skin.SkinnableEntity;
Expand All @@ -32,12 +31,9 @@ public HumanController() {
@Override
protected Entity createEntity(final Location at, final NPC npc) {
final WorldServer nmsWorld = ((CraftWorld) at.getWorld()).getHandle();
String coloredName = Colorizer.parseColors(npc.getFullName());
String coloredName = npc.getFullName();

String[] nameSplit = Util.splitPlayerName(coloredName);
String name = nameSplit[0];

final String prefixCapture = nameSplit[1], suffixCapture = nameSplit[2];
String name = coloredName.length() > 16 ? coloredName.substring(0, 16) : coloredName;

UUID uuid = npc.getUniqueId();
if (uuid.version() == 4) { // clear version
Expand Down Expand Up @@ -76,12 +72,6 @@ public void run() {
team = scoreboard.registerNewTeam(teamName);
mode = 0;
}
if (prefixCapture != null) {
team.setPrefix(prefixCapture);
}
if (suffixCapture != null) {
team.setSuffix(suffixCapture);
}
team.addPlayer(handle.getBukkitEntity());

handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
Expand Down
Expand Up @@ -15,7 +15,6 @@
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Colorizer;
import net.citizensnpcs.npc.AbstractEntityController;
import net.citizensnpcs.npc.skin.Skin;
import net.citizensnpcs.npc.skin.SkinnableEntity;
Expand All @@ -32,12 +31,8 @@ public HumanController() {
@Override
protected Entity createEntity(final Location at, final NPC npc) {
final WorldServer nmsWorld = ((CraftWorld) at.getWorld()).getHandle();
String coloredName = Colorizer.parseColors(npc.getFullName());

String[] nameSplit = Util.splitPlayerName(coloredName);
String name = nameSplit[0];

final String prefixCapture = nameSplit[1], suffixCapture = nameSplit[2];
String coloredName = npc.getFullName();
String name = coloredName.length() > 16 ? coloredName.substring(0, 16) : coloredName;

UUID uuid = npc.getUniqueId();
if (uuid.version() == 4) { // clear version
Expand Down Expand Up @@ -76,12 +71,6 @@ public void run() {
team = scoreboard.registerNewTeam(teamName);
mode = 0;
}
if (prefixCapture != null) {
team.setPrefix(prefixCapture);
}
if (suffixCapture != null) {
team.setSuffix(suffixCapture);
}
team.addPlayer(handle.getBukkitEntity());

handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
Expand Down

0 comments on commit 0a58215

Please sign in to comment.