/
RecipeMap.java
129 lines (108 loc) · 4.54 KB
/
RecipeMap.java
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
126
127
128
129
package mezz.jei.util;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.google.common.collect.Table;
import mezz.jei.Internal;
import mezz.jei.api.recipe.IRecipeCategory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
/**
* A RecipeMap efficiently links Recipes, IRecipeCategory, and ItemStacks.
*/
public class RecipeMap {
private final Table<IRecipeCategory, String, List<Object>> recipeTable = HashBasedTable.create();
private final ArrayListMultimap<String, IRecipeCategory> categoryMap = ArrayListMultimap.create();
private final Ordering<IRecipeCategory> recipeCategoryOrdering;
public RecipeMap(final RecipeCategoryComparator recipeCategoryComparator) {
this.recipeCategoryOrdering = Ordering.from(recipeCategoryComparator);
}
public ImmutableList<IRecipeCategory> getRecipeCategories(ItemStack itemStack) {
Set<IRecipeCategory> recipeCategories = new HashSet<IRecipeCategory>();
for (String stackKey : Internal.getStackHelper().getUniqueIdentifiersWithWildcard(itemStack)) {
recipeCategories.addAll(categoryMap.get(stackKey));
}
return recipeCategoryOrdering.immutableSortedCopy(recipeCategories);
}
public ImmutableList<IRecipeCategory> getRecipeCategories(FluidStack fluid) {
String key = getKeyForFluid(fluid);
return recipeCategoryOrdering.immutableSortedCopy(categoryMap.get(key));
}
public void addRecipeCategory(IRecipeCategory recipeCategory, ItemStack itemStack) {
String stackKey = Internal.getStackHelper().getUniqueIdentifierForStack(itemStack);
List<IRecipeCategory> recipeCategories = categoryMap.get(stackKey);
if (!recipeCategories.contains(recipeCategory)) {
recipeCategories.add(recipeCategory);
}
}
private void addRecipeCategory(IRecipeCategory recipeCategory, FluidStack fluidStack) {
String key = getKeyForFluid(fluidStack);
List<IRecipeCategory> recipeCategories = categoryMap.get(key);
if (!recipeCategories.contains(recipeCategory)) {
recipeCategories.add(recipeCategory);
}
}
private String getKeyForFluid(FluidStack fluidStack) {
if (fluidStack.tag != null) {
return "fluid:" + fluidStack.getFluid().getName() + ":" + fluidStack.tag;
}
return "fluid:" + fluidStack.getFluid().getName();
}
public ImmutableList<Object> getRecipes(IRecipeCategory recipeCategory, ItemStack stack) {
Map<String, List<Object>> recipesForType = recipeTable.row(recipeCategory);
ImmutableList.Builder<Object> listBuilder = ImmutableList.builder();
for (String name : Internal.getStackHelper().getUniqueIdentifiersWithWildcard(stack)) {
List<Object> recipes = recipesForType.get(name);
if (recipes != null) {
listBuilder.addAll(recipes);
}
}
return listBuilder.build();
}
public List<Object> getRecipes(IRecipeCategory recipeCategory, FluidStack fluidStack) {
Map<String, List<Object>> recipesForType = recipeTable.row(recipeCategory);
String name = getKeyForFluid(fluidStack);
List<Object> recipes = recipesForType.get(name);
if (recipes == null) {
return ImmutableList.of();
}
return Collections.unmodifiableList(recipes);
}
public void addRecipe(Object recipe, IRecipeCategory recipeCategory, Iterable<ItemStack> itemStacks, Iterable<FluidStack> fluidStacks) {
Map<String, List<Object>> recipesForType = recipeTable.row(recipeCategory);
StackHelper stackHelper = Internal.getStackHelper();
for (ItemStack itemStack : itemStacks) {
if (itemStack == null) {
continue;
}
String stackKey = stackHelper.getUniqueIdentifierForStack(itemStack);
List<Object> recipes = recipesForType.get(stackKey);
if (recipes == null) {
recipes = Lists.newArrayList();
recipesForType.put(stackKey, recipes);
}
recipes.add(recipe);
addRecipeCategory(recipeCategory, itemStack);
}
for (FluidStack fluidStack : fluidStacks) {
if (fluidStack == null || fluidStack.getFluid() == null) {
continue;
}
String fluidKey = getKeyForFluid(fluidStack);
List<Object> recipes = recipesForType.get(fluidKey);
if (recipes == null) {
recipes = Lists.newArrayList();
recipesForType.put(fluidKey, recipes);
}
recipes.add(recipe);
addRecipeCategory(recipeCategory, fluidStack);
}
}
}