Skip to content

Commit

Permalink
Add support for advanced TextDisplay customization
Browse files Browse the repository at this point in the history
Implemented new properties for TextDisplay, including brightness, display dimensions, text opacity, scale, and see-through. Introduced serialization adapters for Brightness and Vector3f, alongside modifications to commands and character options to handle these customizations.
  • Loading branch information
NonSwag committed Jan 22, 2025
1 parent 0dffacb commit d38d4af
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 9 deletions.
31 changes: 31 additions & 0 deletions api/src/main/java/net/thenextlvl/character/tag/TagOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,53 @@
import core.nbt.serialization.TagSerializable;
import org.bukkit.Color;
import org.bukkit.entity.Display.Billboard;
import org.bukkit.entity.Display.Brightness;
import org.bukkit.entity.TextDisplay.TextAlignment;
import org.joml.Vector3f;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
public interface TagOptions extends TagSerializable {
Billboard getBillboard();

@Nullable
Brightness getBrightness();

@Nullable
Color getBackgroundColor();

TextAlignment getAlignment();

Vector3f getScale();

boolean isDefaultBackground();

boolean isSeeThrough();

boolean setAlignment(TextAlignment alignment);

boolean setBackgroundColor(@Nullable Color color);

boolean setBillboard(Billboard billboard);

boolean setBrightness(@Nullable Brightness brightness);

boolean setDefaultBackground(boolean enabled);

boolean setDisplayHeight(float height);

boolean setDisplayWidth(float width);

boolean setScale(Vector3f vector3f);

boolean setSeeThrough(boolean seeThrough);

boolean setTextOpacity(byte opacity);

byte getTextOpacity();

float getDisplayHeight();

float getDisplayWidth();
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.thenextlvl.character.plugin.listener.test;
import net.thenextlvl.character.plugin.serialization.ActionTypeAdapter;
import net.thenextlvl.character.plugin.serialization.AddressAdapter;
import net.thenextlvl.character.plugin.serialization.BrightnessAdapter;
import net.thenextlvl.character.plugin.serialization.CharacterSerializer;
import net.thenextlvl.character.plugin.serialization.ClickActionAdapter;
import net.thenextlvl.character.plugin.serialization.ComponentAdapter;
Expand All @@ -45,16 +46,19 @@
import net.thenextlvl.character.plugin.serialization.SoundAdapter;
import net.thenextlvl.character.plugin.serialization.TitleAdapter;
import net.thenextlvl.character.plugin.serialization.TitleTimesAdapter;
import net.thenextlvl.character.plugin.serialization.Vector3fAdapter;
import net.thenextlvl.character.plugin.serialization.WorldAdapter;
import org.bstats.bukkit.Metrics;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Display.Brightness;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.Unmodifiable;
import org.joml.Vector3f;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

Expand All @@ -81,6 +85,7 @@ public class CharacterPlugin extends JavaPlugin {

private final NBT nbt = NBT.builder()
.registerTypeHierarchyAdapter(ActionType.class, new ActionTypeAdapter(this))
.registerTypeHierarchyAdapter(Brightness.class, new BrightnessAdapter())
.registerTypeHierarchyAdapter(Character.class, new CharacterSerializer())
.registerTypeHierarchyAdapter(ClickAction.class, new ClickActionAdapter())
.registerTypeHierarchyAdapter(Component.class, new ComponentAdapter())
Expand All @@ -93,6 +98,7 @@ public class CharacterPlugin extends JavaPlugin {
.registerTypeHierarchyAdapter(Sound.class, new SoundAdapter())
.registerTypeHierarchyAdapter(Title.Times.class, new TitleTimesAdapter())
.registerTypeHierarchyAdapter(Title.class, new TitleAdapter())
.registerTypeHierarchyAdapter(Vector3f.class, new Vector3fAdapter())
.registerTypeHierarchyAdapter(World.class, new WorldAdapter(getServer()))
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.bukkit.attribute.Attributable;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Display.Billboard;
import org.bukkit.entity.Display.Brightness;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
Expand All @@ -29,6 +30,7 @@
import org.bukkit.entity.TextDisplay.TextAlignment;
import org.bukkit.metadata.FixedMetadataValue;
import org.jetbrains.annotations.Unmodifiable;
import org.joml.Vector3f;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

Expand Down Expand Up @@ -598,15 +600,27 @@ private File file() {
}

private class PaperTagOptions implements TagOptions {
private @Nullable Brightness brightness = new Brightness(15, 0);
private @Nullable Color backgroundColor = null;
private Billboard billboard = Billboard.CENTER;
private TextAlignment alignment = TextAlignment.CENTER;
private Vector3f scale = new Vector3f(1);
private boolean defaultBackground = false;
private boolean seeThrough = false;
private byte textOpacity;
private float displayHeight;
private float displayWidth;

@Override
public Billboard getBillboard() {
return billboard;
}

@Override
public @Nullable Brightness getBrightness() {
return brightness;
}

@Override
public @Nullable Color getBackgroundColor() {
return backgroundColor;
Expand All @@ -617,6 +631,21 @@ public TextAlignment getAlignment() {
return alignment;
}

@Override
public Vector3f getScale() {
return scale;
}

@Override
public boolean isDefaultBackground() {
return defaultBackground;
}

@Override
public boolean isSeeThrough() {
return seeThrough;
}

@Override
public boolean setAlignment(TextAlignment alignment) {
if (Objects.equals(alignment, this.alignment)) return false;
Expand All @@ -641,21 +670,106 @@ public boolean setBillboard(Billboard billboard) {
return true;
}

@Override
public boolean setBrightness(@Nullable Brightness brightness) {
if (Objects.equals(brightness, this.brightness)) return false;
this.brightness = brightness;
getEntity().ifPresent(PaperCharacter.this::updateDisplayName);
return true;
}

@Override
public boolean setDefaultBackground(boolean enabled) {
if (enabled == defaultBackground) return false;
this.defaultBackground = enabled;
getEntity().ifPresent(PaperCharacter.this::updateDisplayName);
return true;
}

@Override
public boolean setDisplayHeight(float height) {
if (height == displayHeight) return false;
this.displayHeight = height;
getEntity().ifPresent(PaperCharacter.this::updateDisplayName);
return true;
}

@Override
public boolean setDisplayWidth(float width) {
if (width == displayWidth) return false;
this.displayWidth = width;
getEntity().ifPresent(PaperCharacter.this::updateDisplayName);
return true;
}

@Override
public boolean setScale(Vector3f vector3f) {
if (Objects.equals(vector3f, this.scale)) return false;
this.scale = vector3f;
getEntity().ifPresent(PaperCharacter.this::updateDisplayName);
return true;
}

@Override
public boolean setSeeThrough(boolean seeThrough) {
if (seeThrough == this.seeThrough) return false;
this.seeThrough = seeThrough;
getEntity().ifPresent(PaperCharacter.this::updateDisplayName);
return true;
}

@Override
public boolean setTextOpacity(byte opacity) {
if (opacity == textOpacity) return false;
this.textOpacity = opacity;
getEntity().ifPresent(PaperCharacter.this::updateDisplayName);
return true;
}

@Override
public byte getTextOpacity() {
return textOpacity;
}

@Override
public float getDisplayHeight() {
return displayHeight;
}

@Override
public float getDisplayWidth() {
return displayWidth;
}

@Override
public Tag serialize() throws ParserException {
var tag = new CompoundTag();
if (backgroundColor != null) tag.add("backgroundColor", backgroundColor.asARGB());
if (brightness != null) tag.add("brightness", plugin.nbt().toTag(brightness));
tag.add("alignment", alignment.name());
tag.add("billboard", billboard.name());
tag.add("defaultBackground", defaultBackground);
tag.add("displayHeight", displayHeight);
tag.add("displayWidth", displayWidth);
tag.add("scale", plugin.nbt().toTag(scale));
tag.add("seeThrough", seeThrough);
tag.add("textOpacity", textOpacity);
return tag;
}

@Override
public void deserialize(Tag tag) throws ParserException {
var root = tag.getAsCompound();
root.optional("alignment").map(Tag::getAsString).map(TextAlignment::valueOf).ifPresent(this::setAlignment);
root.optional("backgroundColor").map(Tag::getAsInt).map(Color::fromARGB).ifPresent(this::setBackgroundColor);
root.optional("billboard").map(Tag::getAsString).map(Billboard::valueOf).ifPresent(this::setBillboard);
root.optional("defaultBackground").map(Tag::getAsBoolean).ifPresent(this::setDefaultBackground);
root.optional("displayHeight").map(Tag::getAsFloat).ifPresent(this::setDisplayHeight);
root.optional("displayWidth").map(Tag::getAsFloat).ifPresent(this::setDisplayWidth);
root.optional("scale").map(t -> plugin.nbt().fromTag(t, Vector3f.class)).ifPresent(this::setScale);
root.optional("seeThrough").map(Tag::getAsBoolean).ifPresent(this::setSeeThrough);
root.optional("textOpacity").map(Tag::getAsByte).ifPresent(this::setTextOpacity);
setBackgroundColor(root.optional("backgroundColor").map(Tag::getAsInt).map(Color::fromARGB).orElse(null));
setBrightness(root.optional("brightness").map(t -> plugin.nbt().fromTag(t, Brightness.class)).orElse(null));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.bukkit.scoreboard.Team;
import org.bukkit.scoreboard.Team.Option;
import org.bukkit.scoreboard.Team.OptionStatus;
import org.bukkit.util.Transformation;
import org.jetbrains.annotations.NotNull;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
Expand Down Expand Up @@ -383,9 +384,21 @@ private void updateDisplayNameHologram(TextDisplay display) {
if (tagOptions.getBackgroundColor() != null)
display.setBackgroundColor(tagOptions.getBackgroundColor());
display.setBillboard(tagOptions.getBillboard());
display.setBrightness(tagOptions.getBrightness());
display.setDefaultBackground(tagOptions.isDefaultBackground());
display.setDisplayHeight(tagOptions.getDisplayHeight());
display.setDisplayWidth(tagOptions.getDisplayWidth());
display.setGravity(false);
display.setPersistent(false);
display.setSeeThrough(tagOptions.isSeeThrough());
display.setTeleportDuration(3);
display.setTextOpacity(tagOptions.getTextOpacity());
display.setTransformation(new Transformation(
display.getTransformation().getTranslation(),
display.getTransformation().getLeftRotation(),
tagOptions.getScale(),
display.getTransformation().getRightRotation()
));
var component = displayName == null ? Component.text(getName()) : displayName;
display.text(component.colorIfAbsent(teamColor));
}
Expand Down
Loading

0 comments on commit d38d4af

Please sign in to comment.