Using Messaging Channels with Bungeecord

Hello. I am creating a plugin that allows me to execute commands on all servers under my bungeecord proxy from my bungeecord console(for donation/voting rewards). I have everything setup and the commands gets sent to my sponge server and everything is working but when my sponge server receives the message it throws an error. The BungeeCord code is working because my spigot version successfully gets the command and executes it.
Sponge code:

        public void handlePayload(ChannelBuf data, RemoteConnection connection, Platform.Type side) {
        String command = data.readString();
        System.out.println("Received Command " + command + " from Bungeecord! Executing");
        Sponge.getCommandManager().process(Sponge.getServer().getConsole(), command);
    }

Bungeecord Code:

    private void sendToBukkit(String channel, String message, ServerInfo server) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(stream);
    try {
        out.writeUTF(channel);
        out.writeUTF(message);
    } catch (IOException e) {
        e.printStackTrace();
    }
    server.sendData("Return", stream.toByteArray());

Error:

Any help is appreciated!

I am guessing, that that is happening because in your sponge code, you are receiving the data async, but then are processing a command on that async thread. Iirc, sponge stuff should be called on the main thread, so you will have to sync the task to the main thread using something like Tasks.

While @d4rkfly3r is correct, there’s another thing you should probably do with your code - ByteArrayDataOutput#writeUTF should be read by ChannelBuf#readUTF, not ChannelBuf#readString.

How would I fix this? Sorry I new to the sponge api.

https://docs.spongepowered.org/stable/en/plugin/scheduler.html