diff options
author | SKProCH <29896317+SKProCH@users.noreply.github.com> | 2023-09-05 07:54:47 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-05 06:54:47 +0200 |
commit | 8beb872db7d19665a09518c5a5d90577b9f3f847 (patch) | |
tree | a024db315009e544c2ffc1a0a87dc9a82692d157 /src/main/java/gregtech/api/recipe | |
parent | 1249b547f0813db813d1c12713b5dafbf46996df (diff) | |
download | GT5-Unofficial-8beb872db7d19665a09518c5a5d90577b9f3f847.tar.gz GT5-Unofficial-8beb872db7d19665a09518c5a5d90577b9f3f847.tar.bz2 GT5-Unofficial-8beb872db7d19665a09518c5a5d90577b9f3f847.zip |
Proper recipe selection for output overflow in LCR and other multiblocks (#2247)
* Implement Stream<FindRecipeResult> findRecipesWithResult for GT_RecipeMap
* Change ProcessingLogic.process to actually use new findRecipesWithResult
* Change ProcessingLogic.process to start finding something only for OUTPUT_FULL result
* Refactor ProcessingLogic.process to make logic more readable
* Replace while with for loop, remove NOT_FOUND return in end of findRecipesWithResult
* Apply spotless
* Make findRecipe use findRecipes, add annotation to GT_Recipe and FindRecipeResult for processRecipe and make method protected, replace wildcard imports
* Remake isRecipeWithOutputFullFound
* Add @Nonnull to methods
* Apply spotless
* Remove Stream version of findRecipeWithResult, replace with predicate one. Add GT_Predicated_Recipe_Map class for utilizing this method. Changes some existent recipe maps to inherit from base class.
* Remove GT_Predicated_Recipe_Map, add Predicate directly to GT_Recipe_Map#findRecipeWithResult. Add AdvancedRecipeValidatorPredicate and FindRecipeWithAdvancedValidatorResult to allow store validation calculations for further use and proper errors displaying.
* Fix InsufficientVoltage errors
* Changes according to review comments. Integrate FindRecipeWithAdvancedValidatorResult to FindRecipeResult, rename AdvancedRecipeValidatorPredicate, encapsulate AdvancedRecipeValidatorPredicate fields, fixes some typos, etc
* Moves InsufficientVoltage check to GT_ParallelHelper. Removes FindRecipeResult#State#INSUFFICIENT_VOLTAGE
* Return an old findRecipeWithResult
* Renames things, call old methods for singleblocks
* Renames things, makes FindRecipeResult ctor private
* Apply spotless
* Move RecipeValidator, fix comments typos
Diffstat (limited to 'src/main/java/gregtech/api/recipe')
-rw-r--r-- | src/main/java/gregtech/api/recipe/check/FindRecipeResult.java | 40 | ||||
-rw-r--r-- | src/main/java/gregtech/api/recipe/check/RecipeValidator.java | 80 |
2 files changed, 109 insertions, 11 deletions
diff --git a/src/main/java/gregtech/api/recipe/check/FindRecipeResult.java b/src/main/java/gregtech/api/recipe/check/FindRecipeResult.java index 5791cb05e1..fa0e251fa1 100644 --- a/src/main/java/gregtech/api/recipe/check/FindRecipeResult.java +++ b/src/main/java/gregtech/api/recipe/check/FindRecipeResult.java @@ -5,6 +5,8 @@ import java.util.Objects; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import org.jetbrains.annotations.NotNull; + import gregtech.api.util.GT_Recipe; /** @@ -17,6 +19,8 @@ public class FindRecipeResult { private final State state; @Nullable private final GT_Recipe recipe; + @Nullable + private RecipeValidator recipeValidator; private FindRecipeResult(@Nonnull State state, @Nullable GT_Recipe recipe) { this.state = state; @@ -41,7 +45,7 @@ public class FindRecipeResult { } /** - * You should use this ONLY WHEN state == FOUND or INSUFFICIENT_VOLTAGE. + * You should use this ONLY WHEN state == FOUND. */ @Nonnull public GT_Recipe getRecipeNonNull() { @@ -49,17 +53,35 @@ public class FindRecipeResult { } /** - * Successfully found recipe. + * Gets recipeValidator if it is not null. + * Be sure to call hasRecipeValidator before to determine if recipeValidator exists + * + * @return not null recipe validator */ - public static FindRecipeResult ofSuccess(@Nonnull GT_Recipe recipe) { - return new FindRecipeResult(State.FOUND, Objects.requireNonNull(recipe)); + @NotNull + public RecipeValidator getRecipeValidator() { + return Objects.requireNonNull(recipeValidator); + } + + /** + * Sets recipeValidator which used to get this result + */ + public void setRecipeValidator(@Nullable RecipeValidator recipeValidator) { + this.recipeValidator = recipeValidator; } /** - * Recipe was found, but voltage is not sufficient to run. + * Gets if this result has recipeValidator */ - public static FindRecipeResult ofInsufficientVoltage(@Nonnull GT_Recipe recipe) { - return new FindRecipeResult(State.INSUFFICIENT_VOLTAGE, Objects.requireNonNull(recipe)); + public boolean hasRecipeValidator() { + return recipeValidator != null; + } + + /** + * Successfully found recipe. + */ + public static FindRecipeResult ofSuccess(@Nonnull GT_Recipe recipe) { + return new FindRecipeResult(State.FOUND, Objects.requireNonNull(recipe)); } /** @@ -82,10 +104,6 @@ public class FindRecipeResult { */ FOUND(true), /** - * Recipe was found, but voltage is not sufficient to run. - */ - INSUFFICIENT_VOLTAGE(false), - /** * No recipe found. */ NOT_FOUND(false), diff --git a/src/main/java/gregtech/api/recipe/check/RecipeValidator.java b/src/main/java/gregtech/api/recipe/check/RecipeValidator.java new file mode 100644 index 0000000000..8fb7b87cfe --- /dev/null +++ b/src/main/java/gregtech/api/recipe/check/RecipeValidator.java @@ -0,0 +1,80 @@ +package gregtech.api.recipe.check; + +import java.util.function.Function; +import java.util.function.Predicate; + +import gregtech.api.util.GT_OverclockCalculator; +import gregtech.api.util.GT_ParallelHelper; +import gregtech.api.util.GT_Recipe; + +/** + * Predicate for simple recipe validation. + * Also store some validation results for reusing it + */ +public class RecipeValidator implements Predicate<GT_Recipe> { + + private CheckRecipeResult firstCheckResult; + private CheckRecipeResult lastCheckResult; + private GT_ParallelHelper lastParallelHelper; + private GT_OverclockCalculator lastOverclockCalculator; + private boolean wasExecutedAtLeastOnce = false; + private final Function<GT_Recipe, CheckRecipeResult> recipeValidator; + private final Function<GT_Recipe, GT_ParallelHelper> parallelHelperFactory; + private final Function<GT_Recipe, GT_OverclockCalculator> overclockCalculatorFactory; + + public RecipeValidator(Function<GT_Recipe, CheckRecipeResult> recipeValidator, + Function<GT_Recipe, GT_ParallelHelper> parallelHelperFactory, + Function<GT_Recipe, GT_OverclockCalculator> overclockCalculatorFactory) { + this.recipeValidator = recipeValidator; + this.parallelHelperFactory = parallelHelperFactory; + this.overclockCalculatorFactory = overclockCalculatorFactory; + } + + @Override + public boolean test(GT_Recipe recipe) { + wasExecutedAtLeastOnce = true; + CheckRecipeResult checkRecipeResult = checkRecipe(recipe); + if (firstCheckResult == null) { + firstCheckResult = checkRecipeResult; + } + return checkRecipeResult.wasSuccessful(); + } + + private CheckRecipeResult checkRecipe(GT_Recipe recipe) { + lastCheckResult = recipeValidator.apply(recipe); + + if (!lastCheckResult.wasSuccessful()) { + return lastCheckResult; + } + + lastParallelHelper = parallelHelperFactory.apply(recipe); + lastOverclockCalculator = overclockCalculatorFactory.apply(recipe); + lastParallelHelper.setCalculator(lastOverclockCalculator); + lastParallelHelper.build(); + + return lastParallelHelper.getResult(); + } + + public Boolean isExecutedAtLeastOnce() { + return wasExecutedAtLeastOnce; + } + + /** + * Gets first check result in case if nothing matching recipe found. + */ + public CheckRecipeResult getFirstCheckResult() { + return firstCheckResult; + } + + public CheckRecipeResult getLastCheckResult() { + return lastCheckResult; + } + + public GT_ParallelHelper getLastParallelHelper() { + return lastParallelHelper; + } + + public GT_OverclockCalculator getLastOverclockCalculator() { + return lastOverclockCalculator; + } +} |