From 4f53e7546f75839b4fd2433713e4f7643d0f4825 Mon Sep 17 00:00:00 2001 From: mikerooni <139889766+mikerooni@users.noreply.github.com> Date: Tue, 19 Mar 2024 12:49:50 +0100 Subject: [PATCH] refactor: remove async recipe searching --- .../gtceu/api/machine/trait/RecipeLogic.java | 51 ++----------------- .../gtceu/config/ConfigHolder.java | 3 -- 2 files changed, 5 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/gregtechceu/gtceu/api/machine/trait/RecipeLogic.java b/src/main/java/com/gregtechceu/gtceu/api/machine/trait/RecipeLogic.java index 55cdfe6f60..d3a23276d1 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/machine/trait/RecipeLogic.java +++ b/src/main/java/com/gregtechceu/gtceu/api/machine/trait/RecipeLogic.java @@ -75,10 +75,6 @@ public enum Status { protected long totalContinuousRunningTime; protected TickableSubscription subscription; protected Object workingSound; - @Nullable - protected CompletableFuture completableFuture = null; - // if storage is dirty while async searching recipe, it will be set to true. - protected boolean dirtySearching = false; public RecipeLogic(IRecipeLogicMachine machine) { super(machine.self()); @@ -126,9 +122,6 @@ public void updateTickSubscription() { } } else { subscription = getMachine().subscribeServerTick(subscription, this::serverTick); - if (completableFuture != null) { - dirtySearching = true; - } } } @@ -176,11 +169,7 @@ public void serverTick() { boolean unsubscribe = false; if (isSuspend()) { unsubscribe = true; - if (completableFuture != null) { - completableFuture.cancel(true); - completableFuture = null; - } - } else if (completableFuture == null && lastRecipe == null && isIdle() && !machine.keepSubscribing() && !recipeDirty && lastFailedMatches == null) { + } else if (lastRecipe == null && isIdle() && !machine.keepSubscribing() && !recipeDirty && lastFailedMatches == null) { // machine isn't working enabled // or // there is no available recipes, so it will wait for notification. @@ -270,48 +259,19 @@ public void findAndHandleRecipe() { } else { // try to find and handle a new recipe lastRecipe = null; lastOriginRecipe = null; - if (completableFuture == null) { - // try to search recipe in threads. - if (ConfigHolder.INSTANCE.machines.asyncRecipeSearching) { - completableFuture = supplyAsyncSearchingTask(); - } else { - handleSearchingRecipes(searchRecipe()); - } - dirtySearching = false; - } else if (completableFuture.isDone()) { - var lastFuture = this.completableFuture; - completableFuture = null; - if (!lastFuture.isCancelled()) { - // if searching task is done, try to handle searched recipes. - try { - boolean matches = lastFuture.join(); - if (!matches && dirtySearching) { - completableFuture = supplyAsyncSearchingTask(); - } - } catch (Throwable throwable) { - // if error occurred, schedule a new async task. - completableFuture = supplyAsyncSearchingTask(); - } - } else { - handleSearchingRecipes(searchRecipe()); - } - dirtySearching = false; - } + handleSearchingRecipes(searchRecipe()); } recipeDirty = false; } - private CompletableFuture supplyAsyncSearchingTask() { - return CompletableFuture.supplyAsync(Util.wrapThreadWithTaskName("GTCEu Recipe Search", () -> handleSearchingRecipes(searchRecipe())), Util.backgroundExecutor()); - } - - private boolean handleSearchingRecipes(Iterator matches) { + private void handleSearchingRecipes(Iterator matches) { while (matches != null && matches.hasNext()) { GTRecipe match = matches.next(); if (match == null) continue; // If a new recipe was found, cache found recipe. - if (checkMatchedRecipeAvailable(match)) return true; + if (checkMatchedRecipeAvailable(match)) + return; // cache matching recipes. if (lastFailedMatches == null) { @@ -319,7 +279,6 @@ private boolean handleSearchingRecipes(Iterator matches) { } lastFailedMatches.add(match); } - return false; } public boolean handleFuelRecipe() { diff --git a/src/main/java/com/gregtechceu/gtceu/config/ConfigHolder.java b/src/main/java/com/gregtechceu/gtceu/config/ConfigHolder.java index d9aa4dbacc..3fc1a46f74 100644 --- a/src/main/java/com/gregtechceu/gtceu/config/ConfigHolder.java +++ b/src/main/java/com/gregtechceu/gtceu/config/ConfigHolder.java @@ -322,9 +322,6 @@ public static class MachineConfigs { "Other mods can override this to true, regardless of the config file.", "Default: false"}) public boolean highTierContent = false; - @Configurable - @Configurable.Comment({"Whether search for recipes asynchronously.", " Default: true"}) - public boolean asyncRecipeSearching = true; } public static class ToolConfigs {