Resclone/src/main/java/io/gitlab/jfronny/resclone/fetchers/CurseforgeFetcher.java

51 lines
1.7 KiB
Java
Raw Normal View History

2020-12-29 16:14:53 +01:00
package io.gitlab.jfronny.resclone.fetchers;
2021-05-07 17:39:13 +02:00
import io.gitlab.jfronny.resclone.Resclone;
2021-02-19 12:39:22 +01:00
import io.gitlab.jfronny.resclone.data.CfAddon;
2021-04-03 02:40:38 +02:00
import io.gitlab.jfronny.resclone.util.UrlUtils;
2021-02-19 12:39:22 +01:00
import net.minecraft.MinecraftVersion;
import java.text.SimpleDateFormat;
import java.util.Date;
2020-12-29 16:14:53 +01:00
public class CurseforgeFetcher extends PackFetcher {
2021-04-03 02:40:38 +02:00
2020-12-29 16:14:53 +01:00
@Override
public String getSourceTypeName() {
return "curseforge";
}
@Override
2021-04-03 02:40:38 +02:00
public String getDownloadUrl(String baseUrl) throws Exception {
2020-12-29 16:14:53 +01:00
try {
2021-02-19 12:39:22 +01:00
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
String version = MinecraftVersion.field_25319.getName();
CfAddon latest = null;
Date latestDate = null;
boolean foundMatchingVersion = false;
2021-04-03 02:40:38 +02:00
for (CfAddon addon : UrlUtils.readJsonFromURLSet("https://addons-ecs.forgesvc.net/api/v2/addon/" + baseUrl + "/files", CfAddon.class)) {
2021-02-19 12:39:22 +01:00
Date d = df.parse(addon.fileDate);
if (foundMatchingVersion && !addon.gameVersion.contains(version))
continue;
if (!foundMatchingVersion && addon.gameVersion.contains(version)) {
foundMatchingVersion = true;
latest = null;
}
if (latest == null || d.after(latestDate)) {
latest = addon;
latestDate = d;
}
}
if (!foundMatchingVersion)
2021-05-07 17:39:13 +02:00
Resclone.LOGGER.error("Could not find pack for matching version, using latest");
2021-02-19 12:39:22 +01:00
return latest.downloadUrl;
2020-12-29 16:14:53 +01:00
} catch (Throwable e) {
2021-04-03 02:40:38 +02:00
throw new Exception("Could not get CF download for " + baseUrl, e);
2020-12-29 16:14:53 +01:00
}
}
2021-04-03 02:40:38 +02:00
}