diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/HologramOwner.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/HologramOwner.java index a1b539afd..ab18e61b3 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/HologramOwner.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/HologramOwner.java @@ -1,11 +1,9 @@ package io.github.thebusybiscuit.slimefun4.core.attributes; import javax.annotation.Nonnull; -import javax.annotation.Nullable; import org.bukkit.Location; import org.bukkit.block.Block; -import org.bukkit.entity.ArmorStand; import org.bukkit.util.Vector; import io.github.thebusybiscuit.cscorelib2.chat.ChatColors; @@ -35,17 +33,7 @@ public interface HologramOwner extends ItemAttribute { */ default void updateHologram(@Nonnull Block b, @Nonnull String text) { Location loc = b.getLocation().add(getHologramOffset()); - - SlimefunPlugin.getHologramsService().updateHologram(loc, hologram -> { - hologram.setCustomName(ChatColors.color(text)); - hologram.setCustomNameVisible(true); - }); - } - - @Nullable - default ArmorStand getHologram(@Nonnull Block b, boolean createIfNoneExists) { - Location loc = b.getLocation().add(getHologramOffset()); - return SlimefunPlugin.getHologramsService().getHologram(loc, createIfNoneExists); + SlimefunPlugin.getHologramsService().setHologramLabel(loc, ChatColors.color(text)); } /** diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/holograms/CachedArmorStand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/holograms/CachedArmorStand.java deleted file mode 100644 index e49703e1f..000000000 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/holograms/CachedArmorStand.java +++ /dev/null @@ -1,79 +0,0 @@ -package io.github.thebusybiscuit.slimefun4.core.services.holograms; - -import java.util.UUID; -import java.util.concurrent.TimeUnit; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import org.bukkit.Bukkit; -import org.bukkit.entity.ArmorStand; -import org.bukkit.entity.Entity; - -/** - * This represents an {@link ArmorStand} or hologram that can expire. - * - * @author TheBusyBiscuit - * - */ -class CachedArmorStand { - - /** - * This is the minimum duration after which the {@link CachedArmorStand} will expire. - */ - private static final long EXPIRES_AFTER = TimeUnit.MINUTES.toMillis(10); - - /** - * The {@link UUID} of the {@link ArmorStand}. - */ - private final UUID uniqueId; - - /** - * The timestamp of when the {@link ArmorStand} was last accessed. - */ - private long lastAccess; - - /** - * This creates a new {@link CachedArmorStand} for the given {@link UUID}. - * - * @param uniqueId - * The {@link UUID} of the corresponding {@link ArmorStand} - */ - public CachedArmorStand(@Nonnull UUID uniqueId) { - this.uniqueId = uniqueId; - this.lastAccess = System.currentTimeMillis(); - } - - /** - * This returns the corresponding {@link ArmorStand} - * and also updates the "lastAccess" timestamp. - *
- * If the {@link ArmorStand} was removed, it will return null. - * - * @return The {@link ArmorStand} or null. - */ - @Nullable - public ArmorStand getArmorStand() { - Entity n = Bukkit.getEntity(uniqueId); - - if (n instanceof ArmorStand && n.isValid()) { - this.lastAccess = System.currentTimeMillis(); - return (ArmorStand) n; - } else { - this.lastAccess = 0; - return null; - } - } - - /** - * This returns whether this {@link CachedArmorStand} has expired. - * The armorstand will expire if the last access has been more than 10 - * minutes ago. - * - * @return Whether this {@link CachedArmorStand} has expired - */ - public boolean isExpired() { - return System.currentTimeMillis() - lastAccess > EXPIRES_AFTER; - } - -} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/holograms/Hologram.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/holograms/Hologram.java new file mode 100644 index 000000000..1a6db53c8 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/holograms/Hologram.java @@ -0,0 +1,136 @@ +package io.github.thebusybiscuit.slimefun4.core.services.holograms; + +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import org.bukkit.Bukkit; +import org.bukkit.entity.ArmorStand; +import org.bukkit.entity.Entity; + +import com.google.common.base.Objects; + +/** + * This represents an {@link ArmorStand} that can expire and be renamed. + * + * @author TheBusyBiscuit + * + */ +class Hologram { + + /** + * This is the minimum duration after which the {@link Hologram} will expire. + */ + private static final long EXPIRES_AFTER = TimeUnit.MINUTES.toMillis(10); + + /** + * The {@link UUID} of the {@link ArmorStand}. + */ + private final UUID uniqueId; + + /** + * The timestamp of when the {@link ArmorStand} was last accessed. + */ + private long lastAccess; + + /** + * The label of this {@link Hologram}. + */ + private String label; + + /** + * This creates a new {@link Hologram} for the given {@link UUID}. + * + * @param uniqueId + * The {@link UUID} of the corresponding {@link ArmorStand} + */ + Hologram(@Nonnull UUID uniqueId) { + this.uniqueId = uniqueId; + this.lastAccess = System.currentTimeMillis(); + } + + /** + * This returns the corresponding {@link ArmorStand} + * and also updates the "lastAccess" timestamp. + *
+ * If the {@link ArmorStand} was removed, it will return null.
+ *
+ * @return The {@link ArmorStand} or null.
+ */
+ @Nullable
+ ArmorStand getArmorStand() {
+ Entity n = Bukkit.getEntity(uniqueId);
+
+ if (n instanceof ArmorStand && n.isValid()) {
+ this.lastAccess = System.currentTimeMillis();
+ return (ArmorStand) n;
+ } else {
+ this.lastAccess = 0;
+ return null;
+ }
+ }
+
+ /**
+ * This checks if the associated {@link ArmorStand} has despawned.
+ *
+ * @return Whether the {@link ArmorStand} despawned
+ */
+ boolean hasDespawned() {
+ return getArmorStand() == null;
+ }
+
+ /**
+ * This returns whether this {@link Hologram} has expired.
+ * The armorstand will expire if the last access has been more than 10
+ * minutes ago.
+ *
+ * @return Whether this {@link Hologram} has expired
+ */
+ boolean hasExpired() {
+ return System.currentTimeMillis() - lastAccess > EXPIRES_AFTER;
+ }
+
+ /**
+ * This method sets the label of this {@link Hologram}.
+ *
+ * @param label
+ * The label to set
+ */
+ void setLabel(@Nullable String label) {
+ if (Objects.equal(this.label, label)) {
+ /*
+ * Label is already set, no need to cause an entity
+ * update. But we can update the lastAccess flag.
+ */
+ this.lastAccess = System.currentTimeMillis();
+ } else {
+ this.label = label;
+ ArmorStand entity = getArmorStand();
+
+ if (entity != null) {
+ if (label != null) {
+ entity.setCustomNameVisible(true);
+ entity.setCustomName(label);
+ } else {
+ entity.setCustomNameVisible(false);
+ entity.setCustomName(null);
+ }
+ }
+ }
+ }
+
+ /**
+ * This will remove the {@link ArmorStand} and expire this {@link Hologram}.
+ */
+ void remove() {
+ ArmorStand armorstand = getArmorStand();
+
+ if (armorstand != null) {
+ lastAccess = 0;
+ armorstand.remove();
+ }
+ }
+
+}
diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/holograms/HologramsService.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/holograms/HologramsService.java
index 9fe906a7e..6777466c4 100644
--- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/holograms/HologramsService.java
+++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/holograms/HologramsService.java
@@ -27,7 +27,6 @@ import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
/**
* This service is responsible for handling holograms.
- * This includes error management when something goes wrong.
*
* @author TheBusyBiscuit
*
@@ -59,7 +58,7 @@ public class HologramsService {
/**
* Our cache to save {@link Entity} lookups
*/
- private final Map
+ * This method must be executed on the main {@link Server} {@link Thread}.
+ *
+ * @param loc
+ * The {@link Location}
+ * @param consumer
+ * The callback to run
+ */
+ private void updateHologram(@Nonnull Location loc, @Nonnull Consumer
* This method must be executed on the main {@link Server} {@link Thread}.
*
* @param loc
* The {@link Location}
*
- * @return Whether the hologram could be removed, false if the hologram does not exist or was already removed
+ * @return Whether the {@link Hologram} could be removed, false if the {@link Hologram} does not exist or was
+ * already
+ * removed
*/
public boolean removeHologram(@Nonnull Location loc) {
Validate.notNull(loc, "Location cannot be null");
if (Bukkit.isPrimaryThread()) {
- ArmorStand hologram = getHologram(loc, false);
+ Hologram hologram = getHologram(loc, false);
if (hologram != null) {
hologram.remove();
@@ -197,52 +274,17 @@ public class HologramsService {
}
/**
- * This updates the hologram.
- * You can use it to set the nametag or other properties.
- *
- * This method must be executed on the main {@link Server} {@link Thread}.
+ * This will update the label of the {@link Hologram}.
*
* @param loc
- * The {@link Location}
- * @param consumer
- * The callback to run
+ * The {@link Location} of this {@link Hologram}
+ * @param label
+ * The label to set, can be null
*/
- public void updateHologram(@Nonnull Location loc, @Nonnull Consumer