From 029c78378068f3d4ec42d8a71bc1388427dcec96 Mon Sep 17 00:00:00 2001 From: Justin <33465177+BitByLogics@users.noreply.github.com> Date: Sun, 3 Nov 2024 04:48:22 -0500 Subject: [PATCH] Fixes #148 and resolves #140 (#149) * fixes #148 * add brightness command * fix brightness command NPE * fix visibility distance * add brightness command to help --- .../api/data/DisplayHologramData.java | 16 +++++ .../hologram/version/Hologram1_19_4.java | 3 + .../hologram/version/Hologram1_20_1.java | 3 + .../hologram/version/Hologram1_20_2.java | 3 + .../hologram/version/Hologram1_20_4.java | 3 + .../oliver/fancyholograms/FancyHolograms.java | 4 ++ .../fancyholograms/commands/HologramCMD.java | 16 ++++- .../commands/hologram/BrightnessCMD.java | 71 +++++++++++++++++++ .../hologram/version/HologramImpl.java | 2 + .../storage/FlatFileHologramStorage.java | 5 ++ .../oliver/fancyholograms/util/Constants.java | 1 + 11 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/oliver/fancyholograms/commands/hologram/BrightnessCMD.java diff --git a/api/src/main/java/de/oliver/fancyholograms/api/data/DisplayHologramData.java b/api/src/main/java/de/oliver/fancyholograms/api/data/DisplayHologramData.java index b559d5f2..a72b57fa 100644 --- a/api/src/main/java/de/oliver/fancyholograms/api/data/DisplayHologramData.java +++ b/api/src/main/java/de/oliver/fancyholograms/api/data/DisplayHologramData.java @@ -132,6 +132,16 @@ public boolean read(ConfigurationSection section, String name) { default -> Display.Billboard.CENTER; }; + int blockBrightness = Math.min(15, section.getInt("block_brightness", -1)); + int skyBrightness = Math.min(15, section.getInt("sky_brightness", -1)); + + if(blockBrightness > -1 || skyBrightness > -1) { + brightness = new Display.Brightness( + Math.max(0, blockBrightness), + Math.max(0, skyBrightness) + ); + } + return true; } @@ -143,6 +153,12 @@ public boolean write(ConfigurationSection section, String name) { section.set("scale_z", scale.z); section.set("shadow_radius", shadowRadius); section.set("shadow_strength", shadowStrength); + + if(brightness != null) { + section.set("block_brightness", brightness.getBlockLight()); + section.set("sky_brightness", brightness.getSkyLight()); + } + section.set("billboard", billboard != Display.Billboard.CENTER ? billboard.name().toLowerCase(Locale.ROOT) : null); return true; diff --git a/implementation_1_19_4/src/main/java/de/oliver/fancyholograms/hologram/version/Hologram1_19_4.java b/implementation_1_19_4/src/main/java/de/oliver/fancyholograms/hologram/version/Hologram1_19_4.java index 7c8a3009..b0723bdf 100644 --- a/implementation_1_19_4/src/main/java/de/oliver/fancyholograms/hologram/version/Hologram1_19_4.java +++ b/implementation_1_19_4/src/main/java/de/oliver/fancyholograms/hologram/version/Hologram1_19_4.java @@ -174,6 +174,9 @@ public void update() { // entity shadow display.setShadowRadius(displayData.getShadowRadius()); display.setShadowStrength(displayData.getShadowStrength()); + + // view range + display.setViewRange(displayData.getVisibilityDistance()); } } diff --git a/implementation_1_20_1/src/main/java/de/oliver/fancyholograms/hologram/version/Hologram1_20_1.java b/implementation_1_20_1/src/main/java/de/oliver/fancyholograms/hologram/version/Hologram1_20_1.java index fc5918b5..1d449a5c 100644 --- a/implementation_1_20_1/src/main/java/de/oliver/fancyholograms/hologram/version/Hologram1_20_1.java +++ b/implementation_1_20_1/src/main/java/de/oliver/fancyholograms/hologram/version/Hologram1_20_1.java @@ -174,6 +174,9 @@ public void update() { // entity shadow display.setShadowRadius(displayData.getShadowRadius()); display.setShadowStrength(displayData.getShadowStrength()); + + // view range + display.setViewRange(displayData.getVisibilityDistance()); } } diff --git a/implementation_1_20_2/src/main/java/de/oliver/fancyholograms/hologram/version/Hologram1_20_2.java b/implementation_1_20_2/src/main/java/de/oliver/fancyholograms/hologram/version/Hologram1_20_2.java index 1a51aa84..86f042d2 100644 --- a/implementation_1_20_2/src/main/java/de/oliver/fancyholograms/hologram/version/Hologram1_20_2.java +++ b/implementation_1_20_2/src/main/java/de/oliver/fancyholograms/hologram/version/Hologram1_20_2.java @@ -174,6 +174,9 @@ public void update() { // entity shadow display.setShadowRadius(displayData.getShadowRadius()); display.setShadowStrength(displayData.getShadowStrength()); + + // view range + display.setViewRange(displayData.getVisibilityDistance()); } } diff --git a/implementation_1_20_4/src/main/java/de/oliver/fancyholograms/hologram/version/Hologram1_20_4.java b/implementation_1_20_4/src/main/java/de/oliver/fancyholograms/hologram/version/Hologram1_20_4.java index 8a71fb6d..09aa2511 100644 --- a/implementation_1_20_4/src/main/java/de/oliver/fancyholograms/hologram/version/Hologram1_20_4.java +++ b/implementation_1_20_4/src/main/java/de/oliver/fancyholograms/hologram/version/Hologram1_20_4.java @@ -174,6 +174,9 @@ public void update() { // entity shadow display.setShadowRadius(displayData.getShadowRadius()); display.setShadowStrength(displayData.getShadowStrength()); + + // view range + display.setViewRange(displayData.getVisibilityDistance()); } } diff --git a/src/main/java/de/oliver/fancyholograms/FancyHolograms.java b/src/main/java/de/oliver/fancyholograms/FancyHolograms.java index e87c9381..f0839f5c 100644 --- a/src/main/java/de/oliver/fancyholograms/FancyHolograms.java +++ b/src/main/java/de/oliver/fancyholograms/FancyHolograms.java @@ -93,6 +93,10 @@ public FancyHolograms() { return Objects.requireNonNull(INSTANCE, "plugin is not initialized"); } + public static boolean canGet() { + return INSTANCE != null; + } + @Override public void onLoad() { final var adapter = resolveHologramAdapter(); diff --git a/src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java b/src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java index 6237dbee..d398f737 100644 --- a/src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java +++ b/src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java @@ -156,7 +156,7 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String label, @No final var usingNpcs = PluginUtils.isFancyNpcsEnabled(); - List suggestions = new ArrayList<>(Arrays.asList("position", "moveHere", "center", "moveTo", "rotate", "rotatepitch", "billboard", "scale", "visibilityDistance", "visibility", "shadowRadius", "shadowStrength", usingNpcs ? "linkWithNpc" : "", usingNpcs ? "unlinkWithNpc" : "")); + List suggestions = new ArrayList<>(Arrays.asList("position", "moveHere", "center", "moveTo", "rotate", "rotatepitch", "billboard", "scale", "visibilityDistance", "visibility", "shadowRadius", "shadowStrength", "brightness", usingNpcs ? "linkWithNpc" : "", usingNpcs ? "unlinkWithNpc" : "")); suggestions.addAll(type.getCommands()); return suggestions.stream().filter(input -> input.toLowerCase().startsWith(args[2].toLowerCase(Locale.ROOT))).toList(); @@ -204,6 +204,7 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String label, @No TextHologramData textData = (TextHologramData) hologram.getData(); yield Stream.of(!textData.hasTextShadow()).map(Object::toString); } + case "brightness" -> Stream.of("block", "sky"); case "textalignment" -> Arrays.stream(TextDisplay.TextAlignment.values()).map(Enum::name); case "setline", "removeline" -> { TextHologramData textData = (TextHologramData) hologram.getData(); @@ -273,6 +274,18 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String label, @No return suggestions; } + if(args[2].equalsIgnoreCase("brightness")) { + if(args.length == 4) { + return List.of("block", "sky"); + } + + if(args.length > 5) { + return Collections.emptyList(); + } + + return List.of("0", "5", "10", "15"); + } + return Collections.emptyList(); } @@ -313,6 +326,7 @@ private boolean edit(@NotNull final CommandSender player, @NotNull final Hologra case "linkwithnpc" -> new LinkWithNpcCMD().run(player, hologram, args); case "shadowradius" -> new ShadowRadiusCMD().run(player, hologram, args); case "shadowstrength" -> new ShadowStrengthCMD().run(player, hologram, args); + case "brightness" -> new BrightnessCMD().run(player, hologram, args); // text data case "background" -> new BackgroundCMD().run(player, hologram, args); diff --git a/src/main/java/de/oliver/fancyholograms/commands/hologram/BrightnessCMD.java b/src/main/java/de/oliver/fancyholograms/commands/hologram/BrightnessCMD.java new file mode 100644 index 00000000..688054b0 --- /dev/null +++ b/src/main/java/de/oliver/fancyholograms/commands/hologram/BrightnessCMD.java @@ -0,0 +1,71 @@ +package de.oliver.fancyholograms.commands.hologram; + +import de.oliver.fancyholograms.FancyHolograms; +import de.oliver.fancyholograms.api.data.DisplayHologramData; +import de.oliver.fancyholograms.api.hologram.Hologram; +import de.oliver.fancyholograms.commands.Subcommand; +import de.oliver.fancyholograms.util.NumberHelper; +import de.oliver.fancylib.MessageHelper; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Display; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +public class BrightnessCMD implements Subcommand { + + @Override + public List tabcompletion(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) { + return null; + } + + @Override + public boolean run(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) { + if (!(hologram.getData() instanceof DisplayHologramData displayData)) { + MessageHelper.error(player, "This command can only be used on display holograms"); + return false; + } + + if(args.length < 5) { + MessageHelper.error(player, "You must provide a brightness type and value."); + return false; + } + + final var brightnessType = args[3]; + + if(!brightnessType.equalsIgnoreCase("block") && !brightnessType.equalsIgnoreCase("sky")) { + MessageHelper.error(player, "Invalid brightness type, valid options are BLOCK or SKY"); + return false; + } + + final var parsedNumber = NumberHelper.parseInt(args[4]); + + if(parsedNumber.isEmpty()) { + MessageHelper.error(player, "Invalid brightness value."); + return false; + } + + final var brightnessValue = parsedNumber.get(); + + if(brightnessValue < 0 || brightnessValue > 15) { + MessageHelper.error(player, "Invalid brightness value, must be between 0 and 15"); + return false; + } + + final var currentBrightness = displayData.getBrightness(); + final var blockBrightness = brightnessType.equalsIgnoreCase("block") ? brightnessValue : + currentBrightness == null ? 0 : currentBrightness.getBlockLight(); + final var skyBrightness = brightnessType.equalsIgnoreCase("sky") ? brightnessValue : + currentBrightness == null ? 0 : currentBrightness.getSkyLight(); + + displayData.setBrightness(new Display.Brightness(blockBrightness, skyBrightness)); + + if (FancyHolograms.get().getHologramConfiguration().isSaveOnChangedEnabled()) { + FancyHolograms.get().getHologramStorage().save(hologram); + } + + MessageHelper.success(player, "Changed " + brightnessType.toLowerCase() + " brightness to " + brightnessValue); + return true; + } +} diff --git a/src/main/java/de/oliver/fancyholograms/hologram/version/HologramImpl.java b/src/main/java/de/oliver/fancyholograms/hologram/version/HologramImpl.java index e95cccb4..e917665f 100644 --- a/src/main/java/de/oliver/fancyholograms/hologram/version/HologramImpl.java +++ b/src/main/java/de/oliver/fancyholograms/hologram/version/HologramImpl.java @@ -115,6 +115,8 @@ public void update() { // entity shadow fsDisplay.setShadowRadius(displayData.getShadowRadius()); fsDisplay.setShadowStrength(displayData.getShadowStrength()); + + fsDisplay.setViewRange(displayData.getVisibilityDistance()); } } diff --git a/src/main/java/de/oliver/fancyholograms/storage/FlatFileHologramStorage.java b/src/main/java/de/oliver/fancyholograms/storage/FlatFileHologramStorage.java index b426e6f6..aad9ecf7 100644 --- a/src/main/java/de/oliver/fancyholograms/storage/FlatFileHologramStorage.java +++ b/src/main/java/de/oliver/fancyholograms/storage/FlatFileHologramStorage.java @@ -213,6 +213,11 @@ private void saveConfig(YamlConfiguration config) { } finally { lock.writeLock().unlock(); } + + if(!FancyHolograms.canGet()) { + return; + } + FancyHolograms.get().getFancyLogger().debug("Saved config to file"); }); } diff --git a/src/main/java/de/oliver/fancyholograms/util/Constants.java b/src/main/java/de/oliver/fancyholograms/util/Constants.java index 8a6b291e..f8ff65c0 100644 --- a/src/main/java/de/oliver/fancyholograms/util/Constants.java +++ b/src/main/java/de/oliver/fancyholograms/util/Constants.java @@ -37,6 +37,7 @@ public enum Constants { <%primary_color%>- /hologram edit seeThrough - Enables/disables whether the text can be seen through blocks <%primary_color%>- /hologram edit shadowRadius - Changes the shadow radius of the hologram <%primary_color%>- /hologram edit shadowStrength - Changes the shadow strength of the hologram + <%primary_color%>- /hologram edit brightness <0-15> - Changes the brightness of the hologram <%primary_color%>- /hologram edit updateTextInterval - Sets the interval for updating the text """.replace("%primary_color%", MessageHelper.getPrimaryColor());