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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
package me.shedaniel.rei.client;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import me.shedaniel.rei.RoughlyEnoughItemsCore;
import me.shedaniel.rei.api.IRecipeCategory;
import me.shedaniel.rei.api.IRecipeCategoryCraftable;
import me.shedaniel.rei.api.IRecipeDisplay;
import me.shedaniel.rei.api.IRecipePlugin;
import me.shedaniel.rei.listeners.RecipeSync;
import net.minecraft.client.gui.Gui;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.RecipeManager;
import net.minecraft.util.Identifier;
import java.util.*;
import java.util.stream.Collectors;
public class RecipeHelper implements RecipeSync {
private static Map<Identifier, List<IRecipeDisplay>> recipeCategoryListMap;
private static List<IRecipeCategory> categories;
private static RecipeManager recipeManager;
private static Map<Class, IRecipeCategoryCraftable> craftables;
public RecipeHelper() {
this.recipeCategoryListMap = Maps.newHashMap();
this.categories = Lists.newArrayList();
this.craftables = Maps.newHashMap();
}
public static List<ItemStack> findCraftableByItems(List<ItemStack> inventoryItems) {
List<ItemStack> craftables = new ArrayList<>();
for(List<IRecipeDisplay> value : recipeCategoryListMap.values())
for(IRecipeDisplay recipeDisplay : value) {
int slotsCraftable = 0;
List<List<ItemStack>> requiredInput = (List<List<ItemStack>>) recipeDisplay.getRequiredItems();
for(List<ItemStack> slot : requiredInput) {
if (slot.isEmpty()) {
slotsCraftable++;
continue;
}
boolean slotDone = false;
for(ItemStack possibleType : inventoryItems) {
for(ItemStack slotPossible : slot)
if (ItemStack.areEqualIgnoreTags(slotPossible, possibleType)) {
slotsCraftable++;
slotDone = true;
break;
}
if (slotDone)
break;
}
}
if (slotsCraftable == recipeDisplay.getRequiredItems().size())
craftables.addAll((List<ItemStack>) recipeDisplay.getOutput());
}
return craftables.stream().distinct().collect(Collectors.toList());
}
public static void registerCategory(IRecipeCategory category) {
categories.add(0, category);
recipeCategoryListMap.put(category.getIdentifier(), Lists.newArrayList());
}
public static void registerRecipe(Identifier categoryIdentifier, IRecipeDisplay display) {
if (!recipeCategoryListMap.containsKey(categoryIdentifier))
return;
recipeCategoryListMap.get(categoryIdentifier).add(display);
}
public static Map<IRecipeCategory, List<IRecipeDisplay>> getRecipesFor(ItemStack stack) {
Map<Identifier, List<IRecipeDisplay>> categoriesMap = new HashMap<>();
categories.forEach(f -> categoriesMap.put(f.getIdentifier(), new LinkedList<>()));
for(List<IRecipeDisplay> value : recipeCategoryListMap.values())
for(IRecipeDisplay recipeDisplay : value)
for(ItemStack outputStack : (List<ItemStack>) recipeDisplay.getOutput())
if (ItemStack.areEqualIgnoreTags(stack, outputStack))
categoriesMap.get(recipeDisplay.getRecipeCategory()).add(recipeDisplay);
categoriesMap.keySet().removeIf(f -> categoriesMap.get(f).isEmpty());
Map<IRecipeCategory, List<IRecipeDisplay>> recipeCategoryListMap = Maps.newHashMap();
categories.forEach(category -> {
if (categoriesMap.containsKey(category.getIdentifier()))
recipeCategoryListMap.put(category, categoriesMap.get(category.getIdentifier()));
});
return recipeCategoryListMap;
}
public static RecipeManager getRecipeManager() {
return recipeManager;
}
public static Map<IRecipeCategory, List<IRecipeDisplay>> getUsagesFor(ItemStack stack) {
Map<Identifier, List<IRecipeDisplay>> categoriesMap = new HashMap<>();
categories.forEach(f -> categoriesMap.put(f.getIdentifier(), new LinkedList<>()));
for(List<IRecipeDisplay> value : recipeCategoryListMap.values())
for(IRecipeDisplay recipeDisplay : value) {
boolean found = false;
for(List<ItemStack> input : (List<List<ItemStack>>) recipeDisplay.getInput()) {
for(ItemStack itemStack : input) {
if (ItemStack.areEqualIgnoreTags(itemStack, stack)) {
categoriesMap.get(recipeDisplay.getRecipeCategory()).add(recipeDisplay);
found = true;
break;
}
}
if (found)
break;
}
}
categoriesMap.keySet().removeIf(f -> categoriesMap.get(f).isEmpty());
Map<IRecipeCategory, List<IRecipeDisplay>> recipeCategoryListMap = Maps.newHashMap();
categories.forEach(category -> {
if (categoriesMap.containsKey(category.getIdentifier()))
recipeCategoryListMap.put(category, categoriesMap.get(category.getIdentifier()));
});
return recipeCategoryListMap;
}
public static List<IRecipeCategory> getCategories() {
return categories;
}
public static void registerCategoryCraftable(Class<? extends IRecipeDisplay> guiClass, IRecipeCategoryCraftable categoryCraftable) {
craftables.put(guiClass, categoryCraftable);
}
public static void registerCategoryCraftable(Class<? extends IRecipeDisplay>[] guiClasses, IRecipeCategoryCraftable categoryCraftable) {
for(Class<? extends IRecipeDisplay> guiClass : guiClasses) craftables.put(guiClass, categoryCraftable);
}
public static IRecipeCategoryCraftable getCategoryCraftable(IRecipeDisplay gui) {
if (!craftables.containsKey(gui.getClass()))
return null;
return craftables.get(gui.getClass());
}
@Override
public void recipesLoaded(RecipeManager recipeManager) {
this.recipeManager = recipeManager;
this.recipeCategoryListMap.clear();
this.categories.clear();
this.craftables.clear();
RoughlyEnoughItemsCore.getListeners(IRecipePlugin.class).forEach(plugin -> {
plugin.registerPluginCategories();
plugin.registerRecipes();
plugin.registerAutoCraftingGui();
});
Collections.reverse(categories);
RoughlyEnoughItemsCore.LOGGER.info("Registered REI Categories: " + String.join(", ", categories.stream().map(category -> {
return category.getCategoryName();
}).collect(Collectors.toList())));
}
}
|