Skip to content

Commit

Permalink
Merge pull request #129 from TacoMonkey11/update_checker
Browse files Browse the repository at this point in the history
port UpdateChecker to java.net.http
  • Loading branch information
KonaeAkira authored Apr 3, 2023
2 parents 4fdf580 + 56ebe5d commit 30cba58
Showing 1 changed file with 45 additions and 42 deletions.
87 changes: 45 additions & 42 deletions src/main/java/me/xmrvizzy/skyblocker/utils/UpdateChecker.java
Original file line number Diff line number Diff line change
@@ -1,64 +1,67 @@
package me.xmrvizzy.skyblocker.utils;

import com.google.gson.*;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import me.xmrvizzy.skyblocker.SkyblockerMod;
import me.xmrvizzy.skyblocker.config.SkyblockerConfig;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.*;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.HoverEvent;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import org.spongepowered.asm.util.VersionNumber;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UpdateChecker {
public static boolean shouldUpdate = false;
public static final Pattern pattern = Pattern.compile("v(\\d+)\\.(\\d+)\\.(\\d+)");
public static final Pattern localPattern = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)");
public static final Pattern pattern = Pattern.compile("v(.*)\\+.*");
public static final Pattern localPattern = Pattern.compile("(.*)\\+.*");
public static Matcher matcher;
public static VersionNumber localVersion = null;
public static VersionNumber latestVersion = null;
public static boolean shouldUpdate(){
if (SkyblockerConfig.get().general.enableUpdateNotification){
new Thread(() -> {
try{
URL url = new URL("https://api.modrinth.com/v2/project/skyblocker-liap/version");
private static VersionNumber latestVersion = null;
private static VersionNumber localVersion = null;
public static void shouldUpdate(){
if (!SkyblockerConfig.get().general.enableUpdateNotification) return;
try {
URI uri = new URI("https://api.modrinth.com/v2/project/skyblocker-liap/version");
HttpRequest request = HttpRequest.newBuilder(uri).GET().build();
CompletableFuture<HttpResponse<String>> response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString());
response.thenAccept(httpResponse -> {
JsonObject versionJson = new Gson().fromJson(httpResponse.body(), JsonElement.class).getAsJsonArray().get(0).getAsJsonObject();
matcher = pattern.matcher(versionJson.get("version_number").getAsString());
if (matcher.find()) {
latestVersion = VersionNumber.parse(matcher.group(1));
}
matcher = localPattern.matcher(FabricLoader.getInstance().getModContainer(SkyblockerMod.NAMESPACE).get().getMetadata().getVersion().getFriendlyString());
if (matcher.find()) {
localVersion = VersionNumber.parse(matcher.group(1));
}
if (latestVersion != null && localVersion != null) {
if (localVersion.compareTo(latestVersion) < 0) {
MutableText linkMessage = Text.translatable("skyblocker.update.update_message");
MutableText linkMessageEnding = Text.translatable("skyblocker.update.update_message_end");
MutableText link = Text.translatable("skyblocker.update.update_link");
MutableText hoverText = Text.translatable("skyblocker.update.hover_text");
linkMessage.append(link.styled(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://modrinth.com/mod/skyblocker-liap/versions")).withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverText)))).append(linkMessageEnding);

InputStreamReader reader = new InputStreamReader(url.openStream());
JsonObject versionJson = new Gson().fromJson(reader, JsonElement.class).getAsJsonArray().get(0).getAsJsonObject();
matcher = pattern.matcher(versionJson.get("version_number").getAsString());
if (matcher.find()){
latestVersion = VersionNumber.parse(matcher.group(1) + "." + matcher.group(2) + "." + matcher.group(3));
}
matcher = localPattern.matcher(FabricLoader.getInstance().getModContainer(SkyblockerMod.NAMESPACE).get().getMetadata().getVersion().getFriendlyString());
if (matcher.find()){
localVersion = VersionNumber.parse(matcher.group(1) + "." + matcher.group(2) + "." + matcher.group(3));
MinecraftClient.getInstance().player.sendMessage(linkMessage, false);
}
if (localVersion != null && latestVersion != null)
if (localVersion.compareTo(latestVersion) < 0) shouldUpdate = true;

} catch (IOException e) {
e.printStackTrace();
}
}).start();
});
} catch (URISyntaxException e) {
e.printStackTrace();
}
return shouldUpdate;
}

public static void init(){
SkyblockEvents.JOIN.register(() -> {
if (shouldUpdate()) {
MutableText linkMessage = Text.translatable("skyblocker.update.update_message");
MutableText linkMessageEnding = Text.translatable("skyblocker.update.update_message_end");
MutableText link = Text.translatable("skyblocker.update.update_link");
MutableText hoverText = Text.translatable("skyblocker.update.hover_text");
linkMessage.append(link.styled(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://modrinth.com/mod/skyblocker-liap/versions")).withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverText)))).append(linkMessageEnding);

MinecraftClient.getInstance().player.sendMessage(linkMessage, false);
}
});
SkyblockEvents.JOIN.register(UpdateChecker::shouldUpdate);
}
}

0 comments on commit 30cba58

Please sign in to comment.