diff --git a/build.gradle.kts b/build.gradle.kts index 5ed723f..7e64031 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } group = "me.youhavetrouble" -version = "1.15.0" +version = "1.16.0" description = "Nameplates using display entities" java { diff --git a/src/main/java/me/youhavetrouble/notjustnameplates/NJNConfig.java b/src/main/java/me/youhavetrouble/notjustnameplates/NJNConfig.java index 4438c5d..f7568ba 100644 --- a/src/main/java/me/youhavetrouble/notjustnameplates/NJNConfig.java +++ b/src/main/java/me/youhavetrouble/notjustnameplates/NJNConfig.java @@ -98,30 +98,72 @@ public class NJNConfig { framesSection.getKeys(false).forEach(frameName -> { ConfigurationSection frameSection = framesSection.getConfigurationSection(frameName); if (frameSection == null) return; - String text = frameSection.getString("text", null); - String backgroundColor = frameSection.getString("background"); - float scaleX = (float) frameSection.getDouble("scale-x", 1); - float scaleY = (float) frameSection.getDouble("scale-y", 1); - float scaleZ = (float) frameSection.getDouble("scale-z", 1); - float offsetX = (float) frameSection.getDouble("offset-x", 0); - float offsetY = (float) frameSection.getDouble("offset-y", 0); - float offsetZ = (float) frameSection.getDouble("offset-z", 0); - boolean shadowed = frameSection.getBoolean("shadowed", false); - byte textOpacity = (byte) Math.min(Math.max(frameSection.getInt("text-opacity", 255), 0), 255); - displayContent.addFrame(new DisplayFrame( - text, - colorFromHex(backgroundColor), - new Vector3f(scaleX, scaleY, scaleZ), - new Vector3f(offsetX, offsetY, offsetZ), - shadowed, - textOpacity - )); + ConfigurationSection sneakOverrideSection = frameSection.getConfigurationSection("sneak-override"); + displayContent.addFrame(parseFrame(frameSection, sneakOverrideSection)); }); Permission permission = new Permission("notjustnameplates.display." + displayContentSection.getName(), "Allows player to use " + displayContentSection.getName() + " nameplate", PermissionDefault.FALSE); plugin.getServer().getPluginManager().addPermission(permission); return displayContent; } + private DisplayFrame parseFrame(ConfigurationSection frameSection, ConfigurationSection sneakOverrideSection) { + String text = frameSection.getString("text", null); + String backgroundColor = frameSection.getString("background"); + float scaleX = (float) frameSection.getDouble("scale-x", 1); + float scaleY = (float) frameSection.getDouble("scale-y", 1); + float scaleZ = (float) frameSection.getDouble("scale-z", 1); + float offsetX = (float) frameSection.getDouble("offset-x", 0); + float offsetY = (float) frameSection.getDouble("offset-y", 0); + float offsetZ = (float) frameSection.getDouble("offset-z", 0); + boolean shadowed = frameSection.getBoolean("shadowed", false); + byte textOpacity = (byte) Math.clamp(frameSection.getInt("text-opacity", 255), 0, 255); + + DisplayFrame sneakOverride = null; + if (sneakOverrideSection != null) { + if (!sneakOverrideSection.isSet("text")) { + sneakOverrideSection.set("text", text); + } + if (!sneakOverrideSection.isSet("background")) { + sneakOverrideSection.set("background", backgroundColor); + } + if (!sneakOverrideSection.isSet("scale-x")) { + sneakOverrideSection.set("scale-x", scaleX); + } + if (!sneakOverrideSection.isSet("scale-y")) { + sneakOverrideSection.set("scale-y", scaleY); + } + if (!sneakOverrideSection.isSet("scale-z")) { + sneakOverrideSection.set("scale-z", scaleZ); + } + if (!sneakOverrideSection.isSet("offset-x")) { + sneakOverrideSection.set("offset-x", offsetX); + } + if (!sneakOverrideSection.isSet("offset-y")) { + sneakOverrideSection.set("offset-y", offsetY); + } + if (!sneakOverrideSection.isSet("offset-z")) { + sneakOverrideSection.set("offset-z", offsetZ); + } + if (!sneakOverrideSection.isSet("shadowed")) { + sneakOverrideSection.set("shadowed", shadowed); + } + if (!sneakOverrideSection.isSet("text-opacity")) { + sneakOverrideSection.set("text-opacity", frameSection.getInt("text-opacity", 255)); + } + sneakOverride = parseFrame(sneakOverrideSection, null); + } + + return new DisplayFrame( + text, + colorFromHex(backgroundColor), + new Vector3f(scaleX, scaleY, scaleZ), + new Vector3f(offsetX, offsetY, offsetZ), + shadowed, + textOpacity, + sneakOverride + ); + } + private Color colorFromHex(@Nullable String hex) { if (hex == null) return null; if (!hex.startsWith("#")) { diff --git a/src/main/java/me/youhavetrouble/notjustnameplates/displays/DisplayFrame.java b/src/main/java/me/youhavetrouble/notjustnameplates/displays/DisplayFrame.java index d41d7de..d321868 100644 --- a/src/main/java/me/youhavetrouble/notjustnameplates/displays/DisplayFrame.java +++ b/src/main/java/me/youhavetrouble/notjustnameplates/displays/DisplayFrame.java @@ -1,6 +1,7 @@ package me.youhavetrouble.notjustnameplates.displays; import org.bukkit.Color; +import org.jetbrains.annotations.Nullable; import org.joml.Vector3f; public record DisplayFrame( @@ -9,6 +10,7 @@ public record DisplayFrame( Vector3f scale, Vector3f offset, boolean shadowed, - byte textOpacity + byte textOpacity, + @Nullable DisplayFrame sneakOverride ) { } diff --git a/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/Nameplate.java b/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/Nameplate.java index 452970a..d0e00bd 100644 --- a/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/Nameplate.java +++ b/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/Nameplate.java @@ -3,6 +3,7 @@ package me.youhavetrouble.notjustnameplates.nameplates; import de.myzelyam.api.vanish.VanishAPI; import me.youhavetrouble.notjustnameplates.NotJustNameplates; import me.youhavetrouble.notjustnameplates.displays.DisplayContent; +import me.youhavetrouble.notjustnameplates.displays.DisplayFrame; import me.youhavetrouble.notjustnameplates.text.TextParser; import net.kyori.adventure.text.Component; import org.bukkit.Bukkit; @@ -46,10 +47,14 @@ public class Nameplate { private void createDisplayEntity() { if (textDisplay != null && !textDisplay.isDead()) return; if (this.content == null) return; - if (content.getCurrentFrame().text() == null) return; + Player player = Bukkit.getPlayer(playerUuid); if (player == null) return; + DisplayFrame currentFrame = player.isSneaking() && content.getCurrentFrame().sneakOverride() != null ? content.getCurrentFrame().sneakOverride() : content.getCurrentFrame(); + + if (currentFrame.text() == null) return; + this.textDisplay = (TextDisplay) player.getWorld().spawnEntity( player.getEyeLocation(), EntityType.TEXT_DISPLAY, @@ -65,18 +70,18 @@ public class Nameplate { textDisplay.setShadowRadius(0); textDisplay.setInterpolationDuration(content.getInterpolationDuration()); textDisplay.setInterpolationDelay(content.getInterpolationDelay()); - textDisplay.setShadowed(content.getCurrentFrame().shadowed()); - textDisplay.setTextOpacity(content.getCurrentFrame().textOpacity()); + textDisplay.setShadowed(currentFrame.shadowed()); + textDisplay.setTextOpacity(currentFrame.textOpacity()); - Color backgroundColor = this.content.getCurrentFrame().backgroundColor(); + Color backgroundColor = currentFrame.backgroundColor(); if (backgroundColor != null) textDisplay.setBackgroundColor(backgroundColor); - textDisplay.text(parseText(this.content.getCurrentFrame().text(), player)); + textDisplay.text(parseText(currentFrame.text(), player)); textDisplay.setTransformation(new Transformation( - content.getCurrentFrame().offset(), + currentFrame.offset(), new AxisAngle4f(0, 0, 0, 0), // left rotation - content.getCurrentFrame().scale(), + currentFrame.scale(), new AxisAngle4f(0, 0, 0, 0) // right rotation )); @@ -100,6 +105,10 @@ public class Nameplate { Bukkit.getScheduler().runTask(NotJustNameplates.getInstance(), this::remove); } + public DisplayContent getContent() { + return this.content; + } + public void setAlignment(@NotNull TextDisplay.TextAlignment alignment) { this.alignment = alignment; if (textDisplay == null || textDisplay.isDead()) return; @@ -139,10 +148,6 @@ public class Nameplate { remove(); return; } - if (content.getCurrentFrame().text() == null) { - remove(); - return; - } if (player.getGameMode() == GameMode.SPECTATOR) { remove(); return; @@ -151,12 +156,10 @@ public class Nameplate { remove(); return; } - if (NotJustNameplates.isSuperVanishHooked() && VanishAPI.isInvisible(player)) { remove(); return; } - if (NotJustNameplates.isVanishNoPacketHooked()) { VanishPlugin vanishPlugin = (VanishPlugin) Bukkit.getPluginManager().getPlugin("VanishNoPacket"); if (vanishPlugin != null && vanishPlugin.getManager().isVanished(player)) { @@ -164,6 +167,15 @@ public class Nameplate { return; } } + if (content == null) { + remove(); + return; + } + DisplayFrame currentFrame = player.isSneaking() && content.getCurrentFrame().sneakOverride() != null ? content.getCurrentFrame().sneakOverride() : content.getCurrentFrame(); + if (currentFrame.text() == null) { + remove(); + return; + } createDisplayEntity(); if (textDisplay == null || textDisplay.isDead()) return; @@ -180,19 +192,19 @@ public class Nameplate { } } - textDisplay.text(parseText(this.content.getCurrentFrame().text(), player)); + textDisplay.text(parseText(currentFrame.text(), player)); textDisplay.setBillboard(this.content.getBillboard()); - textDisplay.setShadowed(content.getCurrentFrame().shadowed()); - textDisplay.setTextOpacity(content.getCurrentFrame().textOpacity()); + textDisplay.setShadowed(currentFrame.shadowed()); + textDisplay.setTextOpacity(currentFrame.textOpacity()); textDisplay.setTransformation(new Transformation( - content.getCurrentFrame().offset(), + currentFrame.offset(), new AxisAngle4f(0, 0, 0, 0), // left rotation - content.getCurrentFrame().scale(), + currentFrame.scale(), new AxisAngle4f(0, 0, 0, 0) // right rotation )); - Color backgroundColor = this.content.getCurrentFrame().backgroundColor(); + Color backgroundColor = currentFrame.backgroundColor(); if (backgroundColor == null) { textDisplay.setDefaultBackground(true); } else { diff --git a/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/NameplateManager.java b/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/NameplateManager.java index e4c3be9..05a921d 100644 --- a/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/NameplateManager.java +++ b/src/main/java/me/youhavetrouble/notjustnameplates/nameplates/NameplateManager.java @@ -51,6 +51,15 @@ public class NameplateManager implements Listener { nameplate.remove(); } + @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) + public void onPlayerSneak(PlayerToggleSneakEvent event) { + Player player = event.getPlayer(); + Nameplate nameplate = nameplates.get(player.getUniqueId()); + if (nameplate == null) return; + if (nameplate.getContent().getCurrentFrame().sneakOverride() == null) return; + nameplate.update(); + } + @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) public void onPlayerMove(PlayerMoveEvent event) { Player player = event.getPlayer();