aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/recipe/check/FindRecipeResult.java
blob: fa0e251fa1f2ce9cfe7cd63fb39973dacf13c321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package gregtech.api.recipe.check;

import java.util.Objects;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.jetbrains.annotations.NotNull;

import gregtech.api.util.GT_Recipe;

/**
 * Wrapper class to get result of recipe search for recipemap. Note that this only validates recipe input and voltage,
 * and does not involve in actual check in the machine such as output space or special value.
 */
public class FindRecipeResult {

    @Nonnull
    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;
        this.recipe = recipe;
    }

    @Nonnull
    public State getState() {
        return state;
    }

    public boolean isSuccessful() {
        return state.success;
    }

    /**
     * If you already checked {@link #isSuccessful()}, you can use {@link #getRecipeNonNull()} instead.
     */
    @Nullable
    public GT_Recipe getRecipe() {
        return recipe;
    }

    /**
     * You should use this ONLY WHEN state == FOUND.
     */
    @Nonnull
    public GT_Recipe getRecipeNonNull() {
        return Objects.requireNonNull(recipe);
    }

    /**
     * Gets recipeValidator if it is not null.
     * Be sure to call hasRecipeValidator before to determine if recipeValidator exists
     *
     * @return not null recipe validator
     */
    @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;
    }

    /**
     * Gets if this result has recipeValidator
     */
    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));
    }

    /**
     * No recipe found.
     */
    public static final FindRecipeResult NOT_FOUND = new FindRecipeResult(State.NOT_FOUND, null);
    /**
     * For Microwave.
     */
    public static final FindRecipeResult EXPLODE = new FindRecipeResult(State.EXPLODE, null);
    /**
     * For Microwave.
     */
    public static final FindRecipeResult ON_FIRE = new FindRecipeResult(State.ON_FIRE, null);

    public enum State {

        /**
         * Successfully found recipe.
         */
        FOUND(true),
        /**
         * No recipe found.
         */
        NOT_FOUND(false),
        /**
         * For Microwave.
         */
        EXPLODE(false),
        /**
         * For Microwave.
         */
        ON_FIRE(false);

        private final boolean success;

        State(boolean success) {
            this.success = success;
        }
    }
}