BungeeCord doesn't receive PluginMessage

Discussion in 'BungeeCord Plugin Development' started by ostylk, Mar 20, 2020.

  1. Hello,
    being new to the BungeeCord API I've ran into a problem regarding communication through custom plugin messaging channels.

    I'm able to successfully send a message from Bungee to Spigot however the reverse doesn't quite work. At the moment the communication works like this:
    1. Players connect to the network
    2. The BungeeCord listens for a ServerConnectedEvent and sends a message to Spigot through the custom plugin channel
    3. The Spigot server successfully receives the message, processes it, and tries to answer back to Bungee
    4. The answer however never reached the Bungee

    I've posted both main classes to show my channel registration and the RequestListener class which is on Spigot listening for the message from BungeeCord. The code is executed as the print statement is shown in console. However in BungeeCord no PluginMessageEvent is fired with the correct channel.
    Only the following PluginMessageEvents are received by BungeeCord:

    Code (Text):
    [22:08:28 INFO]: minecraft:register
    [22:08:28 INFO]: minecraft:brand
    [22:08:29 INFO]: minecraft:brand

    BungeeCord Main class:
    Code (Java):
        @Override
        public void onEnable() {
            this.getProxy().registerChannel(RegistrationChannel.TAG);
            this.getProxy().getPluginManager().registerListener(this, new SelectListener(this));
        }
    Spigot Main class:
    Code (Java):
        @Override
        public void onEnable() {
            this.getServer().getMessenger().registerOutgoingPluginChannel(this, RegistrationChannel.TAG);
            this.getServer().getMessenger().registerIncomingPluginChannel(this, RegistrationChannel.TAG, new RequestListener(this));
        }
    RequestListener class (on Spigot):
    Code (Java):
    public class RequestListener implements PluginMessageListener {
     
        private ChatSpigot spigot;
     
        public RequestListener(ChatSpigot spigot) {
            this.spigot = spigot;
        }
     
        @Override
        public void onPluginMessageReceived(String channel, Player player, byte[] data) {
            if (!channel.equals(RegistrationChannel.TAG)) {
                return;
            }
     
            /* omitted some data processing for clarity */
     
            val outChannel = ByteStreams.newDataOutput();
            val selectorPacket = new RegistrationChannel.SelectPacket(groups);
            /* this method just fills the ByteStream with some data, this is however insignificant as the problem is a different one */
            selectorPacket.send(outChannel);
     
            System.out.println("y1");
            player.sendPluginMessage(this.spigot, RegistrationChannel.TAG, outChannel.toByteArray());
        }
    }
    SelectListener class (on BungeeCord):
    Code (Java):
        @EventHandler
        public void onPluginMessage(PluginMessageEvent e) {
            System.out.println(e.getTag()); // <-- My channel never shows up
            if (!e.getTag().equals(RegistrationChannel.TAG)) {
                return;
            }
     
            System.out.println("y2"); // <-- Never gets called
            if (!(e.getSender() instanceof Server)) {
                return;
            }
            System.out.println("y3");
       
            /* data processing omitted for clarity */
        }
    RegistrationChannel.TAG is defined as "roninchat:register"

    I also tried adding a delay as the wiki says that sending a plugin message immediately after joining is not possible, however that didn't change anything.

    Any help is appreciated, thanks.
     
  2. Where do you send the triggering message?
     
  3. Code (Java):
        @EventHandler
        public void onServerConnected(ServerConnectedEvent e) {
            val outChannel = ByteStreams.newDataOutput();
         
            val requestPacket = new RegistrationChannel.RequestPacket();
            requestPacket.send(outChannel);
         
            e.getServer().sendData(RegistrationChannel.TAG, outChannel.toByteArray());
        }
    Here you go, but the problem probably doesn't originate from here as the message sent from BungeeCord to Spigot is received correctly.

    The weird thing is that this line here in the RequestListener class on Spigot doesn't cause a PluginMessage to be received on BungeeCord:
    Code (Java):
    player.sendPluginMessage(this.spigot, RegistrationChannel.TAG, outChannel.toByteArray());
     
  4. Yeah, so you're sending it to the server, but not thru a player. When you send back you should get not this player, but other player. Or include the player in the triggering message to send back.
     
  5. Correct me if I'm wrong, but BungeeCord has no way to send data to the server, it always has to use a player connection. So it shouldn't matter whether I "send it to the server" or "send it through a player" because it all uses the same connections. So eventhough in BungeeCord I send it to the server, on Spigot the message is received through a player because communication is otherwise not possible.

    If you looked into the implementation of Server#sendData method, you'd see that it just selects any player connection to send the data. As I'm testing, I'm the only player online on the network I can't send the message through a different player. That can't be the problem.

    Maybe you can further elaborate your point? Your help is appreciated.
     
    #5 ostylk, Mar 21, 2020
    Last edited: Mar 21, 2020
  6. Does anyone have any idea?

    I've created a plugin with minimal code to reproduce my problem (Sourcecode and jar attached).
     

    Attached Files: