Getting the server name from Bungee - Channels

Hey everyone,

I am currently trying to get the server name from BungeeCord. I came across this Send a Message to Bungeecord x2 - #4 by simon816 .

My current codes looks like this

        channelRegistrar = this.getGame().getChannelRegistrar();

        ChannelBinding.RawDataChannel channel = game.getChannelRegistrar().createRawChannel(this, "BungeeCord");

        channel.sendToServer(buf -> buf.writeString("PreInitialization").writeString("ServerName"));

        channel.addListener(Platform.Type.SERVER, (buf, con, side) -> {
            String subChannel = buf.readString();

            if (subChannel.equals("ServerName")) {
                serverName = buf.readString();
                logger.info("Received the server name: " + serverName);
            }
        });

Because I want to save the server name on start up I tried to use sendToServer instead of sendTo.

Thanks in advance,
Cludch

Could you perhaps explain your question / what is or isn’t working?

If this is executed on the server then sendToServer won’t work because you’re already in the server perspective. The network API is designed so that you communicate on either a one or two way channel, player <--> server

http://pastebin.com/q6MXMEVH

I want the server to communicate with the bungee

You can’t do a server --> server(itself) message, that’s why it’s crashing. You have to send to a player and BungeeCord will intercept it (I believe).
The documentation here: https://www.spigotmc.org/threads/the-bungee-bukkit-plugin-messaging-channel.499/ also suggests that you have to have a player instance.

Do you have another idea how I could get the server name from bungee?

channel.sendToServer will only work if you install this on the client side - that is, you’re getting the error you are getting because you’re trying to run this on the server.

@simon816 is right - Bungee uses players to communicate with servers. You can’t communicate with Bungee without a player joining the server first. As a result, you can’t communicate with Bungee using the normal plugin lifecycle events, you have to wait for the player to have at least joined the server completely.

The server technically has no knowledge of Bungee existing unless a player connects and you send a message asking the player if it’s connected to Bungee.

Bear in mind that you could in theory have players connect to a server from many different Bungee instances - I could set up two Bungee servers to connect to one actual server, so this would also cause your plugin a problem. You are better off just giving your server a name through a config file.

1 Like