Skip to content

Commit

Permalink
Fixes #148 and resolves #140 (#149)
Browse files Browse the repository at this point in the history
* fixes #148

* add brightness command

* fix brightness command NPE

* fix visibility distance

* add brightness command to help
  • Loading branch information
BitByLogics authored Nov 3, 2024
1 parent 961c735 commit 029c783
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ public void update() {
// entity shadow
display.setShadowRadius(displayData.getShadowRadius());
display.setShadowStrength(displayData.getShadowStrength());

// view range
display.setViewRange(displayData.getVisibilityDistance());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ public void update() {
// entity shadow
display.setShadowRadius(displayData.getShadowRadius());
display.setShadowStrength(displayData.getShadowStrength());

// view range
display.setViewRange(displayData.getVisibilityDistance());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ public void update() {
// entity shadow
display.setShadowRadius(displayData.getShadowRadius());
display.setShadowStrength(displayData.getShadowStrength());

// view range
display.setViewRange(displayData.getVisibilityDistance());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ public void update() {
// entity shadow
display.setShadowRadius(displayData.getShadowRadius());
display.setShadowStrength(displayData.getShadowStrength());

// view range
display.setViewRange(displayData.getVisibilityDistance());
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/de/oliver/fancyholograms/FancyHolograms.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String label, @No

final var usingNpcs = PluginUtils.isFancyNpcsEnabled();

List<String> suggestions = new ArrayList<>(Arrays.asList("position", "moveHere", "center", "moveTo", "rotate", "rotatepitch", "billboard", "scale", "visibilityDistance", "visibility", "shadowRadius", "shadowStrength", usingNpcs ? "linkWithNpc" : "", usingNpcs ? "unlinkWithNpc" : ""));
List<String> 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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public void update() {
// entity shadow
fsDisplay.setShadowRadius(displayData.getShadowRadius());
fsDisplay.setShadowStrength(displayData.getShadowStrength());

fsDisplay.setViewRange(displayData.getVisibilityDistance());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/de/oliver/fancyholograms/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public enum Constants {
<%primary_color%>- /hologram edit <hologram> seeThrough <true|false> <dark_gray>- <white>Enables/disables whether the text can be seen through blocks
<%primary_color%>- /hologram edit <hologram> shadowRadius <value> <dark_gray>- <white>Changes the shadow radius of the hologram
<%primary_color%>- /hologram edit <hologram> shadowStrength <value> <dark_gray>- <white>Changes the shadow strength of the hologram
<%primary_color%>- /hologram edit <hologram> brightness <block|sky> <0-15> <dark_gray>- <white>Changes the brightness of the hologram
<%primary_color%>- /hologram edit <hologram> updateTextInterval <seconds> <dark_gray>- <white>Sets the interval for updating the text
""".replace("%primary_color%", MessageHelper.getPrimaryColor());

Expand Down

0 comments on commit 029c783

Please sign in to comment.