Focus the previous recipe if it's still visible when loading usage re… (#263)

* Focus the previous recipe if it's still visible when loading usage recipes.

* Reduced code duplication by extracting getRecipeHandlers

* Moved RecipeHandlerQuery to its own package-private file

* Refactored RecipeHandlerQuery methods

* Saving the current recipe when left clicking a machine's "Recipes" button

* Restored prevScreen so we can restore it with overlay

* Genericized GuiRecipe so that currentHandlers is no longer hidden in subclasses

* Cleaned up overlay handling

* Fixed trivial IDE warnings & bad JavaDoc on files I touched
This commit is contained in:
YannickMG authored and GitHub committed 2022-06-29 20:18:48 +02:00
1 parent f7a900e910
commit 5bbf275980
15 files changed
+177 -201

No files matched your search

@@ -1,88 +1,46 @@
package codechicken.nei.recipe;
import codechicken.core.TaskProfiler;
import codechicken.nei.ItemList;
import codechicken.nei.ItemPanels;
import codechicken.nei.NEIClientConfig;
import codechicken.nei.NEIClientUtils;
import codechicken.nei.PositionedStack;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;
public class GuiCraftingRecipe extends GuiRecipe {
public class GuiCraftingRecipe extends GuiRecipe<ICraftingHandler> {
public static ArrayList<ICraftingHandler> craftinghandlers = new ArrayList<>();
public static ArrayList<ICraftingHandler> serialCraftingHandlers = new ArrayList<>();
public static boolean openRecipeGui(String outputId, Object... results) {
return openRecipeGui(outputId, false, results);
}
public static boolean openRecipeGui(String outputId, Boolean overlay, Object... results) {
Minecraft mc = NEIClientUtils.mc();
GuiScreen prevscreen = mc.currentScreen; // instanceof GuiContainer ? (GuiContainer) mc.currentScreen : null;
ArrayList<ICraftingHandler> handlers;
TaskProfiler profiler = ProfilerRecipeHandler.getProfiler();
profiler.start("recipe.concurrent.crafting");
// Pre-find the fuels so we're not fighting over it
FuelRecipeHandler.findFuelsOnceParallel();
try {
handlers = serialCraftingHandlers.stream()
.map(h -> h.getRecipeHandler(outputId, results))
.filter(h -> h.numRecipes() > 0)
.collect(Collectors.toCollection(ArrayList::new));
handlers.addAll(ItemList.forkJoinPool
.submit(() -> craftinghandlers.parallelStream()
.map(h -> h.getRecipeHandler(outputId, results))
.filter(h -> h.numRecipes() > 0)
.collect(Collectors.toCollection(ArrayList::new)))
.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
EntityPlayer player = Minecraft.getMinecraft().thePlayer;
if (player != null) {
IChatComponent chat = new ChatComponentTranslation("nei.chat.recipe.error");
chat.getChatStyle().setColor(EnumChatFormatting.RED);
player.addChatComponentMessage(chat);
}
return false;
} finally {
profiler.end();
}
RecipeHandlerQuery<ICraftingHandler> recipeQuery = new RecipeHandlerQuery<>(
h -> h.getRecipeHandler(outputId, results), craftinghandlers, serialCraftingHandlers);
ArrayList<ICraftingHandler> handlers = recipeQuery.runWithProfiling("recipe.concurrent.crafting");
if (handlers.isEmpty()) return false;
handlers.sort(NEIClientConfig.HANDLER_COMPARATOR);
Minecraft mc = NEIClientUtils.mc();
BookmarkRecipeId recipeId = null;
BookmarkRecipeId recipeId = (NEIClientConfig.saveCurrentRecipeInBookmarksEnabled() && "item".equals(outputId))
? getRecipeId(mc.currentScreen, (ItemStack) results[0])
: getCurrentRecipe();
if ("item".equals(outputId)) {
recipeId = getRecipeId(prevscreen, (ItemStack) results[0]);
}
if (overlay && recipeId == null) return false;
GuiCraftingRecipe gui = new GuiCraftingRecipe(prevscreen, handlers, recipeId);
GuiCraftingRecipe gui = new GuiCraftingRecipe(handlers, recipeId);
mc.displayGuiScreen(gui);
if (NEIClientConfig.saveCurrentRecipeInBookmarksEnabled())
if (!NEIClientUtils.shiftKey() || overlay) {
gui.openTargetRecipe(gui.recipeId);
}
if (recipeId != null && (!NEIClientUtils.shiftKey() || overlay)) {
gui.openTargetRecipe(gui.recipeId);
}
if (overlay) {
if (!NEIClientConfig.saveCurrentRecipeInBookmarksEnabled() || gui.recipeId == null) {
mc.displayGuiScreen(prevscreen);
return false;
}
gui.overlayRecipe(gui.recipeId.position);
}
@@ -92,8 +50,8 @@ public class GuiCraftingRecipe extends GuiRecipe {
protected static BookmarkRecipeId getRecipeId(GuiScreen gui, ItemStack stackover) {
if (gui instanceof GuiRecipe) {
final List<PositionedStack> ingredients = ((GuiRecipe) gui).getFocusedRecipeIngredients();
final String handlerName = ((GuiRecipe) gui).getHandlerName();
final List<PositionedStack> ingredients = ((GuiRecipe<?>) gui).getFocusedRecipeIngredients();
final String handlerName = ((GuiRecipe<?>) gui).getHandlerName();
if (ingredients != null && !ingredients.isEmpty()) {
return new BookmarkRecipeId(handlerName, ingredients);
@@ -103,16 +61,12 @@ public class GuiCraftingRecipe extends GuiRecipe {
return ItemPanels.bookmarkPanel.getBookmarkRecipeId(stackover);
}
private GuiCraftingRecipe(GuiScreen prevgui, ArrayList<ICraftingHandler> handlers, BookmarkRecipeId recipeId) {
this(prevgui, handlers);
private GuiCraftingRecipe(ArrayList<ICraftingHandler> handlers, BookmarkRecipeId recipeId) {
super(NEIClientUtils.mc().currentScreen);
this.currenthandlers = handlers;
this.recipeId = recipeId;
}
private GuiCraftingRecipe(GuiScreen prevgui, ArrayList<ICraftingHandler> handlers) {
super(prevgui);
currenthandlers = handlers;
}
public static void registerRecipeHandler(ICraftingHandler handler) {
final String handlerId = handler.getHandlerId();
if (craftinghandlers.stream().anyMatch(h -> h.getHandlerId().equals(handlerId))
@@ -123,12 +77,8 @@ public class GuiCraftingRecipe extends GuiRecipe {
else craftinghandlers.add(handler);
}
public ArrayList<? extends IRecipeHandler> getCurrentRecipeHandlers() {
@Override
public ArrayList<ICraftingHandler> getCurrentRecipeHandlers() {
return currenthandlers;
}
public ArrayList<ICraftingHandler> currenthandlers;
public static ArrayList<ICraftingHandler> craftinghandlers = new ArrayList<>();
public static ArrayList<ICraftingHandler> serialCraftingHandlers = new ArrayList<>();
}