Skip to content

Commit

Permalink
refactor: remove async recipe searching
Browse files Browse the repository at this point in the history
  • Loading branch information
mikerooni committed Mar 19, 2024
1 parent 8fd38b9 commit 4f53e75
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ public enum Status {
protected long totalContinuousRunningTime;
protected TickableSubscription subscription;
protected Object workingSound;
@Nullable
protected CompletableFuture<Boolean> 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());
Expand Down Expand Up @@ -126,9 +122,6 @@ public void updateTickSubscription() {
}
} else {
subscription = getMachine().subscribeServerTick(subscription, this::serverTick);
if (completableFuture != null) {
dirtySearching = true;
}
}
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -270,56 +259,26 @@ 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<Boolean> supplyAsyncSearchingTask() {
return CompletableFuture.supplyAsync(Util.wrapThreadWithTaskName("GTCEu Recipe Search", () -> handleSearchingRecipes(searchRecipe())), Util.backgroundExecutor());
}

private boolean handleSearchingRecipes(Iterator<GTRecipe> matches) {
private void handleSearchingRecipes(Iterator<GTRecipe> 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) {
lastFailedMatches = new ArrayList<>();
}
lastFailedMatches.add(match);
}
return false;
}

public boolean handleFuelRecipe() {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/gregtechceu/gtceu/config/ConfigHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4f53e75

Please sign in to comment.