Remove mojang utils cache

This commit is contained in:
themode 2024-09-05 01:33:58 +02:00
parent c24f3a5d99
commit 6e0ee029a6

View File

@ -1,10 +1,7 @@
package net.minestom.server.utils.mojang;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.minestom.server.MinecraftServer;
import net.minestom.server.utils.url.URLUtils;
import org.jetbrains.annotations.Blocking;
import org.jetbrains.annotations.NotNull;
@ -12,7 +9,6 @@ import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
* Utils class using mojang API.
@ -20,13 +16,10 @@ import java.util.concurrent.TimeUnit;
public final class MojangUtils {
private static final String FROM_UUID_URL = "https://sessionserver.mojang.com/session/minecraft/profile/%s?unsigned=false";
private static final String FROM_USERNAME_URL = "https://api.mojang.com/users/profiles/minecraft/%s";
private static final Cache<String, JsonObject> URL_CACHE = Caffeine.newBuilder()
.expireAfterWrite(30, TimeUnit.SECONDS)
.softValues()
.build();
/**
* Gets a player's UUID from their username
*
* @param username The players username
* @return The {@link UUID}
* @throws IOException with text detailing the exception
@ -46,6 +39,7 @@ public final class MojangUtils {
/**
* Gets a player's username from their UUID
*
* @param playerUUID The {@link UUID} of the player
* @return The player's username
* @throws IOException with text detailing the exception
@ -57,6 +51,7 @@ public final class MojangUtils {
/**
* Gets a {@link JsonObject} with the response from the mojang API
*
* @param uuid The UUID as a {@link UUID}
* @return The {@link JsonObject} or {@code null} if the mojang API is down or the UUID is invalid
*/
@ -67,6 +62,7 @@ public final class MojangUtils {
/**
* Gets a {@link JsonObject} with the response from the mojang API
*
* @param uuid The UUID as a {@link String}
* @return The {@link JsonObject} or {@code null} if the mojang API is down or the UUID is invalid
*/
@ -81,6 +77,7 @@ public final class MojangUtils {
/**
* Gets a {@link JsonObject} with the response from the mojang API
*
* @param username The username as a {@link String}
* @return The {@link JsonObject} or {@code null} if the mojang API is down or the username is invalid
*/
@ -95,37 +92,21 @@ public final class MojangUtils {
/**
* Gets the JsonObject from a URL, expects a mojang player URL so the errors might not make sense if it is not
*
* @param url The url to retrieve
* @return The {@link JsonObject} of the result
* @throws IOException with the text detailing the exception
*/
private static @NotNull JsonObject retrieve(@NotNull String url) throws IOException {
@Nullable final var cacheResult = URL_CACHE.getIfPresent(url);
if (cacheResult != null) {
return cacheResult;
}
final String response;
try {
// Retrieve from the rate-limited Mojang API
response = URLUtils.getText(url);
} catch (IOException e) {
MinecraftServer.getExceptionManager().handleException(e);
throw new RuntimeException(e);
}
// Retrieve from the rate-limited Mojang API
final String response = URLUtils.getText(url);
// If our response is "", that means the url did not get a proper object from the url
// So the username or UUID was invalid, and therefore we return null
if (response.isEmpty()) {
throw new IOException("The Mojang API is down");
}
if (response.isEmpty()) throw new IOException("The Mojang API is down");
JsonObject jsonObject = JsonParser.parseString(response).getAsJsonObject();
if (jsonObject.has("errorMessage")) {
throw new IOException(jsonObject.get("errorMessage").getAsString());
}
URL_CACHE.put(url, jsonObject);
return jsonObject;
}
}