1616import java .time .ZoneId ;
1717import java .util .*;
1818import java .util .concurrent .atomic .AtomicInteger ;
19+ import java .util .stream .Collectors ;
1920
2021public class Modpack {
2122 private static final JSONParser parser = new JSONParser ();
@@ -49,17 +50,19 @@ private static void install(String[] ids) {
4950 File packFolder = new File (Config .modpackDir .getPath () + File .separator +slug +"_" +id );
5051 if (packFolder .exists ()) throw new Exception ("Found old folder of " +name +". Installation of " +name +" cancelled." );
5152 packFolder .mkdir ();
52- String downloadUrl = ((String ) ((JSONObject ) Utils .getLast ((JSONArray ) json .get ("latestFiles" ))).get ("downloadUrl" )).replaceFirst ("edge" , "media" );
53+ JSONObject latest = (JSONObject ) Utils .getLast ((JSONArray ) json .get ("latestFiles" ));
54+ String downloadUrl = ((String ) latest .get ("downloadUrl" )).replaceFirst ("edge" , "media" );
5355 String loc = Utils .downloadFile (downloadUrl , packFolder .getPath ());
5456 if (loc == null ) throw new Exception ("Failed to download modpack " + name );
5557 boolean success = Utils .unzip (loc );
5658 if (!success ) throw new Exception ("Failed to extract modpack content of " +name );
57- File manifest = new File (packFolder + File .separator + "manifest.json" );
59+ File manifest = new File (packFolder + File .separator + getManifestFile ( latest ) );
5860 if (!manifest .exists ()) throw new Exception ("Cannot find modpack manifest of " +name );
59- copyFromOverride (packFolder .getPath (), (String ) ((JSONObject ) parser .parse (new FileReader (manifest ))).get ("overrides" ));
61+ JSONObject manifestJson = (JSONObject ) parser .parse (new FileReader (manifest ));
62+ copyFromOverride (packFolder .getPath (), (String ) manifestJson .get ("overrides" ));
6063 downloadMods (packFolder .getPath ());
6164 String thumb = downloadThumb (json );
62- genProfile (name , packFolder .getAbsolutePath (), thumb );
65+ genProfile (name , packFolder .getAbsolutePath (), thumb , getModVersion ( manifestJson ) );
6366 System .out .println (Ansi .ansi ().fg (Ansi .Color .GREEN ).a ("Finished download of " + name ).reset ());
6467 } catch (Exception e ) {
6568 e .printStackTrace ();
@@ -110,6 +113,39 @@ public static void downloadMods(String folder) {
110113 }
111114 }
112115
116+ // Pass in latestFiles' last element
117+ private static String getManifestFile (JSONObject latest ) {
118+ if (latest .containsKey ("modules" )) {
119+ JSONArray modules = (JSONArray ) latest .get ("modules" );
120+ Optional optional = modules .stream ().filter (obj -> ((int ) ((JSONObject ) obj ).getOrDefault ("type" , 0 )) == 3 ).findFirst ();
121+ if (optional .isPresent ()) return (String ) ((JSONObject ) optional .get ()).get ("foldername" );
122+ }
123+ return "manifest.json" ;
124+ }
125+
126+ // Get mod loader version from manifest
127+ private static String getModVersion (JSONObject manifest ) {
128+ if (manifest .containsKey ("minecraft" )) {
129+ JSONObject minecraft = (JSONObject ) manifest .get ("minecraft" );
130+ if (minecraft .containsKey ("version" ) && minecraft .containsKey ("modLoaders" )) {
131+ JSONArray modLoaders = (JSONArray ) minecraft .get ("modLoaders" );
132+ Optional optional = modLoaders .stream ().filter (obj -> (boolean ) ((JSONObject ) obj ).getOrDefault ("primary" , false )).findFirst ();
133+ if (optional .isPresent ()) {
134+ String version = (String ) minecraft .get ("version" );
135+ String id = (String ) ((JSONObject ) optional .get ()).get ("id" );
136+ String [] splitted = id .split ("-" );
137+ String loader = splitted [0 ];
138+ String modVer = Arrays .stream (splitted ).skip (1 ).collect (Collectors .joining ("-" ));
139+ if (loader .equalsIgnoreCase ("forge" )) id = version + "-forge-" + modVer ;
140+ else if (loader .equalsIgnoreCase ("fabric" )) id = "fabric-loader-" + modVer + "-" + version ;
141+ else id = null ;
142+ return id ;
143+ }
144+ }
145+ }
146+ return null ;
147+ }
148+
113149 private static String downloadThumb (JSONObject json ) {
114150 if (!(json .get ("attachments" ) instanceof JSONArray ) || !(((JSONArray ) json .get ("attachments" )).get (0 ) instanceof JSONObject )) return null ;
115151 JSONObject attachment = (JSONObject ) ((JSONArray ) json .get ("attachments" )).get (0 );
@@ -121,7 +157,7 @@ private static String downloadThumb(JSONObject json) {
121157 return null ;
122158 }
123159
124- private static void genProfile (String name , String path , String icon ) {
160+ private static void genProfile (String name , String path , String icon , String loader ) {
125161 String base64 = null ;
126162 if (icon != null ) {
127163 try {
@@ -143,7 +179,7 @@ private static void genProfile(String name, String path, String icon) {
143179 int memory = (int ) Math .ceil (Runtime .getRuntime ().maxMemory () / 1024.0 / 1024.0 / 1024.0 );
144180 profile .put ("javaArgs" , String .format ("-Xmx%dG -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M" , memory ));
145181 profile .put ("lastUsed" , LocalDateTime .of (LocalDate .MIN , LocalTime .MIN ).atZone (ZoneId .of ("UTC" )).toString ());
146- profile .put ("lastVersionId" , "latest-release" );
182+ profile .put ("lastVersionId" , loader == null ? "latest-release" : loader );
147183 profile .put ("name" , name );
148184 profile .put ("type" , "custom" );
149185 profiles .put (UUID .randomUUID (), profile );
@@ -154,9 +190,12 @@ private static void genProfile(String name, String path, String icon) {
154190
155191 pw .flush ();
156192 pw .close ();
157- System .out .println (Ansi .ansi ().fg (Ansi .Color .YELLOW ).a ("Created profile for " + name + ". However, the mod loader is not configured correctly. Please open/restart your Minecraft Launcher to edit it. Installation of mod loader might be needed, and can be downloaded in the following:" ).reset ());
158- System .out .println (Ansi .ansi ().fg (Ansi .Color .RED ).a ("Forge: " ).fg (Ansi .Color .CYAN ).a ("https://files.minecraftforge.net/" ).reset ());
159- System .out .println (Ansi .ansi ().fg (Ansi .Color .RED ).a ("Fabric: " ).fg (Ansi .Color .CYAN ).a ("https://fabricmc.net/use/installer/" ).reset ());
193+ System .out .println (Ansi .ansi ().fg (Ansi .Color .YELLOW ).a ("Created profile for " + name + "." ).reset ());
194+ if (loader == null ) {
195+ System .out .println (Ansi .ansi ().fg (Ansi .Color .YELLOW ).a (" However, the mod loader is not configured correctly. Please open/restart your Minecraft Launcher to edit it. Installation of mod loader might be needed, and can be downloaded in the following:" ).reset ());
196+ System .out .println (Ansi .ansi ().fg (Ansi .Color .RED ).a ("Forge: " ).fg (Ansi .Color .CYAN ).a ("https://files.minecraftforge.net/" ).reset ());
197+ System .out .println (Ansi .ansi ().fg (Ansi .Color .RED ).a ("Fabric: " ).fg (Ansi .Color .CYAN ).a ("https://fabricmc.net/use/installer/" ).reset ());
198+ }
160199 } catch (Exception ignored ) { }
161200 }
162201
0 commit comments