diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java index f830a76..bf90a41 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java @@ -12,6 +12,7 @@ import com.google.common.io.ByteStreams; import com.google.gson.Gson; import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent; import com.imaginarycode.minecraft.redisbungee.util.NameFetcher; +import com.imaginarycode.minecraft.redisbungee.util.UUIDFetcher; import com.imaginarycode.minecraft.redisbungee.util.UUIDTranslator; import com.squareup.okhttp.OkHttpClient; import lombok.Getter; @@ -434,6 +435,7 @@ public final class RedisBungee extends Plugin { service = Executors.newFixedThreadPool(16); httpClient = new OkHttpClient(); NameFetcher.setHttpClient(httpClient); + UUIDFetcher.setHttpClient(httpClient); return null; } }); diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDFetcher.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDFetcher.java index 741e1db..78821d1 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDFetcher.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDFetcher.java @@ -9,8 +9,10 @@ package com.imaginarycode.minecraft.redisbungee.util; import com.google.common.collect.ImmutableList; import com.imaginarycode.minecraft.redisbungee.RedisBungee; import com.squareup.okhttp.MediaType; +import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.RequestBody; +import lombok.Setter; import java.util.*; import java.util.concurrent.Callable; @@ -23,6 +25,9 @@ public class UUIDFetcher implements Callable> { private final List names; private final boolean rateLimiting; + @Setter + private static OkHttpClient httpClient; + public UUIDFetcher(List names, boolean rateLimiting) { this.names = ImmutableList.copyOf(names); this.rateLimiting = rateLimiting; @@ -42,7 +47,7 @@ public class UUIDFetcher implements Callable> { for (int i = 0; i < requests; i++) { String body = RedisBungee.getGson().toJson(names.subList(i * 100, Math.min((i + 1) * 100, names.size()))); Request request = new Request.Builder().url(PROFILE_URL).post(RequestBody.create(JSON, body)).build(); - String response = RedisBungee.getHttpClient().newCall(request).execute().body().string(); + String response = httpClient.newCall(request).execute().body().string(); Profile[] array = RedisBungee.getGson().fromJson(response, Profile[].class); for (Profile profile : array) { UUID uuid = UUIDFetcher.getUUID(profile.id); diff --git a/src/test/java/com/imaginarycode/minecraft/redisbungee/test/UUIDNameTest.java b/src/test/java/com/imaginarycode/minecraft/redisbungee/test/UUIDNameTest.java index d9e399f..244187a 100644 --- a/src/test/java/com/imaginarycode/minecraft/redisbungee/test/UUIDNameTest.java +++ b/src/test/java/com/imaginarycode/minecraft/redisbungee/test/UUIDNameTest.java @@ -6,16 +6,42 @@ import com.squareup.okhttp.OkHttpClient; import org.junit.Test; import java.io.IOException; +import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.UUID; public class UUIDNameTest { + private String[] uuidsToTest = {"68ec43f7234b41b48764dfb38b9ffe8c", "652a2bc4e8cd405db7b698156ee2dc09"}; + private String[] namesToTest = {"vemacs"}; + @Test public void testUuidToName() throws IOException { OkHttpClient httpClient = new OkHttpClient(); - String uuid = "68ec43f7234b41b48764dfb38b9ffe8c"; NameFetcher.setHttpClient(httpClient); - List names = NameFetcher.nameHistoryFromUuid(UUIDFetcher.getUUID(uuid)); - String currentName = names.get(names.size() - 1); - System.out.println("Current name for UUID " + uuid + " is " + currentName); + for (String uuid : uuidsToTest) { + List names = NameFetcher.nameHistoryFromUuid(UUIDFetcher.getUUID(uuid)); + String currentName = names.get(names.size() - 1); + System.out.println("Current name for UUID " + uuid + " is " + currentName); + } + } + + @Test + public void testNameToUuid() throws IOException { + OkHttpClient httpClient = new OkHttpClient(); + UUIDFetcher.setHttpClient(httpClient); + for (String name : namesToTest) { + Map uuidMap1; + try { + uuidMap1 = new UUIDFetcher(Collections.singletonList(name)).call(); + for (Map.Entry entry : uuidMap1.entrySet()) { + if (entry.getKey().equalsIgnoreCase(name)) { + System.out.println("Current UUID for name " + name + " is " + entry.getValue()); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } } }