Skip to content

Commit

Permalink
use top location on FancyHolograms
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Aug 4, 2023
1 parent f885526 commit 7908415
Showing 1 changed file with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
* The hologram for FancyHolograms
*/
public class FHHologram implements me.hsgamer.unihologram.common.api.Hologram<Location>, PlayerVisibility {
private static final double LINE_HEIGHT = 0.25;
private final Hologram hologram;

/**
Expand All @@ -44,6 +46,32 @@ public FHHologram(Hologram hologram) {
this.hologram = hologram;
}

private static Location getTopLocation(Location location, float scale, int lineCount) {
Location newLocation = Objects.requireNonNull(location).clone();
newLocation.setY(newLocation.getY() + LINE_HEIGHT * scale * lineCount);
return newLocation;
}

private static Location getBottomLocation(Location location, float scale, int lineCount) {
Location newLocation = Objects.requireNonNull(location).clone();
newLocation.setY(newLocation.getY() - LINE_HEIGHT * scale * lineCount);
return newLocation;
}

private Location getTopLocation() {
return getTopLocation(hologram.getData().getLocation(), hologram.getData().getScale(), hologram.getData().getText().size());
}

private void setTopLocation(Location location) {
hologram.getData().setLocation(getBottomLocation(location, hologram.getData().getScale(), hologram.getData().getText().size()));
}

private void updateTopLocation(int lineCount) {
Location topLocation = getTopLocation(hologram.getData().getLocation(), hologram.getData().getScale(), hologram.getData().getText().size());
Location bottomLocation = getBottomLocation(topLocation, hologram.getData().getScale(), lineCount);
hologram.getData().setLocation(bottomLocation);
}

private void checkHologramInitialized() {
Preconditions.checkArgument(isInitialized(), "Hologram is not initialized");
}
Expand All @@ -70,6 +98,7 @@ private void editLine(Consumer<List<HologramLine>> consumer) {
checkHologramInitialized();
List<HologramLine> lines = new ArrayList<>(getLines());
consumer.accept(lines);
updateTopLocation(lines.size());
setLines(lines);
}

Expand Down Expand Up @@ -115,13 +144,13 @@ public boolean isInitialized() {
@Override
public Location getLocation() {
checkHologramInitialized();
return hologram.getData().getLocation();
return getTopLocation();
}

@Override
public void setLocation(Location location) {
checkHologramInitialized();
hologram.getData().setLocation(location);
setTopLocation(location);
updateHologram();
}

Expand Down

0 comments on commit 7908415

Please sign in to comment.