2022-06-29 14:18:48 -04:00
|
|
|
package codechicken.nei.recipe;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
import java.util.stream.Collectors;
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2022-06-29 14:18:48 -04:00
|
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
|
import net.minecraft.util.ChatComponentTranslation;
|
|
|
|
|
import net.minecraft.util.EnumChatFormatting;
|
|
|
|
|
import net.minecraft.util.IChatComponent;
|
|
|
|
|
|
|
|
|
|
import codechicken.core.TaskProfiler;
|
|
|
|
|
import codechicken.nei.ItemList;
|
|
|
|
|
import codechicken.nei.NEIClientConfig;
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2022-06-29 14:18:48 -04:00
|
|
|
class RecipeHandlerQuery<T extends IRecipeHandler> {
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2022-06-29 14:18:48 -04:00
|
|
|
private final Function<T, T> recipeHandlerFunction;
|
|
|
|
|
private final List<T> recipeHandlers;
|
|
|
|
|
private final List<T> serialRecipeHandlers;
|
2023-02-05 19:27:52 +09:00
|
|
|
private final String[] errorMessage;
|
2023-02-08 16:54:43 +09:00
|
|
|
private boolean error = false;
|
2022-06-29 14:18:48 -04:00
|
|
|
|
2023-02-05 19:27:52 +09:00
|
|
|
RecipeHandlerQuery(Function<T, T> recipeHandlerFunction, List<T> recipeHandlers, List<T> serialRecipeHandlers,
|
|
|
|
|
String... errorMessage) {
|
2022-06-29 14:18:48 -04:00
|
|
|
this.recipeHandlerFunction = recipeHandlerFunction;
|
|
|
|
|
this.recipeHandlers = recipeHandlers;
|
|
|
|
|
this.serialRecipeHandlers = serialRecipeHandlers;
|
2023-02-05 19:27:52 +09:00
|
|
|
this.errorMessage = errorMessage;
|
2022-06-29 14:18:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArrayList<T> runWithProfiling(String profilerSection) {
|
|
|
|
|
TaskProfiler profiler = ProfilerRecipeHandler.getProfiler();
|
|
|
|
|
profiler.start(profilerSection);
|
|
|
|
|
try {
|
2023-02-08 16:54:43 +09:00
|
|
|
ArrayList<T> handlers = getRecipeHandlersParallel();
|
|
|
|
|
if (error) {
|
|
|
|
|
displayRecipeLookupError();
|
|
|
|
|
}
|
|
|
|
|
return handlers;
|
2022-06-29 14:18:48 -04:00
|
|
|
} catch (InterruptedException | ExecutionException e) {
|
2023-02-08 16:54:43 +09:00
|
|
|
printLog(e);
|
|
|
|
|
displayRecipeLookupError();
|
2022-06-29 14:18:48 -04:00
|
|
|
return new ArrayList<>(0);
|
|
|
|
|
} finally {
|
|
|
|
|
profiler.end();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ArrayList<T> getRecipeHandlersParallel() throws InterruptedException, ExecutionException {
|
|
|
|
|
// Pre-find the fuels so we're not fighting over it
|
|
|
|
|
FuelRecipeHandler.findFuelsOnceParallel();
|
|
|
|
|
ArrayList<T> handlers = getSerialHandlersWithRecipes();
|
|
|
|
|
handlers.addAll(getHandlersWithRecipes());
|
|
|
|
|
handlers.sort(NEIClientConfig.HANDLER_COMPARATOR);
|
|
|
|
|
return handlers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ArrayList<T> getSerialHandlersWithRecipes() {
|
2023-02-08 16:54:43 +09:00
|
|
|
return serialRecipeHandlers.stream().map(handler -> {
|
|
|
|
|
try {
|
|
|
|
|
return recipeHandlerFunction.apply(handler);
|
|
|
|
|
} catch (Throwable t) {
|
|
|
|
|
printLog(t);
|
|
|
|
|
error = true;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}).filter(h -> h != null && h.numRecipes() > 0).collect(Collectors.toCollection(ArrayList::new));
|
2022-06-29 14:18:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ArrayList<T> getHandlersWithRecipes() throws InterruptedException, ExecutionException {
|
2023-02-08 16:54:43 +09:00
|
|
|
return ItemList.forkJoinPool.submit(() -> recipeHandlers.parallelStream().map(handler -> {
|
|
|
|
|
try {
|
|
|
|
|
return recipeHandlerFunction.apply(handler);
|
|
|
|
|
} catch (Throwable t) {
|
|
|
|
|
printLog(t);
|
|
|
|
|
error = true;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}).filter(h -> h != null && h.numRecipes() > 0).collect(Collectors.toCollection(ArrayList::new))).get();
|
2022-06-29 14:18:48 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-08 16:54:43 +09:00
|
|
|
private void printLog(Throwable t) {
|
2023-02-05 19:27:52 +09:00
|
|
|
for (String message : errorMessage) {
|
|
|
|
|
NEIClientConfig.logger.error(message);
|
|
|
|
|
}
|
2023-02-08 16:54:43 +09:00
|
|
|
t.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void displayRecipeLookupError() {
|
2022-06-29 14:18:48 -04:00
|
|
|
EntityPlayer player = Minecraft.getMinecraft().thePlayer;
|
|
|
|
|
if (player != null) {
|
|
|
|
|
IChatComponent chat = new ChatComponentTranslation("nei.chat.recipe.error");
|
|
|
|
|
chat.getChatStyle().setColor(EnumChatFormatting.RED);
|
|
|
|
|
player.addChatComponentMessage(chat);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|