Skip to content

Commit

Permalink
Further bugfixes for item holograms
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Apr 15, 2024
1 parent fe5a088 commit b0ba7b2
Showing 1 changed file with 64 additions and 5 deletions.
69 changes: 64 additions & 5 deletions main/src/main/java/net/citizensnpcs/trait/HologramTrait.java
Expand Up @@ -17,6 +17,7 @@
import org.bukkit.entity.Display.Billboard;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ItemDisplay;
import org.bukkit.entity.Player;
import org.bukkit.entity.TextDisplay;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
Expand Down Expand Up @@ -194,7 +195,6 @@ private void reloadLineHolograms() {
nameLine.removeNPC();
nameLine = null;
}
currentLoc = null;
}

/**
Expand Down Expand Up @@ -261,7 +261,7 @@ public void run() {
lines.remove(i--).removeNPC();
continue;
}
if (updatePosition) {
if (updatePosition || line.renderer.getEntity() == null) {
offset.y = getHeight(i);
line.render(offset);
}
Expand Down Expand Up @@ -425,7 +425,7 @@ public void render(Vector3d vector3d) {

public void setText(String text) {
this.text = text == null ? "" : text;
if (ITEM_MATCHER.matcher(text).find()) {
if (ITEM_MATCHER.matcher(text).find() && !(renderer instanceof ItemRenderer)) {
renderer.destroy();
mb = 0.21;
mt = 0.07;
Expand Down Expand Up @@ -480,15 +480,15 @@ public void render0(NPC npc, Vector3d offset) {
}
}

public class ItemRenderer extends SingleEntityHologramRenderer {
public class ItemDisplayRenderer extends SingleEntityHologramRenderer {
@Override
protected NPC createNPC(Entity base, String name, Vector3d offset) {
Matcher itemMatcher = ITEM_MATCHER.matcher(name);
itemMatcher.find();
Material item = SpigotUtil.isUsing1_13API() ? Material.matchMaterial(itemMatcher.group(1), false)
: Material.matchMaterial(itemMatcher.group(1));
ItemStack itemStack = new ItemStack(item, 1);
NPC npc = registry.createNPCUsingItem(EntityType.DROPPED_ITEM, "", itemStack);
NPC npc = registry.createNPCUsingItem(EntityType.ITEM_DISPLAY, "", itemStack);
npc.data().setPersistent(NPC.Metadata.NAMEPLATE_VISIBLE, false);
if (itemMatcher.group(2) != null) {
if (itemMatcher.group(2).charAt(1) == '{') {
Expand All @@ -502,12 +502,71 @@ protected NPC createNPC(Entity base, String name, Vector3d offset) {
return npc;
}

@Override
public void render0(NPC base, Vector3d offset) {
ItemDisplay disp = (ItemDisplay) hologram.getEntity();
Transformation tf = disp.getTransformation();
tf.getTranslation().y = (float) offset.y + 0.1f;
disp.setTransformation(tf);
if (hologram.getEntity().getVehicle() == null) {
base.getEntity().addPassenger(hologram.getEntity());
}
}

@Override
public void updateText(NPC npc, String text) {
this.text = Placeholders.replace(text, null, npc);
}
}

public class ItemRenderer extends SingleEntityHologramRenderer {
private NPC itemNPC;

@Override
protected NPC createNPC(Entity base, String name, Vector3d offset) {
NPC mount = registry.createNPC(EntityType.ARMOR_STAND, "");
mount.getOrAddTrait(ArmorStandTrait.class).setAsPointEntity();
Matcher itemMatcher = ITEM_MATCHER.matcher(name);
itemMatcher.find();
Material item = SpigotUtil.isUsing1_13API() ? Material.matchMaterial(itemMatcher.group(1), false)
: Material.matchMaterial(itemMatcher.group(1));
ItemStack itemStack = new ItemStack(item, 1);
itemNPC = registry.createNPCUsingItem(EntityType.DROPPED_ITEM, "", itemStack);
itemNPC.data().setPersistent(NPC.Metadata.NAMEPLATE_VISIBLE, false);
if (itemMatcher.group(2) != null) {
if (itemMatcher.group(2).charAt(1) == '{') {
Bukkit.getUnsafe().modifyItemStack(itemStack, itemMatcher.group(2).substring(1));
itemNPC.setItemProvider(() -> itemStack);
} else {
itemNPC.getOrAddTrait(ScoreboardTrait.class)
.setColor(Util.matchEnum(ChatColor.values(), itemMatcher.group(2).substring(1)));
}
}
itemNPC.spawn(base.getLocation());
itemNPC.getOrAddTrait(MountTrait.class).setMountedOn(mount.getUniqueId());
return mount;
}

@Override
public void destroy() {
super.destroy();
if (itemNPC == null)
return;
itemNPC.destroy();
itemNPC = null;
}

@Override
protected void render0(NPC npc, Vector3d offset) {
hologram.getEntity().teleport(
npc.getStoredLocation().clone().add(offset.x, offset.y + getEntityBbHeight(), offset.z),
TeleportCause.PLUGIN);
}

@Override
public void updateText(NPC npc, String text) {
this.text = Placeholders.replace(text, null, npc);
}
}

public abstract class SingleEntityHologramRenderer implements HologramRenderer {
Expand Down

0 comments on commit b0ba7b2

Please sign in to comment.