Sending a custom plugin message from BungeeCord

Jul 10, 2019
Sending a custom plugin message from BungeeCord
  • Sending a custom plugin message from BungeeCord

    A guide for custom plugin messaging between proxy -> server -> proxy



    This kinda "tutorial" is divided by two parts: BungeeCord (sending) and Spigot (receiving) part.

    BungeeCord part
    Firstly, we need to register our custom channel into onEnable() in our main class. We do this with ProxyServer#registerChannel(String). The channel should be lowercase and should contain ":".

    Example:
    Code (Java):
    public class ExampleMain extends Plugin
    {

        @Override
        public void onEnable()
        {
            getProxy().registerChannel( "my:channel" );
            getLogger().info( "Plugin enabled!" );
        }

        @Override
        public void onDisable()
        {
            getLogger().info( "Plugin disabled!" );
        }
    }
    Now, we can send messages via this channel!

    Sending a custom message is simple. You can create a method into the class where you want the plugin message to be send. Remember, for the plugin messaging channel to work you need atleast 1 player online at the server. Always check if the online players globally are null.

    Example:
    Code (Java):
    public void sendCustomData(ProxiedPlayer player, String data1, int data2)
    {
        Collection<ProxiedPlayer> networkPlayers = ProxyServer.getInstance().getPlayers();
        // perform a check to see if globally are no players
        if ( networkPlayers == null || networkPlayers.isEmpty() )
        {
            return;
        }
        ByteArrayDataOutput out = ByteStreams.newDataOutput();
        out.writeUTF( "MySubChannel" ); // the channel could be whatever you want
        out.writeUTF( data1 ); // this data could be whatever you want
        out.writeInt( data2 ); // this data could be whatever you want
       
        // we send the data to the server
        // using ServerInfo the packet is being queued if there are no players in the server
        // using only the server to send data the packet will be lost if no players are in it
        player.getServer().getInfo().sendData( "my:channel", out.toByteArray() );
    }
    Since I see alot of people having trouble with receiving plugin messages from Spigot to BungeeCord, this is how you should do it.
    In Spigot, its a listener, in BungeeCord it's an event.
    Code (Java):
    public class PluginMessageReceiver implements Listener
    {

        @EventHandler
        public void on(PluginMessageEvent event)
        {
            if ( !event.getTag().equalsIgnoreCase( "my:channel" ) )
            {
                return;
            }
            ByteArrayDataInput in = ByteStreams.newDataInput( event.getData() );
            String subChannel = in.readUTF();
            if ( subChannel.equalsIgnoreCase( "OtherSubchannel" ) )
            {
                // the receiver is a ProxiedPlayer when a server talks to the proxy
                if ( event.getReceiver() instanceof ProxiedPlayer )
                {
                    ProxiedPlayer receiver = (ProxiedPlayer) event.getReceiver();
                    // do things
                }
                // the receiver is a server when the proxy talks to a server
                if ( event.getReceiver() instanceof Server )
                {
                    Server receiver = (Server) event.getReceiver();
                    // do things
                }
            }
        }
    }
    Spigot part
    Receiving the messages from the specified channel is easy. Also, when you create your receiver, make sure to check if the server is BungeeCord (how? its showed in the example).

    Example main:

    Code (Java):
    public class ExampleMainDriver extends JavaPlugin implements PluginMessageListener
    {

        @Override
        public void onEnable()
        {
            checkIfBungee();
            if ( !getServer().getPluginManager().isPluginEnabled( this ) )
            {
                return;
            }
            getServer().getMessenger().registerIncomingPluginChannel( this, "my:channel", this ); // we register the incoming channel
            // you can register outgoing channel if you want to send messages to the proxy
            // getServer().getMessenger().registerOutgoingPluginChannel( this, "my:channel" );
            getLogger().info( "<pluginName> driver enabled successfully." );
        }

        @Override
        public void onDisable()
        {
            getLogger().info( "<pluginName> driver was disabled!" );
        }

        @Override
        public void onPluginMessageReceived(String channel, Player player, byte[] bytes)
        {
            if ( !channel.equalsIgnoreCase( "my:channel" ) )
            {
                return;
            }
            ByteArrayDataInput in = ByteStreams.newDataInput( bytes );
            String subChannel = in.readUTF();
            if ( subChannel.equalsIgnoreCase( "MySubChannel" ) )
            {
                String data1 = in.readUTF();
                int data2 = in.readInt();

                // do things with the data
            }
        }

        // we check like that if the specified server is BungeeCord.
        private void checkIfBungee()
        {
            // we check if the server is Spigot/Paper (because of the spigot.yml file)
            if ( !getServer().getVersion().contains( "Spigot" ) || !getServer().getVersion().contains( "Paper" ) )
            {
                getLogger().severe( "You probably run CraftBukkit... Please update atleast to spigot for this to work..." );
                getLogger().severe( "Plugin disabled!" );
                getServer().getPluginManager().disablePlugin( this );
                return;
            }
            if ( getServer().spigot().getConfig().getBoolean( "settings.bungeecord" ) )
            {
                getLogger().severe( "This server is not BungeeCord." );
                getLogger().severe( "If the server is already hooked to BungeeCord, please enable it into your spigot.yml aswell." );
                getLogger().severe( "Plugin disabled!" );
                getServer().getPluginManager().disablePlugin( this );
            }
        }
    }
    I think you now understood how to send custom plugin messages! Its literally so easy!
  • Loading...
  • Loading...