Solved PluginMessageListener not recieving messages

Discussion in 'Spigot Plugin Development' started by SmokingIsBadMkay, May 11, 2018.

Thread Status:
Not open for further replies.
  1. Hi all,
    I'm currently working on a project of mine and I need to be able to sync chat over multiple servers. I guessed the easiest way would be to use the Bukkit.getServer().getMessenger() API as I do not need any databases or other software for this to work. Now I've set everything up and as far as I know everything should be correct, however the PluginMessageListener method isn't being called after sending a message from another server.

    Code used to send message:
    Code (Text):
            Player source = Iterables.getFirst(Bukkit.getOnlinePlayers(), null);
         
            if(source == null) return;
         
            if(packet instanceof SkyBlockMessengerPacket) {
                ByteArrayDataOutput output = ByteStreams.newDataOutput();
             
                output.writeUTF("skyblock_" + packet.getChannel());
                ((SkyBlockMessengerPacket) packet).onSend(output);
                source.sendPluginMessage(core, "skyblock_" + packet.getChannel(), output.toByteArray());
                CoreLogger.log("Sent packet to channel: " + "skyblock_" + packet.getChannel());
    Message listener:
    Code (Text):
            CoreLogger.log("register test");
         
            if(format_instance instanceof SkyBlockMessengerPacket) {
                CoreLogger.log("Registered packet for channel: " + "skyblock_" + format_instance.getChannel());
                core.getServer().getMessenger().registerOutgoingPluginChannel(core, "skyblock_" + format_instance.getChannel());
                core.getServer().getMessenger().registerIncomingPluginChannel(core, "skyblock_" + format_instance.getChannel(), new PluginMessageListener() {
                    @Override
                    public void onPluginMessageReceived(String channel, Player source, byte[] bytes) {
                        CoreLogger.log("Recieved packet for channel: " + "skyblock_" + format_instance.getChannel());
                     
                        ByteArrayDataInput input = ByteStreams.newDataInput(bytes);
                        List<SkyBlockPacketHandler> handlers = listeners.get(channel);
                     
                        /* Ignore, no listeners */
                        if(handlers == null) return;
                     
                        CoreLogger.log("packet test 2");
                     
                        for(SkyBlockPacketHandler i : handlers) {
                            CoreLogger.log("packet test 3");
                         
                            SkyBlockMessengerPacket packet = (SkyBlockMessengerPacket) getInstance(format);
                         
                            /* Packet instance is somehow null */
                            if(packet == null) return;
                         
                            CoreLogger.log("packet test 4");
                         
                            /* Fill ByteArrayDataInput */
                            packet.onRecieve(input);
                         
                            /* Call onRecieve for registered listeners and remove on true */
                            if(i.handlePacket(packet)) {
                                listeners.remove(i);
                            }
                        }
                    }
                });
    Code (Text):
    CoreLogger.log("Registered packet for channel: " + "skyblock_" + format_instance.getChannel());
    This message is shown in the console and I've also made sure that the send code is actually being executed. All debug messages are printed except for the ones inside the onPluginMessageReceived function.

    Could anyone please tell me what I'm doing wrong or if this is even possible? Thanks in advance! :)

    EDIT: Before someone asks me, yes my two servers had at least one player on them.
     
    #1 SmokingIsBadMkay, May 11, 2018
    Last edited: May 11, 2018
  2. Anyone? I've read the JavaDocs again and tried to find any problems but as far as I know this code should work :p
     
  3. Are you using bungeecord?
     
  4. Of course. My two (test) spigot servers are linked together using BungeeCord. :)
     
  5. I don't believe you need to be writing the subchannel here, just a little nitpick that might save you some time.

    That said, you haven't showed very much code, are you sure you have registered your plugin channel?
     
  6. Yeah I thought so, I added the code that writes the subchannel in an attempt to solve my issue's haha. I am registering my channels using the following code:
    Code (Text):
    core.getServer().getMessenger().registerOutgoingPluginChannel(core, "skyblock_" + format_instance.getChannel());
    core.getServer().getMessenger().registerIncomingPluginChannel(core, "skyblock_" + format_instance.getChannel(), new PluginMessageListener() {
    Is it important that I register my channels in the onEnable method or can I register them at any time?
     
  7. They should work any time, as long as it's before you send the message and before you receive it
     
    • Informative Informative x 1
  8. I actually looked at your code before writing this and used the same technique, so I've got no clue why this wouldn't work. If I can't get this to work I might use your api, not sure if it has all the features I need.

    Edit: You know what. Your library looks pretty dope, Ill give it a try. Don't want to waiste more time on this because there is a lot of players waiting for me to finish this game haha. Thanks!
     
    #9 SmokingIsBadMkay, May 13, 2018
    Last edited: May 13, 2018
    • Friendly Friendly x 1
  9. Hmm well I register them when my PacketManager class is instantiated and send the message when a player sends a chat message so I guess this should work :unsure:
     
  10. Going to try @ExpDev 's library, will add the "solved" tag if I manage to make this work :)
     
    • Friendly Friendly x 1
    • Funny Funny x 1
  11. I mean there's no practical reason to not register in your onEnable. If it doesn't work as a result of that, I'd be interested to know why it's necessary to not do it there, or at least why you wouldn't use a solution that works already.
     
  12. Okay so I've been testing for a while and Commy also doesn't work. The incoming listeners aren't being triggered. I wrote this test code and again the MessageListener method isn't being called.

    OnEnable:
    Code (Text):
            getServer().getMessenger().registerIncomingPluginChannel(this, "test", new PluginMessageListener() {
                @Override
                public void onPluginMessageReceived(String channel, Player player, byte[] data) {
                    CoreLogger.log("WOW");
                }
            });
            getServer().getMessenger().registerOutgoingPluginChannel(this, "test");
    On test command use:
    Code (Text):
                ByteArrayDataOutput output = ByteStreams.newDataOutput();
                output.writeUTF("test_data");
                player.sendPluginMessage(core, "test", output.toByteArray());
                player.sendMessage("sent!");
    Any idea why it isn't working? Maybe there is something wrong with my test (bungee) setup?
     
  13. Did you register the outgoing channel as well?
     
  14. Yes look at the last line of my first code example, that should do the trick right?

    EDIT: Do I need to register the "BungeeCord" channel in order for other channels to work maybe?
     
  15. I mean in your second code example.
     
  16. I only need to register an outgoing channel once right? I register them in my onEnable and then send messages from my PacketManager/test command class.
     
  17. Well depending on the definition of "once" for you, then no, you need to register "twice."

    This should be what you're doing:

    BC instance
    \_ Server A
    \_ Server B

    Server A
    \_ registerIncoming
    \_ onPluginMessageReceived

    Server B
    \_ registerOutgoing
    \_ sendPluginMessage

    The idea is each server registers the channel for whatever action they'll be doing
     
  18. Yeah well that pretty much what my setup looks like haha. The only difference is that I register both a listener and an outgoing channel at both clients since they will be messaging each other. Pretty confused, it almost looks like there is something wrong with my BungeeCord setup. I don't need to do anything with Bungee plugins right? Just connect both spigot servers to the bungee?
     
Thread Status:
Not open for further replies.

Share This Page