mirror of
https://github.com/filoghost/HolographicDisplays.git
synced 2025-01-31 20:21:22 +01:00
Move distance check for packets to ProtocolLibHook
This commit is contained in:
parent
fe626053e6
commit
4f25e6550b
@ -6,15 +6,21 @@
|
||||
package me.filoghost.holographicdisplays.core.hologram;
|
||||
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface StandardHologram {
|
||||
|
||||
Location getLocation();
|
||||
|
||||
World getWorld();
|
||||
|
||||
double getX();
|
||||
|
||||
double getY();
|
||||
|
||||
double getZ();
|
||||
|
||||
boolean isInChunk(Chunk chunk);
|
||||
|
||||
|
@ -8,11 +8,13 @@ package me.filoghost.holographicdisplays.bridge.protocollib;
|
||||
import me.filoghost.fcommons.Preconditions;
|
||||
import me.filoghost.fcommons.logging.ErrorCollector;
|
||||
import me.filoghost.fcommons.logging.Log;
|
||||
import me.filoghost.holographicdisplays.core.Utils;
|
||||
import me.filoghost.holographicdisplays.core.hologram.StandardHologram;
|
||||
import me.filoghost.holographicdisplays.core.nms.NMSManager;
|
||||
import me.filoghost.holographicdisplays.core.nms.ProtocolPacketSettings;
|
||||
import me.filoghost.holographicdisplays.util.VersionUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
@ -63,12 +65,33 @@ public class ProtocolLibHook {
|
||||
|
||||
public static void sendDestroyEntitiesPacket(Player player, StandardHologram hologram) {
|
||||
checkState();
|
||||
packetSender.sendDestroyEntitiesPacket(player, hologram);
|
||||
|
||||
if (shouldReceivePacket(player, hologram)) {
|
||||
packetSender.sendDestroyEntitiesPacket(player, hologram);
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendCreateEntitiesPacket(Player player, StandardHologram hologram) {
|
||||
checkState();
|
||||
packetSender.sendCreateEntitiesPacket(player, hologram);
|
||||
|
||||
if (shouldReceivePacket(player, hologram)) {
|
||||
packetSender.sendCreateEntitiesPacket(player, hologram);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean shouldReceivePacket(Player player, StandardHologram hologram) {
|
||||
if (!player.isOnline()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!player.getWorld().equals(hologram.getWorld())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Location playerLocation = player.getLocation();
|
||||
double distanceSquared = Utils.distanceSquared(playerLocation.getX(), hologram.getX(), playerLocation.getZ(), hologram.getZ());
|
||||
|
||||
return distanceSquared < 64 * 64;
|
||||
}
|
||||
|
||||
public static boolean isEnabled() {
|
||||
|
@ -10,7 +10,6 @@ import me.filoghost.holographicdisplays.api.VisibilityManager;
|
||||
import me.filoghost.holographicdisplays.bridge.protocollib.ProtocolLibHook;
|
||||
import me.filoghost.holographicdisplays.core.hologram.StandardHologram;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.HashSet;
|
||||
@ -20,8 +19,6 @@ import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class DefaultVisibilityManager implements VisibilityManager {
|
||||
|
||||
private static final int VISIBILITY_DISTANCE_SQUARED = 64 * 64;
|
||||
|
||||
private final StandardHologram hologram;
|
||||
private Map<UUID, Boolean> playersVisibilityMap;
|
||||
@ -54,10 +51,10 @@ public class DefaultVisibilityManager implements VisibilityManager {
|
||||
|
||||
if (visibleByDefault) {
|
||||
// Now it's visible, and it previously wasn't because the value has changed
|
||||
sendCreatePacketIfNear(player, hologram);
|
||||
sendCreatePacket(player, hologram);
|
||||
} else {
|
||||
// Opposite case: now it's not visible
|
||||
sendDestroyPacketIfNear(player, hologram);
|
||||
sendDestroyPacket(player, hologram);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -76,7 +73,7 @@ public class DefaultVisibilityManager implements VisibilityManager {
|
||||
playersVisibilityMap.put(player.getUniqueId(), true);
|
||||
|
||||
if (!wasVisible) {
|
||||
sendCreatePacketIfNear(player, hologram);
|
||||
sendCreatePacket(player, hologram);
|
||||
}
|
||||
}
|
||||
|
||||
@ -95,7 +92,7 @@ public class DefaultVisibilityManager implements VisibilityManager {
|
||||
playersVisibilityMap.put(player.getUniqueId(), false);
|
||||
|
||||
if (wasVisible) {
|
||||
sendDestroyPacketIfNear(player, hologram);
|
||||
sendDestroyPacket(player, hologram);
|
||||
}
|
||||
}
|
||||
|
||||
@ -126,10 +123,10 @@ public class DefaultVisibilityManager implements VisibilityManager {
|
||||
playersVisibilityMap.remove(player.getUniqueId());
|
||||
|
||||
if (visibleByDefault && !wasVisible) {
|
||||
sendCreatePacketIfNear(player, hologram);
|
||||
sendCreatePacket(player, hologram);
|
||||
|
||||
} else if (!visibleByDefault && wasVisible) {
|
||||
sendDestroyPacketIfNear(player, hologram);
|
||||
sendDestroyPacket(player, hologram);
|
||||
}
|
||||
}
|
||||
|
||||
@ -151,26 +148,17 @@ public class DefaultVisibilityManager implements VisibilityManager {
|
||||
}
|
||||
}
|
||||
|
||||
private void sendCreatePacketIfNear(Player player, StandardHologram hologram) {
|
||||
if (ProtocolLibHook.isEnabled() && isNear(player, hologram)) {
|
||||
private void sendCreatePacket(Player player, StandardHologram hologram) {
|
||||
if (ProtocolLibHook.isEnabled()) {
|
||||
ProtocolLibHook.sendCreateEntitiesPacket(player, hologram);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendDestroyPacketIfNear(Player player, StandardHologram hologram) {
|
||||
if (ProtocolLibHook.isEnabled() && isNear(player, hologram)) {
|
||||
private void sendDestroyPacket(Player player, StandardHologram hologram) {
|
||||
if (ProtocolLibHook.isEnabled()) {
|
||||
ProtocolLibHook.sendDestroyEntitiesPacket(player, hologram);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isNear(Player player, StandardHologram hologram) {
|
||||
Location playerLocation = player.getLocation();
|
||||
Location hologramLocation = hologram.getLocation();
|
||||
|
||||
return player.isOnline()
|
||||
&& playerLocation.getWorld().equals(hologramLocation.getWorld())
|
||||
&& playerLocation.distanceSquared(hologramLocation) < VISIBILITY_DISTANCE_SQUARED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
Loading…
Reference in New Issue
Block a user