Use UUIDs in DefaultVisibilityManager

This commit is contained in:
filoghost 2021-05-05 10:29:14 +02:00
parent de3f967a21
commit 9f7e2915c1

View File

@ -16,6 +16,7 @@ import org.bukkit.entity.Player;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
public class DefaultVisibilityManager implements VisibilityManager { public class DefaultVisibilityManager implements VisibilityManager {
@ -23,7 +24,7 @@ public class DefaultVisibilityManager implements VisibilityManager {
private static final int VISIBILITY_DISTANCE_SQUARED = 64 * 64; private static final int VISIBILITY_DISTANCE_SQUARED = 64 * 64;
private final StandardHologram hologram; private final StandardHologram hologram;
private Map<String, Boolean> playersVisibilityMap; private Map<UUID, Boolean> playersVisibilityMap;
private boolean visibleByDefault; private boolean visibleByDefault;
public DefaultVisibilityManager(StandardHologram hologram) { public DefaultVisibilityManager(StandardHologram hologram) {
@ -46,7 +47,7 @@ public class DefaultVisibilityManager implements VisibilityManager {
this.visibleByDefault = visibleByDefault; this.visibleByDefault = visibleByDefault;
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers()) {
if (playersVisibilityMap != null && playersVisibilityMap.containsKey(player.getName().toLowerCase())) { if (playersVisibilityMap != null && playersVisibilityMap.containsKey(player.getUniqueId())) {
// Has a specific value set // Has a specific value set
continue; continue;
} }
@ -72,7 +73,7 @@ public class DefaultVisibilityManager implements VisibilityManager {
playersVisibilityMap = new ConcurrentHashMap<>(); playersVisibilityMap = new ConcurrentHashMap<>();
} }
playersVisibilityMap.put(player.getName().toLowerCase(), true); playersVisibilityMap.put(player.getUniqueId(), true);
if (!wasVisible) { if (!wasVisible) {
sendCreatePacketIfNear(player, hologram); sendCreatePacketIfNear(player, hologram);
@ -91,7 +92,7 @@ public class DefaultVisibilityManager implements VisibilityManager {
playersVisibilityMap = new ConcurrentHashMap<>(); playersVisibilityMap = new ConcurrentHashMap<>();
} }
playersVisibilityMap.put(player.getName().toLowerCase(), false); playersVisibilityMap.put(player.getUniqueId(), false);
if (wasVisible) { if (wasVisible) {
sendDestroyPacketIfNear(player, hologram); sendDestroyPacketIfNear(player, hologram);
@ -103,7 +104,7 @@ public class DefaultVisibilityManager implements VisibilityManager {
Preconditions.notNull(player, "player"); Preconditions.notNull(player, "player");
if (playersVisibilityMap != null) { if (playersVisibilityMap != null) {
Boolean value = playersVisibilityMap.get(player.getName().toLowerCase()); Boolean value = playersVisibilityMap.get(player.getUniqueId());
if (value != null) { if (value != null) {
return value; return value;
} }
@ -122,7 +123,7 @@ public class DefaultVisibilityManager implements VisibilityManager {
boolean wasVisible = isVisibleTo(player); boolean wasVisible = isVisibleTo(player);
playersVisibilityMap.remove(player.getName().toLowerCase()); playersVisibilityMap.remove(player.getUniqueId());
if (visibleByDefault && !wasVisible) { if (visibleByDefault && !wasVisible) {
sendCreatePacketIfNear(player, hologram); sendCreatePacketIfNear(player, hologram);
@ -136,10 +137,10 @@ public class DefaultVisibilityManager implements VisibilityManager {
public void resetVisibilityAll() { public void resetVisibilityAll() {
if (playersVisibilityMap != null) { if (playersVisibilityMap != null) {
// We need to refresh all the players // We need to refresh all the players
Set<String> playerNames = new HashSet<>(playersVisibilityMap.keySet()); Set<UUID> playerIDs = new HashSet<>(playersVisibilityMap.keySet());
for (String playerName : playerNames) { for (UUID playerID : playerIDs) {
Player onlinePlayer = Bukkit.getPlayerExact(playerName); Player onlinePlayer = Bukkit.getPlayer(playerID);
if (onlinePlayer != null) { if (onlinePlayer != null) {
resetVisibility(onlinePlayer); resetVisibility(onlinePlayer);
} }