Skip to content

Commit

Permalink
Always use CF.allOf
Browse files Browse the repository at this point in the history
  • Loading branch information
Technici4n committed May 2, 2024
1 parent 7476536 commit 967344c
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions loader/src/main/java/net/neoforged/fml/ModLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public static void dispatchParallelTask(String name, Executor parallelExecutor,
Map<IModInfo, CompletableFuture<Void>> modFutures = new IdentityHashMap<>();
var futureList = modList.getSortedMods().stream()
.map(modContainer -> {
// Build future for all dependencies first
// Collect futures for all dependencies first
var deps = LoadingModList.get().getDependencies(modContainer.getModInfo());
@SuppressWarnings("unchecked")
CompletableFuture<Void>[] depFutures = new CompletableFuture[deps.size()];
Expand All @@ -219,28 +219,27 @@ public static void dispatchParallelTask(String name, Executor parallelExecutor,
}
}

var combinedFuture = depFutures.length == 0 ? CompletableFuture.completedFuture(null) : CompletableFuture.allOf(depFutures);

// Build the future for this container
var future = combinedFuture.<Void>handleAsync((void_, exception) -> {
if (exception != null) {
// If there was any exception, short circuit.
// The exception will already be handled by `waitForFuture` since it comes from another mod.
LOGGER.error("Skipping {} future for mod {} because a dependency threw an exception.", name, modContainer.getModId());
progress.increment();
// Throw a marker exception to make sure that dependencies of *this* task don't get executed.
throw new DependentFutureFailedException();
}

try {
ModLoadingContext.get().setActiveContainer(modContainer);
task.accept(modContainer);
} finally {
progress.increment();
ModLoadingContext.get().setActiveContainer(null);
}
return null;
}, parallelExecutor);
var future = CompletableFuture.allOf(depFutures)
.<Void>handleAsync((void_, exception) -> {
if (exception != null) {
// If there was any exception, short circuit.
// The exception will already be handled by `waitForFuture` since it comes from another mod.
LOGGER.error("Skipping {} future for mod {} because a dependency threw an exception.", name, modContainer.getModId());
progress.increment();
// Throw a marker exception to make sure that dependencies of *this* task don't get executed.
throw new DependentFutureFailedException();
}

try {
ModLoadingContext.get().setActiveContainer(modContainer);
task.accept(modContainer);
} finally {
progress.increment();
ModLoadingContext.get().setActiveContainer(null);
}
return null;
}, parallelExecutor);
modFutures.put(modContainer.getModInfo(), future);
return future;
})
Expand Down

0 comments on commit 967344c

Please sign in to comment.