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: item lines not showing to players upon joining (#204) #205

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
6 changes: 6 additions & 0 deletions src/main/java/eu/decentsoftware/holograms/api/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public class Settings {
public static int DEFAULT_LRU_CACHE_SIZE = 500;
@Key("allow-placeholders-inside-animations")
public static boolean ALLOW_PLACEHOLDERS_INSIDE_ANIMATIONS = false;
@Key(value = "defaults.minimum-ticks-lived-item-line", min = 0)
public static int DEFAULT_MINIMUM_TICKS_LIVED_ITEM_LINE = 40;
@Key(value = "defaults.limit-hologram-updates-per-tick")
public static boolean LIMIT_HOLOGRAM_UPDATES_PER_TICK = true;
@Key(value = "defaults.maximum-hologram-updates-per-tick", min = 1)
public static int MAXIMUM_HOLOGRAM_UPDATES_PER_TICK = 10;

public static Map<String, String> CUSTOM_REPLACEMENTS = ImmutableMap.<String, String>builder()
.put("[x]", "\u2588")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,21 +633,21 @@ public boolean show(@NonNull Player player, int pageIndex) {
if (page != null && page.size() > 0 && canShow(player) && isInDisplayRange(player)) {
if (isVisible(player)) {
hide(player);
return true; // Skip a tick before updating, should fix the previous issue with clients desyncing.
}
if (Version.after(8)) {
showPageTo(player, page, pageIndex);
} else {
// We need to run the task later on older versions as, if we don't, it causes issues with some holograms *randomly* becoming invisible.
// I *think* this is from despawning and spawning the entities (with the same ID) in the same tick.
S.sync(() -> showPageTo(player, page, pageIndex), 0L);
}

showPageTo(player, page, pageIndex);
return true;
}
return false;
}
}

private void showPageTo(@NonNull Player player, @NonNull HologramPage page, int pageIndex) {
if (page.getLines().stream().anyMatch(line -> line.shouldAwaitDisplay(player))) {
return;
}

page.getLines().forEach(line -> line.show(player));
// Add player to viewers
viewerPages.put(player.getUniqueId(), pageIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ public void show(Player... players) {
if (parent != null && parent.getParent().isHideState(player)) {
continue;
}

if (!isVisible(player) && canShow(player) && isInDisplayRange(player)) {
switch (type) {
case TEXT:
Expand Down Expand Up @@ -571,4 +572,8 @@ public boolean canShow(@NonNull Player player) {
return super.canShow(player) && (parent == null || parent.getParent().canShow(player));
}

// Could be considered hacky but it works?
public boolean shouldAwaitDisplay(Player player) {
return (type == HologramLineType.ICON || type == HologramLineType.HEAD) && player.getTicksLived() < Settings.DEFAULT_MINIMUM_TICKS_LIVED_ITEM_LINE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you check for head here now? Isn't the issue with floating items alone?
And if it is with heads too, then you have to add small head too...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you check for head here now? Isn't the issue with floating items alone? And if it is with heads too, then you have to add small head too...

Yeah, appears to be with all items..

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,18 @@ public HologramManager(DecentHolograms decentHolograms) {

@Override
public synchronized void tick() {
final Map<UUID, Integer> updates = new LinkedHashMap<>();

for (Hologram hologram : Hologram.getCachedHolograms()) {
if (hologram.isEnabled()) {
for (Player player : Bukkit.getOnlinePlayers()) {
updateVisibility(player, hologram);
if (Settings.LIMIT_HOLOGRAM_UPDATES_PER_TICK && updates.getOrDefault(player.getUniqueId(), 0) >= Settings.MAXIMUM_HOLOGRAM_UPDATES_PER_TICK) {
continue;
}

if (updateVisibility(player, hologram) && Settings.LIMIT_HOLOGRAM_UPDATES_PER_TICK) {
updates.put(player.getUniqueId(), updates.getOrDefault(player.getUniqueId(), 0) + 1);
}
}
}
}
Expand All @@ -68,24 +76,34 @@ public void updateVisibility(@NonNull Player player) {
}
}

public void updateVisibility(@NonNull Player player, @NonNull Hologram hologram) {
/**
* Update the visibility of the hologram for the given player.
*
* @param player - The player to update the visibility for.
* @param hologram - The hologram to update the visibility for.
* @return True if the hologram was shown, false otherwise.
*/
public boolean updateVisibility(@NonNull Player player, @NonNull Hologram hologram) {
if (hologram.isDisabled()) {
return;
return false;
}

// Determine the player's display state of this hologram.
if (hologram.isHideState(player) || (!hologram.isDefaultVisibleState() && !hologram.isShowState(player))) {
if (hologram.isVisible(player)) {
hologram.hide(player);
}
return;
return false;
}

if (!hologram.isVisible(player) && hologram.canShow(player) && hologram.isInDisplayRange(player)) {
hologram.show(player, hologram.getPlayerPage(player));
return true;
} else if (hologram.isVisible(player) && !(hologram.canShow(player) && hologram.isInDisplayRange(player))) {
hologram.hide(player);
}

return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public PlayerListener(DecentHolograms decentHolograms) {
@EventHandler(priority = EventPriority.MONITOR)
public void onJoin(PlayerJoinEvent e) {
Player player = e.getPlayer();
S.async(() -> decentHolograms.getHologramManager().updateVisibility(player));
decentHolograms.getPacketListener().hook(player);
if (decentHolograms.isUpdateAvailable() && player.hasPermission("dh.admin")) {
Lang.sendUpdateMessage(player);
Expand Down