Skip to content

Commit

Permalink
Add support for see through Text Holograms (#85)
Browse files Browse the repository at this point in the history
* Initial see through implementation

* Added command implementation

* Added storage implementation

* Added missing references
  • Loading branch information
OakLoaf authored May 7, 2024
1 parent f12616b commit 4de65ca
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 4 deletions.
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", "setline", "removeline", "addline", "insertbefore", "insertafter", "updatetextinterval")),
TEXT(Arrays.asList("background", "textshadow", "textalignment", "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 @@ -14,19 +14,22 @@ public class TextHologramData implements Data {

public static final TextDisplay.TextAlignment DEFAULT_TEXT_ALIGNMENT = TextDisplay.TextAlignment.CENTER;
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;

private List<String> text;
private TextColor background;
private TextDisplay.TextAlignment textAlignment;
private boolean textShadow;
private boolean seeThrough;
private int textUpdateInterval;

public TextHologramData(List<String> text, TextColor background, TextDisplay.TextAlignment textAlignment, boolean textShadow, int textUpdateInterval) {
public TextHologramData(List<String> text, TextColor background, TextDisplay.TextAlignment textAlignment, boolean textShadow, boolean seeThrough, int textUpdateInterval) {
this.text = text;
this.background = background;
this.textAlignment = textAlignment;
this.textShadow = textShadow;
this.seeThrough = seeThrough;
this.textUpdateInterval = textUpdateInterval;
}

Expand All @@ -42,6 +45,7 @@ public static TextHologramData getDefault(String name) {
null,
DEFAULT_TEXT_ALIGNMENT,
DEFAULT_TEXT_SHADOW_STATE,
DEFAULT_SEE_THROUGH,
DEFAULT_TEXT_UPDATE_INTERVAL
);
}
Expand Down Expand Up @@ -80,6 +84,7 @@ public void read(ConfigurationSection section, String name) {
public void write(ConfigurationSection section, String name) {
section.set("text", text);
section.set("text_shadow", textShadow);
section.set("see_through", seeThrough);
section.set("text_alignment", textAlignment.name().toLowerCase(Locale.ROOT));
section.set("update_text_interval", textUpdateInterval);

Expand Down Expand Up @@ -141,6 +146,15 @@ public TextHologramData setTextShadow(boolean textShadow) {
return this;
}

public boolean isSeeThrough() {
return seeThrough;
}

public TextHologramData setSeeThrough(boolean seeThrough) {
this.seeThrough = seeThrough;
return this;
}

public int getTextUpdateInterval() {
return textUpdateInterval;
}
Expand All @@ -157,6 +171,7 @@ public Data copy() {
background,
textAlignment,
textShadow,
seeThrough,
textUpdateInterval
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public enum HologramModification {
* @see HologramData#getTextAlignment()
*/
TEXT_ALIGNMENT,
SEE_THROUGH,
/**
* @see HologramData#getShadowRadius()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ public void update() {
textDisplay.setFlags((byte) (textDisplay.getFlags() & ~TextDisplay.FLAG_ALIGN_LEFT));
}

// see through
if (textData.isSeeThrough()) {
textDisplay.setFlags((byte) (textDisplay.getFlags() | TextDisplay.FLAG_SEE_THROUGH));
} else {
textDisplay.setFlags((byte) (textDisplay.getFlags() & ~TextDisplay.FLAG_SEE_THROUGH));
}

if (textData.getTextAlignment() == org.bukkit.entity.TextDisplay.TextAlignment.RIGHT) {
textDisplay.setFlags((byte) (textDisplay.getFlags() | TextDisplay.FLAG_ALIGN_RIGHT));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ public void update() {
textDisplay.setFlags((byte) (textDisplay.getFlags() & ~TextDisplay.FLAG_ALIGN_LEFT));
}

// see through
if (textData.isSeeThrough()) {
textDisplay.setFlags((byte) (textDisplay.getFlags() | TextDisplay.FLAG_SEE_THROUGH));
} else {
textDisplay.setFlags((byte) (textDisplay.getFlags() & ~TextDisplay.FLAG_SEE_THROUGH));
}

if (textData.getTextAlignment() == org.bukkit.entity.TextDisplay.TextAlignment.RIGHT) {
textDisplay.setFlags((byte) (textDisplay.getFlags() | TextDisplay.FLAG_ALIGN_RIGHT));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ public void update() {
textDisplay.setFlags((byte) (textDisplay.getFlags() & ~TextDisplay.FLAG_ALIGN_LEFT));
}

// see through
if (textData.isSeeThrough()) {
textDisplay.setFlags((byte) (textDisplay.getFlags() | TextDisplay.FLAG_SEE_THROUGH));
} else {
textDisplay.setFlags((byte) (textDisplay.getFlags() & ~TextDisplay.FLAG_SEE_THROUGH));
}

if (textData.getTextAlignment() == org.bukkit.entity.TextDisplay.TextAlignment.RIGHT) {
textDisplay.setFlags((byte) (textDisplay.getFlags() | TextDisplay.FLAG_ALIGN_RIGHT));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ public void update() {
textDisplay.setFlags((byte) (textDisplay.getFlags() & ~TextDisplay.FLAG_ALIGN_LEFT));
}

// see through
if (textData.isSeeThrough()) {
textDisplay.setFlags((byte) (textDisplay.getFlags() | TextDisplay.FLAG_SEE_THROUGH));
} else {
textDisplay.setFlags((byte) (textDisplay.getFlags() & ~TextDisplay.FLAG_SEE_THROUGH));
}

if (textData.getTextAlignment() == org.bukkit.entity.TextDisplay.TextAlignment.RIGHT) {
textDisplay.setFlags((byte) (textDisplay.getFlags() | TextDisplay.FLAG_ALIGN_RIGHT));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String label, @No
yield FancyNpcsPlugin.get().getNpcManager().getAllNpcs().stream().map(npc -> npc.getData().getName());
}
case "block" -> Arrays.stream(Material.values()).filter(Material::isBlock).map(Enum::name);
case "visiblebydefault" -> Stream.of("true", "false");
case "seethrough", "visiblebydefault" -> Stream.of("true", "false");

default -> null;
};
Expand Down Expand Up @@ -311,6 +311,7 @@ private boolean edit(@NotNull final CommandSender player, @NotNull final Hologra
case "insertafter" -> new InsertAfterCMD().run(player, hologram, args);
case "textshadow" -> new TextShadowCMD().run(player, hologram, args);
case "textalignment" -> new TextAlignmentCMD().run(player, hologram, args);
case "seethrough" -> new SeeThroughCMD().run(player, hologram, args);

// block data
case "block" -> new BlockCMD().run(player, hologram, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public boolean run(@NotNull CommandSender player, @Nullable Hologram hologram, @
}

MessageHelper.info(player, "Text alignment: <gray>" + textData.getTextAlignment().name());
MessageHelper.info(player, "See through: <gray>" + (textData.isSeeThrough() ? "enabled" : "disabled"));
MessageHelper.info(player, "Text shadow: <gray>" + (textData.isTextShadow() ? "enabled" : "disabled"));
if (textData.getTextUpdateInterval() == -1) {
MessageHelper.info(player, "Update text interval: <gray>not updating");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package de.oliver.fancyholograms.commands.hologram;

import de.oliver.fancyholograms.FancyHolograms;
import de.oliver.fancyholograms.api.Hologram;
import de.oliver.fancyholograms.api.data.TextHologramData;
import de.oliver.fancyholograms.api.events.HologramUpdateEvent;
import de.oliver.fancyholograms.commands.HologramCMD;
import de.oliver.fancyholograms.commands.Subcommand;
import de.oliver.fancylib.MessageHelper;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Locale;

public class SeeThroughCMD 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().getTypeData() instanceof TextHologramData textData)) {
MessageHelper.error(player, "This command can only be used on text holograms");
return false;
}


final var enabled = switch (args[3].toLowerCase(Locale.ROOT)) {
case "true" -> true;
case "false" -> false;
default -> null;
};

if (enabled == null) {
MessageHelper.error(player, "Could not parse see through flag");
return false;
}

if (enabled == textData.isSeeThrough()) {
MessageHelper.warning(player, "This hologram already has see through " + (enabled ? "enabled" : "disabled"));
return false;
}

final var copied = hologram.getData().copy();
((TextHologramData) copied.getTypeData()).setSeeThrough(enabled);

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

if (enabled == textData.isSeeThrough()) {
MessageHelper.warning(player, "This hologram already has see through " + (enabled ? "enabled" : "disabled"));
return false;
}

textData.setSeeThrough(((TextHologramData) copied.getTypeData()).isSeeThrough());

if (FancyHolograms.get().getHologramConfiguration().isSaveOnChangedEnabled()) {
FancyHolograms.get().getHologramStorage().save(hologram);
}

MessageHelper.success(player, "Changed see through");
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public static HologramData readHologram(String name, ConfigurationSection config

final var text = config.getStringList("text");
final var textHasShadow = config.getBoolean("text_shadow", TextHologramData.DEFAULT_TEXT_SHADOW_STATE);
final var isSeeThrough = config.getBoolean("see_through", TextHologramData.DEFAULT_SEE_THROUGH);
final var textUpdateInterval = config.getInt("update_text_interval", TextHologramData.DEFAULT_TEXT_UPDATE_INTERVAL);
final var visibilityDistance = config.getInt("visibility_distance", DisplayHologramData.DEFAULT_VISIBILITY_DISTANCE);
final var scaleX = config.getDouble("scale_x", 1);
Expand Down Expand Up @@ -260,7 +261,7 @@ public static HologramData readHologram(String name, ConfigurationSection config

DisplayHologramData displayData = new DisplayHologramData(location, billboard, new Vector3f((float) scaleX, (float) scaleY, (float) scaleZ), DisplayHologramData.DEFAULT_TRANSLATION, null, (float) shadowRadius, (float) shadowStrength, visibilityDistance, linkedNpc, visibleByDefault);

TextHologramData textData = new TextHologramData(text, background, textAlignment, textHasShadow, textUpdateInterval);
TextHologramData textData = new TextHologramData(text, background, textAlignment, textHasShadow, isSeeThrough, textUpdateInterval);

return new HologramData(name, displayData, HologramType.TEXT, textData);
}
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 @@ -33,6 +33,7 @@ public enum Constants {
<%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> 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> updateTextInterval <seconds> <dark_gray>- <white>Sets the interval for updating the text
Expand Down

0 comments on commit 4de65ca

Please sign in to comment.