slowly abstracting away

This commit is contained in:
2023-06-24 01:30:32 +02:00
parent 9acaf9ecc7
commit f1d0a1f341
4 changed files with 104 additions and 32 deletions
@@ -5,9 +5,16 @@ import org.bukkit.plugin.java.JavaPlugin;
public final class NotJustNameplates extends JavaPlugin { public final class NotJustNameplates extends JavaPlugin {
private static NotJustNameplates instance;
@Override @Override
public void onEnable() { public void onEnable() {
instance = this;
getServer().getPluginManager().registerEvents(new TeamManagementListener(this), this); getServer().getPluginManager().registerEvents(new TeamManagementListener(this), this);
} }
public static NotJustNameplates getInstance() {
return instance;
}
} }
@@ -1,8 +1,9 @@
package me.youhavetrouble.notjustnameplates.nameplates; package me.youhavetrouble.notjustnameplates.nameplates;
import me.youhavetrouble.notjustnameplates.NotJustNameplates;
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.Color;
import org.bukkit.entity.Display; import org.bukkit.entity.Display;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -10,6 +11,7 @@ import org.bukkit.entity.TextDisplay;
import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.util.Transformation; import org.bukkit.util.Transformation;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.joml.AxisAngle4f; import org.joml.AxisAngle4f;
import org.joml.Vector3f; import org.joml.Vector3f;
@@ -19,11 +21,15 @@ public class Nameplate {
private Component name; private Component name;
private final UUID playerUuid; 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; private TextDisplay textDisplay;
public Nameplate(@NotNull UUID playerUuid, @NotNull Component name) { public Nameplate(@NotNull UUID playerUuid, Component name) {
this.playerUuid = playerUuid; this.playerUuid = playerUuid;
this.name = name; this.name = name;
} }
@@ -32,28 +38,28 @@ public class Nameplate {
if (textDisplay != null && !textDisplay.isDead()) return; if (textDisplay != null && !textDisplay.isDead()) return;
Player player = Bukkit.getPlayer(playerUuid); Player player = Bukkit.getPlayer(playerUuid);
if (player == null) return; if (player == null) return;
this.textDisplay = (TextDisplay) player.getWorld() this.textDisplay = (TextDisplay) player.getWorld().spawnEntity(
.spawnEntity( player.getEyeLocation(),
calculateCurrentLocation(), EntityType.TEXT_DISPLAY,
EntityType.TEXT_DISPLAY, CreatureSpawnEvent.SpawnReason.CUSTOM, entity -> {
CreatureSpawnEvent.SpawnReason.CUSTOM, entity -> { TextDisplay textDisplay = (TextDisplay) entity;
TextDisplay textDisplay = (TextDisplay) entity; textDisplay.setInvulnerable(true);
textDisplay.setCustomNameVisible(true); textDisplay.setPersistent(false);
textDisplay.setSilent(true); textDisplay.setAlignment(alignment);
textDisplay.setInvulnerable(true); textDisplay.setBillboard(billboard);
textDisplay.setPersistent(false); textDisplay.setShadowRadius(0);
textDisplay.text(this.name); if (this.backgroundColor != null) textDisplay.setBackgroundColor(this.backgroundColor);
textDisplay.setAlignment(TextDisplay.TextAlignment.CENTER); textDisplay.setTransformation(
textDisplay.setBillboard(Display.Billboard.CENTER); new Transformation(
textDisplay.setInvulnerable(true); new Vector3f(0, heightOffset, 0), // offset
textDisplay.setTransformation( new AxisAngle4f(0, 0, 0, 0), // left rotation
new Transformation( new Vector3f(1, 1, 1), // scale
new Vector3f(0, heightOffset, 0), // offset new AxisAngle4f(0, 0, 0, 0) // right rotation
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); player.addPassenger(textDisplay);
} }
@@ -74,6 +80,56 @@ public class Nameplate {
textDisplay.text(name); 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 * Set height offset from the player's eye location
*/ */
@@ -81,10 +137,8 @@ public class Nameplate {
this.heightOffset = heightOffset; this.heightOffset = heightOffset;
} }
public Location calculateCurrentLocation() { public float getHeightOffset() {
Player player = Bukkit.getPlayer(playerUuid); return heightOffset;
if (player == null) return null;
return player.getEyeLocation().add(0, heightOffset, 0);
} }
/** /**
@@ -92,8 +146,7 @@ public class Nameplate {
*/ */
public void update() { public void update() {
Player player = Bukkit.getPlayer(playerUuid); Player player = Bukkit.getPlayer(playerUuid);
if (player == null) return; if (player == null || player.isDead() || name == null) {
if (player.isDead()) {
remove(); remove();
return; return;
} }
@@ -101,6 +154,7 @@ public class Nameplate {
if (!player.getPassengers().contains(textDisplay)) { if (!player.getPassengers().contains(textDisplay)) {
player.addPassenger(textDisplay); player.addPassenger(textDisplay);
} }
textDisplay.text(this.name);
} }
protected void remove() { protected void remove() {
@@ -4,7 +4,6 @@ import me.youhavetrouble.notjustnameplates.NotJustNameplates;
import net.minecraft.network.protocol.game.ClientboundSetPlayerTeamPacket; import net.minecraft.network.protocol.game.ClientboundSetPlayerTeamPacket;
import net.minecraft.world.scores.PlayerTeam; import net.minecraft.world.scores.PlayerTeam;
import net.minecraft.world.scores.Scoreboard; import net.minecraft.world.scores.Scoreboard;
import net.minecraft.world.scores.Team;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer; import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
+12
View File
@@ -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"