• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CyclopsMC / IntegratedCrafting / #479011820

21 Nov 2025 06:51PM UTC coverage: 24.876% (+0.05%) from 24.826%
#479011820

push

github

rubensworks
Bump mod version

752 of 3023 relevant lines covered (24.88%)

0.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.64
/src/main/java/org/cyclops/integratedcrafting/core/RecipeIndexDefault.java
1
package org.cyclops.integratedcrafting.core;
2

3
import com.google.common.collect.Iterators;
4
import com.google.common.collect.Maps;
5
import com.google.common.collect.Sets;
6
import org.cyclops.commoncapabilities.api.capability.recipehandler.IRecipeDefinition;
7
import org.cyclops.commoncapabilities.api.ingredient.IngredientComponent;
8
import org.cyclops.cyclopscore.datastructure.DistinctIterator;
9
import org.cyclops.cyclopscore.datastructure.MultitransformIterator;
10
import org.cyclops.cyclopscore.ingredient.collection.IIngredientMapMutable;
11
import org.cyclops.cyclopscore.ingredient.collection.IngredientHashMap;
12
import org.cyclops.cyclopscore.ingredient.collection.IngredientMapSingleClassified;
13
import org.cyclops.integratedcrafting.api.recipe.IRecipeIndex;
14
import org.cyclops.integratedcrafting.api.recipe.IRecipeIndexModifiable;
15

16
import javax.annotation.Nullable;
17
import java.util.Collections;
18
import java.util.Iterator;
19
import java.util.Map;
20
import java.util.Set;
21

22
/**
23
 * A default implementation of {@link IRecipeIndex} and {@link IRecipeIndexModifiable}.
24
 * @author rubensworks
25
 */
26
public class RecipeIndexDefault implements IRecipeIndexModifiable {
27

28
    private final Map<IngredientComponent<?, ?>, IIngredientMapMutable<?, ?, Set<IRecipeDefinition>>> recipeComponentIndexes;
29
    private final Set<IRecipeDefinition> recipes;
30

31
    public RecipeIndexDefault() {
1✔
32
        this.recipeComponentIndexes = Maps.newIdentityHashMap();
1✔
33
        this.recipes = Sets.newHashSet();
1✔
34
    }
1✔
35

36
    @Override
37
    public Set<IRecipeDefinition> getRecipes() {
38
        return Collections.unmodifiableSet(recipes);
×
39
    }
40

41
    @Override
42
    public <T, M> Iterator<IRecipeDefinition> getRecipes(IngredientComponent<T, M> outputType, T output, M matchCondition) {
43
        IIngredientMapMutable<?, ?, Set<IRecipeDefinition>> index = recipeComponentIndexes.get(outputType);
1✔
44
        if (index == null) {
1✔
45
            return Iterators.forArray();
1✔
46
        }
47
        return new DistinctIterator<>(MultitransformIterator.flattenIterableIterator(
1✔
48
                Iterators.transform(((IIngredientMapMutable<T, M, Set<IRecipeDefinition>>) index)
1✔
49
                        .iterator(output, matchCondition), (entry) -> entry.getValue())), true);
1✔
50
    }
51

52
    @Nullable
53
    protected <T, M> IIngredientMapMutable<T, M, Set<IRecipeDefinition>> initializeIndex(IngredientComponent<T, M> recipeComponent) {
54
        // TODO: Consider moving/copying this logic to IngredientCollectionHelpers in next/major
55
        if (recipeComponent.getCategoryTypes().size() == 1) {
1✔
56
            return new IngredientHashMap<>(recipeComponent);
1✔
57
        }
58
        return new IngredientMapSingleClassified<>(recipeComponent, () -> new IngredientHashMap<>(recipeComponent), recipeComponent.getCategoryTypes().get(0));
1✔
59
    }
60

61
    @Override
62
    public void addRecipe(IRecipeDefinition prioritizedRecipe) {
63
        recipes.add(prioritizedRecipe);
1✔
64
        for (IngredientComponent<?, ?> recipeComponent : prioritizedRecipe.getOutput().getComponents()) {
1✔
65
            IIngredientMapMutable<?, ?, Set<IRecipeDefinition>> index = recipeComponentIndexes.computeIfAbsent(recipeComponent, this::initializeIndex);
1✔
66
            if (index != null) {
1✔
67
                addRecipeForComponent(index, prioritizedRecipe);
1✔
68
            }
69
        }
1✔
70
    }
1✔
71

72
    protected <T, M> void addRecipeForComponent(IIngredientMapMutable<T, M, Set<IRecipeDefinition>> index,
73
                                                IRecipeDefinition prioritizedRecipe) {
74
        for (T instance : prioritizedRecipe.getOutput().getInstances(index.getComponent())) {
1✔
75
            Set<IRecipeDefinition> set = index.get(instance);
1✔
76
            if (set == null) {
1✔
77
                set = Sets.newLinkedHashSet(); // Keeps insertion priority
1✔
78
                index.put(instance, set);
1✔
79
            }
80
            set.add(prioritizedRecipe);
1✔
81
        }
1✔
82
    }
1✔
83

84
    @Override
85
    public void removeRecipe(IRecipeDefinition prioritizedRecipe) {
86
        recipes.remove(prioritizedRecipe);
×
87
        for (IngredientComponent<?, ?> recipeComponent : prioritizedRecipe.getOutput().getComponents()) {
×
88
            IIngredientMapMutable<?, ?, Set<IRecipeDefinition>> index = recipeComponentIndexes.get(recipeComponent);
×
89
            if (index != null) {
×
90
                removeRecipeForComponent(index, prioritizedRecipe);
×
91
            }
92
        }
×
93
    }
×
94

95
    protected <T, M> void removeRecipeForComponent(IIngredientMapMutable<T, M, Set<IRecipeDefinition>> index,
96
                                                   IRecipeDefinition prioritizedRecipe) {
97
        for (T instance : prioritizedRecipe.getOutput().getInstances(index.getComponent())) {
×
98
            Set<IRecipeDefinition> set = index.get(instance);
×
99
            if (set != null) {
×
100
                if (set.remove(prioritizedRecipe)) {
×
101
                    if (set.isEmpty()) {
×
102
                        index.remove(instance);
×
103
                    }
104
                }
105
            }
106
        }
×
107
    }
×
108

109
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc