#3241: Support ping passthrough for legacy pings

This commit is contained in:
Kevin Ludwig 2022-07-02 10:26:28 +10:00 committed by md_5
parent e151a6cf92
commit d221e52929
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11
2 changed files with 49 additions and 34 deletions

View File

@ -28,18 +28,13 @@ public abstract class AbstractReconnectHandler implements ReconnectHandler
public static ServerInfo getForcedHost(PendingConnection con) public static ServerInfo getForcedHost(PendingConnection con)
{ {
if ( con.getVirtualHost() == null ) String forced = ( con.getVirtualHost() == null ) ? null : con.getListener().getForcedHosts().get( con.getVirtualHost().getHostString() );
{
return null;
}
String forced = con.getListener().getForcedHosts().get( con.getVirtualHost().getHostString() );
if ( forced == null && con.getListener().isForceDefault() ) if ( forced == null && con.getListener().isForceDefault() )
{ {
forced = con.getListener().getDefaultServer(); forced = con.getListener().getDefaultServer();
} }
return ProxyServer.getInstance().getServerInfo( forced ); return ( forced == null ) ? null : ProxyServer.getInstance().getServerInfo( forced );
} }
protected abstract ServerInfo getStoredServer(ProxiedPlayer player); protected abstract ServerInfo getStoredServer(ProxiedPlayer player);

View File

@ -25,7 +25,6 @@ import net.md_5.bungee.Util;
import net.md_5.bungee.api.AbstractReconnectHandler; import net.md_5.bungee.api.AbstractReconnectHandler;
import net.md_5.bungee.api.Callback; import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.Favicon;
import net.md_5.bungee.api.ServerPing; import net.md_5.bungee.api.ServerPing;
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;
@ -175,44 +174,65 @@ public class InitialHandler extends PacketHandler implements PendingConnection
this.legacy = true; this.legacy = true;
final boolean v1_5 = ping.isV1_5(); final boolean v1_5 = ping.isV1_5();
ServerPing legacy = new ServerPing( new ServerPing.Protocol( bungee.getName() + " " + bungee.getGameVersion(), bungee.getProtocolVersion() ), ServerInfo forced = AbstractReconnectHandler.getForcedHost( this );
new ServerPing.Players( listener.getMaxPlayers(), bungee.getOnlineCount(), null ), final String motd = ( forced != null ) ? forced.getMotd() : listener.getMotd();
new TextComponent( TextComponent.fromLegacyText( listener.getMotd() ) ), (Favicon) null ); final int protocol = bungee.getProtocolVersion();
Callback<ProxyPingEvent> callback = new Callback<ProxyPingEvent>() Callback<ServerPing> pingBack = new Callback<ServerPing>()
{ {
@Override @Override
public void done(ProxyPingEvent result, Throwable error) public void done(ServerPing result, Throwable error)
{ {
if ( ch.isClosed() ) if ( error != null )
{ {
return; result = getPingInfo( bungee.getTranslation( "ping_cannot_connect" ), protocol );
bungee.getLogger().log( Level.WARNING, "Error pinging remote server", error );
} }
ServerPing legacy = result.getResponse(); Callback<ProxyPingEvent> callback = new Callback<ProxyPingEvent>()
String kickMessage;
if ( v1_5 )
{ {
kickMessage = ChatColor.DARK_BLUE @Override
+ "\00" + 127 public void done(ProxyPingEvent result, Throwable error)
+ '\00' + legacy.getVersion().getName() {
+ '\00' + getFirstLine( legacy.getDescription() ) if ( ch.isClosed() )
+ '\00' + legacy.getPlayers().getOnline() {
+ '\00' + legacy.getPlayers().getMax(); return;
} else }
{
// Clients <= 1.3 don't support colored motds because the color char is used as delimiter
kickMessage = ChatColor.stripColor( getFirstLine( legacy.getDescription() ) )
+ '\u00a7' + legacy.getPlayers().getOnline()
+ '\u00a7' + legacy.getPlayers().getMax();
}
ch.close( kickMessage ); ServerPing legacy = result.getResponse();
String kickMessage;
if ( v1_5 )
{
kickMessage = ChatColor.DARK_BLUE
+ "\00" + 127
+ '\00' + legacy.getVersion().getName()
+ '\00' + getFirstLine( legacy.getDescription() )
+ '\00' + legacy.getPlayers().getOnline()
+ '\00' + legacy.getPlayers().getMax();
} else
{
// Clients <= 1.3 don't support colored motds because the color char is used as delimiter
kickMessage = ChatColor.stripColor( getFirstLine( legacy.getDescription() ) )
+ '\u00a7' + legacy.getPlayers().getOnline()
+ '\u00a7' + legacy.getPlayers().getMax();
}
ch.close( kickMessage );
}
};
bungee.getPluginManager().callEvent( new ProxyPingEvent( InitialHandler.this, result, callback ) );
} }
}; };
bungee.getPluginManager().callEvent( new ProxyPingEvent( this, legacy, callback ) ); if ( forced != null && listener.isPingPassthrough() )
{
( (BungeeServerInfo) forced ).ping( pingBack, bungee.getProtocolVersion() );
} else
{
pingBack.done( getPingInfo( motd, protocol ), null );
}
} }
private static String getFirstLine(String str) private static String getFirstLine(String str)