Advertisement
Moonquistico

Untitled

Jan 4th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. // onPostLogin - Triggered right after a player logs on.
  2. @EventHandler
  3. public void onPostLogin(PostLoginEvent event){
  4. // Gets the event player.
  5. ProxiedPlayer player = event.getPlayer();
  6. String currentLobby = player.getServer().getInfo().getName();
  7.  
  8. // Adds all Main Lobby servers to a HashMap.
  9. HashMap<String, Integer> lobbies = new HashMap<String,Integer>();
  10. // hashMap format: (serverName, connectionPort)
  11. lobbies.put("mainLobby#1", 25567);
  12. lobbies.put("mainLobby#2", 25568);
  13. lobbies.put("mainLobby#3", 25569);
  14.  
  15. ArrayList<String> onlineLobbies = new ArrayList<String>();
  16. // Loops through all Main Lobbies and checks if they're online.
  17. for (HashMap.Entry<String, Integer> lobby : lobbies.entrySet()) {
  18. // Pinging looped lobby to check online status.
  19. int port = lobby.getValue();
  20. try {
  21. Socket s = new Socket("212.83.164.100", port);
  22. // Adds looped lobby to onlineLobbies list if connection is successful.
  23. onlineLobbies.add(lobby.getKey());
  24. s.close();
  25. } catch (UnknownHostException e) {
  26. // Lobby is offline! (ERR1)
  27. } catch (IOException e) {
  28. // Lobby is offline! (ERR2)
  29. }
  30. }
  31.  
  32. // Checks if there are no online Main Lobby servers.
  33. if (onlineLobbies.isEmpty()) {
  34. // Kicks the player if there aren't any Main Lobbies available.
  35. 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."));
  36. 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.");
  37. } else {
  38. // Loops through all online servers and picks a random one.
  39. Random pickingRandomLobby = new Random();
  40. String targetLobby = onlineLobbies.get(pickingRandomLobby.nextInt(onlineLobbies.size()));
  41.  
  42. // Teleports player to Target Main Lobby server.
  43. player.connect(ProxyServer.getInstance().getServerInfo(targetLobby));
  44. // Notifies the bungee console where the player went.
  45. System.out.println(ChatColor.GREEN + "Player " + ChatColor.AQUA + player.getName() + ChatColor.GREEN + " has joined. Connecting them to server " + ChatColor.YELLOW + targetLobby + ChatColor.GREEN + ".");
  46. }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement