Skip to content

Commit

Permalink
Fix holograms
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofaa2 committed Jun 10, 2024
1 parent 4c0ca70 commit 2a3732f
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 97 deletions.
130 changes: 54 additions & 76 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions api/src/main/java/me/tofaa/entitylib/utils/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.jetbrains.annotations.Nullable;

import java.text.MessageFormat;
import java.util.List;
import java.util.Objects;

/**
Expand All @@ -14,6 +15,18 @@ public final class Check {

private Check() {}


public static <T> void arrayLength(List<T> lines, int index, T e) {
if (index >= lines.size()) {
for (int i = lines.size(); i < index; i++) {
lines.add(null);
}
lines.add(e);
}
else {lines.set(index, e);
}
}

@Contract("null, _ -> fail")
public static void notNull(@Nullable Object object, @NotNull String reason) {
if (Objects.isNull(object)) {
Expand Down
30 changes: 19 additions & 11 deletions api/src/main/java/me/tofaa/entitylib/wrapper/hologram/Hologram.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
package me.tofaa.entitylib.wrapper.hologram;

import com.github.retrooper.packetevents.protocol.player.User;
import com.github.retrooper.packetevents.protocol.world.Location;
import me.tofaa.entitylib.meta.display.TextDisplayMeta;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;

public interface Hologram<W> {
public interface Hologram {

static <C> Hologram.@NotNull Legacy<C> legacy(@NotNull Location location) {
return new LegacyHologram<>(location);
static Hologram.@NotNull Legacy legacy(@NotNull Location location) {
return new LegacyHologram(location);
}

static <C> Hologram.@NotNull Legacy<C> legacy(@NotNull Location location, List<Component> lines) {
return new LegacyHologram<>(location, lines);
static Hologram.@NotNull Legacy legacy(@NotNull Location location, List<Component> lines) {
return new LegacyHologram(location, lines);
}

static <C> Hologram.@NotNull Modern<C> modern(@NotNull Location location) {
return new ModernHologram<>(location);

static Hologram.@NotNull Modern modern(@NotNull Location location) {
return new ModernHologram(location);
}

static <C> Hologram.@NotNull Modern<C> modern(@NotNull Location location, List<Component> lines) {
return new ModernHologram<>(location, lines);
static Hologram.@NotNull Modern modern(@NotNull Location location, List<Component> lines) {
return new ModernHologram(location, lines);
}

@NotNull Location getLocation();
Expand All @@ -44,13 +47,18 @@ public interface Hologram<W> {

void addLine(@Nullable Component line);

interface Modern<W> extends Hologram<W> {
void addViewer(@NotNull UUID viewer);
default void addViewer(@NotNull User user) {
addViewer(user.getUUID());
}

interface Modern extends Hologram {

// I got too lazy
void setModifier(@NotNull Consumer<TextDisplayMeta> consumer);

}
interface Legacy<W> extends Hologram<W> {
interface Legacy extends Hologram {

float getLineOffset(boolean marker);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
import com.github.retrooper.packetevents.protocol.world.Location;
import me.tofaa.entitylib.EntityLib;
import me.tofaa.entitylib.meta.other.ArmorStandMeta;
import me.tofaa.entitylib.utils.Check;
import me.tofaa.entitylib.wrapper.WrapperEntity;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

final class LegacyHologram<W> implements Hologram.Legacy<W> {
final class LegacyHologram implements Hologram.Legacy {

private Location location;
private List<WrapperEntity> lines = new ArrayList<>(3);
private final List<WrapperEntity> lines = new ArrayList<>(3);
private float lineOffset = -0.9875f;
private float markerOffset = -0.40625f;
private boolean marker;
Expand All @@ -31,6 +33,13 @@ final class LegacyHologram<W> implements Hologram.Legacy<W> {
}
}

@Override
public void addViewer(@NotNull UUID viewer) {
for (WrapperEntity line : lines) {
line.addViewer(viewer);
}
}

@Override
public boolean isMarker() {
return marker;
Expand Down Expand Up @@ -95,7 +104,7 @@ public void setLine(int index, @Nullable Component line) {
meta.setHasNoGravity(true);
meta.setSmall(true);
meta.setMarker(marker);
this.lines.set(index, e);
Check.arrayLength(lines, index, e);
e.spawn(location);
teleport(location);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
import com.github.retrooper.packetevents.protocol.world.Location;
import me.tofaa.entitylib.EntityLib;
import me.tofaa.entitylib.meta.display.TextDisplayMeta;
import me.tofaa.entitylib.utils.Check;
import me.tofaa.entitylib.wrapper.WrapperEntity;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;

final class ModernHologram<W> implements Hologram.Modern<W> {
final class ModernHologram implements Hologram.Modern {

private Location location;
private List<WrapperEntity> lines = new ArrayList<>(3);
private final List<WrapperEntity> lines = new ArrayList<>(3);
private Consumer<TextDisplayMeta> modifier;
private boolean spawned = false;

ModernHologram(@NotNull Location location) {
this.location = location;
Expand All @@ -36,13 +39,15 @@ public void show() {
line.spawn(location);
}
teleport(location);
spawned = true;
}

@Override
public void hide() {
for (WrapperEntity line : lines) {
line.despawn();
}
spawned = false;
}

@Override
Expand Down Expand Up @@ -72,17 +77,28 @@ public void setLine(int index, @Nullable Component line) {
meta.setInvisible(true);
meta.setHasNoGravity(true);
meta.setText(line);
this.modifier.accept(meta);
this.lines.set(index, e);
e.spawn(location);
teleport(location);
if (this.modifier != null) {
this.modifier.accept(meta);
}
Check.arrayLength(lines, index, e);
if (spawned) {
e.spawn(location);
teleport(location);
}
}

@Override
public void addLine(@Nullable Component line) {
setLine(lines.size(), line);
}

@Override
public void addViewer(@NotNull UUID viewer) {
for (WrapperEntity line : lines) {
line.addViewer(viewer);
}
}


@Override
public int length() {
Expand Down
2 changes: 1 addition & 1 deletion test-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies {
compileOnly('org.spigotmc:spigot-api:1.20.1-R0.1-SNAPSHOT')
compileOnly('com.github.retrooper.packetevents:spigot:2.3.0')
// implementation(project(":platforms:spigot"))
implementation("com.github.Tofaa2.EntityLib:spigot:2.2.0-SNAPSHOT")
implementation(project(":platforms:spigot"))
}


Expand Down
Loading

0 comments on commit 2a3732f

Please sign in to comment.