2021-01-06 19:02:35 +01:00
|
|
|
package net.minestom.server.utils.mojang;
|
|
|
|
|
2021-03-05 23:03:18 +01:00
|
|
|
import com.google.common.cache.Cache;
|
|
|
|
import com.google.common.cache.CacheBuilder;
|
2021-01-06 19:02:35 +01:00
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.JsonParser;
|
2021-01-19 18:25:54 +01:00
|
|
|
import net.minestom.server.MinecraftServer;
|
2021-01-06 19:02:35 +01:00
|
|
|
import net.minestom.server.utils.url.URLUtils;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2021-03-05 23:03:18 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2021-01-06 19:02:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Utils class using mojang API.
|
|
|
|
*/
|
|
|
|
public final class MojangUtils {
|
|
|
|
|
2021-03-05 23:03:18 +01:00
|
|
|
private static final Cache<String, JsonObject> UUID_CACHE = CacheBuilder.newBuilder()
|
|
|
|
.expireAfterWrite(30, TimeUnit.SECONDS)
|
|
|
|
.softValues()
|
|
|
|
.build();
|
|
|
|
|
|
|
|
private static final Cache<String, JsonObject> USERNAME_CACHE = CacheBuilder.newBuilder()
|
|
|
|
.expireAfterWrite(30, TimeUnit.SECONDS)
|
|
|
|
.softValues()
|
|
|
|
.build();
|
|
|
|
|
2021-01-06 19:02:35 +01:00
|
|
|
@Nullable
|
|
|
|
public static JsonObject fromUuid(@NotNull String uuid) {
|
2021-03-05 23:03:18 +01:00
|
|
|
|
|
|
|
// Check cache
|
|
|
|
{
|
|
|
|
final JsonObject jsonObject = UUID_CACHE.getIfPresent(uuid);
|
|
|
|
if (jsonObject != null) {
|
|
|
|
return jsonObject;
|
|
|
|
}
|
2021-01-06 19:02:35 +01:00
|
|
|
}
|
2021-03-05 23:03:18 +01:00
|
|
|
|
|
|
|
// Retrieve from the rate-limited Mojang API
|
|
|
|
final String url = "https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false";
|
|
|
|
return retrieve(url, uuid, UUID_CACHE);
|
2021-01-06 19:02:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
public static JsonObject fromUsername(@NotNull String username) {
|
2021-03-05 23:03:18 +01:00
|
|
|
|
|
|
|
// Check cache
|
|
|
|
{
|
|
|
|
final JsonObject jsonObject = USERNAME_CACHE.getIfPresent(username);
|
|
|
|
if (jsonObject != null) {
|
|
|
|
return jsonObject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve from the rate-limited Mojang API
|
2021-01-06 19:02:35 +01:00
|
|
|
final String url = "https://api.mojang.com/users/profiles/minecraft/" + username;
|
2021-03-05 23:03:18 +01:00
|
|
|
return retrieve(url, username, USERNAME_CACHE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
private static JsonObject retrieve(@NotNull String url, @NotNull String key,
|
|
|
|
@NotNull Cache<String, JsonObject> cache) {
|
2021-01-06 19:02:35 +01:00
|
|
|
try {
|
|
|
|
final String response = URLUtils.getText(url);
|
2021-03-05 23:03:18 +01:00
|
|
|
final JsonObject jsonObject = JsonParser.parseString(response).getAsJsonObject();
|
|
|
|
cache.put(key, jsonObject);
|
|
|
|
return jsonObject;
|
2021-01-06 19:02:35 +01:00
|
|
|
} catch (IOException e) {
|
2021-01-19 18:25:54 +01:00
|
|
|
MinecraftServer.getExceptionManager().handleException(e);
|
2021-01-06 19:02:35 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|