From bc48ab3fb8acdfca4be7464739e0bebaca1abfdb Mon Sep 17 00:00:00 2001 From: Steve Anton Date: Fri, 5 Sep 2014 20:23:01 -0700 Subject: [PATCH] Make ProxyPingEvent async --- .../md_5/bungee/api/event/ProxyPingEvent.java | 12 +++-- .../bungee/connection/InitialHandler.java | 49 ++++++++++++++----- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/api/src/main/java/net/md_5/bungee/api/event/ProxyPingEvent.java b/api/src/main/java/net/md_5/bungee/api/event/ProxyPingEvent.java index 84552852..05474d88 100644 --- a/api/src/main/java/net/md_5/bungee/api/event/ProxyPingEvent.java +++ b/api/src/main/java/net/md_5/bungee/api/event/ProxyPingEvent.java @@ -1,9 +1,9 @@ package net.md_5.bungee.api.event; -import lombok.AllArgsConstructor; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.ToString; +import net.md_5.bungee.api.Callback; import net.md_5.bungee.api.ServerPing; import net.md_5.bungee.api.connection.PendingConnection; import net.md_5.bungee.api.plugin.Event; @@ -12,10 +12,9 @@ import net.md_5.bungee.api.plugin.Event; * Called when the proxy is pinged with packet 0xFE from the server list. */ @Data -@AllArgsConstructor @ToString(callSuper = false) @EqualsAndHashCode(callSuper = false) -public class ProxyPingEvent extends Event +public class ProxyPingEvent extends AsyncEvent { /** @@ -26,4 +25,11 @@ public class ProxyPingEvent extends Event * The data to respond with. */ private ServerPing response; + + public ProxyPingEvent(PendingConnection connection, ServerPing response, Callback done) + { + super( done ); + this.connection = connection; + this.response = response; + } } 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 58a7d121..fa9753e1 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 @@ -134,17 +134,32 @@ public class InitialHandler extends PacketHandler implements PendingConnection { ServerPing legacy = new ServerPing( new ServerPing.Protocol( bungee.getName() + " " + bungee.getGameVersion(), bungee.getProtocolVersion() ), new ServerPing.Players( listener.getMaxPlayers(), bungee.getOnlineCount(), null ), listener.getMotd(), (Favicon) null ); - legacy = bungee.getPluginManager().callEvent( new ProxyPingEvent( this, legacy ) ).getResponse(); - String kickMessage = ChatColor.DARK_BLUE - + "\00" + 127 - + "\00" + legacy.getVersion().getName() - + "\00" + legacy.getDescription() - + "\00" + legacy.getPlayers().getOnline() - + "\00" + legacy.getPlayers().getMax(); + Callback callback = new Callback() + { + @Override + public void done(ProxyPingEvent result, Throwable error) + { + if ( ch.isClosed() ) + { + return; + } - ch.getHandle().writeAndFlush( kickMessage ); - ch.close(); + ServerPing ping = result.getResponse(); + + String kickMessage = ChatColor.DARK_BLUE + + "\00" + 127 + + "\00" + ping.getVersion().getName() + + "\00" + ping.getDescription() + + "\00" + ping.getPlayers().getOnline() + + "\00" + ping.getPlayers().getMax(); + + ch.getHandle().writeAndFlush( kickMessage ); + ch.close(); + } + }; + + bungee.getPluginManager().callEvent( new ProxyPingEvent( this, legacy, callback ) ); } @Override @@ -166,11 +181,19 @@ public class InitialHandler extends PacketHandler implements PendingConnection result.setDescription( bungee.getTranslation( "ping_cannot_connect" ) ); bungee.getLogger().log( Level.WARNING, "Error pinging remote server", error ); } - result = bungee.getPluginManager().callEvent( new ProxyPingEvent( InitialHandler.this, result ) ).getResponse(); - BungeeCord.getInstance().getConnectionThrottle().unthrottle( getAddress().getAddress() ); - Gson gson = handshake.getProtocolVersion() == ProtocolConstants.MINECRAFT_1_7_2 ? BungeeCord.getInstance().gsonLegacy : BungeeCord.getInstance().gson; - unsafe.sendPacket( new StatusResponse( gson.toJson( result ) ) ); + Callback callback = new Callback() + { + @Override + public void done(ProxyPingEvent pingResult, Throwable error) + { + BungeeCord.getInstance().getConnectionThrottle().unthrottle( getAddress().getAddress() ); + Gson gson = handshake.getProtocolVersion() == ProtocolConstants.MINECRAFT_1_7_2 ? BungeeCord.getInstance().gsonLegacy : BungeeCord.getInstance().gson; + unsafe.sendPacket( new StatusResponse( gson.toJson( pingResult.getResponse() ) ) ); + } + }; + + bungee.getPluginManager().callEvent( new ProxyPingEvent( InitialHandler.this, result, callback ) ); } };