Skip to content

Commit

Permalink
move invalidly placed methods to the right place
Browse files Browse the repository at this point in the history
  • Loading branch information
derklaro committed May 21, 2023
1 parent d3d76a1 commit ad09fa8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ public DefaultCachedProfileResolver(@NotNull ProfileResolver delegate) {
this.delegate = delegate;
}

private static @Nullable <K, V> V findCacheEntry(@NotNull Map<K, CacheEntry<V>> cache, @NotNull K key) {
// check if an entry is associated with the given key
CacheEntry<V> entry = cache.get(key);
if (entry == null) {
return null;
}

// check if the entry is outdated
if (entry.timeoutTime <= System.currentTimeMillis()) {
cache.remove(key);
return null;
}

// all fine
return entry.value;
}

@Override
public @NotNull CompletableFuture<Profile.Resolved> resolveProfile(@NotNull Profile profile) {
// check if we can get the profile instantly from the cache
Expand Down Expand Up @@ -101,23 +118,6 @@ public DefaultCachedProfileResolver(@NotNull ProfileResolver delegate) {
return null;
}

private static @Nullable <K, V> V findCacheEntry(@NotNull Map<K, CacheEntry<V>> cache, @NotNull K key) {
// check if an entry is associated with the given key
CacheEntry<V> entry = cache.get(key);
if (entry == null) {
return null;
}

// check if the entry is outdated
if (entry.timeoutTime <= System.currentTimeMillis()) {
cache.remove(key);
return null;
}

// all fine
return entry.value;
}

private static final class CacheEntry<T> {

private final T value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,6 @@ final class MojangProfileResolver implements ProfileResolver {
private static final String NAME_TO_UUID_ENDPOINT = "https://api.mojang.com/users/profiles/minecraft/%s";
private static final String UUID_TO_PROFILE_ENDPOINT = "https://sessionserver.mojang.com/session/minecraft/profile/%s";

@Override
public @NotNull CompletableFuture<Profile.Resolved> resolveProfile(@NotNull Profile profile) {
return CompletableFuture.supplyAsync(Util.callableToSupplier(() -> {
// check if we need to resolve the uuid of the profile
UUID uniqueId = profile.uniqueId();
if (uniqueId == null) {
// this will give us either a valid object or throw an exception
JsonObject responseData = makeRequest(String.format(NAME_TO_UUID_ENDPOINT, profile.name()));
String rawUniqueId = responseData.get("id").getAsString();

// insert dashes into the unique id string we get to parse it
String dashedId = UUID_DASHER_PATTERN.matcher(rawUniqueId).replaceAll("$1-$2-$3-$4-$5");
uniqueId = UUID.fromString(dashedId);
}

// now as the unique id is present we can send the request to get the all the other information about the profile
String profileId = UUID_NO_DASH_PATTERN.matcher(uniqueId.toString()).replaceAll("");
JsonObject responseData = makeRequest(String.format(UUID_TO_PROFILE_ENDPOINT, profileId));

// get the name of the player
String name = responseData.get("name").getAsString();
Set<ProfileProperty> properties = GSON.fromJson(responseData.get("properties"), PROFILE_PROPERTIES_TYPE);

// create the profile from the received data
return Profile.resolved(name, uniqueId, properties);
}));
}

private static @NotNull JsonObject makeRequest(@NotNull String endpoint) throws IOException {
HttpURLConnection connection = createBaseConnection(endpoint);

Expand Down Expand Up @@ -160,6 +132,34 @@ final class MojangProfileResolver implements ProfileResolver {
return connection;
}

@Override
public @NotNull CompletableFuture<Profile.Resolved> resolveProfile(@NotNull Profile profile) {
return CompletableFuture.supplyAsync(Util.callableToSupplier(() -> {
// check if we need to resolve the uuid of the profile
UUID uniqueId = profile.uniqueId();
if (uniqueId == null) {
// this will give us either a valid object or throw an exception
JsonObject responseData = makeRequest(String.format(NAME_TO_UUID_ENDPOINT, profile.name()));
String rawUniqueId = responseData.get("id").getAsString();

// insert dashes into the unique id string we get to parse it
String dashedId = UUID_DASHER_PATTERN.matcher(rawUniqueId).replaceAll("$1-$2-$3-$4-$5");
uniqueId = UUID.fromString(dashedId);
}

// now as the unique id is present we can send the request to get the all the other information about the profile
String profileId = UUID_NO_DASH_PATTERN.matcher(uniqueId.toString()).replaceAll("");
JsonObject responseData = makeRequest(String.format(UUID_TO_PROFILE_ENDPOINT, profileId));

// get the name of the player
String name = responseData.get("name").getAsString();
Set<ProfileProperty> properties = GSON.fromJson(responseData.get("properties"), PROFILE_PROPERTIES_TYPE);

// create the profile from the received data
return Profile.resolved(name, uniqueId, properties);
}));
}

private static final class ProfilePropertyTypeAdapter extends TypeAdapter<ProfileProperty> {

@Override
Expand Down

0 comments on commit ad09fa8

Please sign in to comment.