mirror of
https://github.com/filoghost/HolographicDisplays.git
synced 2024-10-07 12:07:38 +02: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 {
|
abstract class ActivePlaceholder {
|
||||||
|
|
||||||
private final @Nullable PlaceholderExpansion source;
|
private final @Nullable PlaceholderExpansion source;
|
||||||
|
private long lastRequestTick;
|
||||||
|
|
||||||
ActivePlaceholder(@Nullable PlaceholderExpansion source) {
|
ActivePlaceholder(@Nullable PlaceholderExpansion source) {
|
||||||
this.source = source;
|
this.source = source;
|
||||||
|
this.lastRequestTick = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
final @Nullable PlaceholderExpansion getSource() {
|
final @Nullable PlaceholderExpansion getSource() {
|
||||||
return source;
|
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 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.PlaceholderException;
|
||||||
import me.filoghost.holographicdisplays.plugin.placeholder.StandardPlaceholder;
|
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.PlaceholderOccurrence;
|
||||||
import me.filoghost.holographicdisplays.plugin.placeholder.parsing.StringWithPlaceholders;
|
import me.filoghost.holographicdisplays.plugin.placeholder.parsing.StringWithPlaceholders;
|
||||||
import me.filoghost.holographicdisplays.plugin.placeholder.registry.PlaceholderExpansion;
|
import me.filoghost.holographicdisplays.plugin.placeholder.registry.PlaceholderExpansion;
|
||||||
import me.filoghost.holographicdisplays.plugin.placeholder.registry.PlaceholderRegistry;
|
import me.filoghost.holographicdisplays.plugin.placeholder.registry.PlaceholderRegistry;
|
||||||
|
import me.filoghost.holographicdisplays.plugin.tick.TickClock;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.WeakHashMap;
|
|
||||||
|
|
||||||
public class ActivePlaceholderTracker {
|
public class ActivePlaceholderTracker {
|
||||||
|
|
||||||
private final PlaceholderRegistry registry;
|
private final PlaceholderRegistry registry;
|
||||||
private final TickClock tickClock;
|
private final TickClock tickClock;
|
||||||
private final PlaceholderExceptionHandler exceptionHandler;
|
private final PlaceholderExceptionHandler exceptionHandler;
|
||||||
|
private final Map<PlaceholderOccurrence, ActivePlaceholder> activePlaceholders;
|
||||||
// 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 long lastRegistryVersion;
|
private long lastRegistryVersion;
|
||||||
|
|
||||||
@ -35,10 +33,10 @@ public class ActivePlaceholderTracker {
|
|||||||
this.registry = registry;
|
this.registry = registry;
|
||||||
this.tickClock = tickClock;
|
this.tickClock = tickClock;
|
||||||
this.exceptionHandler = new PlaceholderExceptionHandler(tickClock);
|
this.exceptionHandler = new PlaceholderExceptionHandler(tickClock);
|
||||||
this.activePlaceholders = new WeakHashMap<>();
|
this.activePlaceholders = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update() {
|
public void clearOutdatedEntries() {
|
||||||
long currentRegistryVersion = registry.getVersion();
|
long currentRegistryVersion = registry.getVersion();
|
||||||
if (lastRegistryVersion == currentRegistryVersion) {
|
if (lastRegistryVersion == currentRegistryVersion) {
|
||||||
return;
|
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) {
|
public @Nullable String updateAndGetGlobalReplacement(PlaceholderOccurrence placeholderOccurrence) {
|
||||||
try {
|
try {
|
||||||
ActivePlaceholder activePlaceholder = trackAndGetPlaceholder(placeholderOccurrence);
|
ActivePlaceholder activePlaceholder = trackAndGetPlaceholder(placeholderOccurrence);
|
||||||
|
@ -27,7 +27,7 @@ class GlobalActivePlaceholder extends ActivePlaceholder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Nullable String updateAndGetReplacement(Player player, long currentTick) throws PlaceholderException {
|
@Nullable String doUpdateAndGetReplacement(Player player, long currentTick) throws PlaceholderException {
|
||||||
return replacementHolder.updateAndGet(player, currentTick);
|
return replacementHolder.updateAndGet(player, currentTick);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ class IndividualActivePlaceholder extends ActivePlaceholder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Nullable String updateAndGetReplacement(Player player, long currentTick) throws PlaceholderException {
|
@Nullable String doUpdateAndGetReplacement(Player player, long currentTick) throws PlaceholderException {
|
||||||
return replacementHolderByPlayer
|
return replacementHolderByPlayer
|
||||||
.computeIfAbsent(player, key -> new ReplacementHolder(placeholder, placeholderOccurrence))
|
.computeIfAbsent(player, key -> new ReplacementHolder(placeholder, placeholderOccurrence))
|
||||||
.updateAndGet(player, currentTick);
|
.updateAndGet(player, currentTick);
|
||||||
|
@ -16,7 +16,7 @@ class NullActivePlaceholder extends ActivePlaceholder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Nullable String updateAndGetReplacement(Player player, long currentTick) {
|
@Nullable String doUpdateAndGetReplacement(Player player, long currentTick) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,8 +34,8 @@ public class TickingTask implements Runnable {
|
|||||||
public void run() {
|
public void run() {
|
||||||
tickClock.incrementTick();
|
tickClock.incrementTick();
|
||||||
|
|
||||||
// Update placeholder tracker before updating hologram lines
|
// Remove outdated entries before using them from line trackers
|
||||||
placeholderTracker.update();
|
placeholderTracker.clearOutdatedEntries();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
lineTrackerManager.update();
|
lineTrackerManager.update();
|
||||||
@ -48,6 +48,9 @@ public class TickingTask implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove entries which were not used by line trackers
|
||||||
|
placeholderTracker.clearUnusedEntries();
|
||||||
|
|
||||||
lineClickListener.processQueuedClickEvents();
|
lineClickListener.processQueuedClickEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user