diff --git a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java index 9d05b39da..71f87793b 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java @@ -240,6 +240,7 @@ public class ServerConnector extends PacketHandler // TODO: Move this to the connected() method of DownstreamBridge target.addPlayer( user ); user.getPendingConnects().remove( target ); + user.setServerJoinQueue( null ); user.setDimensionChange( false ); user.setServer( server ); @@ -261,18 +262,7 @@ public class ServerConnector extends PacketHandler @Override public void handle(Kick kick) throws Exception { - user.setLastServerJoined( user.getLastServerJoined() + 1 ); - String serverName = ""; - List servers = user.getPendingConnection().getListener().getServerPriority(); - if ( user.getLastServerJoined() < servers.size() ) - { - serverName = servers.get( user.getLastServerJoined() ); - } - ServerInfo def = ProxyServer.getInstance().getServers().get( serverName ); - if ( Objects.equal( target, def ) ) - { - def = null; - } + ServerInfo def = user.updateAndGetNextServer( target ); ServerKickEvent event = new ServerKickEvent( user, target, ComponentSerializer.parse( kick.getMessage() ), def, ServerKickEvent.State.CONNECTING ); if ( event.getKickReason().toLowerCase().contains( "outdated" ) && def != null ) { diff --git a/proxy/src/main/java/net/md_5/bungee/UserConnection.java b/proxy/src/main/java/net/md_5/bungee/UserConnection.java index e61b76bab..475559b28 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -14,9 +14,11 @@ import java.net.InetSocketAddress; import java.util.Collection; import java.util.Collections; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Queue; import java.util.UUID; import java.util.concurrent.TimeUnit; import java.util.logging.Level; @@ -106,9 +108,8 @@ public final class UserConnection implements ProxiedPlayer @Getter private int compressionThreshold = -1; // Used for trying multiple servers in order - @Getter @Setter - private int lastServerJoined = 0; + private Queue serverJoinQueue; /*========================================================================*/ private final Collection groups = new CaseInsensitiveSet(); private final Collection permissions = new CaseInsensitiveSet(); @@ -222,6 +223,26 @@ public final class UserConnection implements ProxiedPlayer connect( target ); } + public ServerInfo updateAndGetNextServer(ServerInfo currentTarget) + { + if ( serverJoinQueue == null ) + { + serverJoinQueue = new LinkedList<>( getPendingConnection().getListener().getServerPriority() ); + } + + ServerInfo next = null; + while ( !serverJoinQueue.isEmpty() ) + { + ServerInfo candidate = ProxyServer.getInstance().getServerInfo( serverJoinQueue.remove() ); + if ( !Objects.equal( currentTarget, candidate ) ) + { + next = candidate; + } + } + + return next; + } + public void connect(ServerInfo info, final Callback callback, final boolean retry) { Preconditions.checkNotNull( info, "info" ); @@ -274,16 +295,8 @@ public final class UserConnection implements ProxiedPlayer future.channel().close(); pendingConnects.remove( target ); - lastServerJoined++; - String serverName = ""; - List servers = getPendingConnection().getListener().getServerPriority(); - if ( lastServerJoined < servers.size() ) - { - serverName = servers.get( lastServerJoined ); - } - - ServerInfo def = ProxyServer.getInstance().getServers().get( serverName ); - if ( retry && def != null && target != def && ( getServer() == null || def != getServer().getInfo() ) ) + ServerInfo def = updateAndGetNextServer( target ); + if ( retry && def != null && ( getServer() == null || def != getServer().getInfo() ) ) { sendMessage( bungee.getTranslation( "fallback_lobby" ) ); connect( def, null, false ); @@ -294,9 +307,6 @@ public final class UserConnection implements ProxiedPlayer { sendMessage( bungee.getTranslation( "fallback_kick", future.cause().getClass().getName() ) ); } - } else - { - lastServerJoined = 0; } } };