Update placeholder tracker only when necessary

This commit is contained in:
filoghost 2021-08-05 07:52:51 +02:00
parent 3432a33ae6
commit e3179576e4
3 changed files with 21 additions and 5 deletions

View File

@ -134,7 +134,7 @@ public class HolographicDisplays extends FCommonsPlugin {
registerListener(updateNotificationListener);
// Tasks
TickingTask tickingTask = new TickingTask(tickClock, lineTrackerManager, lineClickListener);
TickingTask tickingTask = new TickingTask(tickClock, placeholderTracker, lineTrackerManager, lineClickListener);
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, tickingTask, 0, 1);
updateNotificationListener.runAsyncUpdateCheck(this);

View File

@ -8,17 +8,24 @@ package me.filoghost.holographicdisplays.plugin.placeholder;
import me.filoghost.fcommons.logging.Log;
import me.filoghost.holographicdisplays.plugin.listener.LineClickListener;
import me.filoghost.holographicdisplays.plugin.hologram.tracking.LineTrackerManager;
import me.filoghost.holographicdisplays.plugin.placeholder.tracking.PlaceholderTracker;
public class TickingTask implements Runnable {
private final TickClock tickClock;
private final PlaceholderTracker placeholderTracker;
private final LineTrackerManager lineTrackerManager;
private final LineClickListener lineClickListener;
private long lastErrorLogTick;
public TickingTask(TickClock tickClock, LineTrackerManager lineTrackerManager, LineClickListener lineClickListener) {
public TickingTask(
TickClock tickClock,
PlaceholderTracker placeholderTracker,
LineTrackerManager lineTrackerManager,
LineClickListener lineClickListener) {
this.tickClock = tickClock;
this.placeholderTracker = placeholderTracker;
this.lineTrackerManager = lineTrackerManager;
this.lineClickListener = lineClickListener;
}
@ -27,6 +34,9 @@ public class TickingTask implements Runnable {
public void run() {
tickClock.incrementTick();
// Update placeholder tracker before updating hologram lines
placeholderTracker.update();
try {
lineTrackerManager.update();
} catch (Throwable t) {

View File

@ -29,16 +29,23 @@ public class PlaceholderTracker {
// the corresponding entry is removed from the map automatically.
private final WeakHashMap<PlaceholderOccurrence, TrackedPlaceholder> activePlaceholders;
private volatile boolean registryChanged;
public PlaceholderTracker(PlaceholderRegistry registry, TickClock tickClock) {
this.registry = registry;
this.tickClock = tickClock;
this.exceptionHandler = new PlaceholderExceptionHandler(tickClock);
this.activePlaceholders = new WeakHashMap<>();
registry.setChangeListener(this::onRegistryChange);
registry.setChangeListener(() -> registryChanged = true);
}
private void onRegistryChange() {
public void update() {
if (!registryChanged) {
return;
}
registryChanged = false;
// Remove entries whose placeholder expansion sources are outdated
activePlaceholders.entrySet().removeIf(entry -> {
PlaceholderOccurrence placeholderOccurrence = entry.getKey();
@ -108,5 +115,4 @@ public class PlaceholderTracker {
});
}
}