hopefully working solution for nametags

This commit is contained in:
2023-06-20 00:45:25 +02:00
parent f924c4dd55
commit 35c6176035
@@ -3,7 +3,11 @@ package me.youhavetrouble.notjustnameplates.nameplates;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.TextDisplay;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.UUID; import java.util.UUID;
@@ -14,11 +18,31 @@ public class Nameplate {
private final UUID playerUuid; private final UUID playerUuid;
private double heightOffset = 0.5; private double heightOffset = 0.5;
private TextDisplay textDisplay;
public Nameplate(@NotNull UUID playerUuid, @NotNull Component name) { public Nameplate(@NotNull UUID playerUuid, @NotNull Component name) {
this.playerUuid = playerUuid; this.playerUuid = playerUuid;
this.name = name; this.name = name;
} }
private void createDisplayEntity() {
if (textDisplay != null && !textDisplay.isDead()) return;
Player player = Bukkit.getPlayer(playerUuid);
if (player == null) return;
this.textDisplay = (TextDisplay) player.getWorld()
.spawnEntity(
calculateCurrentLocation(),
EntityType.TEXT_DISPLAY,
CreatureSpawnEvent.SpawnReason.CUSTOM, entity -> {
TextDisplay textDisplay = (TextDisplay) entity;
textDisplay.setCustomNameVisible(true);
textDisplay.setSilent(true);
textDisplay.setInvulnerable(true);
textDisplay.setPersistent(false);
textDisplay.text(this.name);
});
}
/** /**
* Get content of the nameplate * Get content of the nameplate
* @return content of the nameplate * @return content of the nameplate
@@ -32,6 +56,8 @@ public class Nameplate {
*/ */
public void setName(Component name) { public void setName(Component name) {
this.name = name; this.name = name;
if (textDisplay == null || textDisplay.isDead()) return;
textDisplay.text(name);
} }
/** /**
@@ -41,19 +67,19 @@ public class Nameplate {
this.heightOffset = heightOffset; this.heightOffset = heightOffset;
} }
public Location calculateCurrentLocation() {
Player player = Bukkit.getPlayer(playerUuid);
if (player == null) return null;
return player.getEyeLocation().add(0, heightOffset, 0);
}
/** /**
* Update the nameplate position * Update the nameplate position
*/ */
public void updatePosition() { public void updatePosition() {
Player player = Bukkit.getPlayer(playerUuid); createDisplayEntity();
if (player == null) return; Location location = calculateCurrentLocation();
Location location = player this.textDisplay.teleportAsync(location, PlayerTeleportEvent.TeleportCause.PLUGIN);
.getEyeLocation()
.clone()
.add(0, heightOffset, 0);
// TODO move fake entity to location
} }
} }