diff --git a/core/src/main/java/me/filoghost/holographicdisplays/core/tick/CachedPlayer.java b/core/src/main/java/me/filoghost/holographicdisplays/core/tick/CachedPlayer.java index 38fb4d59..f748f2fa 100644 --- a/core/src/main/java/me/filoghost/holographicdisplays/core/tick/CachedPlayer.java +++ b/core/src/main/java/me/filoghost/holographicdisplays/core/tick/CachedPlayer.java @@ -7,22 +7,23 @@ package me.filoghost.holographicdisplays.core.tick; import org.bukkit.Location; import org.bukkit.entity.Player; +import org.jetbrains.annotations.Nullable; public class CachedPlayer { private final Player player; private Location location; + private boolean movedLastTick; public CachedPlayer(Player player) { this.player = player; } - boolean onTick() { + void onTick() { Location newLocation = player.getLocation(); - boolean moved = isDifferentPosition(location, newLocation); + movedLastTick = isDifferentPosition(location, newLocation); location = newLocation; - return moved; } private boolean isDifferentPosition(Location oldLocation, Location newLocation) { @@ -40,10 +41,14 @@ public class CachedPlayer { return player; } - public Location getLocation() { + public @Nullable Location getLocation() { return location; } + public boolean isMovedLastTick() { + return movedLastTick; + } + @Override public boolean equals(Object obj) { if (this == obj) { diff --git a/core/src/main/java/me/filoghost/holographicdisplays/core/tick/TickingTask.java b/core/src/main/java/me/filoghost/holographicdisplays/core/tick/TickingTask.java index 13213770..e75f90a8 100644 --- a/core/src/main/java/me/filoghost/holographicdisplays/core/tick/TickingTask.java +++ b/core/src/main/java/me/filoghost/holographicdisplays/core/tick/TickingTask.java @@ -6,9 +6,9 @@ package me.filoghost.holographicdisplays.core.tick; import me.filoghost.fcommons.logging.Log; -import me.filoghost.holographicdisplays.core.tracking.LineTrackerManager; import me.filoghost.holographicdisplays.core.listener.LineClickListener; import me.filoghost.holographicdisplays.core.placeholder.tracking.ActivePlaceholderTracker; +import me.filoghost.holographicdisplays.core.tracking.LineTrackerManager; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -68,8 +68,7 @@ public class TickingTask implements Runnable { List movedPlayers = new ArrayList<>(); for (CachedPlayer onlinePlayer : onlinePlayers) { - boolean moved = onlinePlayer.onTick(); - if (moved) { + if (onlinePlayer.isMovedLastTick()) { movedPlayers.add(onlinePlayer); } } @@ -91,7 +90,14 @@ public class TickingTask implements Runnable { // Remove placeholders which were not used by line trackers placeholderTracker.clearInactivePlaceholders(); + // Invoke click listeners lineClickListener.processQueuedClickEvents(); + + // Delay position updates, so that they will be handled next tick. + // A tick of delay is necessary to avoid issues such as holograms not being visible after a teleport. + for (CachedPlayer onlinePlayer : onlinePlayers) { + onlinePlayer.onTick(); + } } } diff --git a/core/src/main/java/me/filoghost/holographicdisplays/core/tracking/ItemLineTracker.java b/core/src/main/java/me/filoghost/holographicdisplays/core/tracking/ItemLineTracker.java index e4585f63..830b83a2 100644 --- a/core/src/main/java/me/filoghost/holographicdisplays/core/tracking/ItemLineTracker.java +++ b/core/src/main/java/me/filoghost/holographicdisplays/core/tracking/ItemLineTracker.java @@ -48,7 +48,7 @@ public class ItemLineTracker extends ClickableLineTracker { if (spawnItemEntity && hasViewers() && line.hasPickupCallback()) { for (Viewer viewer : getViewers()) { - if (CollisionHelper.isInPickupRange(viewer.getLocation(), positionCoordinates)) { + if (viewer.getLocation() != null && CollisionHelper.isInPickupRange(viewer.getLocation(), positionCoordinates)) { line.onPickup(viewer.getBukkitPlayer()); } } diff --git a/core/src/main/java/me/filoghost/holographicdisplays/core/tracking/LineTracker.java b/core/src/main/java/me/filoghost/holographicdisplays/core/tracking/LineTracker.java index 569f1a85..35398966 100644 --- a/core/src/main/java/me/filoghost/holographicdisplays/core/tracking/LineTracker.java +++ b/core/src/main/java/me/filoghost/holographicdisplays/core/tracking/LineTracker.java @@ -31,6 +31,7 @@ public abstract class LineTracker { * Flag to indicate that the line has changed in some way and there could be the need to send update packets. */ private boolean lineChanged; + private boolean inLoadedChunk; private int lastVisibilitySettingsVersion; protected LineTracker() { @@ -88,12 +89,21 @@ public abstract class LineTracker { private void modifyViewersAndSendPackets(List onlinePlayers, List movedPlayers, int maxViewRange) { if (!getLine().isInLoadedChunk()) { - resetViewersAndSendDestroyPackets(); + if (inLoadedChunk) { + inLoadedChunk = false; + resetViewersAndSendDestroyPackets(); + } return; } boolean checkAllPlayers = false; + if (!inLoadedChunk) { + // The chunk was just loaded, check all players + inLoadedChunk = true; + checkAllPlayers = true; + } + int visibilitySettingsVersion = getLine().getVisibilitySettings().getVersion(); if (visibilitySettingsVersion != lastVisibilitySettingsVersion) { lastVisibilitySettingsVersion = visibilitySettingsVersion; @@ -150,7 +160,7 @@ public abstract class LineTracker { private boolean shouldTrackPlayer(CachedPlayer player, int maxViewRange) { Location playerLocation = player.getLocation(); - if (playerLocation.getWorld() != getLine().getWorldIfLoaded()) { + if (playerLocation == null || playerLocation.getWorld() != getLine().getWorldIfLoaded()) { return false; } diff --git a/core/src/main/java/me/filoghost/holographicdisplays/core/tracking/Viewer.java b/core/src/main/java/me/filoghost/holographicdisplays/core/tracking/Viewer.java index ee16825e..301f48c9 100644 --- a/core/src/main/java/me/filoghost/holographicdisplays/core/tracking/Viewer.java +++ b/core/src/main/java/me/filoghost/holographicdisplays/core/tracking/Viewer.java @@ -10,6 +10,7 @@ import me.filoghost.holographicdisplays.nms.common.PacketGroup; import me.filoghost.holographicdisplays.core.tick.CachedPlayer; import org.bukkit.Location; import org.bukkit.entity.Player; +import org.jetbrains.annotations.Nullable; class Viewer { @@ -23,7 +24,7 @@ class Viewer { return player.getBukkitPlayer(); } - public Location getLocation() { + public @Nullable Location getLocation() { return player.getLocation(); }