Sending plugin messages

Discussion in 'Spigot Plugin Development' started by Level3301, Jun 13, 2021.

  1. Hi how can i send plugin message from spigot to bungeecord?
    (I am able to send plugin message from bungeecord to spigot, but not vice versa)

    Spigot
    Code (Java):
    public void sendLobbyServerNameRequest() throws IOException {
            var player = Iterables.getFirst(Bukkit.getOnlinePlayers(), null);

            var stream = new ByteArrayOutputStream();
            var out = new DataOutputStream(stream);
            out.writeUTF("LobbyServerName");


            player.sendPluginMessage(
                    plugin,
                    channel,
                    stream.toByteArray()
            );
            System.out.println(player.getName());
            System.out.println(channel);
            System.out.println("sent lobby request");
        }
    Registered here (Spigot)
    Code (Java):
    Bukkit.getMessenger().registerOutgoingPluginChannel(
                    this,
                    bungeeCommunication.getChannel()
            );

            Bukkit.getMessenger().registerIncomingPluginChannel(
                    this,
                    bungeeCommunication.getChannel(),
                    bungeeCommunication
            );
    Bungee
    Code (Text):
    @EventHandler
        fun onPluginMessage(e:PluginMessageEvent){
            val channel_ = e.tag.toString()
            print(channel)
            print(channel_)
            if(channel_ != channel){
                return
            }
            val input = ByteStreams.newDataInput(e.data)
            val subChannel = input.readUTF()

            if(subChannel.equals("LobbyServerName")){
                print("lobby request replying")
                sendLobby()
                return
            }

            val playerName = input.readUTF()

            if(subChannel.equals("Forward")){
                val p = ProxyServer.getInstance().getPlayer(playerName)
                val lobby = ProxyServer.getInstance().getServerInfo(lobbyServer)

                p.connect(lobby)
            }
        }
    Registered here (Bungee)
    Code (Text):
    ProxyServer.getInstance().registerChannel(channel)
    ProxyServer.getInstance().pluginManager.registerListener(this, this)