1717import java .util .Arrays ;
1818import java .util .Locale ;
1919import java .util .Optional ;
20+ import java .util .concurrent .CompletableFuture ;
21+ import java .util .concurrent .ExecutionException ;
22+ import java .util .concurrent .Future ;
2023import java .util .function .Consumer ;
2124
2225import pojlib .account .MinecraftAccount ;
2831import pojlib .util .Constants ;
2932import pojlib .util .FileUtil ;
3033import pojlib .util .VLoader ;
34+ import pojlib .util .download .DownloadManager ;
3135import pojlib .util .json .MinecraftInstances ;
3236import pojlib .util .json .ModsJson ;
3337import pojlib .util .json .ProjectInfo ;
@@ -41,7 +45,6 @@ public class InstanceHandler {
4145 public static final String DEV_MODS = "https://raw.githubusercontent.com/QuestCraftPlusPlus/Pojlib/refs/heads/QuestCraft-6.0.0/devmods.json" ;
4246
4347 public static MinecraftInstances .Instance create (Activity activity , MinecraftInstances instances , String instanceName , String userHome , String modLoader , String mrpackFilePath , String imageURL ) {
44- API .finishedDownloading = false ;
4548 File mrpackJson = new File (Constants .USER_HOME + "/instances/" + instanceName .toLowerCase (Locale .ROOT ).replaceAll (" " , "_" ) + "/setup/modrinth.index.json" );
4649
4750 mrpackJson .getParentFile ().mkdirs ();
@@ -55,7 +58,6 @@ public static MinecraftInstances.Instance create(Activity activity, MinecraftIns
5558 }
5659
5760 return create (activity , instances , instanceName , userHome , false , index .dependencies .minecraft , modLoader , imageURL , (instance ) -> {
58- API .finishedDownloading = false ;
5961 if (instance .extProjects == null ) {
6062 instance .extProjects = new ProjectInfo [0 ];
6163 }
@@ -107,15 +109,13 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
107109 } catch (IOException e ) {
108110 throw new RuntimeException (e );
109111 }
110- API .finishedDownloading = false ;
111112 instance .updateMods (instances );
112113 GsonUtils .objectToJsonFile (userHome + "/instances.json" , instances );
113114 });
114115 }
115116
116117 //creates a new instance of a minecraft version, install game + mod loader, stores non login related launch info to json
117118 public static MinecraftInstances .Instance create (Activity activity , MinecraftInstances instances , String instanceName , String gameDir , boolean useDefaultMods , String minecraftVersion , String modLoader , String imageURL , Consumer <MinecraftInstances .Instance > postInstall ) {
118- API .finishedDownloading = false ;
119119 File instancesFile = new File (gameDir + "/instances.json" );
120120 if (instancesFile .exists ()) {
121121 for (MinecraftInstances .Instance instance : instances .instances ) {
@@ -175,18 +175,22 @@ public static MinecraftInstances.Instance create(Activity activity, MinecraftIns
175175 instances1 .add (instance );
176176 instances .instances = instances1 .toArray (new MinecraftInstances .Instance [0 ]);
177177
178- new Thread (() -> {
178+ CompletableFuture .supplyAsync (() ->
179+ {
179180 try {
180- String clientClasspath = Installer .installClient (minecraftVersionInfo , gameDir );
181- String minecraftClasspath = Installer .installLibraries (minecraftVersionInfo , gameDir );
182- String modLoaderClasspath = Installer .installLibraries (finalModLoaderVersionInfo , gameDir );
181+ CompletableFuture < String > clientClasspath = Installer .installClient (minecraftVersionInfo , gameDir );
182+ CompletableFuture < String > minecraftClasspath = Installer .installLibraries (minecraftVersionInfo , gameDir );
183+ CompletableFuture < String > modLoaderClasspath = Installer .installLibraries (finalModLoaderVersionInfo , gameDir );
183184 String lwjgl = UnityPlayerActivity .installLWJGL (activity );
184185
185- instance .classpath = clientClasspath + File .pathSeparator + minecraftClasspath + File .pathSeparator + modLoaderClasspath + File .pathSeparator + lwjgl ;
186+ CompletableFuture <Void > installFuture = CompletableFuture .allOf (clientClasspath , minecraftClasspath , modLoaderClasspath );
187+ installFuture .get ();
186188
187- instance .assetsDir = Installer .installAssets (minecraftVersionInfo , gameDir );
189+ instance .classpath = clientClasspath .get () + File .pathSeparator + minecraftClasspath .get () + File .pathSeparator + modLoaderClasspath .get () + File .pathSeparator + lwjgl ;
190+
191+ instance .assetsDir = Installer .installAssets (minecraftVersionInfo , gameDir ).get ();
188192 Installer .moveLocalAssets (activity , instance );
189- } catch (IOException e ) {
193+ } catch (IOException | ExecutionException | InterruptedException e ) {
190194 e .printStackTrace ();
191195 }
192196 instance .assetIndex = minecraftVersionInfo .assetIndex .id ;
@@ -198,9 +202,10 @@ public static MinecraftInstances.Instance create(Activity activity, MinecraftIns
198202 if (postInstall != null )
199203 postInstall .accept (instance );
200204
201- API . finishedDownloading = true ;
205+ DownloadManager . reset () ;
202206 Logger .getInstance ().appendToLog ("Finished Creating Instance!" );
203- }).start ();
207+ return null ;
208+ });
204209
205210 return instance ;
206211 }
0 commit comments