diff --git a/src/main/java/net/minestom/server/entity/hologram/Hologram.java b/src/main/java/net/minestom/server/entity/hologram/Hologram.java index fc18fa61d..2eb296f2a 100644 --- a/src/main/java/net/minestom/server/entity/hologram/Hologram.java +++ b/src/main/java/net/minestom/server/entity/hologram/Hologram.java @@ -81,19 +81,39 @@ public class Hologram implements Viewable { armorStandMeta.setNotifyAboutChanges(false); + updateDefaultMeta(armorStandMeta); + + armorStandMeta.setNotifyAboutChanges(true); + + this.entity.setInstance(instance, spawnPosition.clone().add(0, getOffsetY(), 0)); + this.entity.setAutoViewable(autoViewable); + + this.position = spawnPosition; + setText(text); + } + + /** + * Sets the default {@link ArmorStandMeta} flags for this Hologram, + * subclasses may override this method to modify the metadata. + * + * @param armorStandMeta the meta to update + */ + protected void updateDefaultMeta(ArmorStandMeta armorStandMeta) { armorStandMeta.setSmall(true); armorStandMeta.setHasNoGravity(true); armorStandMeta.setCustomName(Component.empty()); armorStandMeta.setCustomNameVisible(true); armorStandMeta.setInvisible(true); + } - armorStandMeta.setNotifyAboutChanges(true); - - this.entity.setInstance(instance, spawnPosition.clone().add(0, OFFSET_Y, 0)); - this.entity.setAutoViewable(autoViewable); - - this.position = spawnPosition; - setText(text); + /** + * Vertical offset used to center the nametag, + * subclasses may override this method to modify the position + * + * @return the vertical offset used to center the nametag + */ + protected float getOffsetY() { + return OFFSET_Y; } /** @@ -112,7 +132,7 @@ public class Hologram implements Viewable { */ public void setPosition(Position position) { checkRemoved(); - position.add(0, OFFSET_Y, 0); + position.add(0, getOffsetY(), 0); this.position = position; this.entity.teleport(position); } diff --git a/src/main/java/net/minestom/server/entity/hologram/MarkerHologram.java b/src/main/java/net/minestom/server/entity/hologram/MarkerHologram.java new file mode 100644 index 000000000..c6cca422f --- /dev/null +++ b/src/main/java/net/minestom/server/entity/hologram/MarkerHologram.java @@ -0,0 +1,99 @@ +package net.minestom.server.entity.hologram; + +import net.kyori.adventure.text.Component; +import net.minestom.server.Viewable; +import net.minestom.server.chat.JsonMessage; +import net.minestom.server.entity.Entity; +import net.minestom.server.entity.EntityType; +import net.minestom.server.entity.Player; +import net.minestom.server.entity.metadata.other.ArmorStandMeta; +import net.minestom.server.instance.Instance; +import net.minestom.server.utils.Position; +import net.minestom.server.utils.validate.Check; +import org.jetbrains.annotations.NotNull; + +/** + * A hologram that sets the marker flag of the Armor Stand, allowing players to place blocks inside of it + */ +public class MarkerHologram extends Hologram { + + //Y Offset such that the spawnPosition represents the center of the rendered nametag + private static final float OFFSET_Y = -0.40625f; + + /** + * Constructs a new {@link MarkerHologram} with the given parameters. + * + * @param instance The instance where the hologram should be spawned. + * @param spawnPosition The spawn position of this hologram. + * @param text The text of this hologram. + * @param autoViewable {@code true}if the hologram should be visible automatically, otherwise {@code false}. + * @deprecated Use {@link #MarkerHologram(Instance, Position, Component, boolean)} + */ + public MarkerHologram(Instance instance, Position spawnPosition, JsonMessage text, boolean autoViewable) { + super(instance, spawnPosition, text, autoViewable); + } + + /** + * Constructs a new {@link MarkerHologram} with the given parameters. + * + * @param instance The instance where the hologram should be spawned. + * @param spawnPosition The spawn position of this hologram. + * @param text The text of this hologram. + * @deprecated Use {@link #MarkerHologram(Instance, Position, Component)} + */ + public MarkerHologram(Instance instance, Position spawnPosition, JsonMessage text) { + super(instance, spawnPosition, text); + } + + /** + * Constructs a new {@link MarkerHologram} with the given parameters. + * + * @param instance The instance where the hologram should be spawned. + * @param spawnPosition The spawn position of this hologram. + * @param text The text of this hologram. + */ + public MarkerHologram(Instance instance, Position spawnPosition, Component text) { + super(instance, spawnPosition, text); + } + + /** + * Constructs a new {@link MarkerHologram} with the given parameters. + * + * @param instance The instance where the hologram should be spawned. + * @param spawnPosition The spawn position of this hologram. + * @param text The text of this hologram. + * @param autoViewable {@code true}if the hologram should be visible automatically, otherwise {@code false}. + */ + public MarkerHologram(Instance instance, Position spawnPosition, Component text, boolean autoViewable) { + super(instance, spawnPosition, text, autoViewable); + } + + /** + * Sets the default {@link ArmorStandMeta} flags for this Hologram, + * subclasses may override this method to modify the metadata. + * + * {@link MarkerHologram}: Set the marker flag to true + * + * @param armorStandMeta the meta to update + */ + @Override + protected void updateDefaultMeta(ArmorStandMeta armorStandMeta) { + super.updateDefaultMeta(armorStandMeta); + + armorStandMeta.setMarker(true); + } + + /** + * Vertical offset used to center the nametag, + * subclasses may override this method to modify the position + * + * {@link MarkerHologram}: Correct the Y offset for marker Armor Stands + * + * @return + */ + @Override + protected float getOffsetY() { + return OFFSET_Y; + } +} +