aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/utils/ApiUtils.java
blob: 6ce5dca1f90c253bad1f6e4eadfcd88249f15f3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package de.hysky.skyblocker.utils;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.gson.JsonParser;
import com.mojang.util.UndashedUuid;

import de.hysky.skyblocker.utils.Http.ApiResponse;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.session.Session;

/*
 * Contains only basic helpers for using Http APIs
 */
public class ApiUtils {
	private static final Logger LOGGER = LoggerFactory.getLogger(ApiUtils.class);
	/**
	 * Similar to how the Auth Lib caches GameProfiles.
	 */
	private static final LoadingCache<String, String> NAME_2_UUID_CACHE = CacheBuilder.newBuilder()
			.expireAfterWrite(20, TimeUnit.MINUTES)
			.build(new CacheLoader<>() {
				@Override
				public String load(String key) throws Exception {
					return name2UuidInternal(key, 0);
				}
			});

	/**
	 * Multithreading is to be handled by the method caller
	 */
	public static String name2Uuid(String name) {
		return NAME_2_UUID_CACHE.getUnchecked(name);
	}

	private static String name2UuidInternal(String name, int retries) {
		Session session = MinecraftClient.getInstance().getSession();

		if (session.getUsername().equalsIgnoreCase(name)) {
			return UndashedUuid.toString(session.getUuidOrNull());
		}

		try (ApiResponse response = Http.sendName2UuidRequest(name)) {
			if (response.ok()) {
				return JsonParser.parseString(response.content()).getAsJsonObject().get("id").getAsString();
			} else if (response.ratelimited() && retries < 3) {
				Thread.sleep(800);

				return name2UuidInternal(name, ++retries);
			}
		} catch (Exception e) {
			LOGGER.error("[Skyblocker] Name to uuid lookup failed! Name: {}", name, e);
		}

		return "";
	}
}