#2466: Use switch in "BungeeCord" plugin message handling

This commit is contained in:
Janmm14 2021-10-09 09:25:29 +11:00 committed by md_5
parent 5cf869df1a
commit d8e293842f
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11
2 changed files with 214 additions and 191 deletions

View File

@ -33,6 +33,7 @@
<!-- See http://checkstyle.sourceforge.net/config_filters.html --> <!-- See http://checkstyle.sourceforge.net/config_filters.html -->
<module name="SuppressionCommentFilter"/> <module name="SuppressionCommentFilter"/>
<module name="SuppressWarningsHolder"/>
<!-- See http://checkstyle.sourceforge.net/config_imports.html --> <!-- See http://checkstyle.sourceforge.net/config_imports.html -->
<module name="AvoidStarImport"/> <module name="AvoidStarImport"/>
@ -84,4 +85,6 @@
<module name="Indentation"/> <module name="Indentation"/>
<module name="UpperEll"/> <module name="UpperEll"/>
</module> </module>
<module name="SuppressWarningsFilter"/>
</module> </module>

View File

@ -248,6 +248,7 @@ public class DownstreamBridge extends PacketHandler
} }
@Override @Override
@SuppressWarnings("checkstyle:avoidnestedblocks")
public void handle(PluginMessage pluginMessage) throws Exception public void handle(PluginMessage pluginMessage) throws Exception
{ {
DataInput in = pluginMessage.getStream(); DataInput in = pluginMessage.getStream();
@ -280,12 +281,36 @@ public class DownstreamBridge extends PacketHandler
ByteArrayDataOutput out = ByteStreams.newDataOutput(); ByteArrayDataOutput out = ByteStreams.newDataOutput();
String subChannel = in.readUTF(); String subChannel = in.readUTF();
if ( subChannel.equals( "ForwardToPlayer" ) ) switch ( subChannel )
{ {
ProxiedPlayer target = bungee.getPlayer( in.readUTF() ); case "ForwardToPlayer":
if ( target != null ) {
ProxiedPlayer target = bungee.getPlayer( in.readUTF() );
if ( target != null )
{
// Read data from server
String channel = in.readUTF();
short len = in.readShort();
byte[] data = new byte[ len ];
in.readFully( data );
// Prepare new data to send
out.writeUTF( channel );
out.writeShort( data.length );
out.write( data );
byte[] payload = out.toByteArray();
target.getServer().sendData( "BungeeCord", payload );
}
// Null out stream, important as we don't want to send to ourselves
out = null;
break;
}
case "Forward":
{ {
// Read data from server // Read data from server
String target = in.readUTF();
String channel = in.readUTF(); String channel = in.readUTF();
short len = in.readShort(); short len = in.readShort();
byte[] data = new byte[ len ]; byte[] data = new byte[ len ];
@ -297,226 +322,221 @@ public class DownstreamBridge extends PacketHandler
out.write( data ); out.write( data );
byte[] payload = out.toByteArray(); byte[] payload = out.toByteArray();
target.getServer().sendData( "BungeeCord", payload ); // Null out stream, important as we don't want to send to ourselves
} out = null;
// Null out stream, important as we don't want to send to ourselves switch ( target )
out = null;
}
if ( subChannel.equals( "Forward" ) )
{
// Read data from server
String target = in.readUTF();
String channel = in.readUTF();
short len = in.readShort();
byte[] data = new byte[ len ];
in.readFully( data );
// Prepare new data to send
out.writeUTF( channel );
out.writeShort( data.length );
out.write( data );
byte[] payload = out.toByteArray();
// Null out stream, important as we don't want to send to ourselves
out = null;
if ( target.equals( "ALL" ) )
{
for ( ServerInfo server : bungee.getServers().values() )
{ {
if ( server != this.server.getInfo() ) case "ALL":
{ for ( ServerInfo server : bungee.getServers().values() )
server.sendData( "BungeeCord", payload ); {
} if ( server != this.server.getInfo() )
} {
} else if ( target.equals( "ONLINE" ) ) server.sendData( "BungeeCord", payload );
{ }
for ( ServerInfo server : bungee.getServers().values() ) }
{ break;
if ( server != this.server.getInfo() ) case "ONLINE":
{ for ( ServerInfo server : bungee.getServers().values() )
server.sendData( "BungeeCord", payload, false ); {
} if ( server != this.server.getInfo() )
} {
} else server.sendData( "BungeeCord", payload, false );
{ }
ServerInfo server = bungee.getServerInfo( target ); }
if ( server != null ) break;
{ default:
server.sendData( "BungeeCord", payload ); ServerInfo server = bungee.getServerInfo( target );
if ( server != null )
{
server.sendData( "BungeeCord", payload );
}
break;
} }
break;
} }
} case "Connect":
if ( subChannel.equals( "Connect" ) )
{
ServerInfo server = bungee.getServerInfo( in.readUTF() );
if ( server != null )
{
con.connect( server, ServerConnectEvent.Reason.PLUGIN_MESSAGE );
}
}
if ( subChannel.equals( "ConnectOther" ) )
{
ProxiedPlayer player = bungee.getPlayer( in.readUTF() );
if ( player != null )
{ {
ServerInfo server = bungee.getServerInfo( in.readUTF() ); ServerInfo server = bungee.getServerInfo( in.readUTF() );
if ( server != null ) if ( server != null )
{ {
player.connect( server ); con.connect( server, ServerConnectEvent.Reason.PLUGIN_MESSAGE );
} }
break;
} }
} case "ConnectOther":
if ( subChannel.equals( "IP" ) )
{
out.writeUTF( "IP" );
if ( con.getSocketAddress() instanceof InetSocketAddress )
{ {
out.writeUTF( con.getAddress().getHostString() ); ProxiedPlayer player = bungee.getPlayer( in.readUTF() );
out.writeInt( con.getAddress().getPort() ); if ( player != null )
} else
{
out.writeUTF( "unix://" + ( (DomainSocketAddress) con.getSocketAddress() ).path() );
out.writeInt( 0 );
}
}
if ( subChannel.equals( "IPOther" ) )
{
ProxiedPlayer player = bungee.getPlayer( in.readUTF() );
if ( player != null )
{
out.writeUTF( "IPOther" );
out.writeUTF( player.getName() );
if ( player.getSocketAddress() instanceof InetSocketAddress )
{ {
InetSocketAddress address = (InetSocketAddress) player.getSocketAddress(); ServerInfo server = bungee.getServerInfo( in.readUTF() );
out.writeUTF( address.getHostString() ); if ( server != null )
out.writeInt( address.getPort() ); {
player.connect( server );
}
}
break;
}
case "IP":
out.writeUTF( "IP" );
if ( con.getSocketAddress() instanceof InetSocketAddress )
{
out.writeUTF( con.getAddress().getHostString() );
out.writeInt( con.getAddress().getPort() );
} else } else
{ {
out.writeUTF( "unix://" + ( (DomainSocketAddress) player.getSocketAddress() ).path() ); out.writeUTF( "unix://" + ( (DomainSocketAddress) con.getSocketAddress() ).path() );
out.writeInt( 0 ); out.writeInt( 0 );
} }
} break;
} case "IPOther":
if ( subChannel.equals( "PlayerCount" ) )
{
String target = in.readUTF();
out.writeUTF( "PlayerCount" );
if ( target.equals( "ALL" ) )
{ {
out.writeUTF( "ALL" ); ProxiedPlayer player = bungee.getPlayer( in.readUTF() );
out.writeInt( bungee.getOnlineCount() );
} else
{
ServerInfo server = bungee.getServerInfo( target );
if ( server != null )
{
out.writeUTF( server.getName() );
out.writeInt( server.getPlayers().size() );
}
}
}
if ( subChannel.equals( "PlayerList" ) )
{
String target = in.readUTF();
out.writeUTF( "PlayerList" );
if ( target.equals( "ALL" ) )
{
out.writeUTF( "ALL" );
out.writeUTF( Util.csv( bungee.getPlayers() ) );
} else
{
ServerInfo server = bungee.getServerInfo( target );
if ( server != null )
{
out.writeUTF( server.getName() );
out.writeUTF( Util.csv( server.getPlayers() ) );
}
}
}
if ( subChannel.equals( "GetServers" ) )
{
out.writeUTF( "GetServers" );
out.writeUTF( Util.csv( bungee.getServers().keySet() ) );
}
if ( subChannel.equals( "Message" ) )
{
String target = in.readUTF();
String message = in.readUTF();
if ( target.equals( "ALL" ) )
{
for ( ProxiedPlayer player : bungee.getPlayers() )
{
player.sendMessage( message );
}
} else
{
ProxiedPlayer player = bungee.getPlayer( target );
if ( player != null ) if ( player != null )
{ {
player.sendMessage( message ); out.writeUTF( "IPOther" );
out.writeUTF( player.getName() );
if ( player.getSocketAddress() instanceof InetSocketAddress )
{
InetSocketAddress address = (InetSocketAddress) player.getSocketAddress();
out.writeUTF( address.getHostString() );
out.writeInt( address.getPort() );
} else
{
out.writeUTF( "unix://" + ( (DomainSocketAddress) player.getSocketAddress() ).path() );
out.writeInt( 0 );
}
} }
break;
} }
} case "PlayerCount":
if ( subChannel.equals( "MessageRaw" ) )
{
String target = in.readUTF();
BaseComponent[] message = ComponentSerializer.parse( in.readUTF() );
if ( target.equals( "ALL" ) )
{ {
for ( ProxiedPlayer player : bungee.getPlayers() ) String target = in.readUTF();
out.writeUTF( "PlayerCount" );
if ( target.equals( "ALL" ) )
{ {
player.sendMessage( message ); out.writeUTF( "ALL" );
out.writeInt( bungee.getOnlineCount() );
} else
{
ServerInfo server = bungee.getServerInfo( target );
if ( server != null )
{
out.writeUTF( server.getName() );
out.writeInt( server.getPlayers().size() );
}
} }
} else break;
}
case "PlayerList":
{ {
ProxiedPlayer player = bungee.getPlayer( target ); String target = in.readUTF();
out.writeUTF( "PlayerList" );
if ( target.equals( "ALL" ) )
{
out.writeUTF( "ALL" );
out.writeUTF( Util.csv( bungee.getPlayers() ) );
} else
{
ServerInfo server = bungee.getServerInfo( target );
if ( server != null )
{
out.writeUTF( server.getName() );
out.writeUTF( Util.csv( server.getPlayers() ) );
}
}
break;
}
case "GetServers":
{
out.writeUTF( "GetServers" );
out.writeUTF( Util.csv( bungee.getServers().keySet() ) );
break;
}
case "Message":
{
String target = in.readUTF();
String message = in.readUTF();
if ( target.equals( "ALL" ) )
{
for ( ProxiedPlayer player : bungee.getPlayers() )
{
player.sendMessage( message );
}
} else
{
ProxiedPlayer player = bungee.getPlayer( target );
if ( player != null )
{
player.sendMessage( message );
}
}
break;
}
case "MessageRaw":
{
String target = in.readUTF();
BaseComponent[] message = ComponentSerializer.parse( in.readUTF() );
if ( target.equals( "ALL" ) )
{
for ( ProxiedPlayer player : bungee.getPlayers() )
{
player.sendMessage( message );
}
} else
{
ProxiedPlayer player = bungee.getPlayer( target );
if ( player != null )
{
player.sendMessage( message );
}
}
break;
}
case "GetServer":
{
out.writeUTF( "GetServer" );
out.writeUTF( server.getInfo().getName() );
break;
}
case "UUID":
{
out.writeUTF( "UUID" );
out.writeUTF( con.getUUID() );
break;
}
case "UUIDOther":
{
ProxiedPlayer player = bungee.getPlayer( in.readUTF() );
if ( player != null ) if ( player != null )
{ {
player.sendMessage( message ); out.writeUTF( "UUIDOther" );
out.writeUTF( player.getName() );
out.writeUTF( player.getUUID() );
} }
break;
} }
} case "ServerIP":
if ( subChannel.equals( "GetServer" ) )
{
out.writeUTF( "GetServer" );
out.writeUTF( server.getInfo().getName() );
}
if ( subChannel.equals( "UUID" ) )
{
out.writeUTF( "UUID" );
out.writeUTF( con.getUUID() );
}
if ( subChannel.equals( "UUIDOther" ) )
{
ProxiedPlayer player = bungee.getPlayer( in.readUTF() );
if ( player != null )
{ {
out.writeUTF( "UUIDOther" ); ServerInfo info = bungee.getServerInfo( in.readUTF() );
out.writeUTF( player.getName() ); if ( info != null && !info.getAddress().isUnresolved() )
out.writeUTF( player.getUUID() ); {
out.writeUTF( "ServerIP" );
out.writeUTF( info.getName() );
out.writeUTF( info.getAddress().getAddress().getHostAddress() );
out.writeShort( info.getAddress().getPort() );
}
break;
} }
} case "KickPlayer":
if ( subChannel.equals( "ServerIP" ) )
{
ServerInfo info = bungee.getServerInfo( in.readUTF() );
if ( info != null && !info.getAddress().isUnresolved() )
{ {
out.writeUTF( "ServerIP" ); ProxiedPlayer player = bungee.getPlayer( in.readUTF() );
out.writeUTF( info.getName() ); if ( player != null )
out.writeUTF( info.getAddress().getAddress().getHostAddress() ); {
out.writeShort( info.getAddress().getPort() ); String kickReason = in.readUTF();
} player.disconnect( new TextComponent( kickReason ) );
} }
if ( subChannel.equals( "KickPlayer" ) ) break;
{
ProxiedPlayer player = bungee.getPlayer( in.readUTF() );
if ( player != null )
{
String kickReason = in.readUTF();
player.disconnect( new TextComponent( kickReason ) );
} }
} }