From f1d0a1f341b4e6de9caf75838521c0489616b2c0 Mon Sep 17 00:00:00 2001 From: youhavetrouble Date: Sat, 24 Jun 2023 01:30:32 +0200 Subject: [PATCH] slowly abstracting away --- .../notjustnameplates/NotJustNameplates.java | 7 ++ .../nameplates/Nameplate.java | 116 +++++++++++++----- .../nameplates/TeamManagementListener.java | 1 - src/main/resources/config.yml | 12 ++ 4 files changed, 104 insertions(+), 32 deletions(-) create mode 100644 src/main/resources/config.yml diff --git a/src/main/java/me/youhavetrouble/notjustnameplates/NotJustNameplates.java b/src/main/java/me/youhavetrouble/notjustnameplates/NotJustNameplates.java index edd0543..de8a11f 100644 --- a/src/main/java/me/youhavetrouble/notjustnameplates/NotJustNameplates.java +++ b/src/main/java/me/youhavetrouble/notjustnameplates/NotJustNameplates.java @@ -5,9 +5,16 @@ import org.bukkit.plugin.java.JavaPlugin; public final class NotJustNameplates extends JavaPlugin { + private static NotJustNameplates instance; + @Override public void onEnable() { + instance = this; getServer().getPluginManager().registerEvents(new TeamManagementListener(this), this); } + public static NotJustNameplates getInstance() { + return instance; + } + } diff --git a/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/Nameplate.java b/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/Nameplate.java index af1670c..93e9219 100644 --- a/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/Nameplate.java +++ b/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/Nameplate.java @@ -1,8 +1,9 @@ package me.youhavetrouble.notjustnameplates.nameplates; +import me.youhavetrouble.notjustnameplates.NotJustNameplates; import net.kyori.adventure.text.Component; import org.bukkit.Bukkit; -import org.bukkit.Location; +import org.bukkit.Color; import org.bukkit.entity.Display; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; @@ -10,6 +11,7 @@ import org.bukkit.entity.TextDisplay; import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.util.Transformation; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.joml.AxisAngle4f; import org.joml.Vector3f; @@ -19,11 +21,15 @@ public class Nameplate { private Component name; private final UUID playerUuid; - private float heightOffset = 0.75f; + private float heightOffset = 0.7f; + private Color backgroundColor = null; + private Display.Billboard billboard = Display.Billboard.CENTER; + private TextDisplay.TextAlignment alignment = TextDisplay.TextAlignment.CENTER; + private boolean visibleForOwner = true; private TextDisplay textDisplay; - public Nameplate(@NotNull UUID playerUuid, @NotNull Component name) { + public Nameplate(@NotNull UUID playerUuid, Component name) { this.playerUuid = playerUuid; this.name = name; } @@ -32,28 +38,28 @@ public class Nameplate { 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); - textDisplay.setAlignment(TextDisplay.TextAlignment.CENTER); - textDisplay.setBillboard(Display.Billboard.CENTER); - textDisplay.setInvulnerable(true); - textDisplay.setTransformation( - new Transformation( - new Vector3f(0, heightOffset, 0), // offset - new AxisAngle4f(0, 0, 0, 0), // left rotation - new Vector3f(1, 1, 1), // scale - new AxisAngle4f(0, 0, 0, 0) // right rotation - )); - }); + this.textDisplay = (TextDisplay) player.getWorld().spawnEntity( + player.getEyeLocation(), + EntityType.TEXT_DISPLAY, + CreatureSpawnEvent.SpawnReason.CUSTOM, entity -> { + TextDisplay textDisplay = (TextDisplay) entity; + textDisplay.setInvulnerable(true); + textDisplay.setPersistent(false); + textDisplay.setAlignment(alignment); + textDisplay.setBillboard(billboard); + textDisplay.setShadowRadius(0); + if (this.backgroundColor != null) textDisplay.setBackgroundColor(this.backgroundColor); + textDisplay.setTransformation( + new Transformation( + new Vector3f(0, heightOffset, 0), // offset + new AxisAngle4f(0, 0, 0, 0), // left rotation + new Vector3f(1, 1, 1), // scale + new AxisAngle4f(0, 0, 0, 0) // right rotation + )); + }); + if (!this.visibleForOwner) { + player.hideEntity(NotJustNameplates.getInstance(), textDisplay); + } player.addPassenger(textDisplay); } @@ -74,6 +80,56 @@ public class Nameplate { textDisplay.text(name); } + public void setBillboard(@NotNull Display.Billboard billboard) { + this.billboard = billboard; + if (textDisplay == null || textDisplay.isDead()) return; + textDisplay.setBillboard(billboard); + } + + public Display.Billboard getBillboard() { + return billboard; + } + + public void setAlignment(@NotNull TextDisplay.TextAlignment alignment) { + this.alignment = alignment; + if (textDisplay == null || textDisplay.isDead()) return; + textDisplay.setAlignment(alignment); + } + + public TextDisplay.TextAlignment getAlignment() { + return alignment; + } + + public void setBackgroundColor(@Nullable Color color) { + this.backgroundColor = color; + if (textDisplay == null || textDisplay.isDead()) return; + if (this.backgroundColor == null) { + textDisplay.setDefaultBackground(true); + return; + } + textDisplay.setBackgroundColor(this.backgroundColor); + } + + public Color getBackgroundColor() { + return backgroundColor; + } + + public void setVisibleForOwner(boolean visible) { + this.visibleForOwner = visible; + if (textDisplay == null || textDisplay.isDead()) return; + Player player = Bukkit.getPlayer(playerUuid); + if (player == null) return; + if (visible) { + player.showEntity(NotJustNameplates.getInstance(), textDisplay); + } else { + player.hideEntity(NotJustNameplates.getInstance(), textDisplay); + } + } + + public boolean isVisibleForOwner() { + return this.visibleForOwner; + } + /** * Set height offset from the player's eye location */ @@ -81,10 +137,8 @@ public class Nameplate { this.heightOffset = heightOffset; } - public Location calculateCurrentLocation() { - Player player = Bukkit.getPlayer(playerUuid); - if (player == null) return null; - return player.getEyeLocation().add(0, heightOffset, 0); + public float getHeightOffset() { + return heightOffset; } /** @@ -92,8 +146,7 @@ public class Nameplate { */ public void update() { Player player = Bukkit.getPlayer(playerUuid); - if (player == null) return; - if (player.isDead()) { + if (player == null || player.isDead() || name == null) { remove(); return; } @@ -101,6 +154,7 @@ public class Nameplate { if (!player.getPassengers().contains(textDisplay)) { player.addPassenger(textDisplay); } + textDisplay.text(this.name); } protected void remove() { diff --git a/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/TeamManagementListener.java b/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/TeamManagementListener.java index d46ba5c..4cd2871 100644 --- a/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/TeamManagementListener.java +++ b/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/TeamManagementListener.java @@ -4,7 +4,6 @@ import me.youhavetrouble.notjustnameplates.NotJustNameplates; import net.minecraft.network.protocol.game.ClientboundSetPlayerTeamPacket; import net.minecraft.world.scores.PlayerTeam; import net.minecraft.world.scores.Scoreboard; -import net.minecraft.world.scores.Team; import org.bukkit.Bukkit; import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer; import org.bukkit.entity.Player; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..800ae60 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,12 @@ +nameplates: + default: + text: + - "%player_displayname%" + + # Billboard options are as follows: + # "center" - pivots around center point + # "vertical" - pivots around vertical axis + # "horizontal" - pivots around horizontal axis + # "fixed" - no rotation + billboard: "center" +