Solved checking what bungeecord server a player is on? [for Spigot]

Discussion in 'Spigot Plugin Development' started by Swiftlicious, Mar 12, 2020.

Thread Status:
Not open for further replies.
  1. so I'm trying to make a /seen command which processes only for certain bungeecord servers to see if they're either currently online the server that the /seen command was issued on or on one of the other bungeecord servers that I am checking on. Here's what I have:
    Code (Text):
        @Override
        public void onPluginMessageReceived(String channel, Player receiver, byte[] message) {
       
            if (!(channel.equalsIgnoreCase("BungeeCord"))) {
                return;
            }
       
            if (channel.equalsIgnoreCase("BungeeCord")) {
                ByteArrayDataInput in = ByteStreams.newDataInput(message);
                String subChannel = in.readUTF();
                if (subChannel.equalsIgnoreCase("PlayerCount")) {
                    String server = in.readUTF();
                    int online = in.readInt();
                    // Verifies if it is the amount of players in the whole network.
                    if (server.equalsIgnoreCase("ALL")) {
                        this.online = online;
                    }
                } else if (subChannel.equalsIgnoreCase("GetServer")) {
                    String name = in.readUTF();
                    this.server = name;
                } else if (subChannel.equalsIgnoreCase("GetServers")) {
                    String[] serverList = in.readUTF().split(", ");
                    ArrayList<String> bwServers = new ArrayList<String>();
                    if (this.server.matches("bw[0-9]+") || this.server.matches("bedwars[0-9]+")) {
                        bwServers.add(this.server);
                    }
                    for (String bedwars : bwServers) {
                        if (server.equalsIgnoreCase(bedwars)) {
                            this.servers = serverList;
                        }
                    }
                } else if (subChannel.equalsIgnoreCase("PlayerList")) {
                    String server = in.readUTF(); // The name of the server you got the player list of, as given in args
                    String[] playerList = in.readUTF().split(", ");
                    ArrayList<String> bwServers = new ArrayList<String>();
                    if (this.server.matches("bw[0-9]+") || this.server.matches("bedwars[0-9]+")) {
                        bwServers.add(this.server);
                    }
                    for (String bedwars : bwServers) {
                        if (server.equalsIgnoreCase(bedwars)) {
                            this.list = playerList;
                        }
                    }
                }
            }
        }
    I'm trying to get the PlayerList and GetServers List to retrieve the correct servers of which I want accepted by matching the server name from getServer.
    I then put them in lists then change them to String[]'s like so:
    Code (Text):
        // Gets all servers from bungee if bungee not enabled just get the server they're on.
        private ArrayList<String> changeableServers = new ArrayList<String>();
        private String[] servers = changeableServers.toArray(new String[0]);
        // Gets the playerList from bungee if bungee not enabled just get the current server's list that they're on.
        private ArrayList<String> changeableList = new ArrayList<String>();
        private String[] list = changeableList.toArray(new String[Bukkit.getOnlinePlayers().size()]);
    The bungeecord messaging plugin listener has the example as String[] so since i don't work much with bungeecord unless I need too I didn't know if it would just take the arraylist alone (though I assume it would).

    I then retrieve those methods like this and update them every 1 second like this:
    Code (Text):
        // The server's list found in bungeecord.
        public String[] getList() {
            return this.list;
        }
     
        // The server's server list found in bungeecord.
        public String[] getServers() {
            return this.servers;
        }
    Code (Text):
        // Gets a player to send and receive messages from BungeeCord
        public void requestServersListUpdate() {
       
            // An online player to send the message to the BungeeCord.
            Player sender = null;
       
            // Gets a player to send and receive messages from BungeeCord.
            for (Player player : Bukkit.getOnlinePlayers()) {
                sender = player;
                break;
            }
       
            if (sender != null) {
                // Package that will be sent.
                ByteArrayDataOutput out = ByteStreams.newDataOutput();
                out.writeUTF("GetServers");
           
                try {
                    // Send the message to BungeeCord.
                    sender.sendPluginMessage(this, "BungeeCord", out.toByteArray());
                } catch (Exception ex) {
                    // Prints in the console if any errors occur.
                    Bukkit.getConsoleSender().sendMessage("[" + this.getName() + "] Failed to request bungeecord server list!");
                    ex.printStackTrace();
                    return;
                }
            }
        }
     
        // Gets a player to send and receive messages from BungeeCord
        public void requestPlayerListUpdate() {
       
            // An online player to send the message to the BungeeCord.
            Player sender = null;
       
            // Gets a player to send and receive messages from BungeeCord.
            for (Player player : Bukkit.getOnlinePlayers()) {
                sender = player;
                break;
            }
       
            if (sender != null) {
                // Package that will be sent.
                ByteArrayDataOutput out = ByteStreams.newDataOutput();
                out.writeUTF("PlayerList");
           
                try {
                    // Send the message to BungeeCord.
                    sender.sendPluginMessage(this, "BungeeCord", out.toByteArray());
                } catch (Exception ex) {
                    // Prints in the console if any errors occur.
                    Bukkit.getConsoleSender().sendMessage("[" + this.getName() + "] Failed to request bungeecord player list!");
                    ex.printStackTrace();
                    return;
                }
            }
        }
    my problem is that I don't know how I would now loop through the players list to see if they're on one of the defined allowed servers I am allowing to be looped through in the plugin messaging listener, what I have though right now is this for my command:
    Code (Text):
                    boolean isServerGlobal = false;
                    for (String globalServers : pl.getServers()) {
                        if (globalServers.equals(pl.getServerName())) {
                            isServerGlobal = true;
                        }
                    }
                 
                    boolean isPlayerGlobal = false;
                    for (String globalPlayers : pl.getList()) {
                        if (globalPlayers.equals(correctusername)) {
                            isPlayerGlobal = true;
                        }
                    }
                 
                    String correctUUID = new StatsAPI().getString(correctusername, "UUID");
                    Player correctplayer = Bukkit.getOfflinePlayer(UUID.fromString(correctUUID)).getPlayer();
                 
                    if (correctplayer != null) {
                        if (correctplayer.isOnline()) {
                            online_same = online_same.replace("{server}", pl.getServerName()).replace("{user}", correctusername);
                            MessageAPI.send(p, online_same);
                            return true;
                        } else if (!correctplayer.isOnline() && isPlayerGlobal && isServerGlobal) {
                            online_different = online_different.replace("{server}", "should be getting whatever the player's server name is").replace("{user}", correctusername).replace("{status}", GameManager.returnGameStateForServer());
                            MessageAPI.send(p, online_different);
                            return true;
                        } else if (!correctplayer.isOnline() && !isPlayerGlobal && !isServerGlobal) {
                            last_seen = last_seen.replace("{user}", correctusername).replace("{lastseen}", String.valueOf(seen));
                            MessageAPI.send(p, last_seen);
                            return true;
                        }
                    } else {
                        MessageAPI.send(p, not_found);
                        return true;
                    }

    So basically my problem is the following:
    how do I get from this code the server's name that the player I'm searching for from /seen **[username]** I believe all of the rest is fine if I am doing it right but I don't work with bungee a lot so maybe not ;-;.
    From the research I've done all I know is that I need to get "GetServers" and "PlayerList" from bungeecord, which I believe i've done?
     
    #1 Swiftlicious, Mar 12, 2020
    Last edited: Mar 12, 2020
Thread Status:
Not open for further replies.