4444public class ModrinthCommand implements Callable <Integer > {
4545
4646 public static final String DATAPACKS_SUBDIR = "datapacks" ;
47- @ Option (names = "--projects" , description = "Project ID or Slug" ,
47+ @ Option (names = "--projects" , description = "Project ID or Slug. Prefix with loader: e.g. fabric:project-id " ,
4848 split = SPLIT_COMMA_NL , splitSynopsisLabel = SPLIT_SYNOPSIS_COMMA_NL ,
49- paramLabel = "id|slug"
49+ paramLabel = "[loader:] id|slug"
5050 )
5151 List <String > projects ;
5252
@@ -157,7 +157,7 @@ private ModrinthManifest loadManifest() throws IOException {
157157 return Manifests .load (outputDirectory , ModrinthManifest .ID , ModrinthManifest .class );
158158 }
159159
160- private Stream <Version > expandDependencies (ModrinthApiClient modrinthApiClient , Project project , Version version ) {
160+ private Stream <Version > expandDependencies (ModrinthApiClient modrinthApiClient , Loader loader , String gameVersion , Project project , Version version ) {
161161 log .debug ("Expanding dependencies of version={}" , version );
162162 return version .getDependencies ().stream ()
163163 .filter (this ::filterDependency )
@@ -170,7 +170,7 @@ private Stream<Version> expandDependencies(ModrinthApiClient modrinthApiClient,
170170 if (dep .getVersionId () == null ) {
171171 log .debug ("Fetching versions of dep={} and picking" , dep );
172172 depVersion = pickVersion (
173- getVersionsForProject (modrinthApiClient , dep .getProjectId ())
173+ getVersionsForProject (modrinthApiClient , dep .getProjectId (), loader , gameVersion )
174174 );
175175 }
176176 else {
@@ -193,7 +193,7 @@ private Stream<Version> expandDependencies(ModrinthApiClient modrinthApiClient,
193193 log .debug ("Resolved version={} for dep={}" , depVersion .getVersionNumber (), dep );
194194 return Stream .concat (
195195 Stream .of (depVersion ),
196- expandDependencies (modrinthApiClient , project , depVersion )
196+ expandDependencies (modrinthApiClient , loader , gameVersion , project , depVersion )
197197 )
198198 .peek (expandedVer -> log .debug ("Expanded dependency={} into version={}" , dep , expandedVer ));
199199 }
@@ -229,16 +229,14 @@ private Version pickVersion(List<Version> versions, VersionType versionType) {
229229 return null ;
230230 }
231231
232- private Path download (boolean isDatapack , VersionFile versionFile ) {
232+ private Path download (Loader loaderRef , VersionFile versionFile ) {
233233 final Path outPath ;
234234 try {
235- if (!isDatapack ) {
236- outPath = Files .createDirectories (outputDirectory
237- .resolve (loader .getType ())
238- )
239- .resolve (versionFile .getFilename ());
240- }
241- else {
235+ final Loader effectiveLoader = loaderRef != null ? loaderRef : loader ;
236+ final String outputType = effectiveLoader .getType ();
237+
238+ if (outputType == null ) {
239+ // Datapack case
242240 if (worldDirectory .isAbsolute ()) {
243241 outPath = Files .createDirectories (worldDirectory
244242 .resolve (DATAPACKS_SUBDIR )
@@ -253,9 +251,15 @@ private Path download(boolean isDatapack, VersionFile versionFile) {
253251 .resolve (versionFile .getFilename ());
254252 }
255253 }
254+ else {
255+ outPath = Files .createDirectories (outputDirectory
256+ .resolve (outputType )
257+ )
258+ .resolve (versionFile .getFilename ());
259+ }
256260
257261 } catch (IOException e ) {
258- throw new RuntimeException ("Creating mods directory" , e );
262+ throw new RuntimeException ("Creating output directory" , e );
259263 }
260264
261265 try {
@@ -267,11 +271,11 @@ private Path download(boolean isDatapack, VersionFile versionFile) {
267271 .handleStatus (Fetch .loggingDownloadStatusHandler (log ))
268272 .execute ();
269273 } catch (IOException e ) {
270- throw new RuntimeException ("Downloading mod file" , e );
274+ throw new RuntimeException ("Downloading file" , e );
271275 }
272276 }
273277
274- private List <Version > getVersionsForProject (ModrinthApiClient modrinthApiClient , String project ) {
278+ private List <Version > getVersionsForProject (ModrinthApiClient modrinthApiClient , String project , Loader loader , String gameVersion ) {
275279 final List <Version > versions = modrinthApiClient .getVersionsForProject (
276280 project , loader , gameVersion
277281 )
@@ -294,10 +298,12 @@ private Stream<Path> processProject(ModrinthApiClient modrinthApiClient, Project
294298 log .debug ("Starting with project='{}' slug={}" , project .getTitle (), project .getSlug ());
295299
296300 if (projectsProcessed .add (project .getId ())) {
301+ final Loader effectiveLoader = projectRef .getLoaderRef () != null ? projectRef .getLoaderRef () : loader ;
302+
297303 final Version version ;
298304 try {
299305 version = modrinthApiClient .resolveProjectVersion (
300- project , projectRef , loader , gameVersion , defaultVersionType
306+ project , projectRef , effectiveLoader , gameVersion , defaultVersionType
301307 )
302308 .block ();
303309 } catch (NoApplicableVersionsException | NoFilesAvailableException e ) {
@@ -309,33 +315,24 @@ private Stream<Path> processProject(ModrinthApiClient modrinthApiClient, Project
309315 throw new GenericException (String .format ("Project %s has no files declared" , project .getSlug ()));
310316 }
311317
312- final boolean isDatapack = isDatapack (version );
313-
314318 return Stream .concat (
315319 Stream .of (version ),
316- expandDependencies (modrinthApiClient , project , version )
320+ expandDependencies (modrinthApiClient , effectiveLoader , gameVersion , project , version )
317321 )
318322 .map (ModrinthApiClient ::pickVersionFile )
319- .map (versionFile -> download (isDatapack , versionFile ))
320- .flatMap (downloadedFile -> ! isDatapack ? expandIfZip (downloadedFile ) : Stream . empty ( ));
323+ .map (versionFile -> download (effectiveLoader , versionFile ))
324+ .flatMap (downloadedFile -> expandIfZip (downloadedFile ));
321325 }
322326 else {
323327 throw new InvalidParameterException (
324328 String .format ("Project %s does not have any matching versions for loader %s, game version %s" ,
325- projectRef , loader , gameVersion
329+ projectRef , effectiveLoader , gameVersion
326330 ));
327331 }
328332 }
329333 return Stream .empty ();
330334 }
331335
332- private boolean isDatapack (Version version ) {
333- return
334- version .getLoaders () != null
335- && version .getLoaders ().size () == 1
336- && version .getLoaders ().get (0 ).equals (Constants .LOADER_DATAPACK );
337- }
338-
339336 /**
340337 * If downloadedFile ends in .zip, then expand it, return its files and given file.
341338 *
0 commit comments