Sending plugin message from BungeeCord to Bukkit

Discussion in 'BungeeCord Plugin Development' started by Lazertx, Apr 10, 2016.

  1. I have a plugin that uses regular expression to find certain phrases and will make players execute a command based on what they said. The plugin utilizes the plugin channel as can be seen here (This is a BungeeCord plugin)
    Code (Text):
    package cc.ophion.fiona.phrase.response;

    import cc.ophion.fiona.Fiona;
    import com.google.common.collect.Iterables;
    import com.google.common.io.ByteArrayDataOutput;
    import com.google.common.io.ByteStreams;
    import net.md_5.bungee.api.CommandSender;
    import net.md_5.bungee.api.connection.ProxiedPlayer;

    import java.io.ByteArrayOutputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;

    /**
    * Created by derek on 3/3/2016.
    */
    public class BukkitCommandResponse extends CommandResponse {

        private String server;

        public BukkitCommandResponse(String command, String server, boolean consoleSender) {
            super(command, consoleSender);

            this.server = server;
        }

        @Override
        public void execute(CommandSender target) {
            if (isConsoleSender()) {
                target = Fiona.getInstance().getProxy().getConsole();
            }

            ByteArrayDataOutput out = ByteStreams.newDataOutput();

            out.writeUTF("Forward");
            out.writeUTF("ALL");
            out.writeUTF("Fiona");

            ByteArrayOutputStream messageBytes = new ByteArrayOutputStream();
            DataOutputStream messageOut = new DataOutputStream(messageBytes);

            try {
                messageOut.writeUTF(server);
                messageOut.writeUTF(getCommand());
                messageOut.writeUTF(target.getName());
            } catch (IOException e) {
                e.printStackTrace();
            }

            out.writeShort(messageBytes.toByteArray().length);
            out.write(messageBytes.toByteArray());

            ProxiedPlayer player = Iterables.getFirst(Fiona.getInstance().getProxy().getPlayers(), null);

            if (player != null) {
                player.sendData("BungeeCord", out.toByteArray());
                System.out.println("CODE CHECK");
            }
        }
    }
     
    Note that the message CODE CHECK is printed out so I know the code is running.

    I registered the channel in the onEnable method (This is in the BungeeCord plugin)
    Code (Text):
        @Override
        public void onEnable() {
            getProxy().registerChannel("BungeeCord");

            instance = this;

            reload();
        }

    Here is my plugin on each of the Bukkit servers that should be receiving the message, however, the CODE CHECK message is never sent. The plugin does show as enabled on /pl (This is the Bukkit plugin that will run on multiple servers)
    Code (Text):

    package cc.ophion.fiona.bukkit;

    import com.google.common.io.ByteArrayDataInput;
    import com.google.common.io.ByteStreams;
    import org.bukkit.Bukkit;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.messaging.PluginMessageListener;

    import java.io.ByteArrayInputStream;
    import java.io.DataInputStream;
    import java.io.IOException;

    /**
    * Created by derek on 3/3/2016.
    */
    public class Fiona extends JavaPlugin implements PluginMessageListener {

        @Override
        public void onEnable() {
            this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
            this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
        }

        @Override
        public void onPluginMessageReceived(String channel, Player player, byte[] message) {
            System.out.println("CODE CHECK");

            if (!channel.equals("BungeeCord")) {
                return;
            }

            ByteArrayDataInput in = ByteStreams.newDataInput(message);
            String subChannel = in.readUTF();
            short length = in.readShort();
            byte[] messageBytes = new byte[length];

            in.readFully(messageBytes);

            DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(messageBytes));

            if (subChannel.equals("Fiona")) {
                try {
                    String server = dataInputStream.readUTF();
                    String command = dataInputStream.readUTF();
                    String sender = dataInputStream.readUTF();

                    if (server.equalsIgnoreCase(getServer().getName())) {
                        CommandSender commandSender = null;
                        if (sender.equals("CONSOLE_SENDER")) {
                            commandSender = Bukkit.getConsoleSender();
                        } else {
                            Player commandPlayer = Bukkit.getPlayer(sender);

                            if (commandPlayer != null && commandPlayer.isOnline()) {
                                commandSender = commandPlayer;
                            }
                        }

                        if (commandSender != null) {
                            Bukkit.dispatchCommand(commandSender, command);
                        }
                    }
                } catch (IOException e) {
                    System.out.println("Unable to read plugin message.");
                }
            }
        }
    }