Rename TrackedPlayer to Viewer

This commit is contained in:
filoghost 2021-09-15 09:18:20 +02:00
parent 15a7372c43
commit 0a80d91d5b
13 changed files with 71 additions and 71 deletions

View File

@ -187,7 +187,7 @@ public class HolographicDisplays extends FCommonsPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
if (lineTrackerManager != null) { if (lineTrackerManager != null) {
lineTrackerManager.clearTrackedPlayersAndSendPackets(); lineTrackerManager.resetViewersAndSendDestroyPackets();
} }
if (nmsManager != null) { if (nmsManager != null) {

View File

@ -35,8 +35,8 @@ public abstract class BaseHologramLine extends BaseHologramComponent implements
tracker.setLineChanged(); tracker.setLineChanged();
} }
protected final boolean isTrackedPlayer(Player player) { protected final boolean isViewer(Player player) {
return tracker.isTrackedPlayer(player); return tracker.isViewer(player);
} }
@Override @Override
@ -72,7 +72,7 @@ public abstract class BaseHologramLine extends BaseHologramComponent implements
return !isDeleted() return !isDeleted()
&& player.isOnline() && player.isOnline()
&& player.getGameMode() != GameMode.SPECTATOR && player.getGameMode() != GameMode.SPECTATOR
&& isTrackedPlayer(player) && isViewer(player)
&& isVisibleTo(player); && isVisibleTo(player);
} }

View File

