Create MarkerHologram.java, modify Hologram.java to support subclasses overriding metadata easily

This commit is contained in:
BuildTools 2021-06-26 19:39:53 +08:00
parent e69c02f46d
commit eb4d29628e
2 changed files with 127 additions and 8 deletions

View File

@ -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);
}

View File

@ -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;
}
}