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
public void onDisable() {
if (lineTrackerManager != null) {
lineTrackerManager.clearTrackedPlayersAndSendPackets();
lineTrackerManager.resetViewersAndSendDestroyPackets();
}
if (nmsManager != null) {

View File

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

View File

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

View File

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

View File

@ -13,10 +13,10 @@ import java.util.Collection;
import java.util.HashMap;
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 Viewers<T> trackedPlayersIterableView;
private final Map<Player, T> viewers;
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.
@ -24,8 +24,8 @@ public abstract class LineTracker<T extends TrackedPlayer> {
private boolean lineChanged;
protected LineTracker() {
this.trackedPlayers = new HashMap<>();
this.trackedPlayersIterableView = action -> trackedPlayers.values().forEach(action);
this.viewers = new HashMap<>();
this.iterableViewers = action -> viewers.values().forEach(action);
}
protected abstract BaseHologramLine getLine();
@ -36,7 +36,7 @@ public abstract class LineTracker<T extends TrackedPlayer> {
@MustBeInvokedByOverriders
public void onRemoval() {
clearTrackedPlayersAndSendPackets();
resetViewersAndSendDestroyPackets();
}
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
if (sendChangesPackets) {
if (hasTrackedPlayers()) {
sendChangesPackets(trackedPlayersIterableView);
if (hasViewers()) {
sendChangesPackets(iterableViewers);
}
clearDetectedChanges();
}
// Finally, add/remove tracked players sending them the full spawn/destroy packets
modifyTrackedPlayersAndSendPackets(onlinePlayers);
modifyViewersAndSendPackets(onlinePlayers);
}
protected abstract void detectChanges();
@ -76,9 +76,9 @@ public abstract class LineTracker<T extends TrackedPlayer> {
protected abstract boolean updatePlaceholders();
private void modifyTrackedPlayersAndSendPackets(Collection<? extends Player> onlinePlayers) {
private void modifyViewersAndSendPackets(Collection<? extends Player> onlinePlayers) {
if (!getLine().isInLoadedChunk()) {
clearTrackedPlayersAndSendPackets();
resetViewersAndSendDestroyPackets();
return;
}
@ -88,21 +88,21 @@ public abstract class LineTracker<T extends TrackedPlayer> {
for (Player player : onlinePlayers) {
if (shouldTrackPlayer(player)) {
if (!trackedPlayers.containsKey(player)) {
T trackedPlayer = createTrackedPlayer(player);
trackedPlayers.put(player, trackedPlayer);
if (!viewers.containsKey(player)) {
T viewer = createViewer(player);
viewers.put(player, viewer);
if (addedPlayers == null) {
addedPlayers = new MutableViewers<>();
}
addedPlayers.add(trackedPlayer);
addedPlayers.add(viewer);
}
} else {
if (trackedPlayers.containsKey(player)) {
T trackedPlayer = trackedPlayers.remove(player);
if (viewers.containsKey(player)) {
T viewer = viewers.remove(player);
if (removedPlayers == null) {
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 final boolean hasTrackedPlayers() {
return !trackedPlayers.isEmpty();
protected final boolean hasViewers() {
return !viewers.isEmpty();
}
protected final Collection<T> getTrackedPlayers() {
return trackedPlayers.values();
protected final Collection<T> getViewers() {
return viewers.values();
}
public final boolean isTrackedPlayer(Player player) {
return trackedPlayers.containsKey(player);
public final boolean isViewer(Player player) {
return viewers.containsKey(player);
}
protected final void removeTrackedPlayer(Player player) {
trackedPlayers.remove(player);
protected final void removeViewer(Player player) {
viewers.remove(player);
}
protected final void clearTrackedPlayersAndSendPackets() {
if (!hasTrackedPlayers()) {
protected final void resetViewersAndSendDestroyPackets() {
if (!hasViewers()) {
return;
}
sendDestroyPackets(trackedPlayersIterableView);
trackedPlayers.clear();
sendDestroyPackets(iterableViewers);
viewers.clear();
}
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) {
tracker.clearTrackedPlayersAndSendPackets();
tracker.resetViewersAndSendDestroyPackets();
}
}
public void onPlayerQuit(Player player) {
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.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
private T viewer;

View File

@ -12,7 +12,7 @@ import org.jetbrains.annotations.MustBeInvokedByOverriders;
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;

View File

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

View File

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

View File

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

View File

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