Vinetos

Lobbys Selector

Apr 20th, 2016
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. public static ServerInfo getBetterLobby() {
  2.      if(UnikBungee.getOnlineLobbys().isEmpty())
  3.      if(getOnlineLobbys().isEmpty())
  4.         return null;
  5.     Map<ServerInfo, Integer> map = sortByValues(getOnlineLobbys());
  6.     return map.keySet().iterator().next();
  7. }
  8. /* Trier une HashMap en fonction des valeurs
  9.     *
  10.     * @param map HashMap à trier
  11.     * @return HashMap triée
  12.     */
  13.     public static HashMap sortByValues(HashMap map) {
  14.         List list = new LinkedList(map.entrySet());
  15.  
  16.         Collections.sort(list, new Comparator() {
  17.             public int compare(Object o1, Object o2) {
  18.                 return ((Comparable) ((Map.Entry) (o1)).getValue())
  19.                         .compareTo(((Map.Entry) (o2)).getValue());
  20.             }
  21.         });
  22.  
  23.  
  24.         HashMap sortedHashMap = new LinkedHashMap();
  25.         for (Iterator it = list.iterator(); it.hasNext(); ) {
  26.             Map.Entry entry = (Map.Entry) it.next();
  27.             sortedHashMap.put(entry.getKey(), entry.getValue());
  28.         }
  29.         return sortedHashMap;
  30.     }
  31. /*Récupérer les Lobbys en ligne
  32.     *
  33.     * @return Un Map de ServerInfo, joueur en ligne.
  34.     */
  35.     public static HashMap<ServerInfo, Integer> getOnlineLobbys() {
  36.         HashMap<ServerInfo, Integer> lobbys = new HashMap<>();
  37.         for (ServerInfo si : ProxyServer.getInstance().getServers().values())
  38.             if (si.getName().contains("Lobby"))
  39.                 lobbys.put(si, si.getPlayers().size());
  40.         return lobbys;
  41.     }
Advertisement
Add Comment
Please, Sign In to add comment