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 ticks


diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
index 3a46a673349de9d9d725516a0fcdb3ebb9b372b3..c316b0468c53825f90d9bc2ad40d655d92857715 100644
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
@@ -151,7 +151,7 @@ public abstract class PlayerList {
     private int viewDistance;
     private EnumGamemode u;
     private boolean v;
-    private int w;
+    private int w; private int getTick() { return this.w; } private int setTick(int i) { return this.w = i; } // Purpur - OBFHELPER
 
     // CraftBukkit start
     private CraftServer cserver;
@@ -1021,22 +1021,23 @@ public abstract class PlayerList {
     }
 
     public void tick() {
-        if (++this.w > 600) {
-            // CraftBukkit start
-            for (int i = 0; i < this.players.size(); ++i) {
-                final EntityPlayer target = (EntityPlayer) this.players.get(i);
-
-                target.playerConnection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.UPDATE_LATENCY, Iterables.filter(this.players, new Predicate<EntityPlayer>() {
-                    @Override
-                    public boolean apply(EntityPlayer input) {
-                        return target.getBukkitEntity().canSee(input.getBukkitEntity());
-                    }
-                })));
+        // Purpur start
+        int tick = getTick();
+        if (tick < this.players.size()) {
+            final org.bukkit.craftbukkit.entity.CraftPlayer target = this.players.get(tick).getBukkitEntity();
+            final java.util.List<EntityPlayer> list = new java.util.ArrayList<>();
+            for (EntityPlayer entityplayer : this.players) {
+                if (target.canSee(entityplayer.getUniqueID())) {
+                    list.add(entityplayer);
+                }
             }
-            // CraftBukkit end
-            this.w = 0;
+            target.getHandle().playerConnection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.UPDATE_LATENCY, list));
         }
-
+        if (++tick > 600) {
+            tick = 0;
+        }
+        setTick(tick);
+        // Purpur end
     }
 
     public void sendAll(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 1ff48eae5e5b732fbd1e720c55e08a30669bebfa..ca465a53a0c6232f912287704fc63cdcc59a28bd 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
@@ -1444,7 +1444,13 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
 
     @Override
     public boolean canSee(Player player) {
-        return !hiddenPlayers.containsKey(player.getUniqueId());
+        // Purpur start
+        return canSee(player.getUniqueId());
+    }
+
+    public boolean canSee(UUID uuid) {
+        return !hiddenPlayers.containsKey(uuid);
+        // Purpur end
     }
 
     @Override
