Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // onPostLogin - Triggered right after a player logs on.
- @EventHandler
- public void onPostLogin(PostLoginEvent event){
- // Gets the event player.
- ProxiedPlayer player = event.getPlayer();
- String currentLobby = player.getServer().getInfo().getName();
- // Adds all Main Lobby servers to a HashMap.
- HashMap<String, Integer> lobbies = new HashMap<String,Integer>();
- // hashMap format: (serverName, connectionPort)
- lobbies.put("mainLobby#1", 25567);
- lobbies.put("mainLobby#2", 25568);
- lobbies.put("mainLobby#3", 25569);
- ArrayList<String> onlineLobbies = new ArrayList<String>();
- // Loops through all Main Lobbies and checks if they're online.
- for (HashMap.Entry<String, Integer> lobby : lobbies.entrySet()) {
- // Pinging looped lobby to check online status.
- int port = lobby.getValue();
- try {
- Socket s = new Socket("212.83.164.100", port);
- // Adds looped lobby to onlineLobbies list if connection is successful.
- onlineLobbies.add(lobby.getKey());
- s.close();
- } catch (UnknownHostException e) {
- // Lobby is offline! (ERR1)
- } catch (IOException e) {
- // Lobby is offline! (ERR2)
- }
- }
- // Checks if there are no online Main Lobby servers.
- if (onlineLobbies.isEmpty()) {
- // Kicks the player if there aren't any Main Lobbies available.
- player.disconnect(new TextComponent(ChatColor.RED + "We couldn't find any " + ChatColor.DARK_GREEN + "Main Lobby " + ChatColor.RED + "servers to connect you to!" + "\n" + ChatColor.RED + "Please contact a " + ChatColor.GOLD + "Developer " + ChatColor.RED + "as soon as possible."));
- System.out.println(ChatColor.RED + "Player " + ChatColor.AQUA + player.getName() + ChatColor.RED + " has attempted to join. Kicking them since we couldn't find any available " + ChatColor.DARK_GREEN + "Main Lobby " + ChatColor.RED + "servers.");
- } else {
- // Loops through all online servers and picks a random one.
- Random pickingRandomLobby = new Random();
- String targetLobby = onlineLobbies.get(pickingRandomLobby.nextInt(onlineLobbies.size()));
- // Teleports player to Target Main Lobby server.
- player.connect(ProxyServer.getInstance().getServerInfo(targetLobby));
- // Notifies the bungee console where the player went.
- System.out.println(ChatColor.GREEN + "Player " + ChatColor.AQUA + player.getName() + ChatColor.GREEN + " has joined. Connecting them to server " + ChatColor.YELLOW + targetLobby + ChatColor.GREEN + ".");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement