Advertisement
Guest User

Untitled

a guest
Apr 8th, 2015
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.net.HttpURLConnection;
  4. import java.net.URL;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import java.util.UUID;
  8. import java.util.concurrent.ExecutorService;
  9. import java.util.concurrent.Executors;
  10. import java.util.function.Consumer;
  11. import org.bukkit.craftbukkit.libs.com.google.gson.Gson;
  12. import org.bukkit.craftbukkit.libs.com.google.gson.GsonBuilder;
  13. import com.mojang.util.UUIDTypeAdapter;
  14. public class UUIDFetcher {
  15.  
  16. /**
  17. * Date when name changes were introduced
  18. * @see UUIDFetcher#getUUIDAt(String, long)
  19. */
  20. public static final long FEBRUARY_2015 = 1422748800000L;
  21.  
  22.  
  23. private static Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create();
  24.  
  25. private static final String UUID_URL = "https://api.mojang.com/users/profiles/minecraft/%s?at=%d";
  26. private static final String NAME_URL = "https://api.mojang.com/user/profiles/%s/names";
  27. private static Map<String, UUID> uuidCache = new HashMap<String, UUID>();
  28. private static Map<UUID, String> nameCache = new HashMap<UUID, String>();
  29. private static ExecutorService pool = Executors.newCachedThreadPool();
  30.  
  31. private String name;
  32. private UUID id;
  33.  
  34. /**
  35. * Fetches the uuid asynchronously and passes it to the consumer
  36. *
  37. * @param name The name
  38. * @param action Do what you want to do with the uuid her
  39. */
  40. public static void getUUID(String name, Consumer<UUID> action) {
  41. pool.execute(() -> action.accept(getUUID(name)));
  42. }
  43.  
  44. /**
  45. * Fetches the uuid synchronously and returns it
  46. *
  47. * @param name The name
  48. * @return The uuid
  49. */
  50. public static UUID getUUID(String name) {
  51. return getUUIDAt(name, System.currentTimeMillis());
  52. }
  53.  
  54. /**
  55. * Fetches the uuid synchronously for a specified name and time and passes the result to the consumer
  56. *
  57. * @param name The name
  58. * @param timestamp Time when the player had this name in milliseconds
  59. * @param action Do what you want to do with the uuid her
  60. */
  61. public static void getUUIDAt(String name, long timestamp, Consumer<UUID> action) {
  62. pool.execute(() -> action.accept(getUUIDAt(name, timestamp)));
  63. }
  64.  
  65. /**
  66. * Fetches the uuid synchronously for a specified name and time
  67. *
  68. * @param name The name
  69. * @param timestamp Time when the player had this name in milliseconds
  70. * @see UUIDFetcher#FEBRUARY_2015
  71. */
  72. public static UUID getUUIDAt(String name, long timestamp) {
  73. name = name.toLowerCase();
  74. if (uuidCache.containsKey(name)) {
  75. return uuidCache.get(name);
  76. }
  77. try {
  78. HttpURLConnection connection = (HttpURLConnection) new URL(String.format(UUID_URL, name, timestamp/1000)).openConnection();
  79. connection.setReadTimeout(5000);
  80. UUIDFetcher data = gson.fromJson(new BufferedReader(new InputStreamReader(connection.getInputStream())), UUIDFetcher.class);
  81.  
  82. uuidCache.put(name, data.id);
  83. nameCache.put(data.id, data.name);
  84. return data.id;
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88.  
  89. return null;
  90. }
  91.  
  92. /**
  93. * Fetches the name asynchronously and passes it to the consumer
  94. *
  95. * @param uuid The uuid
  96. * @param action Do what you want to do with the name her
  97. */
  98. public static void getName(UUID uuid, Consumer<String> action) {
  99. pool.execute(() -> action.accept(getName(uuid)));
  100. }
  101. /**
  102. * Fetches the name synchronously and returns it
  103. *
  104. * @param uuid The uuid
  105. * @return The name
  106. */
  107. public static String getName(UUID uuid) {
  108. if (nameCache.containsKey(uuid)) {
  109. return nameCache.get(uuid);
  110. }
  111. try {
  112. HttpURLConnection connection = (HttpURLConnection) new URL(String.format(NAME_URL, UUIDTypeAdapter.fromUUID(uuid))).openConnection();
  113. connection.setReadTimeout(5000);
  114. UUIDFetcher[] nameHistory = gson.fromJson(new BufferedReader(new InputStreamReader(connection.getInputStream())), UUIDFetcher[].class);
  115. UUIDFetcher currentNameData = nameHistory[nameHistory.length - 1];
  116. uuidCache.put(currentNameData.name.toLowerCase(), uuid);
  117. nameCache.put(uuid, currentNameData.name);
  118. return currentNameData.name;
  119. } catch (Exception e) {
  120. e.printStackTrace();
  121. }
  122. return null;
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement