Refactoring
This commit is contained in:
parent
a8fcacefea
commit
e1818a5a24
|
@ -95,7 +95,7 @@ class DisplayText {
|
|||
|
||||
private @Nullable String computeGlobalText() {
|
||||
if (allowPlaceholders && unreplacedText != null && unreplacedText.containsPlaceholders()) {
|
||||
return unreplacedText.replacePlaceholders(placeholderTracker::updateAndGetGlobalReplacement);
|
||||
return unreplacedText.replacePlaceholders(placeholderTracker::computeGlobalReplacement);
|
||||
} else {
|
||||
return unreplacedText != null ? unreplacedText.getString() : null;
|
||||
}
|
||||
|
@ -107,7 +107,7 @@ class DisplayText {
|
|||
|
||||
return unreplacedText.replaceParts(
|
||||
(PlaceholderOccurrence placeholderOccurrence) -> {
|
||||
return placeholderTracker.updateAndGetReplacement(placeholderOccurrence, player);
|
||||
return placeholderTracker.computeReplacement(placeholderOccurrence, player);
|
||||
},
|
||||
(String literalPart) -> {
|
||||
if (containsPlaceholderAPIPattern
|
||||
|
|
|
@ -7,10 +7,11 @@ package me.filoghost.holographicdisplays.plugin.placeholder.tracking;
|
|||
|
||||
import me.filoghost.holographicdisplays.plugin.placeholder.PlaceholderException;
|
||||
import me.filoghost.holographicdisplays.plugin.placeholder.registry.PlaceholderExpansion;
|
||||
import me.filoghost.holographicdisplays.plugin.tick.TickExpiringValue;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
abstract class ActivePlaceholder {
|
||||
abstract class ActivePlaceholder implements TickExpiringValue {
|
||||
|
||||
private final @Nullable PlaceholderExpansion source;
|
||||
private long lastRequestTick;
|
||||
|
@ -24,17 +25,18 @@ abstract class ActivePlaceholder {
|
|||
return source;
|
||||
}
|
||||
|
||||
final long getLastRequestTick() {
|
||||
@Override
|
||||
public final long getLastUseTick() {
|
||||
return lastRequestTick;
|
||||
}
|
||||
|
||||
final @Nullable String updateAndGetReplacement(Player player, long currentTick) throws PlaceholderException {
|
||||
final @Nullable String computeReplacement(Player player, long currentTick) throws PlaceholderException {
|
||||
this.lastRequestTick = currentTick;
|
||||
return doUpdateAndGetReplacement(player, currentTick);
|
||||
return doComputeReplacement(player, currentTick);
|
||||
}
|
||||
|
||||
abstract boolean isIndividual();
|
||||
|
||||
abstract @Nullable String doUpdateAndGetReplacement(Player player, long currentTick) throws PlaceholderException;
|
||||
abstract @Nullable String doComputeReplacement(Player player, long currentTick) throws PlaceholderException;
|
||||
|
||||
}
|
||||
|
|
|
@ -12,12 +12,12 @@ import me.filoghost.holographicdisplays.plugin.placeholder.parsing.StringWithPla
|
|||
import me.filoghost.holographicdisplays.plugin.placeholder.registry.PlaceholderExpansion;
|
||||
import me.filoghost.holographicdisplays.plugin.placeholder.registry.PlaceholderRegistry;
|
||||
import me.filoghost.holographicdisplays.plugin.tick.TickClock;
|
||||
import me.filoghost.holographicdisplays.plugin.tick.TickExpiringMap;
|
||||
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;
|
||||
|
||||
public class ActivePlaceholderTracker {
|
||||
|
@ -25,7 +25,7 @@ public class ActivePlaceholderTracker {
|
|||
private final PlaceholderRegistry registry;
|
||||
private final TickClock tickClock;
|
||||
private final PlaceholderExceptionHandler exceptionHandler;
|
||||
private final Map<PlaceholderOccurrence, ActivePlaceholder> activePlaceholders;
|
||||
private final TickExpiringMap<PlaceholderOccurrence, ActivePlaceholder> activePlaceholders;
|
||||
|
||||
private long lastRegistryVersion;
|
||||
|
||||
|
@ -33,7 +33,7 @@ public class ActivePlaceholderTracker {
|
|||
this.registry = registry;
|
||||
this.tickClock = tickClock;
|
||||
this.exceptionHandler = new PlaceholderExceptionHandler(tickClock);
|
||||
this.activePlaceholders = new HashMap<>();
|
||||
this.activePlaceholders = new TickExpiringMap<>(new HashMap<>(), 1);
|
||||
}
|
||||
|
||||
public void clearOutdatedEntries() {
|
||||
|
@ -44,9 +44,8 @@ public class ActivePlaceholderTracker {
|
|||
lastRegistryVersion = currentRegistryVersion;
|
||||
|
||||
// Remove entries whose placeholder expansion sources are outdated
|
||||
activePlaceholders.entrySet().removeIf(entry -> {
|
||||
PlaceholderOccurrence placeholderOccurrence = entry.getKey();
|
||||
PlaceholderExpansion currentSource = entry.getValue().getSource();
|
||||
activePlaceholders.removeEntries((PlaceholderOccurrence placeholderOccurrence, ActivePlaceholder activePlaceholder) -> {
|
||||
PlaceholderExpansion currentSource = activePlaceholder.getSource();
|
||||
PlaceholderExpansion newSource = registry.find(placeholderOccurrence);
|
||||
|
||||
return !Objects.equals(currentSource, newSource);
|
||||
|
@ -54,28 +53,26 @@ public class ActivePlaceholderTracker {
|
|||
}
|
||||
|
||||
public void clearUnusedEntries() {
|
||||
long currentTick = tickClock.getCurrentTick();
|
||||
activePlaceholders.values().removeIf(
|
||||
activePlaceholder -> currentTick - activePlaceholder.getLastRequestTick() >= 1);
|
||||
activePlaceholders.clearUnusedEntries(tickClock.getCurrentTick());
|
||||
}
|
||||
|
||||
public @Nullable String updateAndGetGlobalReplacement(PlaceholderOccurrence placeholderOccurrence) {
|
||||
public @Nullable String computeGlobalReplacement(PlaceholderOccurrence placeholderOccurrence) {
|
||||
try {
|
||||
ActivePlaceholder activePlaceholder = trackAndGetPlaceholder(placeholderOccurrence);
|
||||
if (activePlaceholder.isIndividual()) {
|
||||
return null;
|
||||
}
|
||||
return activePlaceholder.updateAndGetReplacement(null, tickClock.getCurrentTick());
|
||||
return activePlaceholder.computeReplacement(null, tickClock.getCurrentTick());
|
||||
} catch (PlaceholderException e) {
|
||||
exceptionHandler.handle(e);
|
||||
return "[Error]";
|
||||
}
|
||||
}
|
||||
|
||||
public @Nullable String updateAndGetReplacement(PlaceholderOccurrence placeholderOccurrence, Player player) {
|
||||
public @Nullable String computeReplacement(PlaceholderOccurrence placeholderOccurrence, Player player) {
|
||||
try {
|
||||
ActivePlaceholder activePlaceholder = trackAndGetPlaceholder(placeholderOccurrence);
|
||||
return activePlaceholder.updateAndGetReplacement(player, tickClock.getCurrentTick());
|
||||
return activePlaceholder.computeReplacement(player, tickClock.getCurrentTick());
|
||||
} catch (PlaceholderException e) {
|
||||
exceptionHandler.handle(e);
|
||||
return "[Error]";
|
||||
|
|
|
@ -27,8 +27,8 @@ class GlobalActivePlaceholder extends ActivePlaceholder {
|
|||
}
|
||||
|
||||
@Override
|
||||
@Nullable String doUpdateAndGetReplacement(Player player, long currentTick) throws PlaceholderException {
|
||||
return replacementHolder.updateAndGet(player, currentTick);
|
||||
@Nullable String doComputeReplacement(Player player, long currentTick) throws PlaceholderException {
|
||||
return replacementHolder.computeReplacement(player, currentTick);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -32,10 +32,10 @@ class IndividualActivePlaceholder extends ActivePlaceholder {
|
|||
}
|
||||
|
||||
@Override
|
||||
@Nullable String doUpdateAndGetReplacement(Player player, long currentTick) throws PlaceholderException {
|
||||
@Nullable String doComputeReplacement(Player player, long currentTick) throws PlaceholderException {
|
||||
return replacementHolderByPlayer
|
||||
.computeIfAbsent(player, mappingFunction)
|
||||
.updateAndGet(player, currentTick);
|
||||
.computeReplacement(player, currentTick);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ class NullActivePlaceholder extends ActivePlaceholder {
|
|||
}
|
||||
|
||||
@Override
|
||||
@Nullable String doUpdateAndGetReplacement(Player player, long currentTick) {
|
||||
@Nullable String doComputeReplacement(Player player, long currentTick) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ class ReplacementHolder {
|
|||
this.placeholderOccurrence = placeholderOccurrence;
|
||||
}
|
||||
|
||||
@Nullable String updateAndGet(Player player, long currentTick) throws PlaceholderException {
|
||||
@Nullable String computeReplacement(Player player, long currentTick) throws PlaceholderException {
|
||||
if (needsRefresh(currentTick)) {
|
||||
currentReplacement = placeholder.getReplacement(player, placeholderOccurrence.getArgument());
|
||||
lastUpdateTick = currentTick;
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright (C) filoghost and contributors
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
package me.filoghost.holographicdisplays.plugin.tick;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.BiPredicate;
|
||||
|
||||
public class TickExpiringMap<K, V extends TickExpiringValue> {
|
||||
|
||||
private final Map<K, V> map;
|
||||
|
||||
// Entries that are not used for this amount of ticks are removed
|
||||
private final long expirationTicks;
|
||||
|
||||
public TickExpiringMap(Map<K, V> map, long expirationTicks) {
|
||||
this.map = map;
|
||||
this.expirationTicks = expirationTicks;
|
||||
}
|
||||
|
||||
public void removeEntries(BiPredicate<? super K, ? super V> filter) {
|
||||
map.entrySet().removeIf(entry -> filter.test(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
|
||||
public void clearUnusedEntries(long currentTick) {
|
||||
map.values().removeIf(value -> currentTick - value.getLastUseTick() >= expirationTicks);
|
||||
}
|
||||
|
||||
public V get(K key) {
|
||||
return map.get(key);
|
||||
}
|
||||
|
||||
public void put(K key, V value) {
|
||||
map.put(key, value);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
/*
|
||||
* Copyright (C) filoghost and contributors
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
package me.filoghost.holographicdisplays.plugin.tick;
|
||||
|
||||
public interface TickExpiringValue {
|
||||
|
||||
long getLastUseTick();
|
||||
|
||||
}
|
Loading…
Reference in New Issue