Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix not disappearing holograms #4197

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ private void purge() {
}
}

/**
* @see HologramsService#getHologram(Location, boolean)
*/
@Nonnull
private Hologram getHologramOrCreate(@Nonnull Location loc) {
return getHologram(loc, true);
}

EpicPlayerA10 marked this conversation as resolved.
Show resolved Hide resolved
/**
* This returns the {@link Hologram} associated with the given {@link Location}.
* If createIfNoneExists is set to true a new {@link ArmorStand} will be spawned
Expand Down Expand Up @@ -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.
* <p>
* <strong>This method must be executed on the main {@link Server} {@link Thread}.</strong>
*
* @param loc
* The {@link Location}
* @param consumer
* The callback to run
*/
private void updateHologram(@Nonnull Location loc, @Nonnull Consumer<Hologram> consumer) {
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 (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);
}
};

if (Bukkit.isPrimaryThread()) {
runnable.run();
} else {
Slimefun.runSync(runnable);
}
}

/**
* This removes the {@link Hologram} at that given {@link Location}.
* <p>
Expand Down Expand Up @@ -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);
}

}
Loading