mirror of
https://github.com/filoghost/HolographicDisplays.git
synced 2024-12-28 19:57:45 +01:00
Manually clear active placeholder which become unused
This commit is contained in:
parent
b7b922fb37
commit
7700675e8b
@ -13,17 +13,28 @@ import org.jetbrains.annotations.Nullable;
|
||||
abstract class ActivePlaceholder {
|
||||
|
||||
private final @Nullable PlaceholderExpansion source;
|
||||
private long lastRequestTick;
|
||||
|
||||
ActivePlaceholder(@Nullable PlaceholderExpansion source) {
|
||||
this.source = source;
|
||||
this.lastRequestTick = -1;
|
||||
}
|
||||
|
||||
final @Nullable PlaceholderExpansion getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
final long getLastRequestTick() {
|
||||
return lastRequestTick;
|
||||
}
|
||||
|
||||
final @Nullable String updateAndGetReplacement(Player player, long currentTick) throws PlaceholderException {
|
||||
this.lastRequestTick = currentTick;
|
||||
return doUpdateAndGetReplacement(player, currentTick);
|
||||
}
|
||||
|
||||
abstract boolean isIndividual();
|
||||
|
||||
abstract @Nullable String updateAndGetReplacement(Player player, long currentTick) throws PlaceholderException;
|
||||
abstract @Nullable String doUpdateAndGetReplacement(Player player, long currentTick) throws PlaceholderException;
|
||||
|
||||
}
|
||||
|
@ -7,27 +7,25 @@ package me.filoghost.holographicdisplays.plugin.placeholder.tracking;
|
||||
|
||||
import me.filoghost.holographicdisplays.plugin.placeholder.PlaceholderException;
|
||||
import me.filoghost.holographicdisplays.plugin.placeholder.StandardPlaceholder;
|
||||
import me.filoghost.holographicdisplays.plugin.tick.TickClock;
|
||||
import me.filoghost.holographicdisplays.plugin.placeholder.parsing.PlaceholderOccurrence;
|
||||
import me.filoghost.holographicdisplays.plugin.placeholder.parsing.StringWithPlaceholders;
|
||||
import me.filoghost.holographicdisplays.plugin.placeholder.registry.PlaceholderExpansion;
|
||||
import me.filoghost.holographicdisplays.plugin.placeholder.registry.PlaceholderRegistry;
|
||||
import me.filoghost.holographicdisplays.plugin.tick.TickClock;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
public class ActivePlaceholderTracker {
|
||||
|
||||
private final PlaceholderRegistry registry;
|
||||
private final TickClock tickClock;
|
||||
private final PlaceholderExceptionHandler exceptionHandler;
|
||||
|
||||
// Use WeakHashMap to ensure that when a PlaceholderOccurrence is no longer referenced in other objects
|
||||
// the corresponding entry is removed from the map automatically.
|
||||
private final WeakHashMap<PlaceholderOccurrence, ActivePlaceholder> activePlaceholders;
|
||||
private final Map<PlaceholderOccurrence, ActivePlaceholder> activePlaceholders;
|
||||
|
||||
private long lastRegistryVersion;
|
||||
|
||||
@ -35,10 +33,10 @@ public class ActivePlaceholderTracker {
|
||||
this.registry = registry;
|
||||
this.tickClock = tickClock;
|
||||
this.exceptionHandler = new PlaceholderExceptionHandler(tickClock);
|
||||
this.activePlaceholders = new WeakHashMap<>();
|
||||
this.activePlaceholders = new HashMap<>();
|
||||
}
|
||||
|
||||
public void update() {
|
||||
public void clearOutdatedEntries() {
|
||||
long currentRegistryVersion = registry.getVersion();
|
||||
if (lastRegistryVersion == currentRegistryVersion) {
|
||||
return;
|
||||
@ -55,6 +53,12 @@ public class ActivePlaceholderTracker {
|
||||
});
|
||||
}
|
||||
|
||||
public void clearUnusedEntries() {
|
||||
long currentTick = tickClock.getCurrentTick();
|
||||
activePlaceholders.values().removeIf(
|
||||
activePlaceholder -> currentTick - activePlaceholder.getLastRequestTick() >= 1);
|
||||
}
|
||||
|
||||
public @Nullable String updateAndGetGlobalReplacement(PlaceholderOccurrence placeholderOccurrence) {
|
||||
try {
|
||||
ActivePlaceholder activePlaceholder = trackAndGetPlaceholder(placeholderOccurrence);
|
||||
|
@ -27,7 +27,7 @@ class GlobalActivePlaceholder extends ActivePlaceholder {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable String updateAndGetReplacement(Player player, long currentTick) throws PlaceholderException {
|
||||
@Nullable String doUpdateAndGetReplacement(Player player, long currentTick) throws PlaceholderException {
|
||||
return replacementHolder.updateAndGet(player, currentTick);
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ class IndividualActivePlaceholder extends ActivePlaceholder {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable String updateAndGetReplacement(Player player, long currentTick) throws PlaceholderException {
|
||||
@Nullable String doUpdateAndGetReplacement(Player player, long currentTick) throws PlaceholderException {
|
||||
return replacementHolderByPlayer
|
||||
.computeIfAbsent(player, key -> new ReplacementHolder(placeholder, placeholderOccurrence))
|
||||
.updateAndGet(player, currentTick);
|
||||
|
@ -16,7 +16,7 @@ class NullActivePlaceholder extends ActivePlaceholder {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable String updateAndGetReplacement(Player player, long currentTick) {
|
||||
@Nullable String doUpdateAndGetReplacement(Player player, long currentTick) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -34,8 +34,8 @@ public class TickingTask implements Runnable {
|
||||
public void run() {
|
||||
tickClock.incrementTick();
|
||||
|
||||
// Update placeholder tracker before updating hologram lines
|
||||
placeholderTracker.update();
|
||||
// Remove outdated entries before using them from line trackers
|
||||
placeholderTracker.clearOutdatedEntries();
|
||||
|
||||
try {
|
||||
lineTrackerManager.update();
|
||||
@ -48,6 +48,9 @@ public class TickingTask implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
// Remove entries which were not used by line trackers
|
||||
placeholderTracker.clearUnusedEntries();
|
||||
|
||||
lineClickListener.processQueuedClickEvents();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user