Spigot get status of a Bungee server

Discussion in 'Spigot Plugin Development' started by JordanOsterberg, May 23, 2015.

  1. Hello everyone, I was wondering how I can get the status of a Bungee server on my network (This isn't a Bungee Plugin) and check if it is online or not. Thank you!
     
    • Optimistic Optimistic x 1
  2. Inkzzz

    Resource Staff

    Ping the server
     
  3. I tried something with this, still didn't work ;-;
    Code:
    PHP:
    package com.thetrollzltd.hubsuite;

    import org.bukkit.entity.Player;

    import java.io.*;

    /**
    * Created by TheTrollzYT. All rights reserved.
    * Any person(s) using this code are subject to
    * a $1000 fine if used without permissions.
    */

    public class PluginMessageListener implements org.bukkit.plugin.messaging.PluginMessageListener {

        public static int getPlayerCount(String server){

            ByteArrayOutputStream b = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(b);

            DataInputStream in = new DataInputStream(new ByteArrayInputStream(message));

            try {
                out.writeUTF("PlayerCount");
                out.writeUTF(server);

                String serverval = in.readUTF(); // Name of server, as given in the arguments
                int playercount = in.readInt();

                return playercount;

            } catch (IOException e){
                e.printStackTrace();
            }
            return 0;
        }

       private static byte[] message;

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

           this.message = message;

        }

    }
     
    Calling getPlayerCount("servernamehere"); causes an NPE.
     
    • Funny Funny x 2
  4. foncused

    Moderator Patron

    Your code for the Bungee subchannel stuff is incomplete and wrong, hence the NPE. But I don't know if subchannels are even what you are looking for. I just store a boolean value in a database for each server - true if online, false if not online.
     
  5. I think what you need to do is this please someone correct me if I'm wrong :)

    Code (Text):
        public void getPlayerCount(String server)
        {
             ByteArrayOutputStream b = new ByteArrayOutputStream();
                DataOutputStream out = new DataOutputStream(b);

                try {
                    out.writeUTF("PlayerCount");
                    out.writeUTF(server);
                } catch (IOException e){
                    e.printStackTrace();
                }
        }
       
        @Override
        public void onPluginMessageReceived(String channel, Player player, byte[] message) {
            if (!channel.equals("BungeeCord")) {
                return;
              }
              ByteArrayDataInput in = ByteStreams.newDataInput(message);
              String subchannel = in.readUTF();
              if (subchannel.equals("PlayerCount")) {
                  String server = in.readUTF(); // Name of server, as given in the arguments
                  int playercount = in.readInt();
                 
                 
              }
             
           
        }
     
  6. foncused

    Moderator Patron

    Pretty much, though you need to send the plugin message. This would go somewhere after the writeUTFs:
    Code (Text):

    // If you don't care about the player
    // Player player = Iterables.getFirst(Bukkit.getOnlinePlayers(), null);
    // Else, specify them
      Player player = Bukkit.getPlayerExact("Example");

      player.sendPluginMessage(this, "BungeeCord", out.toByteArray());
     
    Also, the class which contains onPluginMessageReceived() needs implements PluginMessageListener.
     
    • Agree Agree x 1
  7. LOL subject to a 1000$ fine.
    Sorry, its just kind of funny.
    Also, you could make a website that pings the bungeeserver to get information
    and the spigot plugin just reads the website.
     
  8. I'm using this method:
    PHP:
    package com.thetrollzltd.hubsuite;

    import com.google.common.io.ByteArrayDataInput;
    import com.google.common.io.ByteStreams;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;

    import java.io.*;

    /**
    * Created by TheTrollzYT. All rights reserved.
    * Any person(s) using this code are subject to
    * a $1000 fine if used without permissions.
    */

    public class PluginMessageListener implements org.bukkit.plugin.messaging.PluginMessageListener {

        private static String servern;
        private static int count;

        public static void setPlayerCount(String server)
        {
            ByteArrayOutputStream b = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(b);

            try {
                out.writeUTF("PlayerCount");
                out.writeUTF(server);

                Bukkit.getServer().sendPluginMessage(Core.getInstance(), "BungeeCord", b.toByteArray());
            } catch (IOException e){
                e.printStackTrace();
            }
        }

        @Override
        public void onPluginMessageReceived(String channel, Player player, byte[] message) {
            if (!channel.equals("BungeeCord")) {
                return;
            }
            ByteArrayDataInput in = ByteStreams.newDataInput(message);
            String subchannel = in.readUTF();
            if (subchannel.equals("PlayerCount")) {
                String server = in.readUTF(); // Name of server, as given in the arguments
                int playercount = in.readInt();

                servern = server;
                count = playercount;

                //System.out.print("Server " + servern + " has " + count + " online!");

            }


        }

        public static int getPlayerCount(String server){
            if (servern == server){
                return count;
            }
            return 0;
        }

    }
     
    PHP:
    GUIUtils.item(Material.WORKBENCH, inv, 4, "§cCreative Server §b- §aClick to join", "§cGrabbing data...");

                e.getPlayer().openInventory(inv);

                PluginMessageListener.setPlayerCount("creative");
                int creativecount = PluginMessageListener.getPlayerCount("creative");

                inv.getItem(4).getItemMeta().setLore(Arrays.asList("§c" + creativecount + " players online!"));
    but it still displays as "Grabbing data..."