#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,7 +281,9 @@ 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 )
{
case "ForwardToPlayer":
{ {
ProxiedPlayer target = bungee.getPlayer( in.readUTF() ); ProxiedPlayer target = bungee.getPlayer( in.readUTF() );
if ( target != null ) if ( target != null )
@ -302,8 +305,9 @@ public class DownstreamBridge extends PacketHandler
// Null out stream, important as we don't want to send to ourselves // Null out stream, important as we don't want to send to ourselves
out = null; out = null;
break;
} }
if ( subChannel.equals( "Forward" ) ) case "Forward":
{ {
// Read data from server // Read data from server
String target = in.readUTF(); String target = in.readUTF();
@ -321,8 +325,9 @@ public class DownstreamBridge extends PacketHandler
// Null out stream, important as we don't want to send to ourselves // Null out stream, important as we don't want to send to ourselves
out = null; out = null;
if ( target.equals( "ALL" ) ) switch ( target )
{ {
case "ALL":
for ( ServerInfo server : bungee.getServers().values() ) for ( ServerInfo server : bungee.getServers().values() )
{ {
if ( server != this.server.getInfo() ) if ( server != this.server.getInfo() )
@ -330,8 +335,8 @@ public class DownstreamBridge extends PacketHandler
server.sendData( "BungeeCord", payload ); server.sendData( "BungeeCord", payload );
} }
} }
} else if ( target.equals( "ONLINE" ) ) break;
{ case "ONLINE":
for ( ServerInfo server : bungee.getServers().values() ) for ( ServerInfo server : bungee.getServers().values() )
{ {
if ( server != this.server.getInfo() ) if ( server != this.server.getInfo() )
@ -339,24 +344,27 @@ public class DownstreamBridge extends PacketHandler
server.sendData( "BungeeCord", payload, false ); server.sendData( "BungeeCord", payload, false );
} }
} }
} else break;
{ default:
ServerInfo server = bungee.getServerInfo( target ); ServerInfo server = bungee.getServerInfo( target );
if ( server != null ) if ( server != null )
{ {
server.sendData( "BungeeCord", payload ); server.sendData( "BungeeCord", payload );
} }
break;
} }
break;
} }
if ( subChannel.equals( "Connect" ) ) case "Connect":
{ {
ServerInfo server = bungee.getServerInfo( in.readUTF() ); ServerInfo server = bungee.getServerInfo( in.readUTF() );
if ( server != null ) if ( server != null )
{ {
con.connect( server, ServerConnectEvent.Reason.PLUGIN_MESSAGE ); con.connect( server, ServerConnectEvent.Reason.PLUGIN_MESSAGE );
} }
break;
} }
if ( subChannel.equals( "ConnectOther" ) ) case "ConnectOther":
{ {
ProxiedPlayer player = bungee.getPlayer( in.readUTF() ); ProxiedPlayer player = bungee.getPlayer( in.readUTF() );
if ( player != null ) if ( player != null )
@ -367,9 +375,9 @@ public class DownstreamBridge extends PacketHandler
player.connect( server ); player.connect( server );
} }
} }
break;
} }
if ( subChannel.equals( "IP" ) ) case "IP":
{
out.writeUTF( "IP" ); out.writeUTF( "IP" );
if ( con.getSocketAddress() instanceof InetSocketAddress ) if ( con.getSocketAddress() instanceof InetSocketAddress )
{ {
@ -380,8 +388,8 @@ public class DownstreamBridge extends PacketHandler
out.writeUTF( "unix://" + ( (DomainSocketAddress) con.getSocketAddress() ).path() ); out.writeUTF( "unix://" + ( (DomainSocketAddress) con.getSocketAddress() ).path() );
out.writeInt( 0 ); out.writeInt( 0 );
} }
} break;
if ( subChannel.equals( "IPOther" ) ) case "IPOther":
{ {
ProxiedPlayer player = bungee.getPlayer( in.readUTF() ); ProxiedPlayer player = bungee.getPlayer( in.readUTF() );
if ( player != null ) if ( player != null )
@ -399,8 +407,9 @@ public class DownstreamBridge extends PacketHandler
out.writeInt( 0 ); out.writeInt( 0 );
} }
} }
break;
} }
if ( subChannel.equals( "PlayerCount" ) ) case "PlayerCount":
{ {
String target = in.readUTF(); String target = in.readUTF();
out.writeUTF( "PlayerCount" ); out.writeUTF( "PlayerCount" );
@ -417,8 +426,9 @@ public class DownstreamBridge extends PacketHandler
out.writeInt( server.getPlayers().size() ); out.writeInt( server.getPlayers().size() );
} }
} }
break;
} }
if ( subChannel.equals( "PlayerList" ) ) case "PlayerList":
{ {
String target = in.readUTF(); String target = in.readUTF();
out.writeUTF( "PlayerList" ); out.writeUTF( "PlayerList" );
@ -435,13 +445,15 @@ public class DownstreamBridge extends PacketHandler
out.writeUTF( Util.csv( server.getPlayers() ) ); out.writeUTF( Util.csv( server.getPlayers() ) );
} }
} }
break;
} }
if ( subChannel.equals( "GetServers" ) ) case "GetServers":
{ {
out.writeUTF( "GetServers" ); out.writeUTF( "GetServers" );
out.writeUTF( Util.csv( bungee.getServers().keySet() ) ); out.writeUTF( Util.csv( bungee.getServers().keySet() ) );
break;
} }
if ( subChannel.equals( "Message" ) ) case "Message":
{ {
String target = in.readUTF(); String target = in.readUTF();
String message = in.readUTF(); String message = in.readUTF();
@ -459,8 +471,9 @@ public class DownstreamBridge extends PacketHandler
player.sendMessage( message ); player.sendMessage( message );
} }
} }
break;
} }
if ( subChannel.equals( "MessageRaw" ) ) case "MessageRaw":
{ {
String target = in.readUTF(); String target = in.readUTF();
BaseComponent[] message = ComponentSerializer.parse( in.readUTF() ); BaseComponent[] message = ComponentSerializer.parse( in.readUTF() );
@ -478,18 +491,21 @@ public class DownstreamBridge extends PacketHandler
player.sendMessage( message ); player.sendMessage( message );
} }
} }
break;
} }
if ( subChannel.equals( "GetServer" ) ) case "GetServer":
{ {
out.writeUTF( "GetServer" ); out.writeUTF( "GetServer" );
out.writeUTF( server.getInfo().getName() ); out.writeUTF( server.getInfo().getName() );
break;
} }
if ( subChannel.equals( "UUID" ) ) case "UUID":
{ {
out.writeUTF( "UUID" ); out.writeUTF( "UUID" );
out.writeUTF( con.getUUID() ); out.writeUTF( con.getUUID() );
break;
} }
if ( subChannel.equals( "UUIDOther" ) ) case "UUIDOther":
{ {
ProxiedPlayer player = bungee.getPlayer( in.readUTF() ); ProxiedPlayer player = bungee.getPlayer( in.readUTF() );
if ( player != null ) if ( player != null )
@ -498,8 +514,9 @@ public class DownstreamBridge extends PacketHandler
out.writeUTF( player.getName() ); out.writeUTF( player.getName() );
out.writeUTF( player.getUUID() ); out.writeUTF( player.getUUID() );
} }
break;
} }
if ( subChannel.equals( "ServerIP" ) ) case "ServerIP":
{ {
ServerInfo info = bungee.getServerInfo( in.readUTF() ); ServerInfo info = bungee.getServerInfo( in.readUTF() );
if ( info != null && !info.getAddress().isUnresolved() ) if ( info != null && !info.getAddress().isUnresolved() )
@ -509,8 +526,9 @@ public class DownstreamBridge extends PacketHandler
out.writeUTF( info.getAddress().getAddress().getHostAddress() ); out.writeUTF( info.getAddress().getAddress().getHostAddress() );
out.writeShort( info.getAddress().getPort() ); out.writeShort( info.getAddress().getPort() );
} }
break;
} }
if ( subChannel.equals( "KickPlayer" ) ) case "KickPlayer":
{ {
ProxiedPlayer player = bungee.getPlayer( in.readUTF() ); ProxiedPlayer player = bungee.getPlayer( in.readUTF() );
if ( player != null ) if ( player != null )
@ -518,6 +536,8 @@ public class DownstreamBridge extends PacketHandler
String kickReason = in.readUTF(); String kickReason = in.readUTF();
player.disconnect( new TextComponent( kickReason ) ); player.disconnect( new TextComponent( kickReason ) );
} }
break;
}
} }
// Check we haven't set out to null, and we have written data, if so reply back back along the BungeeCord channel // Check we haven't set out to null, and we have written data, if so reply back back along the BungeeCord channel