From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Thu, 9 Mar 2023 20:50:15 -0800
Subject: [PATCH] Max pending logins

Should help the floodgates on launch

diff --git a/net/minecraft/server/network/ServerLoginPacketListenerImpl.java b/net/minecraft/server/network/ServerLoginPacketListenerImpl.java
index 6173f704b0d093813ec67eb231c75be49a462e7d..159f2f169d26b436a70006f7bc9bdc481315dd32 100644
--- a/net/minecraft/server/network/ServerLoginPacketListenerImpl.java
+++ b/net/minecraft/server/network/ServerLoginPacketListenerImpl.java
@@ -117,7 +117,7 @@ public class ServerLoginPacketListenerImpl implements ServerLoginPacketListener,
             if (this.server.getPlayerList().pushPendingJoin(name, uniqueId, this.connection)) {
             // Folia end - region threading - rewrite login process
             this.verifyLoginAndFinishConnectionSetup(Objects.requireNonNull(this.authenticatedProfile));
-            } // Paper - prevent logins to be processed even though disconnect was called
+            } else { --this.tick; } // Paper - prevent logins to be processed even though disconnect was called // Folia - max concurrent logins
         }
 
         // CraftBukkit start
diff --git a/net/minecraft/server/players/PlayerList.java b/net/minecraft/server/players/PlayerList.java
index 65835fa09cdcd3bb158025f7d8b3cb29ac8b2549..bcfe3af0b0df25d4b308b11a64c361da1ab3eeca 100644
--- a/net/minecraft/server/players/PlayerList.java
+++ b/net/minecraft/server/players/PlayerList.java
@@ -151,6 +151,17 @@ public abstract class PlayerList {
             conflictingId = this.connectionById.get(byId);
 
             if (conflictingName == null && conflictingId == null) {
+                // Folia start - max concurrent login
+                int loggedInCount = 0;
+                for (Connection value : this.connectionById.values()) {
+                    if (value.getPacketListener() instanceof ServerGamePacketListenerImpl) {
+                        ++loggedInCount;
+                    }
+                }
+                if ((this.connectionById.size() - loggedInCount) >= io.papermc.paper.configuration.GlobalConfiguration.get().misc.maxJoinsPerTick) {
+                    return false;
+                }
+                // Folia end - max concurrent login
                 this.connectionByName.put(userName, conn);
                 this.connectionById.put(byId, conn);
             }
