Skip to content

Commit 7908415

Browse files
committed
use top location on FancyHolograms
1 parent f885526 commit 7908415

File tree

1 file changed

+31
-2
lines changed
  • spigot/fancyholograms/src/main/java/me/hsgamer/unihologram/spigot/fancyholograms/hologram

1 file changed

+31
-2
lines changed

spigot/fancyholograms/src/main/java/me/hsgamer/unihologram/spigot/fancyholograms/hologram/FHHologram.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
import java.util.ArrayList;
1616
import java.util.List;
17+
import java.util.Objects;
1718
import java.util.function.Consumer;
1819
import java.util.stream.Collectors;
1920

2021
/**
2122
* The hologram for FancyHolograms
2223
*/
2324
public class FHHologram implements me.hsgamer.unihologram.common.api.Hologram<Location>, PlayerVisibility {
25+
private static final double LINE_HEIGHT = 0.25;
2426
private final Hologram hologram;
2527

2628
/**
@@ -44,6 +46,32 @@ public FHHologram(Hologram hologram) {
4446
this.hologram = hologram;
4547
}
4648

49+
private static Location getTopLocation(Location location, float scale, int lineCount) {
50+
Location newLocation = Objects.requireNonNull(location).clone();
51+
newLocation.setY(newLocation.getY() + LINE_HEIGHT * scale * lineCount);
52+
return newLocation;
53+
}
54+
55+
private static Location getBottomLocation(Location location, float scale, int lineCount) {
56+
Location newLocation = Objects.requireNonNull(location).clone();
57+
newLocation.setY(newLocation.getY() - LINE_HEIGHT * scale * lineCount);
58+
return newLocation;
59+
}
60+
61+
private Location getTopLocation() {
62+
return getTopLocation(hologram.getData().getLocation(), hologram.getData().getScale(), hologram.getData().getText().size());
63+
}
64+
65+
private void setTopLocation(Location location) {
66+
hologram.getData().setLocation(getBottomLocation(location, hologram.getData().getScale(), hologram.getData().getText().size()));
67+
}
68+
69+
private void updateTopLocation(int lineCount) {
70+
Location topLocation = getTopLocation(hologram.getData().getLocation(), hologram.getData().getScale(), hologram.getData().getText().size());
71+
Location bottomLocation = getBottomLocation(topLocation, hologram.getData().getScale(), lineCount);
72+
hologram.getData().setLocation(bottomLocation);
73+
}
74+
4775
private void checkHologramInitialized() {
4876
Preconditions.checkArgument(isInitialized(), "Hologram is not initialized");
4977
}
@@ -70,6 +98,7 @@ private void editLine(Consumer<List<HologramLine>> consumer) {
7098
checkHologramInitialized();
7199
List<HologramLine> lines = new ArrayList<>(getLines());
72100
consumer.accept(lines);
101+
updateTopLocation(lines.size());
73102
setLines(lines);
74103
}
75104

@@ -115,13 +144,13 @@ public boolean isInitialized() {
115144
@Override
116145
public Location getLocation() {
117146
checkHologramInitialized();
118-
return hologram.getData().getLocation();
147+
return getTopLocation();
119148
}
120149

121150
@Override
122151
public void setLocation(Location location) {
123152
checkHologramInitialized();
124-
hologram.getData().setLocation(location);
153+
setTopLocation(location);
125154
updateHologram();
126155
}
127156

0 commit comments

Comments
 (0)