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 b14e32c35c..1b2e1612c0 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 @@ -2,6 +2,7 @@ import javax.annotation.Nonnull; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.util.Vector; @@ -25,15 +26,28 @@ public interface HologramOwner extends ItemAttribute { /** * This will update the hologram text for the given {@link Block}. * - * @param b + * @param hologramOwnerBlock * The {@link Block} to which the hologram belongs * * @param text * The nametag for the hologram */ - default void updateHologram(@Nonnull Block b, @Nonnull String text) { - Location loc = b.getLocation().add(getHologramOffset(b)); - Slimefun.getHologramsService().setHologramLabel(loc, ChatColors.color(text)); + default void updateHologram(@Nonnull Block hologramOwnerBlock, @Nonnull String text) { + Runnable runnable = () -> { + // Fix not disappearing holograms (#3176) + if (Slimefun.getTickerTask().isDeletedSoon(hologramOwnerBlock.getLocation())) { + return; + } + + Location loc = hologramOwnerBlock.getLocation().add(getHologramOffset(hologramOwnerBlock)); + Slimefun.getHologramsService().setHologramLabel(loc, ChatColors.color(text)); + }; + + if (Bukkit.isPrimaryThread()) { + runnable.run(); + } else { + Slimefun.runSync(runnable); + } } /** 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 2673dd9b91..ae582c261d 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 @@ -114,6 +114,14 @@ private void purge() { } } + /** + * @see HologramsService#getHologram(Location, boolean) + */ + @Nonnull + private Hologram getHologramOrCreate(@Nonnull Location loc) { + return getHologram(loc, true); + } + /** * This returns the {@link Hologram} associated with the given {@link Location}. * If createIfNoneExists is set to true a new {@link ArmorStand} will be spawned @@ -237,41 +245,6 @@ private Hologram getAsHologram(@Nonnull BlockPosition position, @Nonnull Entity } } - /** - * This updates the {@link Hologram}. - * You can use it to set the nametag or other properties. - *
- * 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
@@ -318,7 +291,8 @@ public boolean removeHologram(@Nonnull Location loc) {
public void setHologramLabel(@Nonnull Location loc, @Nullable String label) {
Validate.notNull(loc, "Location must not be null");
- updateHologram(loc, hologram -> hologram.setLabel(label));
+ Hologram hologram = getHologramOrCreate(loc);
+ hologram.setLabel(label);
}
}