Fix async/login event

This commit is contained in:
md_5 2013-04-27 12:25:03 +10:00
parent c08764990d
commit 6bf9df31f5
3 changed files with 44 additions and 22 deletions

View File

@ -15,14 +15,16 @@ import net.md_5.bungee.api.plugin.Plugin;
/**
* Represents an event which depends on the result of asynchronous operations.
*
* @param <T> Type of this event
*/
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class AsyncEvent extends Event
public class AsyncEvent<T> extends Event
{
private final Callback done;
private final Callback<T> done;
private final Set<Plugin> intents = Collections.newSetFromMap( new ConcurrentHashMap<Plugin, Boolean>() );
private final AtomicBoolean fired = new AtomicBoolean();
private final AtomicInteger latch = new AtomicInteger();
@ -34,7 +36,7 @@ public class AsyncEvent extends Event
fired.set( true );
if ( latch.get() == 0 )
{
done.done( this, null );
done.done( (T) this, null );
}
}
@ -66,7 +68,7 @@ public class AsyncEvent extends Event
intents.remove( plugin );
if ( latch.decrementAndGet() == 0 )
{
done.done( this, null );
done.done( (T) this, null );
}
}
}

View File

@ -3,9 +3,9 @@ package net.md_5.bungee.api.event;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.connection.PendingConnection;
import net.md_5.bungee.api.plugin.Cancellable;
import net.md_5.bungee.api.plugin.Event;
/**
* Event called to represent a player logging in.
@ -13,7 +13,7 @@ import net.md_5.bungee.api.plugin.Event;
@Data
@ToString(callSuper = false)
@EqualsAndHashCode(callSuper = false)
public class LoginEvent extends Event implements Cancellable
public class LoginEvent extends AsyncEvent<LoginEvent> implements Cancellable
{
/**
@ -28,4 +28,10 @@ public class LoginEvent extends Event implements Cancellable
* Connection attempting to login.
*/
private final PendingConnection connection;
public LoginEvent(PendingConnection connection, Callback<LoginEvent> done)
{
super( done );
this.connection = connection;
}
}

View File

@ -23,6 +23,7 @@ import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.EncryptionUtil;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.Util;
import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.ServerPing;
@ -185,23 +186,36 @@ public class InitialHandler extends PacketHandler implements PendingConnection
old.disconnect( "You are already connected to the server" );
}
Callback<LoginEvent> complete = new Callback<LoginEvent>()
{
@Override
public void done(LoginEvent result, Throwable error)
{
if ( result.isCancelled() )
{
disconnect( result.getCancelReason() );
}
if ( disconnected )
{
return;
}
try
{
Cipher encrypt = EncryptionUtil.getCipher( Cipher.ENCRYPT_MODE, sharedKey );
Cipher decrypt = EncryptionUtil.getCipher( Cipher.DECRYPT_MODE, sharedKey );
ch.write( new PacketFCEncryptionResponse() );
ch.pipeline().addBefore( "decoder", "cipher", new CipherCodec( encrypt, decrypt ) );
thisState = InitialHandler.State.LOGIN;
} catch ( GeneralSecurityException ex )
{
disconnect( "Cipher error: " + Util.exception( ex ) );
}
}
};
// fire login event
LoginEvent event = new LoginEvent( InitialHandler.this );
if ( bungee.getPluginManager().callEvent( event ).isCancelled() )
{
disconnect( event.getCancelReason() );
}
if ( disconnected )
{
return;
}
Cipher encrypt = EncryptionUtil.getCipher( Cipher.ENCRYPT_MODE, sharedKey );
Cipher decrypt = EncryptionUtil.getCipher( Cipher.DECRYPT_MODE, sharedKey );
ch.write( new PacketFCEncryptionResponse() );
ch.pipeline().addBefore( "decoder", "cipher", new CipherCodec( encrypt, decrypt ) );
thisState = InitialHandler.State.LOGIN;
bungee.getPluginManager().callEvent( new LoginEvent( InitialHandler.this, complete ) );
}
@Override