Last active
August 29, 2015 14:18
-
-
Save codebucketdev/e8e141aa2c675fb2e96f to your computer and use it in GitHub Desktop.
This is an example of my UUID Fetcher in Java using the Public API from Razex.de
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.BufferedReader; | |
| import java.io.InputStreamReader; | |
| import java.net.HttpURLConnection; | |
| import java.net.URL; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| import java.util.UUID; | |
| import java.util.concurrent.ExecutorService; | |
| import java.util.concurrent.Executors; | |
| import org.bukkit.craftbukkit.libs.com.google.gson.Gson; | |
| import org.bukkit.craftbukkit.libs.com.google.gson.GsonBuilder; | |
| import com.mojang.util.UUIDTypeAdapter; | |
| public class UUIDFetcher | |
| { | |
| private static Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create(); | |
| private static final String REQUEST_URL = "https://api.razex.de/user/profile/%s"; | |
| private static Map<UUID, CachedProfile> cache = new HashMap<UUID, CachedProfile>(); | |
| private static long cacheTime = -1; | |
| private static ExecutorService pool = Executors.newCachedThreadPool(); | |
| public static void getUniqueId(final String username, Consumer<UUID> action) | |
| { | |
| pool.execute(new Acceptor<UUID>(action) | |
| { | |
| @Override | |
| public UUID getValue() | |
| { | |
| return getUniqueId(username); | |
| } | |
| }); | |
| } | |
| public static UUID getUniqueId(String username) | |
| { | |
| for (CachedProfile profile : cache.values()) | |
| { | |
| if (profile.getUsername().equalsIgnoreCase(username) && profile.isValid()) | |
| { | |
| return profile.getUUID(); | |
| } | |
| } | |
| try | |
| { | |
| HttpURLConnection connection = (HttpURLConnection) new URL(String.format(REQUEST_URL, username)).openConnection(); | |
| connection.setReadTimeout(5000); | |
| InputStreamReader response = new InputStreamReader(connection.getInputStream()); | |
| CachedProfile profile = gson.fromJson(new BufferedReader(response), CachedProfile.class); | |
| cache.put(profile.getUUID(), profile); | |
| return profile.getUUID(); | |
| } | |
| catch (Exception ex) | |
| { | |
| ex.printStackTrace(); | |
| } | |
| return null; | |
| } | |
| public static void getUsername(final UUID uuid, Consumer<String> action) | |
| { | |
| pool.execute(new Acceptor<String>(action) | |
| { | |
| @Override | |
| public String getValue() | |
| { | |
| return getUsername(uuid); | |
| } | |
| }); | |
| } | |
| public static String getUsername(UUID uuid) | |
| { | |
| for (CachedProfile profile : cache.values()) | |
| { | |
| if (profile.getUUID().equals(uuid) && profile.isValid()) | |
| { | |
| return profile.getUsername(); | |
| } | |
| } | |
| try | |
| { | |
| HttpURLConnection connection = (HttpURLConnection) new URL(String.format(REQUEST_URL, UUIDTypeAdapter.fromUUID(uuid))).openConnection(); | |
| connection.setReadTimeout(5000); | |
| InputStreamReader response = new InputStreamReader(connection.getInputStream()); | |
| CachedProfile profile = gson.fromJson(new BufferedReader(response), CachedProfile.class); | |
| cache.put(profile.getUUID(), profile); | |
| return profile.getUsername(); | |
| } | |
| catch (Exception ex) | |
| { | |
| ex.printStackTrace(); | |
| } | |
| return null; | |
| } | |
| public static void setCacheTime(long time) | |
| { | |
| UUIDFetcher.cacheTime = time; | |
| } | |
| public static interface Consumer<T> | |
| { | |
| void accept(T t); | |
| } | |
| public static abstract class Acceptor<T> implements Runnable | |
| { | |
| private Consumer<T> consumer; | |
| public Acceptor(Consumer<T> consumer) | |
| { | |
| this.consumer = consumer; | |
| } | |
| public abstract T getValue(); | |
| @Override | |
| public void run() | |
| { | |
| consumer.accept(getValue()); | |
| } | |
| } | |
| private static class CachedProfile | |
| { | |
| private long timestamp = System.currentTimeMillis(); | |
| private UUID uuid; | |
| private String username; | |
| @SuppressWarnings("unused") | |
| public CachedProfile(UUID uuid, String username) | |
| { | |
| this.uuid = uuid; | |
| this.username = username; | |
| } | |
| public UUID getUUID() | |
| { | |
| return this.uuid; | |
| } | |
| public String getUsername() | |
| { | |
| return this.username; | |
| } | |
| public boolean isValid() | |
| { | |
| return cacheTime < 0 ? true : (System.currentTimeMillis() - timestamp) < cacheTime; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment