Solved Bungee messaging channel "ServerIP" not working

Discussion in 'BungeeCord Plugin Development' started by Flyin, Jan 31, 2016.

  1. Hello! I am extremely new to the development of spigot-bungee plugins and had a question concerning the messaging channels. I am trying to get all the servers and whether or not they are online. The problem I am currently experiencing is with the ServerIP channel. I need this to find the port-to-server connection and I am getting nothing. I have supplied what I have so far below. Any help would be much appreciated.

    DynamicBungee (main class):

    Code (Text):
    package me.flyin.dbungee;

    import me.flyin.dbungee.cmds.ConnectCommand;
    import me.flyin.dbungee.listeners.InventoryClickListener;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.messaging.PluginMessageListener;

    import java.io.ByteArrayInputStream;
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.logging.Logger;

    public class DynamicBungee extends JavaPlugin implements PluginMessageListener
    {
        public static ArrayList<String> servers = new ArrayList<String>();

        PluginManager pm = Bukkit.getServer().getPluginManager();
        Logger log = Bukkit.getServer().getLogger();

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

            pm.registerEvents(new InventoryClickListener(), this);

            getCommand("connect").setExecutor(new ConnectCommand(this));
        }

        public void onDisable()
        {
            //TODO
        }

        @Override
        public void onPluginMessageReceived(String channel, Player player, byte[] message)
        {
            if (!channel.equals("BungeeCord"))
            {
                return;
            }
            try {
                DataInputStream in = new DataInputStream(new ByteArrayInputStream(message));
                String subchannel = in.readUTF();

                if (subchannel.equals("GetServers"))
                {
                    String[] serverList = in.readUTF().split(", ");
                    for (int x = 0; x < serverList.length; x++)
                    {
                        servers.add(serverList[x]);
                    }
                }
                else if (subchannel.equals("ServerIP"))
                {
                    String serverName = in.readUTF();
                    try {
                        String ip = in.readUTF();
                        short port = in.readShort();
                        System.out.println("Received plugin message about " + serverName + ", " + ip+ ":" + port);
                    } catch(Exception e) {
                        e.printStackTrace();
                        System.out.println("error with " + serverName);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    getBungeeServer method
    Code (Text):
        public static void getBungeeServer(String server)
        {
            ByteArrayOutputStream b = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(b);

            try {
                out.writeUTF("ServerIP");
                out.writeUTF(server.toLowerCase());
            } catch (Exception e) {
                e.printStackTrace();
            }
            Bukkit.getServer().sendPluginMessage(plugin, "BungeeCord", b.toByteArray());
        }
    Thanks in advance.

    ~ Flyin
     
  2. Plugin messaging requires a player to be online in order to function.
     
    • Agree Agree x 1
  3. Yes there needs to be a player online on the sever you're sending the packet to. If you're looking to get past this limitation with the plugin messaging channel API, take a look at sockets though I'm not sure whether they're worth the hassle for what you're trying to achieve.
     
  4. Does this apply for all packets?
     
  5. Yes? A packet is a piece of information sent between two entities (the client and server). So in order to send any "packets" it involves having both the client (player) and server connected. As @Shahram said: use sockets for what you're trying to accomplish.
     
  6. Thanks!
     
  7. You can use my APIs NiftyBungee and NiftyBukkit. They require no setup and are (currently) subject to the online player limitation but can get what you want done without needing any work on your part.
    Code (Java):
    BungeeServer server = NiftyBukkit.getBungeeHelper().getServer("hub");
    String ipAddress = server.getAddress().getAddress().getHostAddress();
    If you need this information when no one is online (console access), then currently you'll have to use sockets. I'm working on an experimental dynamic-socket registration feature that requires no setup, but it's unavailable right now.
     
  8. Thanks so much, I will check them out!