Skip to content

Commit

Permalink
Handle rate limits (#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
AzureAaron authored Jan 20, 2024
1 parent d581794 commit 301c827
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/de/hysky/skyblocker/utils/ApiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public static void init() {
* Multithreading is to be handled by the method caller
*/
public static String name2Uuid(String name) {
return name2Uuid(name, 0);
}

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

if (session.getUsername().equals(name)) return UndashedUuid.toString(session.getUuidOrNull());
Expand All @@ -43,6 +47,10 @@ public static String name2Uuid(String name) {
NAME_2_UUID_CACHE.put(name, uuid);

return uuid;
} else if (response.ratelimited() && retries < 3) {
Thread.sleep(800);

return name2Uuid(name, ++retries);
}
} catch (Exception e) {
LOGGER.error("[Skyblocker] Name to uuid lookup failed! Name: {}", name, e);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/de/hysky/skyblocker/utils/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ public boolean ok() {
return statusCode == 200;
}

public boolean ratelimited() {
return statusCode == 429;
}

public boolean cached() {
return cacheStatus.equals("HIT");
}
Expand Down

0 comments on commit 301c827

Please sign in to comment.