Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
Get modrinth id by first getting the mod by the fabric id
Browse files Browse the repository at this point in the history
Closes #18

Signed-off-by: DeathsGun <deathsgun@protonmail.com>
  • Loading branch information
DeathsGun committed Aug 24, 2021
1 parent 364a8fb commit 99b9eba
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ private ArrayList<SummarizedMod> getSummarizedMods(int page, int limit, URIBuild
uriBuilder.addParameter("version", String.format("versions=%s", MinecraftClient.getInstance().getGame().getVersion().getReleaseTarget()));
uriBuilder.addParameter("offset", String.valueOf(page * limit));
uriBuilder.addParameter("limit", String.valueOf(limit));
logger.info("Using {} for query", uriBuilder.toString());
HttpRequest request = HttpRequest.newBuilder().GET().setHeader("User-Agent", "ModManager " + ModManager.getVersion())
.uri(uriBuilder.build()).build();
HttpResponse<String> response = this.http.send(request, HttpResponse.BodyHandlers.ofString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ManipulationService extends Thread {
private final ArrayList<ManipulationTask> tasks = new ArrayList<>();

public ManipulationService() {
setName("ModMenu-M");
setName("ModMenu");
start();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class UpdateCheckService extends Thread {

public UpdateCheckService() {
super();
setName("ModManager-U");
setName("ModManager");
}

@Override
Expand All @@ -71,17 +71,27 @@ private void checkForUpdate(ModMetadata metadata) {
}
updates.add(new AvailableUpdates(modId, metadata.getId(), version));
} catch (Exception e) {
logger.error("Failed to check for updates for {}", metadata.getId());
logger.error("Failed to check for updates for {}: {}", metadata.getId(), e.getMessage());
}
}

private String findModId(ModMetadata container) throws Exception {
IModProvider provider = ModManager.getModProvider();
List<SummarizedMod> hits = provider.getMods(container.getName(), 0, 1);
try {
DetailedMod mod = provider.getMod(container.getId()); // Try mod id most of the time it's also the Modrinth slug
return mod.id();
} catch (Exception ignored) {
}
List<SummarizedMod> hits = provider.getMods(container.getName(), 0, 10);
if (hits.isEmpty()) {
throw new Exception(String.format("Mod %s not found on %s", container.getId(), provider.getName()));
}
return hits.get(0).id();
for (SummarizedMod hit : hits) {
if (hit.name().equals(container.getName())) {
return hit.id();
}
}
throw new Exception(String.format("Mod %s not found on %s", container.getId(), provider.getName()));
}

@Nullable
Expand All @@ -101,10 +111,11 @@ private ModVersion getUpdateVersion(String modId, ModMetadata container) throws
latestVersion = version;
}
}
if (latest == null || installedVersion.compareTo(latestVersion) > 0) {
if (latest == null || installedVersion.compareTo(latestVersion) >= 0) {
logger.info("No update for {} found!", container.getId());
return null;
}
logger.info("Found an update for {} {} new version: {}", container.getId(), container.getVersion().toString(), latestVersion.toString());
return latest;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected void execute() throws Exception {
if (subject == null) {
throw new Exception("Summarized mod is empty");
}
InstallationUtil.downloadMod(http, InstallationUtil.getVersionForMod(subject));
InstallationUtil.downloadMod(http, InstallationUtil.getInstallableVersionForMod(subject));
ModManager.getModManipulationManager().markManuallyInstalled(subject);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected void execute() throws Exception {
}
//TODO: Delete file directly
jar.toFile().deleteOnExit();
InstallationUtil.downloadMod(http, InstallationUtil.getVersionForMod(subject));
InstallationUtil.downloadMod(http, InstallationUtil.getInstallableVersionForMod(subject));
ModManager.getModManipulationManager().markManuallyUpdated(subject);
ModManager.getUpdateChecker().removeUpdate(subject.id());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import xyz.deathsgun.modmanager.api.mod.Asset;
import xyz.deathsgun.modmanager.api.mod.ModVersion;
import xyz.deathsgun.modmanager.api.mod.SummarizedMod;
import xyz.deathsgun.modmanager.api.mod.VersionType;
import xyz.deathsgun.modmanager.api.provider.IModProvider;

import java.net.URI;
Expand All @@ -21,10 +22,11 @@

public class InstallationUtil {

public static ModVersion getVersionForMod(SummarizedMod mod) throws Exception {
public static ModVersion getInstallableVersionForMod(SummarizedMod mod) throws Exception {
IModProvider provider = ModManager.getModProvider();
List<ModVersion> versions = provider.getVersionsForMod(mod.id()).stream()
.filter(value -> value.gameVersions().contains(MinecraftVersion.GAME_VERSION.getReleaseTarget())).collect(Collectors.toList());
.filter(value -> value.gameVersions().contains(MinecraftVersion.GAME_VERSION.getReleaseTarget()))
.filter(value -> value.type() == VersionType.RELEASE).collect(Collectors.toList());
ModVersion latest = null;
SemanticVersion latestVersion = null;
for (ModVersion modVersion : versions) {
Expand Down

0 comments on commit 99b9eba

Please sign in to comment.