Skip to content
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 @@ -49,6 +49,8 @@ public abstract class Hologram {
.maximumSize(512)
.build();
private String lastRawText = "";
private Component cachedGlobalText = null;
private String cachedGlobalTextRaw = "";

protected Hologram(@NotNull final HologramData data) {
this.data = data;
Expand Down Expand Up @@ -208,7 +210,7 @@ public final void forceUpdate() {
* Refreshes the hologram for the players currently viewing it.
*/
public void refreshForViewers() {
final var players = getViewers()
final var players = this.viewers
.stream()
.map(Bukkit::getPlayer)
.filter(Objects::nonNull)
Expand All @@ -221,8 +223,8 @@ public void refreshForViewers() {
* Refreshes the hologram for players currently viewing it in the same world as the hologram.
*/
public void refreshForViewersInWorld() {
World world = data.getLocation().getWorld();
final var players = getViewers()
final World world = data.getLocation().getWorld();
final var players = this.viewers
.stream()
.map(Bukkit::getPlayer)
.filter(player -> player != null && player.getWorld().equals(world))
Expand Down Expand Up @@ -290,10 +292,11 @@ public boolean isWithinVisibilityDistance(@NotNull final Player player) {
return false;
}

int visibilityDistance = data.getVisibilityDistance();
double distanceSquared = location.distanceSquared(player.getLocation());
final int visibilityDistance = data.getVisibilityDistance();
final int visDistSquared = visibilityDistance * visibilityDistance;
final double distanceSquared = location.distanceSquared(player.getLocation());

return distanceSquared <= visibilityDistance * visibilityDistance;
return distanceSquared <= visDistSquared;
}

/**
Expand Down Expand Up @@ -369,14 +372,26 @@ public final Component getShownText(@Nullable final Player player) {

if (!rawText.equals(lastRawText)) {
cachedTextPerPlayer.invalidateAll();
cachedGlobalText = null;
cachedGlobalTextRaw = "";
lastRawText = rawText;
}

if (Bukkit.isStopping()) {
return MiniMessage.miniMessage().deserialize(rawText);
}

final UUID cacheKey = player != null ? player.getUniqueId() : NULL_PLAYER_KEY;
if (player == null) {
if (rawText.equals(cachedGlobalTextRaw) && cachedGlobalText != null) {
return cachedGlobalText;
}
final Component translated = PaperColor.handler().translate(rawText, null);
cachedGlobalText = translated;
cachedGlobalTextRaw = rawText;
return translated;
}

final UUID cacheKey = player.getUniqueId();
final Component cached = cachedTextPerPlayer.getIfPresent(cacheKey);
if (cached != null) {
return cached;
Expand All @@ -390,6 +405,8 @@ public final Component getShownText(@Nullable final Player player) {
@ApiStatus.Internal
public void clearTextCache() {
cachedTextPerPlayer.invalidateAll();
cachedGlobalText = null;
cachedGlobalTextRaw = "";
lastRawText = "";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,9 @@ void initializeTasks() {
this.loadHolograms();

hologramThread.scheduleAtFixedRate(() -> {
final var onlinePlayers = Bukkit.getOnlinePlayers();
for (final Hologram hologram : this.plugin.getHologramsManager().getHolograms()) {
for (final Player player : Bukkit.getOnlinePlayers()) {
for (final Player player : onlinePlayers) {
hologram.forceUpdateShownStateFor(player);
}
}
Expand All @@ -225,19 +226,21 @@ void initializeTasks() {
final var time = System.currentTimeMillis();

for (final var hologram : this.getHolograms()) {
HologramData data = hologram.getData();
if (data.hasChanges()) {
if (data instanceof TextHologramData) {
hologram.clearTextCache();
}
final HologramData data = hologram.getData();
if (!data.hasChanges()) {
continue;
}

if (data instanceof TextHologramData) {
hologram.clearTextCache();
}

hologram.forceUpdate();
hologram.refreshForViewersInWorld();
data.setHasChanges(false);
hologram.forceUpdate();
hologram.refreshForViewersInWorld();
data.setHasChanges(false);

if (data instanceof TextHologramData) {
updateTimes.put(hologram.getData().getName(), time);
}
if (data instanceof TextHologramData) {
updateTimes.put(hologram.getData().getName(), time);
}
}
}, 50, 1000, TimeUnit.MILLISECONDS);
Expand All @@ -246,22 +249,22 @@ void initializeTasks() {
final var time = System.currentTimeMillis();

for (final var hologram : this.getHolograms()) {
if (hologram.getData() instanceof TextHologramData textData) {
final var interval = textData.getTextUpdateInterval();
if (interval < 1) {
continue; // doesn't update
}
if (!(hologram.getData() instanceof TextHologramData textData)) {
continue;
}

final var lastUpdate = updateTimes.asMap().get(textData.getName());
if (lastUpdate != null && time < (lastUpdate + interval)) {
continue;
}
final var interval = textData.getTextUpdateInterval();
if (interval < 1) {
continue;
}

if (lastUpdate == null || time > (lastUpdate + interval)) {
hologram.refreshForViewersInWorld();
updateTimes.put(textData.getName(), time);
}
final var lastUpdate = updateTimes.asMap().get(textData.getName());
if (lastUpdate != null && time < (lastUpdate + interval)) {
continue;
}

hologram.refreshForViewersInWorld();
updateTimes.put(textData.getName(), time);
}
}, 50, this.plugin.getHologramConfiguration().getHologramUpdateInterval(), TimeUnit.MILLISECONDS);
}
Expand Down