2323import me .itzg .helpers .errors .GenericException ;
2424import me .itzg .helpers .errors .InvalidParameterException ;
2525import me .itzg .helpers .files .Manifests ;
26+ import me .itzg .helpers .http .Fetch ;
2627import me .itzg .helpers .http .SharedFetchArgs ;
27- import me .itzg .helpers .http .Uris ;
2828import me .itzg .helpers .json .ObjectMappers ;
2929import me .itzg .helpers .modrinth .model .DependencyType ;
3030import me .itzg .helpers .modrinth .model .Project ;
@@ -109,6 +109,7 @@ private List<Path> processProjects(List<String> projects) {
109109 return
110110 modrinthApiClient .bulkGetProjects (
111111 projects .stream ()
112+ .filter (s -> !s .trim ().isEmpty ())
112113 .map (ProjectRef ::parse )
113114 )
114115 .defaultIfEmpty (Collections .emptyList ())
@@ -148,31 +149,28 @@ private Stream<Version> expandDependencies(ModrinthApiClient modrinthApiClient,
148149 .filter (dep -> projectsProcessed .add (dep .getProjectId ()))
149150 .flatMap (dep -> {
150151 projectsProcessed .add (dep .getProjectId ());
151- try {
152- final Version depVersion ;
153- if (dep .getVersionId () == null ) {
154- log .debug ("Fetching versions of dep={} and picking" , dep );
155- depVersion = pickVersion (
156- getVersionsForProject (modrinthApiClient , dep .getProjectId ())
157- );
158- }
159- else {
160- log .debug ("Fetching version for dep={}" , dep );
161- depVersion = getVersion (dep .getVersionId ());
162- }
163- if (depVersion != null ) {
164- log .debug ("Resolved version={} for dep={}" , depVersion .getVersionNumber (), dep );
165- return Stream .concat (
166- Stream .of (depVersion ),
167- expandDependencies (modrinthApiClient , depVersion )
168- )
169- .peek (expandedVer -> log .debug ("Expanded dependency={} into version={}" , dep , expandedVer ));
170- }
171- else {
172- return Stream .empty ();
173- }
174- } catch (IOException e ) {
175- throw new RuntimeException (e );
152+ final Version depVersion ;
153+ if (dep .getVersionId () == null ) {
154+ log .debug ("Fetching versions of dep={} and picking" , dep );
155+ depVersion = pickVersion (
156+ getVersionsForProject (modrinthApiClient , dep .getProjectId ())
157+ );
158+ }
159+ else {
160+ log .debug ("Fetching version for dep={}" , dep );
161+ depVersion = modrinthApiClient .getVersionFromId (dep .getVersionId ())
162+ .block ();
163+ }
164+ if (depVersion != null ) {
165+ log .debug ("Resolved version={} for dep={}" , depVersion .getVersionNumber (), dep );
166+ return Stream .concat (
167+ Stream .of (depVersion ),
168+ expandDependencies (modrinthApiClient , depVersion )
169+ )
170+ .peek (expandedVer -> log .debug ("Expanded dependency={} into version={}" , dep , expandedVer ));
171+ }
172+ else {
173+ return Stream .empty ();
176174 }
177175 });
178176
@@ -190,15 +188,6 @@ private boolean filterDependency(VersionDependency dep) {
190188 );
191189 }
192190
193- private Version getVersion (String versionId ) throws IOException {
194- return fetch (Uris .populateToUri (
195- baseUrl + "/v2/version/{id}" , versionId
196- ))
197- .userAgentCommand ("modrinth" )
198- .toObject (Version .class )
199- .execute ();
200- }
201-
202191 private Version pickVersion (List <Version > versions ) {
203192 return this .pickVersion (versions , defaultVersionType );
204193 }
@@ -213,13 +202,6 @@ private Version pickVersion(List<Version> versions, VersionType versionType) {
213202 }
214203
215204 private Path download (ProjectType projectType , VersionFile versionFile ) {
216- if (log .isDebugEnabled ()) {
217- log .debug ("Downloading {}" , versionFile );
218- }
219- else {
220- log .info ("Downloading {}" , versionFile .getFilename ());
221- }
222-
223205 if (projectType != ProjectType .mod ) {
224206 throw new InvalidParameterException ("Only mod project types can be downloaded for now" );
225207 }
@@ -236,6 +218,7 @@ private Path download(ProjectType projectType, VersionFile versionFile) {
236218 .userAgentCommand ("modrinth" )
237219 .toFile (outPath )
238220 .skipUpToDate (true )
221+ .handleStatus (Fetch .loggingDownloadStatusHandler (log ))
239222 .execute ();
240223 } catch (IOException e ) {
241224 throw new RuntimeException ("Downloading mod file" , e );
@@ -253,22 +236,20 @@ private List<Version> getVersionsForProject(ModrinthApiClient modrinthApiClient,
253236 return versions ;
254237 }
255238
256- private Version getVersionFromId (String versionId ) {
257- return fetch (Uris .populateToUri (
258- baseUrl + "/v2/version/{id}" ,
259- versionId
260- ))
261- .userAgentCommand ("modrinth" )
262- .toObject (Version .class )
263- .execute ();
264- }
265-
266239
267240 private Stream <Path > processProject (ModrinthApiClient modrinthApiClient , ProjectRef projectRef , Project project ) {
268241 log .debug ("Starting with projectRef={}" , projectRef );
269242
270243 if (projectsProcessed .add (project .getId ())) {
271- final Version version = resolveProjectVersion (modrinthApiClient , project , projectRef );
244+ final Version version ;
245+ try {
246+ version = modrinthApiClient .resolveProjectVersion (
247+ project , projectRef , loader , gameVersion , defaultVersionType
248+ )
249+ .block ();
250+ } catch (NoApplicableVersionsException | NoFilesAvailableException e ) {
251+ throw new InvalidParameterException (e .getMessage (), e );
252+ }
272253
273254 if (version != null ) {
274255 if (version .getFiles ().isEmpty ()) {
@@ -287,29 +268,10 @@ private Stream<Path> processProject(ModrinthApiClient modrinthApiClient, Project
287268 throw new InvalidParameterException (
288269 String .format ("Project %s does not have any matching versions for loader %s, game version %s" ,
289270 projectRef , loader , gameVersion
290- ));
291- }
292- }
293- return Stream .empty ();
294- }
295-
296- private Version resolveProjectVersion (ModrinthApiClient modrinthApiClient , Project project , ProjectRef projectRef ) {
297- if (projectRef .hasVersionType ()) {
298- return pickVersion (getVersionsForProject (modrinthApiClient , project .getId ()), projectRef .getVersionType ());
299- }
300- else if (projectRef .hasVersionId ()) {
301- return getVersionFromId (projectRef .getVersionId ());
302- }
303- else {
304- final List <Version > versions = getVersionsForProject (modrinthApiClient , project .getId ());
305- if (versions .isEmpty ()) {
306- throw new InvalidParameterException (
307- String .format ("No files are available for the project '%s' (%s) for %s loader and Minecraft version %s" ,
308- project .getTitle (), project .getSlug (), loader , gameVersion
309271 ));
310272 }
311- return pickVersion (versions );
312273 }
274+ return Stream .empty ();
313275 }
314276
315277 /**
0 commit comments