Only check viewers every 5 ticks

This commit is contained in:
filoghost 2022-02-20 17:11:54 +01:00
parent 38a99b95e1
commit 928609efb7
8 changed files with 47 additions and 11 deletions

View File

@ -107,7 +107,7 @@ public class HolographicDisplays extends FCommonsPlugin {
TickClock tickClock = new TickClock();
ActivePlaceholderTracker placeholderTracker = new ActivePlaceholderTracker(placeholderRegistry, tickClock);
LineClickListener lineClickListener = new LineClickListener();
lineTrackerManager = new LineTrackerManager(nmsManager, placeholderTracker, lineClickListener);
lineTrackerManager = new LineTrackerManager(nmsManager, placeholderTracker, lineClickListener, tickClock);
internalHologramManager = new InternalHologramManager(lineTrackerManager);
apiHologramManager = new APIHologramManager(lineTrackerManager);
v2HologramManager = new V2HologramManager(lineTrackerManager);

View File

@ -10,6 +10,7 @@ import me.filoghost.holographicdisplays.nms.common.NMSManager;
import me.filoghost.holographicdisplays.nms.common.entity.ClickableNMSPacketEntity;
import me.filoghost.holographicdisplays.plugin.hologram.base.BaseClickableHologramLine;
import me.filoghost.holographicdisplays.plugin.listener.LineClickListener;
import me.filoghost.holographicdisplays.plugin.tick.TickClock;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
public abstract class ClickableLineTracker<T extends Viewer> extends PositionBasedLineTracker<T> {
@ -21,7 +22,8 @@ public abstract class ClickableLineTracker<T extends Viewer> extends PositionBas
private boolean spawnClickableEntity;
private boolean spawnClickableEntityChanged;
public ClickableLineTracker(BaseClickableHologramLine line, NMSManager nmsManager, LineClickListener lineClickListener) {
public ClickableLineTracker(BaseClickableHologramLine line, NMSManager nmsManager, LineClickListener lineClickListener, TickClock tickClock) {
super(tickClock);
this.clickableEntity = nmsManager.newClickablePacketEntity();
this.positionOffsetY = (line.getHeight() - ClickableNMSPacketEntity.SLIME_HEIGHT) / 2;
this.lineClickListener = lineClickListener;

View File

@ -10,6 +10,7 @@ import me.filoghost.holographicdisplays.nms.common.entity.ItemNMSPacketEntity;
import me.filoghost.holographicdisplays.plugin.hologram.base.BaseItemHologramLine;
import me.filoghost.holographicdisplays.plugin.listener.LineClickListener;
import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer;
import me.filoghost.holographicdisplays.plugin.tick.TickClock;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
@ -27,8 +28,12 @@ public class ItemLineTracker extends ClickableLineTracker<Viewer> {
private boolean spawnItemEntity;
private boolean spawnItemEntityChanged;
public ItemLineTracker(BaseItemHologramLine line, NMSManager nmsManager, LineClickListener lineClickListener) {
super(line, nmsManager, lineClickListener);
public ItemLineTracker(
BaseItemHologramLine line,
NMSManager nmsManager,
LineClickListener lineClickListener,
TickClock tickClock) {
super(line, nmsManager, lineClickListener, tickClock);
this.line = line;
this.itemEntity = nmsManager.newItemPacketEntity();
}

View File

@ -7,6 +7,7 @@ package me.filoghost.holographicdisplays.plugin.hologram.tracking;
import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramLine;
import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer;
import me.filoghost.holographicdisplays.plugin.tick.TickClock;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
@ -17,6 +18,9 @@ import java.util.Map;
public abstract class LineTracker<T extends Viewer> {
private static final int MODIFY_VIEWERS_INTERVAL_TICKS = 5;
private final TickClock tickClock;
private final Map<Player, T> viewers;
private final Viewers<T> iterableViewers;
@ -25,7 +29,8 @@ public abstract class LineTracker<T extends Viewer> {
*/
private boolean lineChanged;
protected LineTracker() {
protected LineTracker(TickClock tickClock) {
this.tickClock = tickClock;
this.viewers = new HashMap<>();
this.iterableViewers = new DelegateViewers<>(viewers.values());
}
@ -84,6 +89,11 @@ public abstract class LineTracker<T extends Viewer> {
return;
}
// Add the identity hash code to avoid updating all the lines at the same time
if ((tickClock.getCurrentTick() + hashCode()) % MODIFY_VIEWERS_INTERVAL_TICKS != 0) {
return;
}
// Lazy initialization
MutableViewers<T> addedPlayers = null;
MutableViewers<T> removedPlayers = null;

View File

@ -11,6 +11,7 @@ import me.filoghost.holographicdisplays.plugin.hologram.base.BaseTextHologramLin
import me.filoghost.holographicdisplays.plugin.listener.LineClickListener;
import me.filoghost.holographicdisplays.plugin.placeholder.tracking.ActivePlaceholderTracker;
import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer;
import me.filoghost.holographicdisplays.plugin.tick.TickClock;
import org.bukkit.entity.Player;
import java.util.Collection;
@ -20,26 +21,32 @@ import java.util.List;
public class LineTrackerManager {
private final TickClock tickClock;
private final NMSManager nmsManager;
private final ActivePlaceholderTracker placeholderTracker;
private final LineClickListener lineClickListener;
private final Collection<LineTracker<?>> lineTrackers;
public LineTrackerManager(NMSManager nmsManager, ActivePlaceholderTracker placeholderTracker, LineClickListener lineClickListener) {
public LineTrackerManager(
NMSManager nmsManager,
ActivePlaceholderTracker placeholderTracker,
LineClickListener lineClickListener,
TickClock tickClock) {
this.nmsManager = nmsManager;
this.placeholderTracker = placeholderTracker;
this.lineClickListener = lineClickListener;
this.lineTrackers = new LinkedList<>();
this.tickClock = tickClock;
}
public TextLineTracker startTracking(BaseTextHologramLine line) {
TextLineTracker tracker = new TextLineTracker(line, nmsManager, lineClickListener, placeholderTracker);
TextLineTracker tracker = new TextLineTracker(line, nmsManager, lineClickListener, placeholderTracker, tickClock);
lineTrackers.add(tracker);
return tracker;
}
public ItemLineTracker startTracking(BaseItemHologramLine line) {
ItemLineTracker tracker = new ItemLineTracker(line, nmsManager, lineClickListener);
ItemLineTracker tracker = new ItemLineTracker(line, nmsManager, lineClickListener, tickClock);
lineTrackers.add(tracker);
return tracker;
}

View File

@ -7,6 +7,7 @@ package me.filoghost.holographicdisplays.plugin.hologram.tracking;
import me.filoghost.holographicdisplays.common.PositionCoordinates;
import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer;
import me.filoghost.holographicdisplays.plugin.tick.TickClock;
import org.bukkit.Location;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
@ -19,6 +20,10 @@ abstract class PositionBasedLineTracker<T extends Viewer> extends LineTracker<T>
protected PositionCoordinates position;
private boolean positionChanged;
protected PositionBasedLineTracker(TickClock tickClock) {
super(tickClock);
}
@MustBeInvokedByOverriders
@Override
protected void detectChanges() {

View File

@ -12,6 +12,7 @@ import me.filoghost.holographicdisplays.plugin.hologram.base.BaseTextHologramLin
import me.filoghost.holographicdisplays.plugin.listener.LineClickListener;
import me.filoghost.holographicdisplays.plugin.placeholder.tracking.ActivePlaceholderTracker;
import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer;
import me.filoghost.holographicdisplays.plugin.tick.TickClock;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
import java.util.Objects;
@ -28,8 +29,9 @@ public class TextLineTracker extends ClickableLineTracker<TextLineViewer> {
BaseTextHologramLine line,
NMSManager nmsManager,
LineClickListener lineClickListener,
ActivePlaceholderTracker placeholderTracker) {
super(line, nmsManager, lineClickListener);
ActivePlaceholderTracker placeholderTracker,
TickClock tickClock) {
super(line, nmsManager, lineClickListener, tickClock);
this.line = line;
this.textEntity = nmsManager.newTextPacketEntity();
this.displayText = new DisplayText(placeholderTracker);

View File

@ -9,13 +9,18 @@ import me.filoghost.holographicdisplays.plugin.api.v2.V2HologramManager;
import me.filoghost.holographicdisplays.plugin.hologram.tracking.LineTrackerManager;
import me.filoghost.holographicdisplays.plugin.listener.LineClickListener;
import me.filoghost.holographicdisplays.plugin.placeholder.tracking.ActivePlaceholderTracker;
import me.filoghost.holographicdisplays.plugin.tick.TickClock;
import static org.mockito.Mockito.*;
public class TestV2HologramManager extends V2HologramManager {
public TestV2HologramManager() {
super(new LineTrackerManager(new TestNMSManager(), mock(ActivePlaceholderTracker.class), new LineClickListener()));
super(new LineTrackerManager(
new TestNMSManager(),
mock(ActivePlaceholderTracker.class),
new LineClickListener(),
new TickClock()));
}
}