Send a Message to Bungeecord x2

Hey.
How can I send message to bungeecord server?

old code:

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream os = new DataOutputStream(bos);
        try {
            os.writeUTF...
        } catch ( IOException e ) {
            e.printStackTrace();
        }
        ....getConnection().sendCustomPayload( ...);

Use the network API
https://github.com/SpongePowered/SpongeAPI/blob/master/src/main/java/org/spongepowered/api/network/ChannelRegistrar.java

1 Like

Any example usages?

I need to figure out the best way to get hold of BungeeCord’s channel, but if we were to create our own channel it’d look something like this:

// Create the channel
RawDataChannel channel = game.getChannelRegistrar().createRawChannel(this, "BungeeCord");

// A one-liner to send a player to a server
channel.sendTo(player, buf -> buf.writeString("Connect").writeString("someServer"));

// Another example
channel.sendTo(player, buf -> {
    buf.writeString("PlayerCount");
    buf.writeString("someServer");
});

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

    if (subChannel.equals("PlayerCount")) {
        String server = buf.readString();
        int playerCount = buf.readInteger();
        System.out.println("Player count on server " + server + " is " + playerCount);
    }
});
1 Like

I see, I was trying to use Messages instead. Thanks for the clarification, very useful bud!

@Tzk: It would be helpful to include how to use the network API in the docs :wink:

Using Message is the highly recommended way if you’re in control of the channel’s protocol spec. Because BungeeCord already defined their own spec, you have to use it. If BungeeCord were to create a new version, I’d suggest they use messages following the spec of IndexedMessageChannel

No worries, that one is on our (rather long) issues list. :wink:

1 Like