From eb4d29628ed295fb5af22b0146471f8b3f0598fc Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sat, 26 Jun 2021 19:39:53 +0800 Subject: [PATCH 1/4] Create MarkerHologram.java, modify Hologram.java to support subclasses overriding metadata easily --- .../server/entity/hologram/Hologram.java | 36 +++++-- .../entity/hologram/MarkerHologram.java | 99 +++++++++++++++++++ 2 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 src/main/java/net/minestom/server/entity/hologram/MarkerHologram.java 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; + } +} + From efbb2b6338b73bf4dc6533dfb33eb9e9bcf8ed88 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sat, 26 Jun 2021 19:47:29 +0800 Subject: [PATCH 2/4] Marker Armor Stands no longer prevent block placement --- .../minestom/server/listener/BlockPlacementListener.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java index 6d60411db..72f3833ea 100644 --- a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java +++ b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java @@ -6,6 +6,7 @@ import net.minestom.server.entity.Entity; import net.minestom.server.entity.EntityType; import net.minestom.server.entity.GameMode; import net.minestom.server.entity.Player; +import net.minestom.server.entity.metadata.other.ArmorStandMeta; import net.minestom.server.event.EventDispatcher; import net.minestom.server.event.player.PlayerBlockInteractEvent; import net.minestom.server.event.player.PlayerBlockPlaceEvent; @@ -132,6 +133,14 @@ public class BlockPlacementListener { entity.getEntityType() == EntityType.ITEM) continue; + // Marker Armor Stands should not prevent block placement + if(entity.getEntityType() == EntityType.ARMOR_STAND) { + ArmorStandMeta armorStandMeta = (ArmorStandMeta) entity.getEntityMeta(); + if(armorStandMeta.isMarker()) { + continue; + } + } + intersect = entity.getBoundingBox().intersect(blockPosition); if (intersect) break; From 6a2846a9c617c59af834af0132bc106027ebed47 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sun, 4 Jul 2021 13:58:57 +0800 Subject: [PATCH 3/4] Check whether entityMeta is an instanceof ArmorStandMeta, instead of checking entity type --- .../net/minestom/server/listener/BlockPlacementListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java index 72f3833ea..74fdf4c10 100644 --- a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java +++ b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java @@ -134,7 +134,7 @@ public class BlockPlacementListener { continue; // Marker Armor Stands should not prevent block placement - if(entity.getEntityType() == EntityType.ARMOR_STAND) { + if(entity.getEntityMeta() instanceof ArmorStandMeta) { ArmorStandMeta armorStandMeta = (ArmorStandMeta) entity.getEntityMeta(); if(armorStandMeta.isMarker()) { continue; From 323fcbe114691e77e1b90ab1f879454880122641 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sun, 4 Jul 2021 14:04:55 +0800 Subject: [PATCH 4/4] Remove MarkerHologram class, add parameter to Hologram constructor --- .../server/entity/hologram/Hologram.java | 58 +++++------ .../entity/hologram/MarkerHologram.java | 99 ------------------- 2 files changed, 29 insertions(+), 128 deletions(-) delete mode 100644 src/main/java/net/minestom/server/entity/hologram/MarkerHologram.java 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 2eb296f2a..0c680ff95 100644 --- a/src/main/java/net/minestom/server/entity/hologram/Hologram.java +++ b/src/main/java/net/minestom/server/entity/hologram/Hologram.java @@ -20,8 +20,10 @@ import java.util.Set; public class Hologram implements Viewable { private static final float OFFSET_Y = -0.9875f; + private static final float MARKER_OFFSET_Y = -0.40625f; private final Entity entity; + private final float yOffset; private Position position; private Component text; @@ -75,45 +77,43 @@ public class Hologram implements Viewable { * @param autoViewable {@code true}if the hologram should be visible automatically, otherwise {@code false}. */ public Hologram(Instance instance, Position spawnPosition, Component text, boolean autoViewable) { + this(instance, spawnPosition, text, autoViewable, false); + } + + /** + * Constructs a new {@link Hologram} 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 Hologram(Instance instance, Position spawnPosition, Component text, boolean autoViewable, boolean marker) { this.entity = new Entity(EntityType.ARMOR_STAND); ArmorStandMeta armorStandMeta = (ArmorStandMeta) entity.getEntityMeta(); 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); + if(marker) { + this.yOffset = MARKER_OFFSET_Y; + armorStandMeta.setMarker(true); + } else { + this.yOffset = OFFSET_Y; + armorStandMeta.setSmall(true); + } armorStandMeta.setHasNoGravity(true); armorStandMeta.setCustomName(Component.empty()); armorStandMeta.setCustomNameVisible(true); armorStandMeta.setInvisible(true); - } - /** - * 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; + armorStandMeta.setNotifyAboutChanges(true); + + this.entity.setInstance(instance, spawnPosition.clone().add(0, this.yOffset, 0)); + this.entity.setAutoViewable(autoViewable); + + this.position = spawnPosition; + setText(text); } /** @@ -132,7 +132,7 @@ public class Hologram implements Viewable { */ public void setPosition(Position position) { checkRemoved(); - position.add(0, getOffsetY(), 0); + position.add(0, this.yOffset, 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 deleted file mode 100644 index c6cca422f..000000000 --- a/src/main/java/net/minestom/server/entity/hologram/MarkerHologram.java +++ /dev/null @@ -1,99 +0,0 @@ -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; - } -} -