package tech.nevets.jaml.util; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import org.apache.commons.io.FileUtils; import tech.nevets.jaml.JAML; import tech.nevets.jaml.objects.Version; import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; public class VersionFetcher { public static Version getVersion(String version) { Gson gson = new Gson(); JsonObject versionManifestJson = getList(); JsonArray versionsJsonArray = versionManifestJson.getAsJsonArray("versions"); Version[] versionArray = gson.fromJson(versionsJsonArray, Version[].class); for (Version value : versionArray) { if (value.getId().equalsIgnoreCase(version)) { return value; } } return null; } public static Version getVersion(String version, String type) { Gson gson = new Gson(); JsonObject versionManifestJson = getList(); JsonArray versionsJsonArray = versionManifestJson.getAsJsonArray("versions"); Version[] versionArray = gson.fromJson(versionsJsonArray, Version[].class); for (Version value : versionArray) { if (value.getType().equalsIgnoreCase(type)) { if (value.getId().equalsIgnoreCase(version)) { return value; } } } return null; } /** * * @param branch, release or snapshot * @return * @throws IOException */ public static String getLatestVersion(String branch) { JsonObject versionManifestJson = getList(); JsonObject latestJsonObject = versionManifestJson.get("latest").getAsJsonObject(); return latestJsonObject.get(branch).getAsString(); } @SuppressWarnings("Deprecated") public static JsonObject getList() { try { URL versionManifestUrl = new URL("https://launchermeta.mojang.com/mc/game/version_manifest.json"); FileUtils.copyURLToFile(versionManifestUrl, new File(JAML.path + "\\data\\version.json")); File versionManifestFile = new File(JAML.path + "\\data\\version.json"); return new Gson().fromJson(FileUtils.readFileToString(versionManifestFile, StandardCharsets.UTF_8), JsonObject.class); } catch (IOException e) { e.printStackTrace(); return null; } } }