Solved Get Response from BungeeCord

Discussion in 'Spigot Plugin Development' started by EvilWolwerINC, Oct 12, 2019.

Thread Status:
Not open for further replies.
  1. Hello,

    I need to get response from BungeeCord, but i dont know how.
    I have already read this thread: https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/#kickplayer

    But i cant understand how to get response.

    For example, I coded BungeeCord Connector (send player to server). In this part we don't need to get response from Bungee:
    Code (Java):
    package ru.wolwer.zeroncore.bungee;

    import com.google.common.io.ByteArrayDataInput;
    import com.google.common.io.ByteArrayDataOutput;
    import com.google.common.io.ByteStreams;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.messaging.PluginMessageListener;
    import ru.wolwer.zeroncore.Main;

    public class BungeeListener implements PluginMessageListener {


        private Main plugin = Main.getInstance();
        @Override
        public void onPluginMessageReceived(String channel, Player player, byte[] message) {
            if(!channel.equals("BungeeCord"))return;
            ByteArrayDataInput input = ByteStreams.newDataInput(message);
            String subchannel = input.readUTF();
        }


        public void connect(Player player, String server){
            ByteArrayDataOutput output = ByteStreams.newDataOutput();
            output.writeUTF("Connect");
            output.writeUTF(server);
            player.sendPluginMessage(plugin, "BungeeCord", output.toByteArray());
        }
       
    }
     
    Now, i need to get UUID of player. But in wiki written, that we need to get response from BungeeCord.

    Question: How to get (and read) response from Bungee Cord
     
  2. SteelPhoenix

    Junior Mod

    Make sure to check the subchannel and read the response like the wiki says.

    You're already getting and reading responses from BungeeCord:
    Code (Java):
    @Override
    public void onPluginMessageReceived(String channel, Player player, byte[] message) {
        if(!channel.equals("BungeeCord")) {
            return;
        }
        ByteArrayDataInput input = ByteStreams.newDataInput(message);
        String subchannel = input.readUTF();
    }
     
  3. Yeah, i understand, but how to use em. For example send it to player?
     
  4. You basically send packets between the client (Minecraft Server) and the server (Bungee Server), I don't know if I understood this right but you basically have two minecraft servers connected with a bungeecord? And Server A wants to know the UUID of someone on any linked server instance?

    Server A to Bungee: "I want to know the UUID of EvilWolwerINC"
    Bungee to all Servers: "I want to know the UUID of a player named EvilWolwerINC"
    Server ? to Bungee: "Hey I got a player named EvilWolwerINC, his uuid is ###"
    Bungee to Server A: "Hey I found the UUID of EvilWolwerINC it is ###"

    The JavaPlugin#onPluginMessageReceived method allows to receive the packet/message while the ProxiedPlayer.getServer().getInfo()#sendData method allows you send the packet/message
     
  5. You advicing me to divide plugin to 2 parts:
    1 - on bungee
    2 - on spigot (Server A)

    Or I need to use onPluginMessageRecieved?

    That's my new BungeeListener.java
    Code (Text):
    package ru.wolwer.zeroncore.bungee;

    import com.google.common.io.ByteArrayDataInput;
    import com.google.common.io.ByteArrayDataOutput;
    import com.google.common.io.ByteStreams;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.messaging.PluginMessageListener;
    import ru.wolwer.zeroncore.Main;

    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.net.Socket;

    public class BungeeListener implements PluginMessageListener {


        private Main plugin = Main.getInstance();

        private boolean serveronline = false;

        @Override
        public void onPluginMessageReceived(String channel, Player player, byte[] message) {
            if (!channel.equals("BungeeCord")) return;
            ByteArrayDataInput input = ByteStreams.newDataInput(message);

            String subchannel = input.readUTF();
           
            //Do it:
            //Right?
           
            if (subchannel.equals("PlayerUUID")){
                String uuid = input.readUTF();
            }
           
           
           
        }


        public void connect(Player player, String server){
            ByteArrayDataOutput output = ByteStreams.newDataOutput();
            output.writeUTF("Connect");
            output.writeUTF(server);
            player.sendPluginMessage(plugin, "BungeeCord", output.toByteArray());
        }

        public void getUuid(Player player){
            ByteArrayDataOutput output = ByteStreams.newDataOutput();
            output.writeUTF("UUID");
            player.sendPluginMessage(plugin, "BungeeCord", output.toByteArray());
        }

        public void kickPlayer(Player player, String reason){
            ByteArrayDataOutput output = ByteStreams.newDataOutput();
            output.writeUTF("KickPlayer");
           
            //Testing to kick myself
            output.writeUTF("Wolwerlegit");
           
           
            output.writeUTF( reason );
            player.sendPluginMessage(plugin, "BungeeCord", output.toByteArray());
        }
       
    }
     
    And I need to send UUID to player (requester of other's player UUID)
    Code (Text):
    package ru.wolwer.zeroncore.commands;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import ru.wolwer.zeroncore.bungee.BungeeListener;

    public class testbungeetunell implements CommandExecutor {

        private BungeeListener pluginMessage = new BungeeListener();

        @Override
        public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if(args.length == 2){
                    Player target = Bukkit.getPlayerExact(args[1]);
                    if(args[0].equalsIgnoreCase("kickplayer")){
                       
                        //Kick myself for "test"
                        pluginMessage.kickPlayer(target, "test");

                    }else if(args[0].equalsIgnoreCase("getplayeruuid")){

                        pluginMessage.getUuid(target);
                        sender.sendMessage("tested UUID");
                       
                    }else {
                        sender.sendMessage("Invalid arguments!");
                    }
                }
            }
            return true;
        }

    }
     
    Can you explain me how to send response of BungeeCord to player?


    Thanks <3
     
  6. nvm i made it:
    Code (Text):
    package ru.wolwer.zeroncore.bungee;

    import com.google.common.io.ByteArrayDataInput;
    import com.google.common.io.ByteArrayDataOutput;
    import com.google.common.io.ByteStreams;
    import jdk.internal.instrumentation.Logger;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.messaging.PluginMessageListener;
    import ru.wolwer.zeroncore.Main;

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


    public class BungeeListener implements PluginMessageListener {


        private Main plugin = Main.getInstance();


        @Override
        public void onPluginMessageReceived(String channel, Player player, byte[] message) {
            Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&c[&fPROXY&c] &fPacket recieved from some subchannel"));
            if (!channel.equals("BungeeCord")) return;
            ByteArrayDataInput input = ByteStreams.newDataInput(message);

            String subchannel = input.readUTF();

            //Do it:
            //Right?

            if (subchannel.equals("UUID")){
                Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&c[&fPROXY&c] &fPacket recieved from UUID subchannel"));
                String playerUuid = input.readUTF();

                player.sendMessage(playerUuid);


                plugin.getLogger().info(playerUuid);

            }
            ByteArrayDataInput in = ByteStreams.newDataInput(message);
            if (subchannel.equals("IP")) {
                Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&c[&fPROXY&c] &fPacket recieved from IP subchannel"));
                String playerIp = input.readUTF();

                player.sendMessage(ChatColor.RED + playerIp);



            }



        }



        public void connect(Player player, String server){
            ByteArrayDataOutput output = ByteStreams.newDataOutput();
            output.writeUTF("Connect");
            output.writeUTF(server);
            player.sendPluginMessage(plugin, "BungeeCord", output.toByteArray());
        }

        public void getUuid(Player player){
            ByteArrayDataOutput output = ByteStreams.newDataOutput();
            output.writeUTF("UUID");
            player.sendPluginMessage(plugin, "BungeeCord", output.toByteArray());

        }

        public void kickPlayer(Player player, String reason){
            ByteArrayDataOutput output = ByteStreams.newDataOutput();
            output.writeUTF("KickPlayer");

            //Testing to kick myself
            output.writeUTF("wolwerlegit");


            output.writeUTF( reason );
            player.sendPluginMessage(plugin, "BungeeCord", output.toByteArray());
        }

        public void getIp(Player player){
            ByteArrayDataOutput output = ByteStreams.newDataOutput();
            output.writeUTF("IP");
            player.sendPluginMessage(plugin, "BungeeCord", output.toByteArray());
        }



    }
     
     
Thread Status:
Not open for further replies.