From dc4426d9c999965ecbaab077311a3e68cabfe6fb Mon Sep 17 00:00:00 2001 From: Lasm Gratel Date: Mon, 27 May 2024 20:25:19 +0800 Subject: [PATCH] fix #479 (#483) --- src/main/java/codechicken/nei/NEIClientConfig.java | 4 ++++ .../nei/recipe/ProfilerRecipeHandler.java | 2 +- .../codechicken/nei/recipe/RecipeHandlerQuery.java | 13 ++++++++++--- .../codechicken/nei/util/AsyncTaskProfiler.java | 11 +++++++++-- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/main/java/codechicken/nei/NEIClientConfig.java b/src/main/java/codechicken/nei/NEIClientConfig.java index 07af4e289..a3ee8733a 100644 --- a/src/main/java/codechicken/nei/NEIClientConfig.java +++ b/src/main/java/codechicken/nei/NEIClientConfig.java @@ -667,6 +667,10 @@ public static boolean loadCatalystsFromJar() { return !getBooleanSetting("tools.catalyst_load_from_config"); } + public static boolean isProfileRecipeEnabled() { + return NEIClientConfig.getBooleanSetting("inventory.profileRecipes"); + } + public static void setEnabled(boolean flag) { getSetting("inventory.widgetsenabled").setBooleanValue(flag); } diff --git a/src/main/java/codechicken/nei/recipe/ProfilerRecipeHandler.java b/src/main/java/codechicken/nei/recipe/ProfilerRecipeHandler.java index 451c66804..1e39316d1 100644 --- a/src/main/java/codechicken/nei/recipe/ProfilerRecipeHandler.java +++ b/src/main/java/codechicken/nei/recipe/ProfilerRecipeHandler.java @@ -39,7 +39,7 @@ public String getRecipeName() { @Override public int numRecipes() { - if (!NEIClientConfig.getBooleanSetting("inventory.profileRecipes")) return 0; + if (!NEIClientConfig.isProfileRecipeEnabled()) return 0; return (int) Math.ceil( ((crafting ? GuiCraftingRecipe.craftinghandlers.size() : GuiUsageRecipe.usagehandlers.size()) - 1) diff --git a/src/main/java/codechicken/nei/recipe/RecipeHandlerQuery.java b/src/main/java/codechicken/nei/recipe/RecipeHandlerQuery.java index 66ac825c2..e3e099ab4 100644 --- a/src/main/java/codechicken/nei/recipe/RecipeHandlerQuery.java +++ b/src/main/java/codechicken/nei/recipe/RecipeHandlerQuery.java @@ -35,8 +35,15 @@ class RecipeHandlerQuery { ArrayList runWithProfiling(String profilerSection) { TaskProfiler profiler = ProfilerRecipeHandler.getProfiler(); - profiler.clear(); - profiler.start(profilerSection); + + // Save the current config state here, as it may be altered + // if getRecipeHandlesParallel took so long and player accidentally changed config. + boolean profileRecipes = NEIClientConfig.isProfileRecipeEnabled(); + if (profileRecipes) { + profiler.clear(); + profiler.start(profilerSection); + } + try { ArrayList handlers = getRecipeHandlersParallel(); @@ -49,7 +56,7 @@ ArrayList runWithProfiling(String profilerSection) { displayRecipeLookupError(); return new ArrayList<>(0); } finally { - profiler.end(); + if (profileRecipes) profiler.end(); } } diff --git a/src/main/java/codechicken/nei/util/AsyncTaskProfiler.java b/src/main/java/codechicken/nei/util/AsyncTaskProfiler.java index 97c84d9f5..88c93f7eb 100644 --- a/src/main/java/codechicken/nei/util/AsyncTaskProfiler.java +++ b/src/main/java/codechicken/nei/util/AsyncTaskProfiler.java @@ -14,14 +14,21 @@ public class AsyncTaskProfiler extends TaskProfiler { public Map times = new ConcurrentHashMap<>(); + @Override public void start(String section) { if (section == null) section = ""; threadedProfiler.get().start(section); } + @Override public void end() { - threadedProfiler.get().end(); - times.putAll(threadedProfiler.get().times); + TaskProfiler profiler = threadedProfiler.get(); + if (profiler != null) { + profiler.end(); + if (profiler.times != null && !profiler.times.isEmpty()) { + times.putAll(threadedProfiler.get().times); + } + } } @Override