Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fancyinnovations.fancyholograms.api.data;

import com.fancyinnovations.fancyholograms.api.hologram.HologramType;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Display;
Expand All @@ -26,6 +27,8 @@ public class DisplayHologramData extends HologramData {
private float shadowRadius = DEFAULT_SHADOW_RADIUS;
private float shadowStrength = DEFAULT_SHADOW_STRENGTH;
private int interpolationDuration = DEFAULT_INTERPOLATION_DURATION;
private boolean glowing = false;
private NamedTextColor glowingColor = NamedTextColor.WHITE;

/**
* @param name Name of hologram
Expand Down Expand Up @@ -130,6 +133,32 @@ public DisplayHologramData setInterpolationDuration(int interpolationDuration) {
return this;
}

public boolean isGlowing() {
return glowing;
}

public DisplayHologramData setGlowing(boolean glowing) {
if (this.glowing != glowing) {
this.glowing = glowing;
setHasChanges(true);
}

return this;
}

public NamedTextColor getGlowingColor() {
return glowingColor;
}

public DisplayHologramData setGlowingColor(NamedTextColor glowingColor) {
if (!Objects.equals(this.glowingColor, glowingColor)) {
this.glowingColor = glowingColor;
setHasChanges(true);
}

return this;
}

@Override
@ApiStatus.Internal
public boolean read(ConfigurationSection section, String name) {
Expand Down Expand Up @@ -167,6 +196,9 @@ public boolean read(ConfigurationSection section, String name) {
);
}

glowing = section.getBoolean("glowing", false);
glowingColor = NamedTextColor.NAMES.value(section.getString("glowing_color", "white"));

return true;
}

Expand All @@ -189,6 +221,8 @@ public boolean write(ConfigurationSection section, String name) {
}

section.set("billboard", billboard != Display.Billboard.CENTER ? billboard.name().toLowerCase(Locale.ROOT) : null);
section.set("glowing", glowing);
section.set("glowing_color", glowingColor.toString());

return true;
}
Expand All @@ -203,10 +237,13 @@ public DisplayHologramData copy(String name) {
.setBillboard(this.getBillboard())
.setTranslation(this.getTranslation())
.setBrightness(this.getBrightness())
.setVisibilityDistance(this.getVisibilityDistance())
.setVisibility(this.getVisibility())
.setPersistent(this.isPersistent())
.setLinkedNpcName(this.getLinkedNpcName());
.setGlowing(this.isGlowing())
.setGlowingColor(this.getGlowingColor());

displayHologramData.setVisibilityDistance(this.getVisibilityDistance());
displayHologramData.setVisibility(this.getVisibility());
displayHologramData.setPersistent(this.isPersistent());
displayHologramData.setLinkedNpcName(this.getLinkedNpcName());

return displayHologramData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ public class TextHologramData extends DisplayHologramData {
public static final boolean DEFAULT_TEXT_SHADOW_STATE = false;
public static final boolean DEFAULT_SEE_THROUGH = false;
public static final int DEFAULT_TEXT_UPDATE_INTERVAL = -1;
public static final byte DEFAULT_TEXT_OPACITY = (byte) 255;

private List<String> text = new ArrayList<>(DEFAULT_TEXT);
private Color background = null;
private TextDisplay.TextAlignment textAlignment = DEFAULT_TEXT_ALIGNMENT;
private boolean textShadow = DEFAULT_TEXT_SHADOW_STATE;
private boolean seeThrough = DEFAULT_SEE_THROUGH;
private int textUpdateInterval = DEFAULT_TEXT_UPDATE_INTERVAL;
private byte textOpacity = DEFAULT_TEXT_OPACITY;

/**
* @param name Name of hologram
Expand Down Expand Up @@ -150,6 +152,19 @@ public TextHologramData setTextUpdateInterval(int textUpdateInterval) {
return this;
}

public byte getTextOpacity() {
return textOpacity;
}

public TextHologramData setTextOpacity(byte textOpacity) {
if (this.textOpacity != textOpacity) {
this.textOpacity = textOpacity;
setHasChanges(true);
}

return this;
}

@Override
@ApiStatus.Internal
public boolean read(ConfigurationSection section, String name) {
Expand All @@ -163,6 +178,7 @@ public boolean read(ConfigurationSection section, String name) {
textShadow = section.getBoolean("text_shadow", DEFAULT_TEXT_SHADOW_STATE);
seeThrough = section.getBoolean("see_through", DEFAULT_SEE_THROUGH);
textUpdateInterval = section.getInt("update_text_interval", DEFAULT_TEXT_UPDATE_INTERVAL);
textOpacity = (byte) section.getInt("text_opacity", DEFAULT_TEXT_OPACITY);

String textAlignmentStr = section.getString("text_alignment", DEFAULT_TEXT_ALIGNMENT.name().toLowerCase());
textAlignment = switch (textAlignmentStr.toLowerCase(Locale.ROOT)) {
Expand Down Expand Up @@ -197,6 +213,7 @@ public boolean write(ConfigurationSection section, String name) {
section.set("see_through", seeThrough);
section.set("text_alignment", textAlignment.name().toLowerCase(Locale.ROOT));
section.set("update_text_interval", textUpdateInterval);
section.set("text_opacity", textOpacity);

final String color;
if (background == null) {
Expand All @@ -223,6 +240,7 @@ public TextHologramData copy(String name) {
.setTextShadow(this.hasTextShadow())
.setSeeThrough(this.isSeeThrough())
.setTextUpdateInterval(this.getTextUpdateInterval())
.setTextOpacity(this.getTextOpacity())
.setScale(this.getScale())
.setShadowRadius(this.getShadowRadius())
.setShadowStrength(this.getShadowStrength())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,14 @@ public enum HologramModification {
BACKGROUND,
TEXT_SHADOW,
TEXT_ALIGNMENT,
TEXT_OPACITY,
SEE_THROUGH,
SHADOW_RADIUS,
SHADOW_STRENGTH,
UPDATE_TEXT_INTERVAL,
UPDATE_VISIBILITY_DISTANCE;
UPDATE_VISIBILITY_DISTANCE,
GLOWING,
GLOWING_COLOR;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.List;

public enum HologramType {
TEXT(Arrays.asList("background", "textshadow", "textalignment", "seethrough", "setline", "removeline", "addline", "insertbefore", "insertafter", "updatetextinterval")),
TEXT(Arrays.asList("background", "textshadow", "textalignment", "textopacity", "seethrough", "setline", "removeline", "addline", "insertbefore", "insertafter", "updatetextinterval")),
ITEM(List.of("item")),
BLOCK(List.of("block"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public final class HologramCMD extends Command {
<%primary_color%>- /hologram edit <hologram> background <color> <dark_gray>- <white>Changes the background of the hologram
<%primary_color%>- /hologram edit <hologram> textShadow <true|false> <dark_gray>- <white>Enables/disables the text shadow
<%primary_color%>- /hologram edit <hologram> textAlignment <alignment> <dark_gray>- <white>Sets the text alignment
<%primary_color%>- /hologram edit <hologram> textopacity <0-100> <dark_gray>- <white>Changes the opacity of the text hologram
<%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
Expand Down Expand Up @@ -192,7 +193,7 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String label, @No

final var usingNpcs = PluginUtils.isFancyNpcsEnabled();

List<String> suggestions = new ArrayList<>(Arrays.asList("traits", "position", "moveHere", "center", "moveTo", "rotate", "rotatepitch", "billboard", "scale", "translate", "visibilityDistance", "visibility", "shadowRadius", "shadowStrength", "brightness", usingNpcs ? "linkWithNpc" : "", usingNpcs ? "unlinkWithNpc" : ""));
List<String> suggestions = new ArrayList<>(Arrays.asList("traits", "position", "moveHere", "center", "moveTo", "rotate", "rotatepitch", "billboard", "scale", "translate", "visibilityDistance", "visibility", "shadowRadius", "shadowStrength", "brightness", "glowing", 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 @@ -256,6 +257,7 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String label, @No
case "block" -> Arrays.stream(Material.values()).filter(Material::isBlock).map(Enum::name);
case "seethrough" -> Stream.of("true", "false");
case "visibility" -> new VisibilityCMD().tabcompletion(sender, hologram, args).stream();
case "glowing" -> new GlowingCMD().tabcompletion(sender, hologram, args).stream();

default -> null;
};
Expand Down Expand Up @@ -364,6 +366,7 @@ private boolean edit(@NotNull final CommandSender player, @NotNull final Hologra
case "shadowradius" -> new ShadowRadiusCMD().run(player, hologram, args);
case "shadowstrength" -> new ShadowStrengthCMD().run(player, hologram, args);
case "brightness" -> new BrightnessCMD().run(player, hologram, args);
case "glowing" -> new GlowingCMD().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,130 @@
package com.fancyinnovations.fancyholograms.commands.hologram;

import com.fancyinnovations.fancyholograms.api.data.DisplayHologramData;
import com.fancyinnovations.fancyholograms.api.events.HologramUpdateEvent;
import com.fancyinnovations.fancyholograms.api.hologram.Hologram;
import com.fancyinnovations.fancyholograms.commands.HologramCMD;
import com.fancyinnovations.fancyholograms.commands.Subcommand;
import com.fancyinnovations.fancyholograms.main.FancyHologramsPlugin;
import com.fancyinnovations.fancyholograms.utils.GlowingColor;
import de.oliver.fancylib.MessageHelper;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Collectors;

public class GlowingCMD implements Subcommand {

@Override
public List<String> tabcompletion(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) {
if (args.length == 4) {
return Arrays.stream(GlowingColor.values())
.map(color -> color.name().toLowerCase(Locale.ROOT))
.collect(Collectors.toList());
}
return null;
}

@Override
public boolean run(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) {
if (!(player.hasPermission("fancyholograms.hologram.edit.glowing"))) {
MessageHelper.error(player, "You don't have the required permission to change the glowing of a hologram");
return false;
}

if (!(hologram.getData() instanceof DisplayHologramData displayData)) {
MessageHelper.error(player, "This command can only be used on display holograms");
return false;
}

if (hologram.getData().getType() == com.fancyinnovations.fancyholograms.api.hologram.HologramType.TEXT) {
MessageHelper.error(player, "You can only make item and block holograms glow");
return false;
}

if (args.length == 3) {
final var copied = displayData.copy(displayData.getName());
copied.setGlowing(!displayData.isGlowing());

if (!HologramCMD.callModificationEvent(hologram, player, copied, HologramUpdateEvent.HologramModification.GLOWING)) {
return false;
}

displayData.setGlowing(copied.isGlowing());

if (FancyHologramsPlugin.get().getHologramConfiguration().isSaveOnChangedEnabled()) {
FancyHologramsPlugin.get().getStorage().save(hologram.getData());
}

MessageHelper.success(player, "Toggled glowing " + (displayData.isGlowing() ? "on" : "off"));
return true;
}

final var colorArg = args[3].toLowerCase(Locale.ROOT);

if (colorArg.equals("disabled")) {
final var copied = displayData.copy(displayData.getName());
copied.setGlowing(false);

if (!HologramCMD.callModificationEvent(hologram, player, copied, HologramUpdateEvent.HologramModification.GLOWING)) {
return false;
}

displayData.setGlowing(false);

if (FancyHologramsPlugin.get().getHologramConfiguration().isSaveOnChangedEnabled()) {
FancyHologramsPlugin.get().getStorage().save(hologram.getData());
}

MessageHelper.success(player, "Disabled glowing");
return true;
}

NamedTextColor color;
try {
GlowingColor glowingColor = GlowingColor.valueOf(colorArg.toUpperCase(Locale.ROOT));
color = glowingColor.getColor();
if (color == null) {
MessageHelper.error(player, "Could not parse glowing color");
return false;
}
} catch (IllegalArgumentException e) {
MessageHelper.error(player, "Could not parse glowing color");
return false;
}

if (Objects.equals(color, displayData.getGlowingColor()) && displayData.isGlowing()) {
MessageHelper.warning(player, "This hologram already has this glowing color");
return false;
}

final var copied = displayData.copy(displayData.getName());
copied.setGlowingColor(color);
copied.setGlowing(true);

if (!HologramCMD.callModificationEvent(hologram, player, copied, HologramUpdateEvent.HologramModification.GLOWING_COLOR)) {
return false;
}

if (Objects.equals(copied.getGlowingColor(), displayData.getGlowingColor()) && displayData.isGlowing()) {
MessageHelper.warning(player, "This hologram already has this glowing color");
return false;
}

displayData.setGlowingColor(copied.getGlowingColor());
displayData.setGlowing(true);

if (FancyHologramsPlugin.get().getHologramConfiguration().isSaveOnChangedEnabled()) {
FancyHologramsPlugin.get().getStorage().save(hologram.getData());
}

MessageHelper.success(player, "Changed glowing color");
return true;
}

Check notice on line 129 in plugins/fancyholograms/src/main/java/com/fancyinnovations/fancyholograms/commands/hologram/GlowingCMD.java

View check run for this annotation

codefactor.io / CodeFactor

plugins/fancyholograms/src/main/java/com/fancyinnovations/fancyholograms/commands/hologram/GlowingCMD.java#L34-L129

Complex Method
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.fancyinnovations.fancyholograms.commands.lampCommands.hologram;

import com.fancyinnovations.fancyholograms.api.data.TextHologramData;
import com.fancyinnovations.fancyholograms.api.events.HologramUpdateEvent;
import com.fancyinnovations.fancyholograms.api.hologram.Hologram;
import com.fancyinnovations.fancyholograms.commands.HologramCMD;
import com.fancyinnovations.fancyholograms.commands.lampCommands.suggestions.OpacitySuggestion;
import com.fancyinnovations.fancyholograms.main.FancyHologramsPlugin;
import de.oliver.fancylib.MessageHelper;
import org.jetbrains.annotations.NotNull;
import revxrsal.commands.annotation.Command;
import revxrsal.commands.annotation.Description;
import revxrsal.commands.annotation.Range;
import revxrsal.commands.annotation.SuggestWith;
import revxrsal.commands.bukkit.actor.BukkitCommandActor;
import revxrsal.commands.bukkit.annotation.CommandPermission;

public final class OpacityCMD {

public static final OpacityCMD INSTANCE = new OpacityCMD();

private OpacityCMD() {
}

@Command("hologram-new edit <hologram> textopacity <opacity>")
@Description("Changes the opacity of the text hologram")
@CommandPermission("fancyholograms.hologram.edit.opacity")
public void setOpacity(
final @NotNull BukkitCommandActor actor,
final @NotNull Hologram hologram,
final @Range(min = 0, max = 100) @SuggestWith(OpacitySuggestion.class) int opacity
) {
if (!(hologram.getData() instanceof TextHologramData textData)) {
MessageHelper.error(actor.sender(), "This command can only be used on text holograms");
return;
}

// Convert percentage (0-100) to byte value (0-255)
final byte opacityByte = (byte) Math.round(opacity * 255.0 / 100.0);

if (opacityByte == textData.getTextOpacity()) {
MessageHelper.warning(actor.sender(), "This hologram already has opacity set to " + opacity + "%");
return;
}

final var copied = textData.copy(textData.getName());
copied.setTextOpacity(opacityByte);

if (!HologramCMD.callModificationEvent(hologram, actor.sender(), copied, HologramUpdateEvent.HologramModification.TEXT_OPACITY)) {
return;
}

textData.setTextOpacity(opacityByte);

if (FancyHologramsPlugin.get().getHologramConfiguration().isSaveOnChangedEnabled()) {
FancyHologramsPlugin.get().getStorage().save(hologram.getData());
}

MessageHelper.success(actor.sender(), "Changed text opacity to " + opacity + "%");
}
}
Loading