World world = player.getWorld();
AutoRefMatch match = plugin.getMatch(world);
if (match == null || !match.isSpectator(player)) return;
AutoRefSpectator spec = match.getSpectator(player);
boolean pvis = spec.isInvisible();
double bdistance = Double.MAX_VALUE;
for (AutoRefPlayer apl : match.getPlayers()) if (apl.isOnline())
{
Location ploc = apl.getPlayer().getLocation();
double dist = ploc.distanceSquared(player.getLocation());
if (dist < bdistance) { bdistance = dist; }
}
// if the spectator should be invisible, change visibility and then redo visibility
boolean vis = bdistance <= SPECTATOR_VISIBILITY_RADIUS * SPECTATOR_VISIBILITY_RADIUS;
spec.setInvisible(vis);
// if the visibility status has changed, reconfigure
if (pvis != vis) match.setupVisibility(player);
}