|
| 1 | +package dev.dfonline.flint.util; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.google.gson.JsonParser; |
| 5 | +import dev.dfonline.flint.Flint; |
| 6 | +import dev.dfonline.flint.util.message.impl.prefix.InfoMessage; |
| 7 | +import net.fabricmc.loader.api.FabricLoader; |
| 8 | +import net.kyori.adventure.text.Component; |
| 9 | +import net.kyori.adventure.text.event.ClickEvent; |
| 10 | +import net.kyori.adventure.text.event.HoverEvent; |
| 11 | + |
| 12 | +import java.io.BufferedReader; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.io.InputStreamReader; |
| 16 | +import java.net.URI; |
| 17 | +import java.net.http.HttpClient; |
| 18 | +import java.net.http.HttpRequest; |
| 19 | +import java.net.http.HttpResponse; |
| 20 | + |
| 21 | +public final class FlintUpdate { |
| 22 | + |
| 23 | + private static final Logger LOGGER = Logger.of(FlintUpdate.class); |
| 24 | + private static final String MOD_REPOSITORY = "DFOnline/Flint"; |
| 25 | + private static final String MODRINTH_URL = "https://modrinth.com/mod/flint/versions"; |
| 26 | + private static final String MOD_VERSION = getCurrentVersion(); |
| 27 | + private static String latestVersion; |
| 28 | + |
| 29 | + private FlintUpdate() { |
| 30 | + } |
| 31 | + |
| 32 | + public static void fetchLatestRelease() { |
| 33 | + String url = String.format("https://api.github.com/repos/%s/releases/latest", MOD_REPOSITORY); |
| 34 | + |
| 35 | + try (HttpClient client = HttpClient.newHttpClient()) { |
| 36 | + HttpRequest request = HttpRequest.newBuilder() |
| 37 | + .uri(URI.create(url)) |
| 38 | + .build(); |
| 39 | + |
| 40 | + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) |
| 41 | + .thenApply(HttpResponse::body) |
| 42 | + .thenAccept(responseBody -> { |
| 43 | + JsonObject jsonResponse = JsonParser.parseString(responseBody).getAsJsonObject(); |
| 44 | + latestVersion = jsonResponse.get("tag_name").getAsString(); |
| 45 | + LOGGER.info("Latest version: {}", latestVersion); |
| 46 | + }) |
| 47 | + .exceptionally(e -> { |
| 48 | + LOGGER.error("Error while fetching version", e); |
| 49 | + return null; |
| 50 | + }); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private static String getCurrentVersion() { |
| 55 | + try (InputStream input = Flint.class.getClassLoader().getResourceAsStream("flint_version.txt")) { |
| 56 | + if (input == null) { |
| 57 | + return "unknown"; |
| 58 | + } |
| 59 | + try (BufferedReader reader = new BufferedReader(new InputStreamReader(input))) { |
| 60 | + return reader.readLine(); |
| 61 | + } |
| 62 | + } catch (IOException e) { |
| 63 | + if (FabricLoader.getInstance().isDevelopmentEnvironment()) { |
| 64 | + return "unknown"; |
| 65 | + } |
| 66 | + LOGGER.error("Failed to get mod version", e); |
| 67 | + return "error"; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + public static void sendUpdateMessage() { |
| 73 | + if (MOD_VERSION.equals(latestVersion)) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + // Latest version starts with a v. |
| 79 | + int versionInt = Integer.parseInt(latestVersion.substring(1)); |
| 80 | + // Current version does not start with a v. |
| 81 | + int modInt = Integer.parseInt(MOD_VERSION); |
| 82 | + // The string parsed to an integer, now compare. |
| 83 | + if (versionInt <= modInt) { |
| 84 | + return; |
| 85 | + } |
| 86 | + // We are outdated, inform the user. |
| 87 | + if (Flint.getClient().player != null) { |
| 88 | + Flint.getUser().sendMessage(new InfoMessage("flint.update", |
| 89 | + Component.text("v" + MOD_VERSION), |
| 90 | + Component.text(latestVersion), |
| 91 | + Component.translatable("flint.update.link", PaletteColor.SKY_LIGHT_2) |
| 92 | + .clickEvent(ClickEvent.openUrl(MODRINTH_URL)) |
| 93 | + .hoverEvent(HoverEvent.showText(Component.text(MODRINTH_URL, PaletteColor.GRAY_LIGHT))) |
| 94 | + )); |
| 95 | + } |
| 96 | + } catch (NumberFormatException ignored) { |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments