Solved Registering Plugin Channels

Discussion in 'BungeeCord Plugin Development' started by GamerAPI, Jan 25, 2020.

  1. Most of the time I use 1.8, but I started using 1.15 recently. To register a plugin channel in 1.8, you would just pass a parameter into the registerChannel method. In 1.13, a good amount of stuff was switched over to namespaces. If I register my channels in my spigot plugin without the namespace:(key) then it throws an exception, but the same channel has to be registered in bungee as well.
    Bungee Main Class:
    Code (Text):
    BungeeCord.getInstance().registerChannel("namespace:execute");
    Spigot Main Class:
    Code (Text):
    Bukkit.getMessenger().registerIncomingPluginChannel(this, "namespace:execute", new PluginMessager());
    When I try to send data through the channel with this method:
    Code (Text):
            server.sendData("execute", stream.toByteArray());
    nothing happens. I'm not sure what I am doing wrong with this.
     
  2. server.sendData("namespace:execute", stream.toByteArray());
     
  3. Yeah, I have that
     
  4. md_5

    Administrator Developer

    No you don't, you're missing namespace
     
  5. I just changed it, and it still doesn't work
     
  6. Take a look at this then.
     
  7. I simplified my code down to this in my spigot plugin:
    Code (Text):
    public class Main extends JavaPlugin implements PluginMessageListener {
       
        public void onEnable() {
            Bukkit.getMessenger().registerIncomingPluginChannel(this, "namespace:execute", this);
        }

        public void onPluginMessageReceived(String channel, Player player, byte[] msg) {
            DataInputStream in = new DataInputStream(new ByteArrayInputStream(msg));
            if (channel.equals("namespace:execute")) {
                try {
                    String sub = in.readUTF();
                    if (sub.equals("command")) {
                        String cmd = in.readUTF();
                        System.out.println("Received");
                        getServer().dispatchCommand(getServer().getConsoleSender(), cmd);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
       
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            System.out.println("This worked!");
            return true;
        }
    }
    And this in my Bungee Main class
    Code (Text):
        public void onEnable() {
            getProxy().registerChannel("namespace:execute");
            BungeeCord.getInstance().getPluginManager().registerCommand(this, new Execute());
        }
    And this is the Execute command class in bungeecord
    Code (Text):
    public class Execute extends Command {
        public Execute() {
            super("test");
        }

        public void execute(CommandSender sender, String[] args) {
            sender.sendMessage(new TextComponent("Testing..."));
            Map<String, ServerInfo> servers = BungeeCord.getInstance().getServers();
            for (Entry<String, ServerInfo> en : servers.entrySet()) {
                String name = en.getKey();
                ServerInfo server = BungeeCord.getInstance().getServerInfo(name);
                ByteArrayOutputStream b = new ByteArrayOutputStream();
                DataOutputStream out = new DataOutputStream(b);
                try {
                    out.writeUTF("command");
                    out.writeUTF("test");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                server.sendData("namespace:execute", b.toByteArray());
            }
        }

    }
    It still doesn't work, an I'm not sure whats wrong with it. I basically copy pasted from my 1.8 plugin, which worked fine.
     
  8. There should be a player in the server you want to get that command executed on. I'll make test plugin to show you that you're doing something wrong with testing it, and the code being fine.

    EDIT: You're not registering the command. Also you shouldn't be using api internals.

    EDIT 2: I was able to successfully make this working. Consider changing internals with api methods
     
    #8 MrIvanPlays, Jan 26, 2020
    Last edited: Jan 26, 2020
  9. I don't know what you mean by "internals", but if it's the output and input streams, what else am I supposed to use. Also, you don't need to register a command that is in your Main class. If that were the issue, then it would still sysout the "Received" in the PluginMessageListener before it executes the command.