@ -12,7 +12,7 @@ import me.filoghost.holographicdisplays.plugin.hologram.base.BaseClickableHologr
import me.filoghost.holographicdisplays.plugin.listener.LineClickListener; import me.filoghost.holographicdisplays.plugin.listener.LineClickListener;
import org.jetbrains.annotations.MustBeInvokedByOverriders; import org.jetbrains.annotations.MustBeInvokedByOverriders;
public abstract class ClickableLineTracker<T extends TrackedPlayer> extends PositionBasedLineTracker<T> { public abstract class ClickableLineTracker<T extends Viewer> extends PositionBasedLineTracker<T> {
private final ClickableNMSPacketEntity clickableEntity; private final ClickableNMSPacketEntity clickableEntity;
private final double positionOffsetY; private final double positionOffsetY;

View File

@ -60,13 +60,13 @@ class DisplayText {
return globalText; return globalText;
} }
public boolean updateReplacements(Collection<TextLineTrackedPlayer> trackedPlayers) { public boolean updateReplacements(Collection<TextLineViewer> viewers) {
boolean changed = false; boolean changed = false;
if (containsIndividualPlaceholders()) { if (containsIndividualPlaceholders()) {
for (TextLineTrackedPlayer trackedPlayer : trackedPlayers) { for (TextLineViewer viewer : viewers) {
String individualText = computeIndividualText(trackedPlayer); String individualText = computeIndividualText(viewer);
if (trackedPlayer.updateIndividualText(individualText)) { if (viewer.updateIndividualText(individualText)) {
changed = true; changed = true;
} }
} }
@ -89,9 +89,9 @@ class DisplayText {
} }
} }
public @NotNull String computeIndividualText(TrackedPlayer trackedPlayer) { public @NotNull String computeIndividualText(Viewer viewer) {
Preconditions.notNull(unreplacedText, "unreplacedText"); Preconditions.notNull(unreplacedText, "unreplacedText");
Player player = trackedPlayer.getPlayer(); Player player = viewer.getPlayer();
return unreplacedText.replaceParts( return unreplacedText.replaceParts(
(PlaceholderOccurrence placeholderOccurrence) -> { (PlaceholderOccurrence placeholderOccurrence) -> {

View File

@ -16,7 +16,7 @@ import org.jetbrains.annotations.MustBeInvokedByOverriders;
import java.util.Collection; import java.util.Collection;
import java.util.Objects; import java.util.Objects;
public class ItemLineTracker extends ClickableLineTracker<TrackedPlayer> { public class ItemLineTracker extends ClickableLineTracker<Viewer> {
private final BaseItemHologramLine line; private final BaseItemHologramLine line;
private final ItemNMSPacketEntity itemEntity; private final ItemNMSPacketEntity itemEntity;
@ -43,10 +43,10 @@ public class ItemLineTracker extends ClickableLineTracker<TrackedPlayer> {
protected void update(Collection<? extends Player> onlinePlayers) { protected void update(Collection<? extends Player> onlinePlayers) {
super.update(onlinePlayers); super.update(onlinePlayers);
if (spawnItemEntity && hasTrackedPlayers() && line.hasPickupCallback()) { if (spawnItemEntity && hasViewers() && line.hasPickupCallback()) {
for (TrackedPlayer trackedPlayer : getTrackedPlayers()) { for (Viewer viewer : getViewers()) {
if (CollisionHelper.isInPickupRange(trackedPlayer.getPlayer(), position)) { if (CollisionHelper.isInPickupRange(viewer.getPlayer(), position)) {
line.onPickup(trackedPlayer.getPlayer()); line.onPickup(viewer.getPlayer());
} }
} }
} }
@ -58,8 +58,8 @@ public class ItemLineTracker extends ClickableLineTracker<TrackedPlayer> {
} }
@Override @Override
protected TrackedPlayer createTrackedPlayer(Player player) { protected Viewer createViewer(Player player) {
return new TrackedPlayer(player); return new Viewer(player);
} }
@MustBeInvokedByOverriders @MustBeInvokedByOverriders
@ -90,7 +90,7 @@ public class ItemLineTracker extends ClickableLineTracker<TrackedPlayer> {
@MustBeInvokedByOverriders @MustBeInvokedByOverriders
@Override @Override
protected void sendSpawnPackets(Viewers<TrackedPlayer> viewers) { protected void sendSpawnPackets(Viewers<Viewer> viewers) {
super.sendSpawnPackets(viewers); super.sendSpawnPackets(viewers);
if (spawnItemEntity) { if (spawnItemEntity) {
@ -100,7 +100,7 @@ public class ItemLineTracker extends ClickableLineTracker<TrackedPlayer> {
@MustBeInvokedByOverriders @MustBeInvokedByOverriders
@Override @Override
protected void sendDestroyPackets(Viewers<TrackedPlayer> viewers) { protected void sendDestroyPackets(Viewers<Viewer> viewers) {
super.sendDestroyPackets(viewers); super.sendDestroyPackets(viewers);
if (spawnItemEntity) { if (spawnItemEntity) {
@ -110,7 +110,7 @@ public class ItemLineTracker extends ClickableLineTracker<TrackedPlayer> {
@MustBeInvokedByOverriders @MustBeInvokedByOverriders
@Override @Override
protected void sendChangesPackets(Viewers<TrackedPlayer> viewers) { protected void sendChangesPackets(Viewers<Viewer> viewers) {
super.sendChangesPackets(viewers); super.sendChangesPackets(viewers);
if (spawnItemEntityChanged) { if (spawnItemEntityChanged) {
@ -127,7 +127,7 @@ public class ItemLineTracker extends ClickableLineTracker<TrackedPlayer> {
@MustBeInvokedByOverriders @MustBeInvokedByOverriders
@Override @Override
protected void sendPositionChangePackets(Viewers<TrackedPlayer> viewers) { protected void sendPositionChangePackets(Viewers<Viewer> viewers) {
super.sendPositionChangePackets(viewers); super.sendPositionChangePackets(viewers);
if (spawnItemEntity) { if (spawnItemEntity) {

View File

@ -13,10 +13,10 @@ import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public abstract class LineTracker<T extends TrackedPlayer> { public abstract class LineTracker<T extends Viewer> {
private final Map<Player, T> trackedPlayers; private final Map<Player, T> viewers;
private final Viewers<T> trackedPlayersIterableView; private final Viewers<T> iterableViewers;
/** /**
* Flag to indicate that the line has changed in some way and there could be the need to send update packets. * Flag to indicate that the line has changed in some way and there could be the need to send update packets.
@ -24,8 +24,8 @@ public abstract class LineTracker<T extends TrackedPlayer> {
private boolean lineChanged; private boolean lineChanged;
protected LineTracker() { protected LineTracker() {
this.trackedPlayers = new HashMap<>(); this.viewers = new HashMap<>();
this.trackedPlayersIterableView = action -> trackedPlayers.values().forEach(action); this.iterableViewers = action -> viewers.values().forEach(action);
} }
protected abstract BaseHologramLine getLine(); protected abstract BaseHologramLine getLine();
@ -36,7 +36,7 @@ public abstract class LineTracker<T extends TrackedPlayer> {
@MustBeInvokedByOverriders @MustBeInvokedByOverriders
public void onRemoval() { public void onRemoval() {
clearTrackedPlayersAndSendPackets(); resetViewersAndSendDestroyPackets();
} }
public final void setLineChanged() { public final void setLineChanged() {
@ -60,14 +60,14 @@ public abstract class LineTracker<T extends TrackedPlayer> {
// Then, send the changes (if any) to already tracked players // Then, send the changes (if any) to already tracked players
if (sendChangesPackets) { if (sendChangesPackets) {
if (hasTrackedPlayers()) { if (hasViewers()) {
sendChangesPackets(trackedPlayersIterableView); sendChangesPackets(iterableViewers);
} }
clearDetectedChanges(); clearDetectedChanges();
} }
// Finally, add/remove tracked players sending them the full spawn/destroy packets // Finally, add/remove tracked players sending them the full spawn/destroy packets
modifyTrackedPlayersAndSendPackets(onlinePlayers); modifyViewersAndSendPackets(onlinePlayers);
} }
protected abstract void detectChanges(); protected abstract void detectChanges();
@ -76,9 +76,9 @@ public abstract class LineTracker<T extends TrackedPlayer> {
protected abstract boolean updatePlaceholders(); protected abstract boolean updatePlaceholders();
private void modifyTrackedPlayersAndSendPackets(Collection<? extends Player> onlinePlayers) { private void modifyViewersAndSendPackets(Collection<? extends Player> onlinePlayers) {
if (!getLine().isInLoadedChunk()) { if (!getLine().isInLoadedChunk()) {
clearTrackedPlayersAndSendPackets(); resetViewersAndSendDestroyPackets();
return; return;
} }
@ -88,21 +88,21 @@ public abstract class LineTracker<T extends TrackedPlayer> {
for (Player player : onlinePlayers) { for (Player player : onlinePlayers) {
if (shouldTrackPlayer(player)) { if (shouldTrackPlayer(player)) {
if (!trackedPlayers.containsKey(player)) { if (!viewers.containsKey(player)) {
T trackedPlayer = createTrackedPlayer(player); T viewer = createViewer(player);
trackedPlayers.put(player, trackedPlayer); viewers.put(player, viewer);
if (addedPlayers == null) { if (addedPlayers == null) {
addedPlayers = new MutableViewers<>(); addedPlayers = new MutableViewers<>();
} }
addedPlayers.add(trackedPlayer); addedPlayers.add(viewer);
} }
} else { } else {
if (trackedPlayers.containsKey(player)) { if (viewers.containsKey(player)) {
T trackedPlayer = trackedPlayers.remove(player); T viewer = viewers.remove(player);
if (removedPlayers == null) { if (removedPlayers == null) {
removedPlayers = new MutableViewers<>(); removedPlayers = new MutableViewers<>();
} }
removedPlayers.add(trackedPlayer); removedPlayers.add(viewer);
} }
} }
} }
@ -115,33 +115,33 @@ public abstract class LineTracker<T extends TrackedPlayer> {
} }
} }
protected abstract T createTrackedPlayer(Player player); protected abstract T createViewer(Player player);
protected abstract boolean shouldTrackPlayer(Player player); protected abstract boolean shouldTrackPlayer(Player player);
protected final boolean hasTrackedPlayers() { protected final boolean hasViewers() {
return !trackedPlayers.isEmpty(); return !viewers.isEmpty();
} }
protected final Collection<T> getTrackedPlayers() { protected final Collection<T> getViewers() {
return trackedPlayers.values(); return viewers.values();
} }
public final boolean isTrackedPlayer(Player player) { public final boolean isViewer(Player player) {
return trackedPlayers.containsKey(player); return viewers.containsKey(player);
} }
protected final void removeTrackedPlayer(Player player) { protected final void removeViewer(Player player) {
trackedPlayers.remove(player); viewers.remove(player);
} }
protected final void clearTrackedPlayersAndSendPackets() { protected final void resetViewersAndSendDestroyPackets() {
if (!hasTrackedPlayers()) { if (!hasViewers()) {
return; return;
} }
sendDestroyPackets(trackedPlayersIterableView); sendDestroyPackets(iterableViewers);
trackedPlayers.clear(); viewers.clear();
} }
protected abstract void sendSpawnPackets(Viewers<T> viewers); protected abstract void sendSpawnPackets(Viewers<T> viewers);

View File

@ -61,15 +61,15 @@ public class LineTrackerManager {
} }
} }
public void clearTrackedPlayersAndSendPackets() { public void resetViewersAndSendDestroyPackets() {
for (LineTracker<?> tracker : lineTrackers) { for (LineTracker<?> tracker : lineTrackers) {
tracker.clearTrackedPlayersAndSendPackets(); tracker.resetViewersAndSendDestroyPackets();
} }
} }
public void onPlayerQuit(Player player) { public void onPlayerQuit(Player player) {
for (LineTracker<?> tracker : lineTrackers) { for (LineTracker<?> tracker : lineTrackers) {
tracker.removeTrackedPlayer(player); tracker.removeViewer(player);
} }
} }

View File

@ -9,7 +9,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
public class MutableViewers<T extends TrackedPlayer> implements Viewers<T> { public class MutableViewers<T extends Viewer> implements Viewers<T> {
// Optimization: the viewer is frequently only a single player // Optimization: the viewer is frequently only a single player
private T viewer; private T viewer;

View File

@ -12,7 +12,7 @@ import org.jetbrains.annotations.MustBeInvokedByOverriders;
import java.util.Objects; import java.util.Objects;
abstract class PositionBasedLineTracker<T extends TrackedPlayer> extends LineTracker<T> { abstract class PositionBasedLineTracker<T extends Viewer> extends LineTracker<T> {
private static final int ENTITY_VIEW_RANGE = 64; private static final int ENTITY_VIEW_RANGE = 64;

View File

@ -16,7 +16,7 @@ import org.jetbrains.annotations.MustBeInvokedByOverriders;
import java.util.Objects; import java.util.Objects;
public class TextLineTracker extends ClickableLineTracker<TextLineTrackedPlayer> { public class TextLineTracker extends ClickableLineTracker<TextLineViewer> {
private final BaseTextHologramLine line; private final BaseTextHologramLine line;
private final TextNMSPacketEntity textEntity; private final TextNMSPacketEntity textEntity;
@ -42,7 +42,7 @@ public class TextLineTracker extends ClickableLineTracker<TextLineTrackedPlayer>
@Override @Override
protected boolean updatePlaceholders() { protected boolean updatePlaceholders() {
boolean placeholdersChanged = displayText.updateReplacements(getTrackedPlayers()); boolean placeholdersChanged = displayText.updateReplacements(getViewers());
if (placeholdersChanged) { if (placeholdersChanged) {
displayTextChanged = true; // Mark as changed to trigger a packet send with updated placeholders displayTextChanged = true; // Mark as changed to trigger a packet send with updated placeholders
} }
@ -50,8 +50,8 @@ public class TextLineTracker extends ClickableLineTracker<TextLineTrackedPlayer>
} }
@Override @Override
protected TextLineTrackedPlayer createTrackedPlayer(Player player) { protected TextLineViewer createViewer(Player player) {
return new TextLineTrackedPlayer(player, displayText); return new TextLineViewer(player, displayText);
} }
@MustBeInvokedByOverriders @MustBeInvokedByOverriders
@ -81,7 +81,7 @@ public class TextLineTracker extends ClickableLineTracker<TextLineTrackedPlayer>
@MustBeInvokedByOverriders @MustBeInvokedByOverriders
@Override @Override
protected void sendSpawnPackets(Viewers<TextLineTrackedPlayer> viewers) { protected void sendSpawnPackets(Viewers<TextLineViewer> viewers) {
super.sendSpawnPackets(viewers); super.sendSpawnPackets(viewers);
IndividualTextPacketGroup spawnPackets = textEntity.newSpawnPackets(position); IndividualTextPacketGroup spawnPackets = textEntity.newSpawnPackets(position);
@ -90,13 +90,13 @@ public class TextLineTracker extends ClickableLineTracker<TextLineTrackedPlayer>
@MustBeInvokedByOverriders @MustBeInvokedByOverriders
@Override @Override
protected void sendDestroyPackets(Viewers<TextLineTrackedPlayer> viewers) { protected void sendDestroyPackets(Viewers<TextLineViewer> viewers) {
super.sendDestroyPackets(viewers); super.sendDestroyPackets(viewers);
viewers.sendPackets(textEntity.newDestroyPackets()); viewers.sendPackets(textEntity.newDestroyPackets());
} }
@Override @Override
protected void sendChangesPackets(Viewers<TextLineTrackedPlayer> viewers) { protected void sendChangesPackets(Viewers<TextLineViewer> viewers) {
super.sendChangesPackets(viewers); super.sendChangesPackets(viewers);
if (displayTextChanged) { if (displayTextChanged) {
@ -107,7 +107,7 @@ public class TextLineTracker extends ClickableLineTracker<TextLineTrackedPlayer>
@MustBeInvokedByOverriders @MustBeInvokedByOverriders
@Override @Override
protected void sendPositionChangePackets(Viewers<TextLineTrackedPlayer> viewers) { protected void sendPositionChangePackets(Viewers<TextLineViewer> viewers) {
super.sendPositionChangePackets(viewers); super.sendPositionChangePackets(viewers);
viewers.sendPackets(textEntity.newTeleportPackets(position)); viewers.sendPackets(textEntity.newTeleportPackets(position));
} }

View File

@ -11,14 +11,14 @@ import org.jetbrains.annotations.Nullable;
import java.util.Objects; import java.util.Objects;
class TextLineTrackedPlayer extends TrackedPlayer { class TextLineViewer extends Viewer {
private final DisplayText displayText; private final DisplayText displayText;
private String individualText; private String individualText;
private String lastSentText; private String lastSentText;
TextLineTrackedPlayer(Player player, DisplayText displayText) { TextLineViewer(Player player, DisplayText displayText) {
super(player); super(player);
this.displayText = displayText; this.displayText = displayText;
} }

View File

@ -8,11 +8,11 @@ package me.filoghost.holographicdisplays.plugin.hologram.tracking;
import me.filoghost.holographicdisplays.nms.common.PacketGroup; import me.filoghost.holographicdisplays.nms.common.PacketGroup;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
class TrackedPlayer { class Viewer {
private final Player player; private final Player player;
TrackedPlayer(Player player) { Viewer(Player player) {
this.player = player; this.player = player;
} }

View File

@ -9,7 +9,7 @@ import me.filoghost.holographicdisplays.nms.common.PacketGroup;
import java.util.function.Consumer; import java.util.function.Consumer;
interface Viewers<T extends TrackedPlayer> { interface Viewers<T extends Viewer> {
void forEach(Consumer<? super T> action); void forEach(Consumer<? super T> action);