Skip to content

Commit ee500f4

Browse files
authored
Merge pull request #71 from InstantlyMoist/fix/hologram-older-client
did whatever to fix older client holos
2 parents 4052564 + 3a6b4fd commit ee500f4

4 files changed

Lines changed: 49 additions & 31 deletions

File tree

api/src/main/java/me/tofaa/entitylib/wrapper/hologram/LegacyHologram.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ public void teleport(Location location) {
162162
Location l = new Location(location.getX(), y, location.getZ(), location.getYaw(), location.getPitch());
163163
line.teleport(l, false);
164164
}
165+
165166
}
166167

167168
@Override

api/src/main/java/me/tofaa/entitylib/wrapper/hologram/ModernHologram.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,21 @@ public void hide() {
5050
spawned = false;
5151
}
5252

53+
private static final float LINE_SPACING = 0.28f;
54+
5355
@Override
5456
public void teleport(Location location) {
5557
this.location = location;
5658
if (lines.isEmpty()) return;
57-
59+
5860
if (parent != null) {
5961
return;
6062
}
61-
62-
WrapperEntity first = lines.get(0);
63-
first.teleport(location);
64-
for (WrapperEntity e : lines) {
65-
if (e.getUuid().equals(first.getUuid())) continue;
66-
first.addPassenger(e);
63+
64+
for (int i = 0; i < lines.size(); i++) {
65+
double y = location.getY() + (lines.size() - 1 - i) * LINE_SPACING;
66+
Location lineLoc = new Location(location.getX(), y, location.getZ(), location.getYaw(), location.getPitch());
67+
lines.get(i).teleport(lineLoc);
6768
}
6869
}
6970

spaceNPC/src/main/java/me/tofaa/entitylib/npc/NPC.java

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import me.tofaa.entitylib.wrapper.WrapperPlayer;
3030
import me.tofaa.entitylib.wrapper.hologram.Hologram;
3131
import net.kyori.adventure.text.Component;
32+
import net.kyori.adventure.text.minimessage.MiniMessage;
3233
import org.bukkit.Bukkit;
3334
import org.bukkit.World;
3435
import org.bukkit.entity.Player;
@@ -319,9 +320,32 @@ public void remove() {
319320
NPCRegistry.unregister(this);
320321
}
321322

323+
/**
324+
* Splits a Component into multiple lines on {@code <br>} tags.
325+
* MiniMessage keeps {@code <br>} as a tag token through serialize/deserialize round-trips,
326+
* so we serialize back to a MiniMessage string and split on the literal {@code <br>} token
327+
* before re-deserializing each fragment into its own Component. This allows multi-line NPC
328+
* name tags to be rendered as separate hologram line entities, since armor stands and text
329+
* displays do not visually break a single component on newlines.
330+
*/
331+
private List<Component> splitDisplayNameLines(Component component) {
332+
MiniMessage mm = MiniMessage.miniMessage();
333+
String serialized = mm.serialize(component);
334+
// MiniMessage round-trips <br> as the literal tag string "<br>"
335+
String[] parts = serialized.split("(?i)<br>", -1);
336+
if (parts.length == 1) {
337+
return Collections.singletonList(component);
338+
}
339+
List<Component> lines = new ArrayList<>(parts.length);
340+
for (String part : parts) {
341+
lines.add(mm.deserialize(part));
342+
}
343+
return lines;
344+
}
345+
322346
private void createHologram() {
323347
Location loc = getPosition();
324-
double yOffset = options.isSitting() ? 2.76 : 2.26;
348+
double yOffset = options.isSitting() ? 1.1 : 1.0;
325349
Location hologramLoc = new Location(
326350
loc.getX(),
327351
loc.getY() + yOffset,
@@ -330,24 +354,15 @@ private void createHologram() {
330354
loc.getPitch()
331355
);
332356

333-
int protocolVersion = EntityLib.getApi()
334-
.getPacketEvents()
335-
.getServerManager()
336-
.getVersion()
337-
.getProtocolVersion();
357+
Hologram.Legacy hologram = Hologram.legacy(hologramLoc);
358+
hologram.setLineOffset(-0.28f);
338359

339-
Hologram hologram;
340-
if (protocolVersion >= 760) {
341-
hologram = Hologram.modern(hologramLoc);
342-
} else {
343-
hologram = Hologram.legacy(hologramLoc);
360+
Component displayName = options.getDisplayName() != null
361+
? options.getDisplayName()
362+
: Component.text(name);
363+
for (Component line : splitDisplayNameLines(displayName)) {
364+
hologram.addLine(line);
344365
}
345-
346-
hologram.addLine(
347-
options.getDisplayName() != null
348-
? options.getDisplayName()
349-
: Component.text(name)
350-
);
351366
hologram.show();
352367
// if (hologram.getEntity().getEntityMeta() instanceof AbstractDisplayMeta displayMeta) {
353368
//// displayMeta.setTranslation(new Vector3f(0, 0.5f, 0));
@@ -358,12 +373,10 @@ private void createHologram() {
358373

359374
private void updateHologram() {
360375
if (hologram != null) {
361-
hologram.setLine(
362-
0,
363-
options.getDisplayName() != null
364-
? options.getDisplayName()
365-
: Component.text(name)
366-
);
376+
Component displayName = options.getDisplayName() != null
377+
? options.getDisplayName()
378+
: Component.text(name);
379+
hologram.setLines(splitDisplayNameLines(displayName));
367380
}
368381
}
369382

spaceNPC/src/main/java/me/tofaa/entitylib/npc/NPCMovement.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ private static void processViewerSync() {
9494
Location npcLocation = entity.getLocation();
9595

9696
npc.getHologram().ifPresent(hologram -> {
97-
hologram.setParent(npc.getEntity().get());
97+
boolean isSittingNow = npc.getOptions().isSitting();
98+
double yOff = isSittingNow ? 1.1 : 1.0;
99+
Location npcLoc = npc.getEntity().get().getLocation();
100+
hologram.teleport(new Location(npcLoc.getX(), npcLoc.getY() + yOff, npcLoc.getZ(), npcLoc.getYaw(), npcLoc.getPitch()));
98101
});
99102

100103
boolean permanentlyVisible = npc.getOptions().isPermanentlyVisible();

0 commit comments

Comments
 (0)