diff --git a/api/src/main/java/net/md_5/bungee/api/connection/ProxiedPlayer.java b/api/src/main/java/net/md_5/bungee/api/connection/ProxiedPlayer.java index bc5f6ee5..46faa9fc 100644 --- a/api/src/main/java/net/md_5/bungee/api/connection/ProxiedPlayer.java +++ b/api/src/main/java/net/md_5/bungee/api/connection/ProxiedPlayer.java @@ -1,5 +1,6 @@ package net.md_5.bungee.api.connection; +import net.md_5.bungee.api.Callback; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.tab.TabListHandler; @@ -35,6 +36,18 @@ public interface ProxiedPlayer extends Connection, CommandSender */ void connect(ServerInfo target); + /** + * Connects / transfers this user to the specified connection, gracefully + * closing the current one. Depending on the implementation, this method + * might return before the user has been connected. + * + * @param target the new server to connect to + * @param callback the method called when the connection is complete, or + * when an exception is encountered. The boolean parameter denotes success + * or failure. + */ + void connect(ServerInfo target, Callback callback); + /** * Gets the server this player is connected to. * 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 e51ec343..4ee7fcc2 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -19,6 +19,7 @@ import lombok.Getter; import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.Setter; +import net.md_5.bungee.api.Callback; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.TextComponent; @@ -162,7 +163,13 @@ public final class UserConnection implements ProxiedPlayer @Override public void connect(ServerInfo target) { - connect( target, false ); + connect( target, null ); + } + + @Override + public void connect(ServerInfo target, Callback callback) + { + connect( target, callback, false ); } void sendDimensionSwitch() @@ -178,7 +185,7 @@ public final class UserConnection implements ProxiedPlayer connect( target ); } - public void connect(ServerInfo info, final boolean retry) + public void connect(ServerInfo info, final Callback callback, final boolean retry) { Preconditions.checkNotNull( info, "info" ); @@ -219,6 +226,8 @@ public final class UserConnection implements ProxiedPlayer @Override public void operationComplete(ChannelFuture future) throws Exception { + callback.done( future.isSuccess(), future.cause() ); + if ( !future.isSuccess() ) { future.channel().close(); @@ -228,7 +237,7 @@ public final class UserConnection implements ProxiedPlayer if ( retry && target != def && ( getServer() == null || def != getServer().getInfo() ) ) { sendMessage( bungee.getTranslation( "fallback_lobby" ) ); - connect( def, false ); + connect( def, null, false ); } else { if ( dimensionChange ) @@ -274,7 +283,7 @@ public final class UserConnection implements ProxiedPlayer disconnect0( reason ); } - public synchronized void disconnect0(BaseComponent ...reason) + public synchronized void disconnect0(BaseComponent... reason) { if ( ch.getHandle().isActive() ) { diff --git a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java index 621ef385..26b6e6d4 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java @@ -383,7 +383,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection { server = AbstractReconnectHandler.getForcedHost( InitialHandler.this ); } - userCon.connect( server, true ); + userCon.connect( server, null, true ); thisState = State.FINISHED; } @@ -399,7 +399,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection @Override public synchronized void disconnect(String reason) { - if (!ch.isClosed()) + if ( !ch.isClosed() ) { unsafe().sendPacket( new Kick( ComponentSerializer.toString( TextComponent.fromLegacyText( reason ) ) ) ); ch.close(); @@ -419,7 +419,10 @@ public class InitialHandler extends PacketHandler implements PendingConnection @Override public void disconnect(BaseComponent reason) { - disconnect( new BaseComponent[]{reason} ); + disconnect( new BaseComponent[] + { + reason + } ); } @Override