|
| 1 | +/* |
| 2 | + * Copyright © 2026 DSNS <dominic@seung.dev> |
| 3 | + * |
| 4 | + * This file is part of DolphinClient. |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + * |
| 20 | + * For more information, see the LICENSE file. |
| 21 | + */ |
| 22 | + |
| 23 | +package io.github.axolotlclient.modules.hypixel; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.net.URI; |
| 27 | +import java.net.http.HttpClient; |
| 28 | +import java.net.http.HttpRequest; |
| 29 | +import java.net.http.HttpResponse; |
| 30 | +import java.nio.charset.StandardCharsets; |
| 31 | +import java.time.Duration; |
| 32 | +import java.util.Optional; |
| 33 | + |
| 34 | +import io.github.axolotlclient.util.UUIDHelper; |
| 35 | + |
| 36 | +/** |
| 37 | + * Loads a player's Hypixel level from {@code https://hypixel.dsns.dev/level/{uuid}}. |
| 38 | + * The endpoint returns a single number and only responds when the user agent contains |
| 39 | + * {@code DolphinClient} followed by the mod version. |
| 40 | + */ |
| 41 | +public final class HypixelLevelClient { |
| 42 | + public static final String LEVEL_ENDPOINT = "https://hypixel.dsns.dev/level/"; |
| 43 | + private static final int MAX_ATTEMPTS = 3; |
| 44 | + |
| 45 | + private static final HttpClient HTTP = HttpClient.newBuilder() |
| 46 | + .connectTimeout(Duration.ofSeconds(5)) |
| 47 | + .followRedirects(HttpClient.Redirect.NORMAL) |
| 48 | + .build(); |
| 49 | + |
| 50 | + private HypixelLevelClient() { |
| 51 | + } |
| 52 | + |
| 53 | + public static String userAgent(String modVersion) { |
| 54 | + return "DolphinClient " + modVersion; |
| 55 | + } |
| 56 | + |
| 57 | + public static String formatLevel(Number level) { |
| 58 | + double value = level.doubleValue(); |
| 59 | + if (!Double.isFinite(value) || value < 0) { |
| 60 | + return "???"; |
| 61 | + } |
| 62 | + return Long.toString((long) Math.floor(value)); |
| 63 | + } |
| 64 | + |
| 65 | + public static String levelUrl(String uuid) { |
| 66 | + return LEVEL_ENDPOINT + dashedUuid(uuid); |
| 67 | + } |
| 68 | + |
| 69 | + static String dashedUuid(String uuid) { |
| 70 | + String trimmed = uuid.trim(); |
| 71 | + if (trimmed.indexOf('-') >= 0) { |
| 72 | + return trimmed; |
| 73 | + } |
| 74 | + return UUIDHelper.fromUndashed(trimmed).toString(); |
| 75 | + } |
| 76 | + |
| 77 | + public static Optional<Double> parseLevel(int status, String body) { |
| 78 | + if (status != 200 || body == null) { |
| 79 | + return Optional.empty(); |
| 80 | + } |
| 81 | + String trimmed = body.trim(); |
| 82 | + if (trimmed.isEmpty() || trimmed.charAt(0) == '{' || trimmed.charAt(0) == '<') { |
| 83 | + return Optional.empty(); |
| 84 | + } |
| 85 | + try { |
| 86 | + double level = Double.parseDouble(trimmed); |
| 87 | + if (!Double.isFinite(level) || level < 0) { |
| 88 | + return Optional.empty(); |
| 89 | + } |
| 90 | + return Optional.of(level); |
| 91 | + } catch (NumberFormatException ignored) { |
| 92 | + return Optional.empty(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public static Optional<Double> requestLevel(String uuid, String userAgent) { |
| 97 | + final HttpRequest request; |
| 98 | + try { |
| 99 | + request = HttpRequest.newBuilder() |
| 100 | + .uri(URI.create(levelUrl(uuid))) |
| 101 | + .timeout(Duration.ofSeconds(8)) |
| 102 | + .header("User-Agent", userAgent) |
| 103 | + .header("Accept", "text/plain, application/json") |
| 104 | + .GET() |
| 105 | + .build(); |
| 106 | + } catch (RuntimeException ignored) { |
| 107 | + return Optional.empty(); |
| 108 | + } |
| 109 | + |
| 110 | + for (int attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) { |
| 111 | + try { |
| 112 | + HttpResponse<String> response = HTTP.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)); |
| 113 | + int status = response.statusCode(); |
| 114 | + if ((status == 429 || status >= 500) && attempt < MAX_ATTEMPTS) { |
| 115 | + if (!pause(attempt)) { |
| 116 | + return Optional.empty(); |
| 117 | + } |
| 118 | + continue; |
| 119 | + } |
| 120 | + return parseLevel(status, response.body()); |
| 121 | + } catch (InterruptedException e) { |
| 122 | + Thread.currentThread().interrupt(); |
| 123 | + return Optional.empty(); |
| 124 | + } catch (IOException e) { |
| 125 | + if (attempt == MAX_ATTEMPTS || !pause(attempt)) { |
| 126 | + return Optional.empty(); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + return Optional.empty(); |
| 131 | + } |
| 132 | + |
| 133 | + private static boolean pause(int attempt) { |
| 134 | + try { |
| 135 | + Thread.sleep(200L * attempt); |
| 136 | + return true; |
| 137 | + } catch (InterruptedException e) { |
| 138 | + Thread.currentThread().interrupt(); |
| 139 | + return false; |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments