Implement a connect callback - see #760

This commit is contained in:
md_5 2013-12-21 13:11:27 +11:00
parent f7d3dfd61d
commit 075518b643
3 changed files with 32 additions and 7 deletions

View File

@ -1,5 +1,6 @@
package net.md_5.bungee.api.connection; 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.CommandSender;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.tab.TabListHandler; import net.md_5.bungee.api.tab.TabListHandler;
@ -35,6 +36,18 @@ public interface ProxiedPlayer extends Connection, CommandSender
*/ */
void connect(ServerInfo target); 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<Boolean> callback);
/** /**
* Gets the server this player is connected to. * Gets the server this player is connected to.
* *

View File

@ -19,6 +19,7 @@ import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.Setter; import lombok.Setter;
import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
@ -162,7 +163,13 @@ public final class UserConnection implements ProxiedPlayer
@Override @Override
public void connect(ServerInfo target) public void connect(ServerInfo target)
{ {
connect( target, false ); connect( target, null );
}
@Override
public void connect(ServerInfo target, Callback<Boolean> callback)
{
connect( target, callback, false );
} }
void sendDimensionSwitch() void sendDimensionSwitch()
@ -178,7 +185,7 @@ public final class UserConnection implements ProxiedPlayer
connect( target ); connect( target );
} }
public void connect(ServerInfo info, final boolean retry) public void connect(ServerInfo info, final Callback<Boolean> callback, final boolean retry)
{ {
Preconditions.checkNotNull( info, "info" ); Preconditions.checkNotNull( info, "info" );
@ -219,6 +226,8 @@ public final class UserConnection implements ProxiedPlayer
@Override @Override
public void operationComplete(ChannelFuture future) throws Exception public void operationComplete(ChannelFuture future) throws Exception
{ {
callback.done( future.isSuccess(), future.cause() );
if ( !future.isSuccess() ) if ( !future.isSuccess() )
{ {
future.channel().close(); future.channel().close();
@ -228,7 +237,7 @@ public final class UserConnection implements ProxiedPlayer
if ( retry && target != def && ( getServer() == null || def != getServer().getInfo() ) ) if ( retry && target != def && ( getServer() == null || def != getServer().getInfo() ) )
{ {
sendMessage( bungee.getTranslation( "fallback_lobby" ) ); sendMessage( bungee.getTranslation( "fallback_lobby" ) );
connect( def, false ); connect( def, null, false );
} else } else
{ {
if ( dimensionChange ) if ( dimensionChange )
@ -274,7 +283,7 @@ public final class UserConnection implements ProxiedPlayer
disconnect0( reason ); disconnect0( reason );
} }
public synchronized void disconnect0(BaseComponent ...reason) public synchronized void disconnect0(BaseComponent... reason)
{ {
if ( ch.getHandle().isActive() ) if ( ch.getHandle().isActive() )
{ {

View File

@ -383,7 +383,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
{ {
server = AbstractReconnectHandler.getForcedHost( InitialHandler.this ); server = AbstractReconnectHandler.getForcedHost( InitialHandler.this );
} }
userCon.connect( server, true ); userCon.connect( server, null, true );
thisState = State.FINISHED; thisState = State.FINISHED;
} }
@ -399,7 +399,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
@Override @Override
public synchronized void disconnect(String reason) public synchronized void disconnect(String reason)
{ {
if (!ch.isClosed()) if ( !ch.isClosed() )
{ {
unsafe().sendPacket( new Kick( ComponentSerializer.toString( TextComponent.fromLegacyText( reason ) ) ) ); unsafe().sendPacket( new Kick( ComponentSerializer.toString( TextComponent.fromLegacyText( reason ) ) ) );
ch.close(); ch.close();
@ -419,7 +419,10 @@ public class InitialHandler extends PacketHandler implements PendingConnection
@Override @Override
public void disconnect(BaseComponent reason) public void disconnect(BaseComponent reason)
{ {
disconnect( new BaseComponent[]{reason} ); disconnect( new BaseComponent[]
{
reason
} );
} }
@Override @Override