aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/interfaces/IGT_RecipeMap.java
blob: 69f87161d142a10e2ecdaedf50946f54a5374cc6 (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
package gregtech.api.interfaces;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;

import javax.annotation.Nonnull;

import gregtech.api.util.GT_Recipe;
import gregtech.api.util.GT_RecipeBuilder;
import gregtech.api.util.GT_Utility;

/**
 * Represents the target of a recipe adding action, usually, but not necessarily, is a recipe map itself.
 */
public interface IGT_RecipeMap {

    /**
     * Add a downstream recipe map that will get to handle the original builder.
     * 
     * @param downstream
     */
    void addDownstream(IGT_RecipeMap downstream);

    /**
     * Actually add the recipe represented by the builder. CAN modify the builder's internal states!!!
     */
    @Nonnull
    Collection<GT_Recipe> doAdd(GT_RecipeBuilder builder);

    /**
     * Return a variant of this recipe map that will perform a deep copy on input recipe builder before doing anything
     * to it.
     *
     * The returned recipe map will not have any downstreams, but can accept new downstreams.
     */
    default IGT_RecipeMap deepCopyInput() {
        return newRecipeMap(b -> doAdd(b.copy()));
    }

    static IGT_RecipeMap newRecipeMap(Function<? super GT_RecipeBuilder, Collection<GT_Recipe>> func) {
        return new IGT_RecipeMap() {

            private final Collection<IGT_RecipeMap> downstreams = new ArrayList<>();

            @Override
            public void addDownstream(IGT_RecipeMap downstream) {
                downstreams.add(downstream);
            }

            @Nonnull
            @Override
            public Collection<GT_Recipe> doAdd(GT_RecipeBuilder builder) {
                List<Collection<GT_Recipe>> ret = new ArrayList<>();
                ret.add(func.apply(builder));
                for (IGT_RecipeMap downstream : downstreams) {
                    ret.add(downstream.doAdd(builder));
                }
                return GT_Utility.concat(ret);
            }
        };
    }
}