Skip to content

Commit db64f23

Browse files
committed
Implemented feature from here: #46
1 parent 9a5b6a6 commit db64f23

6 files changed

Lines changed: 103 additions & 1 deletion

File tree

plugins/fancyholograms/fh-api/src/main/java/com/fancyinnovations/fancyholograms/api/data/TextHologramData.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ public class TextHologramData extends DisplayHologramData {
2121
public static final boolean DEFAULT_TEXT_SHADOW_STATE = false;
2222
public static final boolean DEFAULT_SEE_THROUGH = false;
2323
public static final int DEFAULT_TEXT_UPDATE_INTERVAL = -1;
24+
public static final byte DEFAULT_TEXT_OPACITY = (byte) 255;
2425

2526
private List<String> text = new ArrayList<>(DEFAULT_TEXT);
2627
private Color background = null;
2728
private TextDisplay.TextAlignment textAlignment = DEFAULT_TEXT_ALIGNMENT;
2829
private boolean textShadow = DEFAULT_TEXT_SHADOW_STATE;
2930
private boolean seeThrough = DEFAULT_SEE_THROUGH;
3031
private int textUpdateInterval = DEFAULT_TEXT_UPDATE_INTERVAL;
32+
private byte textOpacity = DEFAULT_TEXT_OPACITY;
3133

3234
/**
3335
* @param name Name of hologram
@@ -150,6 +152,19 @@ public TextHologramData setTextUpdateInterval(int textUpdateInterval) {
150152
return this;
151153
}
152154

155+
public byte getTextOpacity() {
156+
return textOpacity;
157+
}
158+
159+
public TextHologramData setTextOpacity(byte textOpacity) {
160+
if (this.textOpacity != textOpacity) {
161+
this.textOpacity = textOpacity;
162+
setHasChanges(true);
163+
}
164+
165+
return this;
166+
}
167+
153168
@Override
154169
@ApiStatus.Internal
155170
public boolean read(ConfigurationSection section, String name) {
@@ -163,6 +178,7 @@ public boolean read(ConfigurationSection section, String name) {
163178
textShadow = section.getBoolean("text_shadow", DEFAULT_TEXT_SHADOW_STATE);
164179
seeThrough = section.getBoolean("see_through", DEFAULT_SEE_THROUGH);
165180
textUpdateInterval = section.getInt("update_text_interval", DEFAULT_TEXT_UPDATE_INTERVAL);
181+
textOpacity = (byte) section.getInt("text_opacity", DEFAULT_TEXT_OPACITY);
166182

167183
String textAlignmentStr = section.getString("text_alignment", DEFAULT_TEXT_ALIGNMENT.name().toLowerCase());
168184
textAlignment = switch (textAlignmentStr.toLowerCase(Locale.ROOT)) {
@@ -197,6 +213,7 @@ public boolean write(ConfigurationSection section, String name) {
197213
section.set("see_through", seeThrough);
198214
section.set("text_alignment", textAlignment.name().toLowerCase(Locale.ROOT));
199215
section.set("update_text_interval", textUpdateInterval);
216+
section.set("text_opacity", textOpacity);
200217

201218
final String color;
202219
if (background == null) {
@@ -223,6 +240,7 @@ public TextHologramData copy(String name) {
223240
.setTextShadow(this.hasTextShadow())
224241
.setSeeThrough(this.isSeeThrough())
225242
.setTextUpdateInterval(this.getTextUpdateInterval())
243+
.setTextOpacity(this.getTextOpacity())
226244
.setScale(this.getScale())
227245
.setShadowRadius(this.getShadowRadius())
228246
.setShadowStrength(this.getShadowStrength())

plugins/fancyholograms/fh-api/src/main/java/com/fancyinnovations/fancyholograms/api/events/HologramUpdateEvent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public enum HologramModification {
7878
BACKGROUND,
7979
TEXT_SHADOW,
8080
TEXT_ALIGNMENT,
81+
TEXT_OPACITY,
8182
SEE_THROUGH,
8283
SHADOW_RADIUS,
8384
SHADOW_STRENGTH,

plugins/fancyholograms/fh-api/src/main/java/com/fancyinnovations/fancyholograms/api/hologram/HologramType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.List;
55

66
public enum HologramType {
7-
TEXT(Arrays.asList("background", "textshadow", "textalignment", "seethrough", "setline", "removeline", "addline", "insertbefore", "insertafter", "updatetextinterval")),
7+
TEXT(Arrays.asList("background", "textshadow", "textalignment", "opacity", "seethrough", "setline", "removeline", "addline", "insertbefore", "insertafter", "updatetextinterval")),
88
ITEM(List.of("item")),
99
BLOCK(List.of("block"));
1010

plugins/fancyholograms/src/main/java/com/fancyinnovations/fancyholograms/commands/HologramCMD.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public final class HologramCMD extends Command {
4949
<%primary_color%>- /hologram edit <hologram> background <color> <dark_gray>- <white>Changes the background of the hologram
5050
<%primary_color%>- /hologram edit <hologram> textShadow <true|false> <dark_gray>- <white>Enables/disables the text shadow
5151
<%primary_color%>- /hologram edit <hologram> textAlignment <alignment> <dark_gray>- <white>Sets the text alignment
52+
<%primary_color%>- /hologram edit <hologram> opacity <0-100> <dark_gray>- <white>Changes the opacity of the text hologram
5253
<%primary_color%>- /hologram edit <hologram> seeThrough <true|false> <dark_gray>- <white>Enables/disables whether the text can be seen through blocks
5354
<%primary_color%>- /hologram edit <hologram> shadowRadius <value> <dark_gray>- <white>Changes the shadow radius of the hologram
5455
<%primary_color%>- /hologram edit <hologram> shadowStrength <value> <dark_gray>- <white>Changes the shadow strength of the hologram
@@ -376,6 +377,7 @@ private boolean edit(@NotNull final CommandSender player, @NotNull final Hologra
376377
case "insertafter" -> new InsertAfterCMD().run(player, hologram, args);
377378
case "textshadow" -> new TextShadowCMD().run(player, hologram, args);
378379
case "textalignment" -> new TextAlignmentCMD().run(player, hologram, args);
380+
case "opacity" -> new OpacityCMD().run(player, hologram, args);
379381
case "seethrough" -> new SeeThroughCMD().run(player, hologram, args);
380382

381383
// block data
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.fancyinnovations.fancyholograms.commands.hologram;
2+
3+
import com.fancyinnovations.fancyholograms.api.data.TextHologramData;
4+
import com.fancyinnovations.fancyholograms.api.events.HologramUpdateEvent;
5+
import com.fancyinnovations.fancyholograms.api.hologram.Hologram;
6+
import com.fancyinnovations.fancyholograms.commands.HologramCMD;
7+
import com.fancyinnovations.fancyholograms.commands.Subcommand;
8+
import com.fancyinnovations.fancyholograms.main.FancyHologramsPlugin;
9+
import com.fancyinnovations.fancyholograms.util.NumberHelper;
10+
import de.oliver.fancylib.MessageHelper;
11+
import org.bukkit.command.CommandSender;
12+
import org.jetbrains.annotations.NotNull;
13+
import org.jetbrains.annotations.Nullable;
14+
15+
import java.util.List;
16+
17+
public class OpacityCMD implements Subcommand {
18+
19+
@Override
20+
public List<String> tabcompletion(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) {
21+
return null;
22+
}
23+
24+
@Override
25+
public boolean run(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) {
26+
27+
if (!(player.hasPermission("fancyholograms.hologram.edit.opacity"))) {
28+
MessageHelper.error(player, "You don't have the required permission to edit a hologram");
29+
return false;
30+
}
31+
32+
if (!(hologram.getData() instanceof TextHologramData textData)) {
33+
MessageHelper.error(player, "This command can only be used on text holograms");
34+
return false;
35+
}
36+
37+
final var parsedNumber = NumberHelper.parseInt(args[3]);
38+
39+
if (parsedNumber.isEmpty()) {
40+
MessageHelper.error(player, "Invalid opacity value.");
41+
return false;
42+
}
43+
44+
final int opacityPercentage = parsedNumber.get();
45+
46+
if (opacityPercentage < 0 || opacityPercentage > 100) {
47+
MessageHelper.error(player, "Invalid opacity value, must be between 0 and 100");
48+
return false;
49+
}
50+
51+
// Convert percentage (0-100) to byte value (0-255)
52+
final byte opacity = (byte) Math.round(opacityPercentage * 255.0 / 100.0);
53+
54+
if (opacity == textData.getTextOpacity()) {
55+
MessageHelper.warning(player, "This hologram already has opacity set to " + opacityPercentage + "%");
56+
return false;
57+
}
58+
59+
final var copied = textData.copy(textData.getName());
60+
copied.setTextOpacity(opacity);
61+
62+
if (!HologramCMD.callModificationEvent(hologram, player, copied, HologramUpdateEvent.HologramModification.TEXT_OPACITY)) {
63+
return false;
64+
}
65+
66+
if (opacity == textData.getTextOpacity()) {
67+
MessageHelper.warning(player, "This hologram already has opacity set to " + opacityPercentage + "%");
68+
return false;
69+
}
70+
71+
textData.setTextOpacity(copied.getTextOpacity());
72+
73+
if (FancyHologramsPlugin.get().getHologramConfiguration().isSaveOnChangedEnabled()) {
74+
FancyHologramsPlugin.get().getStorage().save(hologram.getData());
75+
}
76+
77+
MessageHelper.success(player, "Changed text opacity to " + opacityPercentage + "%");
78+
return true;
79+
}
80+
}

plugins/fancyholograms/src/main/java/com/fancyinnovations/fancyholograms/hologram/HologramImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ private void syncWithData() {
151151
textDisplay.setStyleFlags((byte) 0);
152152
textDisplay.setShadow(textData.hasTextShadow());
153153
textDisplay.setSeeThrough(textData.isSeeThrough());
154+
textDisplay.setTextOpacity(textData.getTextOpacity());
154155

155156
switch (textData.getTextAlignment()) {
156157
case LEFT -> textDisplay.setAlignLeft(true);

0 commit comments

Comments
 (0)