Send message to BungeeCord

Discussion in 'BungeeCord Plugin Development' started by comniemeer, Jul 22, 2013.

  1. Hey,
    I'd like to send a short message from my Bukkit plugin to the BungeeCord Proxy, to inform it about a server status. For example on server shutdown, I'd like the Proxy to know, that the server is going down now. Ho can I do this?
    Regards, comniemeer
     
  2. Check the wiki, there is info on this topic there
     
  3. That means, that I can't send a message from a Bukkit Plugin to a Bungee Plugin?
     
  4. Scrayos

    Supporter

    U can. But not with the api-methods. You could open a socket for cross-serverinteractions.
     
    • Disagree Disagree x 1
  5. OH LORD NO.

    (EDIT : Just wanted to add that the reason why I said "Stay Tuned" was because I was planning on making a tut, but never got around it. I think I'll take what I just posted here and put it there... since it's pretty good xD)

    TL;DR : IT IS ENTIRELY POSSIBLE FOR BUKKIT AND BUNGEE TO COMMUNICATE WITHOUT AN EXTERNAL COMMUNICATION CHANNEL. THOSE WHO CLAIM IT'S NOT POSSIBLE, READ THE API.

    Yes you can. I don't know what's with all these other people talking without knowing. OK, to put it simply, in BungeeCord, there is an event to catch the Plugin Messages that aren't handled internally by BungeeCord. So here's how it works on the BungeeCord side to receive and send :

    Code (Java):
    public class YourPlugin extends Plugin implements Listener {
        public void onEnable() {
            this.getProxy().registerChannel("YourPluginChannel");
            this.getProxy().getPluginManager().registerListener(this, this);
        }
     
        public void onDisable() {
       
        }
     
        public void sendMessage(String message, ServerInfo server) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(stream);
            out.writeUTF("Hello!");
     
            server.sendData("YourPluginChannel", stream.toByteArray());
            // Alright quick note here guys : ProxiedPlayer.sendData() [b]WILL NOT WORK[/b]. It will send it to the client, and not to the server the client is connected. See the difference ? You need to send to Server or ServerInfo.
        }
     
        @EventHandler
        public void onPluginMessage(PluginMessageEvent ev) {
            if (!ev.getTag().equals("YourPluginChannel") {
                return;
            }
       
            if (!(ev.getSender() instanceof Server)) {
                return;
            }
     
            ByteArrayInputStream stream = new ByteArrayInputStream(ev.getData());
            DataInputStream in = new DataInputStream(stream);
            this.getLogger().info(in.readUTF());
        }
    }
    Now let's do the same thing from Bukkit. Slightly more complicated, but still okay.

    Code (Java):
    public class ThothPlugin extends JavaPlugin implements PluginMessageListener {
        public void onEnable() {
            this.getServer().getMessenger().registerOutgoingPluginChannel(this, "YourPluginChannel");
            this.getServer().getMessenger().registerIncomingPluginChannel(this, "YourPluginChannel", this);
        }
     
        public void onDisable() {
       
        }
     
        public void sendMessage(String message) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(stream);
            out.writeUTF("Hello!");
     
            this.getServer().getOnlinePlayers()[0].sendPluginMessage(this, "YourPluginChannel", stream.toByteArray());
        }
     
        public void onPluginMessageReceived(String channel, Player player, byte[] bytes) {
            if (!channel.equals("YourPluginChannel")) {
                return;
            }
       
            ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
            DataInputStream in = new DataInputStream(stream);
            this.getLogger().info(in.readUTF());
        }
    }
    There you go. Now call sendMessage("SSome stuff !") from bukkit and bungeecord console will show "SSome stuff!". Same if you do it the other way around.

    Alright now, here are the shortcommings of this method, because it's more of a hack than anything :
    From bungee, you cannot instantaneously send a message to a server who has no player. The message will go in a "buffer", and wait until a player connects to that server to send it. That can be good, or bad. If you don't want the message to be buffered for a reason or another (like global chat systems), check if the Server has any player before sending the data (Server.getPlayers().getSize() should tell you the size it I think).
    From Bukkit, you simply CANNOT send the message to bungee if nobody is connected, for an obvious reason : getOnlinePlayers()[0] simply doesn't exist. Hurr durr.

    I am currently working on a workaround all this with a plugin named Thoth (For those who remember CommuLib, it's the same plugin, just a different name because CommuLib is ugly. Don't ask.) Development on it is steady, I hope to do a release by September. In the meantime, you either go with what I show above, or do what Scrayos said, but DAYUM that's just ugly if every plugin starts doing this.
     
    #6 roblabla, Aug 1, 2013
    Last edited: Aug 1, 2013
    • Winner Winner x 12
    • Useful Useful x 7
    • Informative Informative x 2
    • Like Like x 1
  6. Scrayos

    Supporter

    Thank you rob for the correction of our foolish Replies. I already used the Plugin-Message-Channels in that way, but didn't thought about the question in the needed depth.
     
    • Agree Agree x 1
    • Winner Winner x 1