From feb3a7e9d0b350de7cf62a56130c7e348569ec6b Mon Sep 17 00:00:00 2001 From: EpicPlayerA10 Date: Wed, 29 May 2024 19:52:13 +0200 Subject: [PATCH 1/3] Fix not disappearing holograms --- .../core/attributes/HologramOwner.java | 22 +++++++++++++--- .../services/holograms/HologramsService.java | 26 ++++++++----------- 2 files changed, 29 insertions(+), 19 deletions(-) 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..68d357fc15 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 @@ -252,23 +252,19 @@ private void updateHologram(@Nonnull Location loc, @Nonnull Consumer c Validate.notNull(loc, "Location must not be null"); Validate.notNull(consumer, "Callbacks must not be null"); - Runnable runnable = () -> { - try { - Hologram hologram = getHologram(loc, true); + if (!Bukkit.isPrimaryThread()) { + throw new UnsupportedOperationException("You cannot update a hologram asynchronously"); + } - if (hologram != null) { - consumer.accept(hologram); - } - } catch (Exception | LinkageError x) { - Slimefun.logger().log(Level.SEVERE, "Hologram located at {0}", new BlockPosition(loc)); - Slimefun.logger().log(Level.SEVERE, "Something went wrong while trying to update this hologram", x); - } - }; + try { + Hologram hologram = getHologram(loc, true); - if (Bukkit.isPrimaryThread()) { - runnable.run(); - } else { - Slimefun.runSync(runnable); + if (hologram != null) { + consumer.accept(hologram); + } + } catch (Exception | LinkageError x) { + Slimefun.logger().log(Level.SEVERE, "Hologram located at {0}", new BlockPosition(loc)); + Slimefun.logger().log(Level.SEVERE, "Something went wrong while trying to update this hologram", x); } } From 508f9af42d0cbf300d519b3026a0c836ad852a4c Mon Sep 17 00:00:00 2001 From: EpicPlayerA10 Date: Thu, 30 May 2024 01:12:55 +0200 Subject: [PATCH 2/3] We don't need to use consumer here --- .../services/holograms/HologramsService.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) 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 68d357fc15..04ffa89a5f 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 @@ -245,26 +245,20 @@ private Hologram getAsHologram(@Nonnull BlockPosition position, @Nonnull Entity * * @param loc * The {@link Location} - * @param consumer - * The callback to run */ - private void updateHologram(@Nonnull Location loc, @Nonnull Consumer consumer) { + @Nullable + private Hologram updateHologram(@Nonnull Location loc) { Validate.notNull(loc, "Location must not be null"); - Validate.notNull(consumer, "Callbacks must not be null"); if (!Bukkit.isPrimaryThread()) { throw new UnsupportedOperationException("You cannot update a hologram asynchronously"); } try { - Hologram hologram = getHologram(loc, true); - - if (hologram != null) { - consumer.accept(hologram); - } + return getHologram(loc, true); } catch (Exception | LinkageError x) { Slimefun.logger().log(Level.SEVERE, "Hologram located at {0}", new BlockPosition(loc)); - Slimefun.logger().log(Level.SEVERE, "Something went wrong while trying to update this hologram", x); + throw new RuntimeException("Something went wrong while trying to update this hologram", x); } } @@ -314,7 +308,10 @@ 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 = updateHologram(loc); + if (hologram != null) { + hologram.setLabel(label); + } } } From 208e2278ecb37e940ed00dc1f7ac827b5b92ba22 Mon Sep 17 00:00:00 2001 From: EpicPlayerA10 Date: Thu, 30 May 2024 12:50:53 +0200 Subject: [PATCH 3/3] remove updateHologram and add getHologramOrCreate --- .../services/holograms/HologramsService.java | 39 +++++-------------- 1 file changed, 10 insertions(+), 29 deletions(-) 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 04ffa89a5f..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,31 +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} - */ - @Nullable - private Hologram updateHologram(@Nonnull Location loc) { - Validate.notNull(loc, "Location must not be null"); - - if (!Bukkit.isPrimaryThread()) { - throw new UnsupportedOperationException("You cannot update a hologram asynchronously"); - } - - try { - return getHologram(loc, true); - } catch (Exception | LinkageError x) { - Slimefun.logger().log(Level.SEVERE, "Hologram located at {0}", new BlockPosition(loc)); - throw new RuntimeException("Something went wrong while trying to update this hologram", x); - } - } - /** * This removes the {@link Hologram} at that given {@link Location}. *

@@ -308,10 +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"); - Hologram hologram = updateHologram(loc); - if (hologram != null) { - hologram.setLabel(label); - } + Hologram hologram = getHologramOrCreate(loc); + hologram.setLabel(label); } }