Optimization
This commit is contained in:
parent
d58adb7db8
commit
c850488ba7
|
@ -13,13 +13,16 @@ import org.jetbrains.annotations.NotNull;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
public class DefaultVisibilitySettings implements VisibilitySettings {
|
public class DefaultVisibilitySettings implements VisibilitySettings {
|
||||||
|
|
||||||
|
private final AtomicInteger version;
|
||||||
private Visibility globalVisibility;
|
private Visibility globalVisibility;
|
||||||
private Map<UUID, Visibility> individualVisibilities;
|
private Map<UUID, Visibility> individualVisibilities;
|
||||||
|
|
||||||
public DefaultVisibilitySettings() {
|
public DefaultVisibilitySettings() {
|
||||||
|
this.version = new AtomicInteger();
|
||||||
this.globalVisibility = Visibility.VISIBLE;
|
this.globalVisibility = Visibility.VISIBLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,6 +38,7 @@ public class DefaultVisibilitySettings implements VisibilitySettings {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.globalVisibility = visibility;
|
this.globalVisibility = visibility;
|
||||||
|
version.incrementAndGet();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -43,7 +47,10 @@ public class DefaultVisibilitySettings implements VisibilitySettings {
|
||||||
if (individualVisibilities == null) {
|
if (individualVisibilities == null) {
|
||||||
individualVisibilities = new ConcurrentHashMap<>();
|
individualVisibilities = new ConcurrentHashMap<>();
|
||||||
}
|
}
|
||||||
individualVisibilities.put(player.getUniqueId(), visibility);
|
Visibility previousVisibility = individualVisibilities.put(player.getUniqueId(), visibility);
|
||||||
|
if (visibility != previousVisibility) {
|
||||||
|
version.incrementAndGet();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -72,16 +79,24 @@ public class DefaultVisibilitySettings implements VisibilitySettings {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
individualVisibilities.remove(player.getUniqueId());
|
Visibility previousVisibility = individualVisibilities.remove(player.getUniqueId());
|
||||||
|
if (previousVisibility != null) {
|
||||||
|
version.incrementAndGet();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clearIndividualVisibilities() {
|
public void clearIndividualVisibilities() {
|
||||||
if (individualVisibilities == null) {
|
if (individualVisibilities == null || individualVisibilities.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
individualVisibilities.clear();
|
individualVisibilities.clear();
|
||||||
|
version.incrementAndGet();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getVersion() {
|
||||||
|
return version.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -33,11 +33,11 @@ public abstract class BaseClickableHologramLine extends BaseHologramLine {
|
||||||
|
|
||||||
private boolean isInClickRange(Player player) {
|
private boolean isInClickRange(Player player) {
|
||||||
Location playerLocation = player.getLocation();
|
Location playerLocation = player.getLocation();
|
||||||
PositionCoordinates position = this.getPosition();
|
PositionCoordinates positionCoordinates = this.getCoordinates();
|
||||||
|
|
||||||
double xDiff = playerLocation.getX() - position.getX();
|
double xDiff = playerLocation.getX() - positionCoordinates.getX();
|
||||||
double yDiff = playerLocation.getY() + 1.3 - position.getY(); // Use shoulder height
|
double yDiff = playerLocation.getY() + 1.3 - positionCoordinates.getY(); // Use shoulder height
|
||||||
double zDiff = playerLocation.getZ() - position.getZ();
|
double zDiff = playerLocation.getZ() - positionCoordinates.getZ();
|
||||||
|
|
||||||
double distanceSquared = (xDiff * xDiff) + (yDiff * yDiff) + (zDiff * zDiff);
|
double distanceSquared = (xDiff * xDiff) + (yDiff * yDiff) + (zDiff * zDiff);
|
||||||
return distanceSquared < 5 * 5;
|
return distanceSquared < 5 * 5;
|
||||||
|
|
|
@ -100,7 +100,7 @@ public abstract class BaseHologram extends BaseHologramComponent {
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Hologram{"
|
return "Hologram{"
|
||||||
+ "position=" + hologramPosition.getPosition()
|
+ "position=" + hologramPosition
|
||||||
+ ", lines=" + getLines()
|
+ ", lines=" + getLines()
|
||||||
+ ", deleted=" + isDeleted()
|
+ ", deleted=" + isDeleted()
|
||||||
+ "}";
|
+ "}";
|
||||||
|
|
|
@ -7,6 +7,7 @@ package me.filoghost.holographicdisplays.core.base;
|
||||||
|
|
||||||
import me.filoghost.fcommons.Preconditions;
|
import me.filoghost.fcommons.Preconditions;
|
||||||
import me.filoghost.holographicdisplays.common.PositionCoordinates;
|
import me.filoghost.holographicdisplays.common.PositionCoordinates;
|
||||||
|
import me.filoghost.holographicdisplays.core.api.current.DefaultVisibilitySettings;
|
||||||
import me.filoghost.holographicdisplays.core.tracking.LineTracker;
|
import me.filoghost.holographicdisplays.core.tracking.LineTracker;
|
||||||
import me.filoghost.holographicdisplays.core.tracking.LineTrackerManager;
|
import me.filoghost.holographicdisplays.core.tracking.LineTrackerManager;
|
||||||
import org.bukkit.GameMode;
|
import org.bukkit.GameMode;
|
||||||
|
@ -21,7 +22,7 @@ public abstract class BaseHologramLine extends BaseHologramComponent implements
|
||||||
private final BaseHologram hologram;
|
private final BaseHologram hologram;
|
||||||
private final LineTracker<?> tracker;
|
private final LineTracker<?> tracker;
|
||||||
|
|
||||||
private PositionCoordinates position;
|
private PositionCoordinates coordinates;
|
||||||
|
|
||||||
protected BaseHologramLine(BaseHologram hologram) {
|
protected BaseHologramLine(BaseHologram hologram) {
|
||||||
Preconditions.notNull(hologram, "hologram");
|
Preconditions.notNull(hologram, "hologram");
|
||||||
|
@ -40,16 +41,20 @@ public abstract class BaseHologramLine extends BaseHologramComponent implements
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void setPosition(double x, double y, double z) {
|
public final void setCoordinates(double x, double y, double z) {
|
||||||
position = new PositionCoordinates(x, y, z);
|
coordinates = new PositionCoordinates(x, y, z);
|
||||||
setChanged();
|
setChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull PositionCoordinates getPosition() {
|
public @NotNull PositionCoordinates getCoordinates() {
|
||||||
if (position == null) {
|
if (coordinates == null) {
|
||||||
throw new IllegalStateException("position not set");
|
throw new IllegalStateException("position not set");
|
||||||
}
|
}
|
||||||
return position;
|
return coordinates;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull String getWorldName() {
|
||||||
|
return hologram.getPosition().getWorldName();
|
||||||
}
|
}
|
||||||
|
|
||||||
public @Nullable World getWorldIfLoaded() {
|
public @Nullable World getWorldIfLoaded() {
|
||||||
|
@ -68,6 +73,10 @@ public abstract class BaseHologramLine extends BaseHologramComponent implements
|
||||||
return hologram.getCreatorPlugin();
|
return hologram.getCreatorPlugin();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final DefaultVisibilitySettings getVisibilitySettings() {
|
||||||
|
return hologram.getVisibilitySettings();
|
||||||
|
}
|
||||||
|
|
||||||
protected boolean canInteract(Player player) {
|
protected boolean canInteract(Player player) {
|
||||||
return !isDeleted()
|
return !isDeleted()
|
||||||
&& player.isOnline()
|
&& player.isOnline()
|
||||||
|
|
|
@ -101,7 +101,7 @@ public class BaseHologramLines<T extends EditableHologramLine> implements Iterab
|
||||||
currentLineY -= spaceBetweenLines;
|
currentLineY -= spaceBetweenLines;
|
||||||
}
|
}
|
||||||
|
|
||||||
line.setPosition(hologramPosition.getX(), currentLineY, hologramPosition.getZ());
|
line.setCoordinates(hologramPosition.getX(), currentLineY, hologramPosition.getZ());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ package me.filoghost.holographicdisplays.core.base;
|
||||||
|
|
||||||
public interface EditableHologramLine {
|
public interface EditableHologramLine {
|
||||||
|
|
||||||
void setPosition(double x, double y, double z);
|
void setCoordinates(double x, double y, double z);
|
||||||
|
|
||||||
double getHeight();
|
double getHeight();
|
||||||
|
|
||||||
|
|
|
@ -99,6 +99,11 @@ class HologramPosition {
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return position.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private enum ChunkLoadState {
|
private enum ChunkLoadState {
|
||||||
|
|
||||||
|
|
|
@ -11,15 +11,29 @@ import org.bukkit.entity.Player;
|
||||||
public class CachedPlayer {
|
public class CachedPlayer {
|
||||||
|
|
||||||
private final Player player;
|
private final Player player;
|
||||||
private final TickClock tickClock;
|
|
||||||
|
|
||||||
private Location location;
|
private Location location;
|
||||||
private long locationUpdateTick;
|
|
||||||
|
|
||||||
public CachedPlayer(Player player, TickClock tickClock) {
|
public CachedPlayer(Player player) {
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.tickClock = tickClock;
|
}
|
||||||
this.locationUpdateTick = -1;
|
|
||||||
|
boolean onTick() {
|
||||||
|
Location newLocation = player.getLocation();
|
||||||
|
boolean moved = isDifferentPosition(location, newLocation);
|
||||||
|
location = newLocation;
|
||||||
|
return moved;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isDifferentPosition(Location oldLocation, Location newLocation) {
|
||||||
|
if (oldLocation == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return newLocation.getWorld() != oldLocation.getWorld()
|
||||||
|
|| newLocation.getX() != oldLocation.getX()
|
||||||
|
|| newLocation.getY() != oldLocation.getY()
|
||||||
|
|| newLocation.getZ() != oldLocation.getZ();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player getBukkitPlayer() {
|
public Player getBukkitPlayer() {
|
||||||
|
@ -27,13 +41,6 @@ public class CachedPlayer {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Location getLocation() {
|
public Location getLocation() {
|
||||||
// Avoid creating a new object on each invocation
|
|
||||||
long currentTick = tickClock.getCurrentTick();
|
|
||||||
if (locationUpdateTick != currentTick) {
|
|
||||||
location = player.getLocation();
|
|
||||||
locationUpdateTick = currentTick;
|
|
||||||
}
|
|
||||||
|
|
||||||
return location;
|
return location;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ public class TickingTask implements Runnable {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onPlayerJoin(Player player) {
|
public void onPlayerJoin(Player player) {
|
||||||
onlinePlayers.add(new CachedPlayer(player, tickClock));
|
onlinePlayers.add(new CachedPlayer(player));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onPlayerQuit(Player player) {
|
public void onPlayerQuit(Player player) {
|
||||||
|
@ -65,8 +65,16 @@ public class TickingTask implements Runnable {
|
||||||
// Remove outdated entries before using them from line trackers
|
// Remove outdated entries before using them from line trackers
|
||||||
placeholderTracker.clearOutdatedEntries();
|
placeholderTracker.clearOutdatedEntries();
|
||||||
|
|
||||||
|
List<CachedPlayer> movedPlayers = new ArrayList<>();
|
||||||
|
for (CachedPlayer onlinePlayer : onlinePlayers) {
|
||||||
|
boolean moved = onlinePlayer.onTick();
|
||||||
|
if (moved) {
|
||||||
|
movedPlayers.add(onlinePlayer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
lineTrackerManager.update(onlinePlayers);
|
lineTrackerManager.update(onlinePlayers, movedPlayers);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
// Catch all types of Throwable because we're using NMS code
|
// Catch all types of Throwable because we're using NMS code
|
||||||
if (tickClock.getCurrentTick() - lastErrorLogTick >= 20) {
|
if (tickClock.getCurrentTick() - lastErrorLogTick >= 20) {
|
||||||
|
|
|
@ -6,11 +6,10 @@
|
||||||
package me.filoghost.holographicdisplays.core.tracking;
|
package me.filoghost.holographicdisplays.core.tracking;
|
||||||
|
|
||||||
import me.filoghost.holographicdisplays.common.PositionCoordinates;
|
import me.filoghost.holographicdisplays.common.PositionCoordinates;
|
||||||
import me.filoghost.holographicdisplays.nms.common.NMSManager;
|
|
||||||
import me.filoghost.holographicdisplays.nms.common.entity.ClickableNMSPacketEntity;
|
|
||||||
import me.filoghost.holographicdisplays.core.base.BaseClickableHologramLine;
|
import me.filoghost.holographicdisplays.core.base.BaseClickableHologramLine;
|
||||||
import me.filoghost.holographicdisplays.core.listener.LineClickListener;
|
import me.filoghost.holographicdisplays.core.listener.LineClickListener;
|
||||||
import me.filoghost.holographicdisplays.core.tick.TickClock;
|
import me.filoghost.holographicdisplays.nms.common.NMSManager;
|
||||||
|
import me.filoghost.holographicdisplays.nms.common.entity.ClickableNMSPacketEntity;
|
||||||
import org.jetbrains.annotations.MustBeInvokedByOverriders;
|
import org.jetbrains.annotations.MustBeInvokedByOverriders;
|
||||||
|
|
||||||
public abstract class ClickableLineTracker<T extends Viewer> extends LineTracker<T> {
|
public abstract class ClickableLineTracker<T extends Viewer> extends LineTracker<T> {
|
||||||
|
@ -22,8 +21,7 @@ public abstract class ClickableLineTracker<T extends Viewer> extends LineTracker
|
||||||
private boolean spawnClickableEntity;
|
private boolean spawnClickableEntity;
|
||||||
private boolean spawnClickableEntityChanged;
|
private boolean spawnClickableEntityChanged;
|
||||||
|
|
||||||
public ClickableLineTracker(BaseClickableHologramLine line, NMSManager nmsManager, LineClickListener lineClickListener, TickClock tickClock) {
|
public ClickableLineTracker(BaseClickableHologramLine line, NMSManager nmsManager, LineClickListener lineClickListener) {
|
||||||
super(tickClock);
|
|
||||||
this.clickableEntity = nmsManager.newClickablePacketEntity();
|
this.clickableEntity = nmsManager.newClickablePacketEntity();
|
||||||
this.positionOffsetY = (line.getHeight() - ClickableNMSPacketEntity.SLIME_HEIGHT) / 2;
|
this.positionOffsetY = (line.getHeight() - ClickableNMSPacketEntity.SLIME_HEIGHT) / 2;
|
||||||
this.lineClickListener = lineClickListener;
|
this.lineClickListener = lineClickListener;
|
||||||
|
@ -102,7 +100,7 @@ public abstract class ClickableLineTracker<T extends Viewer> extends LineTracker
|
||||||
}
|
}
|
||||||
|
|
||||||
private PositionCoordinates getClickableEntityPosition() {
|
private PositionCoordinates getClickableEntityPosition() {
|
||||||
return position.addY(positionOffsetY);
|
return positionCoordinates.addY(positionOffsetY);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,12 +5,11 @@
|
||||||
*/
|
*/
|
||||||
package me.filoghost.holographicdisplays.core.tracking;
|
package me.filoghost.holographicdisplays.core.tracking;
|
||||||
|
|
||||||
import me.filoghost.holographicdisplays.nms.common.NMSManager;
|
|
||||||
import me.filoghost.holographicdisplays.nms.common.entity.ItemNMSPacketEntity;
|
|
||||||
import me.filoghost.holographicdisplays.core.base.BaseItemHologramLine;
|
import me.filoghost.holographicdisplays.core.base.BaseItemHologramLine;
|
||||||
import me.filoghost.holographicdisplays.core.listener.LineClickListener;
|
import me.filoghost.holographicdisplays.core.listener.LineClickListener;
|
||||||
import me.filoghost.holographicdisplays.core.tick.CachedPlayer;
|
import me.filoghost.holographicdisplays.core.tick.CachedPlayer;
|
||||||
import me.filoghost.holographicdisplays.core.tick.TickClock;
|
import me.filoghost.holographicdisplays.nms.common.NMSManager;
|
||||||
|
import me.filoghost.holographicdisplays.nms.common.entity.ItemNMSPacketEntity;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.jetbrains.annotations.MustBeInvokedByOverriders;
|
import org.jetbrains.annotations.MustBeInvokedByOverriders;
|
||||||
|
|
||||||
|
@ -31,9 +30,8 @@ public class ItemLineTracker extends ClickableLineTracker<Viewer> {
|
||||||
public ItemLineTracker(
|
public ItemLineTracker(
|
||||||
BaseItemHologramLine line,
|
BaseItemHologramLine line,
|
||||||
NMSManager nmsManager,
|
NMSManager nmsManager,
|
||||||
LineClickListener lineClickListener,
|
LineClickListener lineClickListener) {
|
||||||
TickClock tickClock) {
|
super(line, nmsManager, lineClickListener);
|
||||||
super(line, nmsManager, lineClickListener, tickClock);
|
|
||||||
this.line = line;
|
this.line = line;
|
||||||
this.itemEntity = nmsManager.newItemPacketEntity();
|
this.itemEntity = nmsManager.newItemPacketEntity();
|
||||||
}
|
}
|
||||||
|
@ -45,12 +43,12 @@ public class ItemLineTracker extends ClickableLineTracker<Viewer> {
|
||||||
|
|
||||||
@MustBeInvokedByOverriders
|
@MustBeInvokedByOverriders
|
||||||
@Override
|
@Override
|
||||||
protected void update(List<CachedPlayer> onlinePlayers) {
|
protected void update(List<CachedPlayer> onlinePlayers, List<CachedPlayer> movedPlayers) {
|
||||||
super.update(onlinePlayers);
|
super.update(onlinePlayers, movedPlayers);
|
||||||
|
|
||||||
if (spawnItemEntity && hasViewers() && line.hasPickupCallback()) {
|
if (spawnItemEntity && hasViewers() && line.hasPickupCallback()) {
|
||||||
for (Viewer viewer : getViewers()) {
|
for (Viewer viewer : getViewers()) {
|
||||||
if (CollisionHelper.isInPickupRange(viewer.getLocation(), position)) {
|
if (CollisionHelper.isInPickupRange(viewer.getLocation(), positionCoordinates)) {
|
||||||
line.onPickup(viewer.getBukkitPlayer());
|
line.onPickup(viewer.getBukkitPlayer());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -99,7 +97,7 @@ public class ItemLineTracker extends ClickableLineTracker<Viewer> {
|
||||||
super.sendSpawnPackets(viewers);
|
super.sendSpawnPackets(viewers);
|
||||||
|
|
||||||
if (spawnItemEntity) {
|
if (spawnItemEntity) {
|
||||||
viewers.sendPackets(itemEntity.newSpawnPackets(position, itemStack));
|
viewers.sendPackets(itemEntity.newSpawnPackets(positionCoordinates, itemStack));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,7 +118,7 @@ public class ItemLineTracker extends ClickableLineTracker<Viewer> {
|
||||||
|
|
||||||
if (spawnItemEntityChanged) {
|
if (spawnItemEntityChanged) {
|
||||||
if (spawnItemEntity) {
|
if (spawnItemEntity) {
|
||||||
viewers.sendPackets(itemEntity.newSpawnPackets(position, itemStack));
|
viewers.sendPackets(itemEntity.newSpawnPackets(positionCoordinates, itemStack));
|
||||||
} else {
|
} else {
|
||||||
viewers.sendPackets(itemEntity.newDestroyPackets());
|
viewers.sendPackets(itemEntity.newDestroyPackets());
|
||||||
}
|
}
|
||||||
|
@ -136,7 +134,7 @@ public class ItemLineTracker extends ClickableLineTracker<Viewer> {
|
||||||
super.sendPositionChangePackets(viewers);
|
super.sendPositionChangePackets(viewers);
|
||||||
|
|
||||||
if (spawnItemEntity) {
|
if (spawnItemEntity) {
|
||||||
viewers.sendPackets(itemEntity.newTeleportPackets(position));
|
viewers.sendPackets(itemEntity.newTeleportPackets(positionCoordinates));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,6 @@ package me.filoghost.holographicdisplays.core.tracking;
|
||||||
import me.filoghost.holographicdisplays.common.PositionCoordinates;
|
import me.filoghost.holographicdisplays.common.PositionCoordinates;
|
||||||
import me.filoghost.holographicdisplays.core.base.BaseHologramLine;
|
import me.filoghost.holographicdisplays.core.base.BaseHologramLine;
|
||||||
import me.filoghost.holographicdisplays.core.tick.CachedPlayer;
|
import me.filoghost.holographicdisplays.core.tick.CachedPlayer;
|
||||||
import me.filoghost.holographicdisplays.core.tick.TickClock;
|
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.jetbrains.annotations.MustBeInvokedByOverriders;
|
import org.jetbrains.annotations.MustBeInvokedByOverriders;
|
||||||
|
@ -21,22 +20,20 @@ import java.util.Objects;
|
||||||
|
|
||||||
public abstract class LineTracker<T extends Viewer> {
|
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 Map<Player, T> viewers;
|
||||||
private final Viewers<T> iterableViewers;
|
private final Viewers<T> iterableViewers;
|
||||||
|
|
||||||
protected PositionCoordinates position;
|
private String positionWorldName;
|
||||||
|
protected PositionCoordinates positionCoordinates;
|
||||||
private boolean positionChanged;
|
private boolean positionChanged;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
*/
|
*/
|
||||||
private boolean lineChanged;
|
private boolean lineChanged;
|
||||||
|
private int lastVisibilitySettingsVersion;
|
||||||
|
|
||||||
protected LineTracker(TickClock tickClock) {
|
protected LineTracker() {
|
||||||
this.tickClock = tickClock;
|
|
||||||
this.viewers = new HashMap<>();
|
this.viewers = new HashMap<>();
|
||||||
this.iterableViewers = new DelegateViewers<>(viewers.values());
|
this.iterableViewers = new DelegateViewers<>(viewers.values());
|
||||||
}
|
}
|
||||||
|
@ -57,7 +54,7 @@ public abstract class LineTracker<T extends Viewer> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@MustBeInvokedByOverriders
|
@MustBeInvokedByOverriders
|
||||||
protected void update(List<CachedPlayer> onlinePlayers) {
|
protected void update(List<CachedPlayer> onlinePlayers, List<CachedPlayer> movedPlayers) {
|
||||||
boolean sendChangesPackets = false;
|
boolean sendChangesPackets = false;
|
||||||
|
|
||||||
// First, detect the changes if the flag is on and set it off
|
// First, detect the changes if the flag is on and set it off
|
||||||
|
@ -75,28 +72,43 @@ public abstract class LineTracker<T extends Viewer> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then, send the changes (if any) to already tracked players
|
// Then, send the changes (if any) to already tracked players
|
||||||
if (sendChangesPackets) {
|
if (sendChangesPackets && hasViewers()) {
|
||||||
if (hasViewers()) {
|
sendChangesPackets(iterableViewers);
|
||||||
sendChangesPackets(iterableViewers);
|
|
||||||
}
|
|
||||||
clearDetectedChanges();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally, add/remove tracked players sending them the full spawn/destroy packets
|
// Finally, add/remove viewers sending them the full spawn/destroy packets
|
||||||
modifyViewersAndSendPackets(onlinePlayers);
|
modifyViewersAndSendPackets(onlinePlayers, movedPlayers);
|
||||||
|
|
||||||
|
if (sendChangesPackets) {
|
||||||
|
clearDetectedChanges();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract boolean updatePlaceholders();
|
protected abstract boolean updatePlaceholders();
|
||||||
|
|
||||||
private void modifyViewersAndSendPackets(List<CachedPlayer> onlinePlayers) {
|
private void modifyViewersAndSendPackets(List<CachedPlayer> onlinePlayers, List<CachedPlayer> movedPlayers) {
|
||||||
if (!getLine().isInLoadedChunk()) {
|
if (!getLine().isInLoadedChunk()) {
|
||||||
resetViewersAndSendDestroyPackets();
|
resetViewersAndSendDestroyPackets();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the identity hash code to avoid updating all the lines at the same time
|
boolean checkAllPlayers = false;
|
||||||
if ((tickClock.getCurrentTick() + hashCode()) % MODIFY_VIEWERS_INTERVAL_TICKS != 0) {
|
|
||||||
return;
|
int visibilitySettingsVersion = getLine().getVisibilitySettings().getVersion();
|
||||||
|
if (visibilitySettingsVersion != lastVisibilitySettingsVersion) {
|
||||||
|
lastVisibilitySettingsVersion = visibilitySettingsVersion;
|
||||||
|
checkAllPlayers = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (positionChanged) {
|
||||||
|
checkAllPlayers = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<CachedPlayer> playersToCheck;
|
||||||
|
if (checkAllPlayers) {
|
||||||
|
playersToCheck = onlinePlayers;
|
||||||
|
} else {
|
||||||
|
playersToCheck = movedPlayers;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lazy initialization
|
// Lazy initialization
|
||||||
|
@ -104,9 +116,9 @@ public abstract class LineTracker<T extends Viewer> {
|
||||||
MutableViewers<T> removedPlayers = null;
|
MutableViewers<T> removedPlayers = null;
|
||||||
|
|
||||||
// Micro-optimization, don't use for-each loop to avoid creating a new Iterator (method called frequently)
|
// Micro-optimization, don't use for-each loop to avoid creating a new Iterator (method called frequently)
|
||||||
int size = onlinePlayers.size();
|
int size = playersToCheck.size();
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
CachedPlayer player = onlinePlayers.get(i);
|
CachedPlayer player = playersToCheck.get(i);
|
||||||
Player bukkitPlayer = player.getBukkitPlayer();
|
Player bukkitPlayer = player.getBukkitPlayer();
|
||||||
if (shouldTrackPlayer(player)) {
|
if (shouldTrackPlayer(player)) {
|
||||||
if (!viewers.containsKey(bukkitPlayer)) {
|
if (!viewers.containsKey(bukkitPlayer)) {
|
||||||
|
@ -142,8 +154,8 @@ public abstract class LineTracker<T extends Viewer> {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
double diffX = Math.abs(playerLocation.getX() - position.getX());
|
double diffX = Math.abs(playerLocation.getX() - positionCoordinates.getX());
|
||||||
double diffZ = Math.abs(playerLocation.getZ() - position.getZ());
|
double diffZ = Math.abs(playerLocation.getZ() - positionCoordinates.getZ());
|
||||||
|
|
||||||
return diffX <= getViewRange()
|
return diffX <= getViewRange()
|
||||||
&& diffZ <= getViewRange()
|
&& diffZ <= getViewRange()
|
||||||
|
@ -172,9 +184,15 @@ public abstract class LineTracker<T extends Viewer> {
|
||||||
|
|
||||||
@MustBeInvokedByOverriders
|
@MustBeInvokedByOverriders
|
||||||
protected void detectChanges() {
|
protected void detectChanges() {
|
||||||
PositionCoordinates position = getLine().getPosition();
|
PositionCoordinates positionCoordinates = getLine().getCoordinates();
|
||||||
if (!Objects.equals(this.position, position)) {
|
if (!Objects.equals(this.positionCoordinates, positionCoordinates)) {
|
||||||
this.position = position;
|
this.positionCoordinates = positionCoordinates;
|
||||||
|
this.positionChanged = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
String positionWorldName = getLine().getWorldName();
|
||||||
|
if (!Objects.equals(this.positionWorldName, positionWorldName)) {
|
||||||
|
this.positionWorldName = positionWorldName;
|
||||||
this.positionChanged = true;
|
this.positionChanged = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,18 +40,18 @@ public class LineTrackerManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public TextLineTracker startTracking(BaseTextHologramLine line) {
|
public TextLineTracker startTracking(BaseTextHologramLine line) {
|
||||||
TextLineTracker tracker = new TextLineTracker(line, nmsManager, lineClickListener, placeholderTracker, tickClock);
|
TextLineTracker tracker = new TextLineTracker(line, nmsManager, lineClickListener, placeholderTracker);
|
||||||
lineTrackers.add(tracker);
|
lineTrackers.add(tracker);
|
||||||
return tracker;
|
return tracker;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemLineTracker startTracking(BaseItemHologramLine line) {
|
public ItemLineTracker startTracking(BaseItemHologramLine line) {
|
||||||
ItemLineTracker tracker = new ItemLineTracker(line, nmsManager, lineClickListener, tickClock);
|
ItemLineTracker tracker = new ItemLineTracker(line, nmsManager, lineClickListener);
|
||||||
lineTrackers.add(tracker);
|
lineTrackers.add(tracker);
|
||||||
return tracker;
|
return tracker;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(List<CachedPlayer> onlinePlayers) {
|
public void update(List<CachedPlayer> onlinePlayers, List<CachedPlayer> movedPlayers) {
|
||||||
Iterator<LineTracker<?>> iterator = lineTrackers.iterator();
|
Iterator<LineTracker<?>> iterator = lineTrackers.iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
LineTracker<?> lineTracker = iterator.next();
|
LineTracker<?> lineTracker = iterator.next();
|
||||||
|
@ -63,7 +63,7 @@ public class LineTrackerManager {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
lineTracker.update(onlinePlayers);
|
lineTracker.update(onlinePlayers, movedPlayers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,14 +5,13 @@
|
||||||
*/
|
*/
|
||||||
package me.filoghost.holographicdisplays.core.tracking;
|
package me.filoghost.holographicdisplays.core.tracking;
|
||||||
|
|
||||||
import me.filoghost.holographicdisplays.nms.common.IndividualTextPacketGroup;
|
|
||||||
import me.filoghost.holographicdisplays.nms.common.NMSManager;
|
|
||||||
import me.filoghost.holographicdisplays.nms.common.entity.TextNMSPacketEntity;
|
|
||||||
import me.filoghost.holographicdisplays.core.base.BaseTextHologramLine;
|
import me.filoghost.holographicdisplays.core.base.BaseTextHologramLine;
|
||||||
import me.filoghost.holographicdisplays.core.listener.LineClickListener;
|
import me.filoghost.holographicdisplays.core.listener.LineClickListener;
|
||||||
import me.filoghost.holographicdisplays.core.placeholder.tracking.ActivePlaceholderTracker;
|
import me.filoghost.holographicdisplays.core.placeholder.tracking.ActivePlaceholderTracker;
|
||||||
import me.filoghost.holographicdisplays.core.tick.CachedPlayer;
|
import me.filoghost.holographicdisplays.core.tick.CachedPlayer;
|
||||||
import me.filoghost.holographicdisplays.core.tick.TickClock;
|
import me.filoghost.holographicdisplays.nms.common.IndividualTextPacketGroup;
|
||||||
|
import me.filoghost.holographicdisplays.nms.common.NMSManager;
|
||||||
|
import me.filoghost.holographicdisplays.nms.common.entity.TextNMSPacketEntity;
|
||||||
import org.jetbrains.annotations.MustBeInvokedByOverriders;
|
import org.jetbrains.annotations.MustBeInvokedByOverriders;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
@ -29,9 +28,8 @@ public class TextLineTracker extends ClickableLineTracker<TextLineViewer> {
|
||||||
BaseTextHologramLine line,
|
BaseTextHologramLine line,
|
||||||
NMSManager nmsManager,
|
NMSManager nmsManager,
|
||||||
LineClickListener lineClickListener,
|
LineClickListener lineClickListener,
|
||||||
ActivePlaceholderTracker placeholderTracker,
|
ActivePlaceholderTracker placeholderTracker) {
|
||||||
TickClock tickClock) {
|
super(line, nmsManager, lineClickListener);
|
||||||
super(line, nmsManager, lineClickListener, tickClock);
|
|
||||||
this.line = line;
|
this.line = line;
|
||||||
this.textEntity = nmsManager.newTextPacketEntity();
|
this.textEntity = nmsManager.newTextPacketEntity();
|
||||||
this.displayText = new DisplayText(placeholderTracker);
|
this.displayText = new DisplayText(placeholderTracker);
|
||||||
|
@ -86,7 +84,7 @@ public class TextLineTracker extends ClickableLineTracker<TextLineViewer> {
|
||||||
protected void sendSpawnPackets(Viewers<TextLineViewer> viewers) {
|
protected void sendSpawnPackets(Viewers<TextLineViewer> viewers) {
|
||||||
super.sendSpawnPackets(viewers);
|
super.sendSpawnPackets(viewers);
|
||||||
|
|
||||||
IndividualTextPacketGroup spawnPackets = textEntity.newSpawnPackets(position);
|
IndividualTextPacketGroup spawnPackets = textEntity.newSpawnPackets(positionCoordinates);
|
||||||
viewers.forEach(viewer -> viewer.sendTextPackets(spawnPackets));
|
viewers.forEach(viewer -> viewer.sendTextPackets(spawnPackets));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,7 +109,7 @@ public class TextLineTracker extends ClickableLineTracker<TextLineViewer> {
|
||||||
@Override
|
@Override
|
||||||
protected void sendPositionChangePackets(Viewers<TextLineViewer> viewers) {
|
protected void sendPositionChangePackets(Viewers<TextLineViewer> viewers) {
|
||||||
super.sendPositionChangePackets(viewers);
|
super.sendPositionChangePackets(viewers);
|
||||||
viewers.sendPackets(textEntity.newTeleportPackets(position));
|
viewers.sendPackets(textEntity.newTeleportPackets(positionCoordinates));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue