From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: James Lyne <jim+github@not-null.co.uk>
Date: Mon, 7 Dec 2020 17:52:36 +0000
Subject: [PATCH] Spread out and optimise player list ticksSpread out and
 optimise player list ticks


diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
index c1d79527abc7c8a165872758db3d5240615a63aa..2381d75295554c0310e36a26f5332063b126adc9 100644
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
@@ -1012,22 +1012,22 @@ public abstract class PlayerList {
     }
 
     public void tick() {
-        if (++this.sendAllPlayerInfoIn > 600) {
-            // CraftBukkit start
-            for (int i = 0; i < this.players.size(); ++i) {
-                final ServerPlayer target = (ServerPlayer) this.players.get(i);
-
-                target.connection.send(new ClientboundPlayerInfoPacket(ClientboundPlayerInfoPacket.Action.UPDATE_LATENCY, this.players.stream().filter(new Predicate<ServerPlayer>() {
-                    @Override
-                    public boolean apply(ServerPlayer input) {
-                        return target.getBukkitEntity().canSee(input.getBukkitEntity());
-                    }
-                }).collect(Collectors.toList())));
+        // Purpur start
+        if (this.sendAllPlayerInfoIn < this.players.size()) {
+            final org.bukkit.craftbukkit.entity.CraftPlayer target = this.players.get(this.sendAllPlayerInfoIn).getBukkitEntity();
+            final List<ServerPlayer> list = new java.util.ArrayList<>();
+            for (ServerPlayer player : this.players) {
+                if (target.canSee(player.getUUID())) {
+                    list.add(player);
+                }
             }
-            // CraftBukkit end
-            this.sendAllPlayerInfoIn = 0;
+            target.getHandle().connection.send(new ClientboundPlayerInfoPacket(ClientboundPlayerInfoPacket.Action.UPDATE_LATENCY, list));
         }
 
+        if (++this.sendAllPlayerInfoIn > 600) {
+            this.sendAllPlayerInfoIn = 0;
+        }
+        // Purpur end
     }
 
     public void broadcastAll(Packet<?> packet) {
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
index ec4397cc98b66fef55a91854c42518479b839bbe..15f7092468fd341d4e5d22163ea0e265803e5139 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
@@ -1569,7 +1569,13 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
 
     @Override
     public boolean canSee(Player player) {
-        return !this.hiddenPlayers.containsKey(player.getUniqueId());
+        // Purpur start
+        return canSee(player.getUniqueId());
+    }
+
+    public boolean canSee(UUID uuid) {
+        return !hiddenPlayers.containsKey(uuid);
+        // Purpur end
     }
 
     @Override
