package io.gitlab.jfronny.resclone.fetchers; import com.google.common.reflect.TypeToken; import io.gitlab.jfronny.commons.HttpUtils; import io.gitlab.jfronny.resclone.Resclone; import io.gitlab.jfronny.resclone.data.modrinth.Version; import net.minecraft.MinecraftVersion; import java.io.FileNotFoundException; import java.io.IOException; import java.lang.reflect.Type; import java.util.Date; import java.util.List; public class ModrinthFetcher extends BasePackFetcher { private static final Type versionType = new TypeToken>() {}.getType(); @Override public String getSourceTypeName() { return "modrinth"; } @Override public String getDownloadUrl(String baseUrl) throws Exception { try { String version = MinecraftVersion.CURRENT.getName(); Version latest = null; Date latestDate = null; boolean foundMatchingVersion = false; for (Version ver : HttpUtils.get("https://api.modrinth.com/v2/project/" + baseUrl + "/version") .userAgent(Resclone.USER_AGENT) .>sendSerialized(versionType)) { if (foundMatchingVersion && !ver.game_versions.contains(version)) continue; if (ver.files.isEmpty()) continue; if (!foundMatchingVersion && ver.game_versions.contains(version)) { foundMatchingVersion = true; latest = null; } if (latest == null || ver.date_published.after(latestDate)) { latest = ver; latestDate = ver.date_published; } } if (latest == null) throw new FileNotFoundException("Could not identify valid version"); if (!foundMatchingVersion) Resclone.LOGGER.error("Could not find matching version of " + baseUrl + ", using latest"); for (Version.File file : latest.files) { if (file.primary) return file.url; } Resclone.LOGGER.error("Could not identify primary file of " + baseUrl + ", attempting identification by file_type"); for (Version.File file : latest.files) { if (file.file_type == Version.File.Type.REQUIRED_RESOURCE_PACK) return file.url; } Resclone.LOGGER.error("Identification failed, using first file of " + baseUrl); return latest.files.get(0).url; } catch (Throwable e) { throw new IOException("Could not get Modrinth download for " + baseUrl, e); } } }