diff --git a/api/src/main/java/net/md_5/bungee/api/event/AsyncEvent.java b/api/src/main/java/net/md_5/bungee/api/event/AsyncEvent.java index cdbca34a..001064a8 100644 --- a/api/src/main/java/net/md_5/bungee/api/event/AsyncEvent.java +++ b/api/src/main/java/net/md_5/bungee/api/event/AsyncEvent.java @@ -15,14 +15,16 @@ import net.md_5.bungee.api.plugin.Plugin; /** * Represents an event which depends on the result of asynchronous operations. + * + * @param Type of this event */ @Data @ToString(callSuper = true) @EqualsAndHashCode(callSuper = true) -public class AsyncEvent extends Event +public class AsyncEvent extends Event { - private final Callback done; + private final Callback done; private final Set intents = Collections.newSetFromMap( new ConcurrentHashMap() ); 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 ); } } } diff --git a/api/src/main/java/net/md_5/bungee/api/event/LoginEvent.java b/api/src/main/java/net/md_5/bungee/api/event/LoginEvent.java index b4451a6f..d2651f82 100644 --- a/api/src/main/java/net/md_5/bungee/api/event/LoginEvent.java +++ b/api/src/main/java/net/md_5/bungee/api/event/LoginEvent.java @@ -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 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 done) + { + super( done ); + this.connection = connection; + } } 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 2145303c..1e39b033 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 @@ -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 complete = new Callback() + { + @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