-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
37 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
8 changes: 6 additions & 2 deletions
8
src/main/java/eu/beezig/hiveapi/wrapper/mojang/UsernameToUuid.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,19 @@ | ||
package eu.beezig.hiveapi.wrapper.mojang; | ||
|
||
import eu.beezig.hiveapi.wrapper.HiveWrapper; | ||
import eu.beezig.hiveapi.wrapper.utils.download.UrlBuilder; | ||
import eu.beezig.hiveapi.wrapper.utils.json.JObject; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class UsernameToUuid { | ||
|
||
public static CompletableFuture<String> getUUID(String username) { | ||
if(HiveWrapper.usernameToUUIDCache == null) return loadUUID(username); | ||
return HiveWrapper.usernameToUUIDCache.get(username, (k, exec) -> loadUUID(k)); | ||
} | ||
|
||
private static CompletableFuture<String> loadUUID(String username) { | ||
CompletableFuture<JObject> responseFromMojang = JObject.get(new UrlBuilder().mojang(username).build()); | ||
return responseFromMojang.thenApplyAsync(json -> json.getString("id")); | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package eu.beezig.hiveapi.wrapper.test; | ||
|
||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import eu.beezig.hiveapi.wrapper.HiveWrapper; | ||
import eu.beezig.hiveapi.wrapper.mojang.UsernameToUuid; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.concurrent.Executors; | ||
|
||
public class Cache { | ||
@BeforeAll | ||
public static void setup() { | ||
HiveWrapper.setAsyncExecutor(Executors.newSingleThreadExecutor()); | ||
HiveWrapper.setUUIDCache(Caffeine.newBuilder().buildAsync()); | ||
} | ||
|
||
@Test | ||
public void correctProfile() { | ||
assert "069a79f444e94726a5befca90e38aaf5".equals(UsernameToUuid.getUUID("Notch").join()); | ||
assert HiveWrapper.usernameToUUIDCache.synchronous().getIfPresent("Notch") != null; | ||
} | ||
} |