Advertisement
Guest User

NameFetcher

a guest
Sep 6th, 2016
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. package org.blazingpvp.charlie.Utils;
  2.  
  3. import com.google.common.collect.ImmutableList;
  4. import org.json.simple.JSONObject;
  5. import org.json.simple.parser.JSONParser;
  6.  
  7. import java.io.InputStreamReader;
  8. import java.net.HttpURLConnection;
  9. import java.net.URL;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.UUID;
  14. import java.util.concurrent.Callable;
  15.  
  16. public class NameFetcher implements Callable<Map<UUID, String>> {
  17.     private static final String PROFILE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/";
  18.     private final JSONParser jsonParser = new JSONParser();
  19.     private final List<UUID> uuids;
  20.  
  21.     public NameFetcher(List<UUID> uuids) {
  22.         this.uuids = ImmutableList.copyOf(uuids);
  23.     }
  24.  
  25.     public Map<UUID, String> call() throws Exception {
  26.         Map<UUID, String> uuidStringMap = new HashMap<UUID, String>();
  27.         for (UUID uuid : uuids) {
  28.             HttpURLConnection connection = (HttpURLConnection) new URL(PROFILE_URL + uuid.toString().replace("-", "")).openConnection();
  29.             JSONObject response = (JSONObject) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
  30.             String name = (String) response.get("name");
  31.             if (name == null) {
  32.                 continue;
  33.             }
  34.             String cause = (String) response.get("cause");
  35.             String errorMessage = (String) response.get("errorMessage");
  36.             if (cause != null && cause.length() > 0) {
  37.                 throw new IllegalStateException(errorMessage);
  38.             }
  39.             uuidStringMap.put(uuid, name);
  40.         }
  41.         return uuidStringMap;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement