Guest User

uuid fetcher

a guest
Sep 6th, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.01 KB | None | 0 0
  1. package org.blazingpvp.charlie.Utils;
  2.  
  3. import com.google.common.collect.ImmutableList;
  4. import org.json.simple.JSONArray;
  5. import org.json.simple.JSONObject;
  6. import org.json.simple.parser.JSONParser;
  7.  
  8. import java.io.InputStreamReader;
  9. import java.io.OutputStream;
  10. import java.net.HttpURLConnection;
  11. import java.net.URL;
  12. import java.nio.ByteBuffer;
  13. import java.util.*;
  14. import java.util.concurrent.Callable;
  15.  
  16. /**
  17.  * Interface to Mojang's API to fetch player UUIDs from player names.
  18.  * <p>
  19.  * Thanks to evilmidget38:
  20.  * http://forums.bukkit.org/threads/player-name-uuid-fetcher.250926/
  21.  */
  22. public class UUIDFetcher implements Callable<Map<String, UUID>> {
  23.  
  24.     private static final double PROFILES_PER_REQUEST = 100;
  25.     private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
  26.     private final JSONParser jsonParser = new JSONParser();
  27.     private final List<String> names;
  28.     private final boolean rateLimiting;
  29.  
  30.     public UUIDFetcher(List<String> names, boolean rateLimiting) {
  31.         this.names = ImmutableList.copyOf(names);
  32.         this.rateLimiting = rateLimiting;
  33.     }
  34.  
  35.     public UUIDFetcher(List<String> names) {
  36.         this(names, true);
  37.     }
  38.  
  39.     @Override
  40.     public Map<String, UUID> call() throws Exception {
  41.         Map<String, UUID> uuidMap = new HashMap<>();
  42.         int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
  43.         for (int i = 0; i < requests; i++) {
  44.             HttpURLConnection connection = createConnection();
  45.             String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
  46.             writeBody(connection, body);
  47.             JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
  48.             for (Object profile : array) {
  49.                 JSONObject jsonProfile = (JSONObject) profile;
  50.                 String id = (String) jsonProfile.get("id");
  51.                 String name = (String) jsonProfile.get("name");
  52.                 UUID uuid = UUIDFetcher.getUUID(id);
  53.                 uuidMap.put(name, uuid);
  54.             }
  55.             if (rateLimiting && i != requests - 1) {
  56.                 Thread.sleep(100L);
  57.             }
  58.         }
  59.         return uuidMap;
  60.     }
  61.  
  62.     private static void writeBody(HttpURLConnection connection, String body) throws Exception {
  63.         try (OutputStream stream = connection.getOutputStream()) {
  64.             stream.write(body.getBytes());
  65.             stream.flush();
  66.         }
  67.     }
  68.  
  69.     private static HttpURLConnection createConnection() throws Exception {
  70.         URL url = new URL(PROFILE_URL);
  71.         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  72.         connection.setRequestMethod("POST");
  73.         connection.setRequestProperty("Content-Type", "application/json");
  74.         connection.setUseCaches(false);
  75.         connection.setDoInput(true);
  76.         connection.setDoOutput(true);
  77.         return connection;
  78.     }
  79.  
  80.     private static UUID getUUID(String id) {
  81.         return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" + id.substring(20, 32));
  82.     }
  83.  
  84.     public static byte[] toBytes(UUID uuid) {
  85.         ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
  86.         byteBuffer.putLong(uuid.getMostSignificantBits());
  87.         byteBuffer.putLong(uuid.getLeastSignificantBits());
  88.         return byteBuffer.array();
  89.     }
  90.  
  91.     public static UUID fromBytes(byte[] array) {
  92.         if (array.length != 16) {
  93.             throw new IllegalArgumentException("Illegal byte array length: " + array.length);
  94.         }
  95.         ByteBuffer byteBuffer = ByteBuffer.wrap(array);
  96.         long mostSignificant = byteBuffer.getLong();
  97.         long leastSignificant = byteBuffer.getLong();
  98.         return new UUID(mostSignificant, leastSignificant);
  99.     }
  100.  
  101.     public static UUID getUUIDOf(String name) throws Exception {
  102.         return new UUIDFetcher(Arrays.asList(name)).call().get(name);
  103.     }
  104. }
Add Comment
Please, Sign In to add comment