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
156
157
|
package me.shedaniel.impl;
import me.shedaniel.api.IREIPlugin;
import me.shedaniel.api.IDisplayCategory;
import me.shedaniel.api.IRecipe;
import me.shedaniel.api.IRecipeManager;
import me.shedaniel.gui.RecipeGui;
import net.minecraft.client.Minecraft;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.RecipeManager;
import org.dimdev.riftloader.RiftLoader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* Created by James on 8/7/2018.
*/
public class REIRecipeManager implements IRecipeManager {
private Map<String, List<IRecipe>> recipeList;
private List<IDisplayCategory> displayAdapters;
public static RecipeManager recipeManager;
private static REIRecipeManager myInstance;
private REIRecipeManager() {
recipeList = new HashMap<>();
displayAdapters = new LinkedList<>();
}
public List<IDisplayCategory> getDisplayAdapters() {
return displayAdapters;
}
public static REIRecipeManager instance() {
if (myInstance == null) {
System.out.println("Newing me up.");
myInstance = new REIRecipeManager();
}
return myInstance;
}
@Override
public void addRecipe(String id, IRecipe recipe) {
if (recipeList.containsKey(id)) {
recipeList.get(id).add(recipe);
} else {
List<IRecipe> recipes = new LinkedList<>();
recipeList.put(id, recipes);
recipes.add(recipe);
}
}
@Override
public void addRecipe(String id, List<? extends IRecipe> recipes) {
if (recipeList.containsKey(id)) {
recipeList.get(id).addAll(recipes);
} else {
List<IRecipe> newRecipeList = new LinkedList<>();
recipeList.put(id, newRecipeList);
newRecipeList.addAll(recipes);
}
}
@Override
public void addDisplayAdapter(IDisplayCategory adapter) {
displayAdapters.add(adapter);
}
@Override
public Map<IDisplayCategory, List<IRecipe>> getRecipesFor(ItemStack stack) {
Map<IDisplayCategory, List<IRecipe>> categories = new HashMap<>();
displayAdapters.forEach(f -> categories.put(f, new LinkedList<>()));
for(List<IRecipe> value : recipeList.values()) {
for(IRecipe iRecipe : value) {
for(Object o : iRecipe.getOutput()) {
if (o instanceof ItemStack) {
if (ItemStack.areItemsEqual(stack, (ItemStack) o)) {
for(IDisplayCategory iDisplayCategory : categories.keySet()) {
if (iDisplayCategory.getId() == iRecipe.getId()) {
categories.get(iDisplayCategory).add(iRecipe);
}
}
}
}
}
}
}
categories.keySet().removeIf(f -> categories.get(f).isEmpty());
return categories;
}
public Map<IDisplayCategory, List<IRecipe>> getUsesFor(ItemStack stack) {
Map<IDisplayCategory, List<IRecipe>> categories = new HashMap<>();
displayAdapters.forEach(f -> categories.put(f, new LinkedList<>()));
for(List<IRecipe> value : recipeList.values()) {
for(IRecipe iRecipe : value) {
boolean found = false;
for(Object o : iRecipe.getInput()) {
List<ItemStack> input = (List<ItemStack>) o;
for(ItemStack itemStack : input) {
if (ItemStack.areItemsEqual(itemStack, stack)) {
for(IDisplayCategory iDisplayCategory : categories.keySet()) {
if (iDisplayCategory.getId() == iRecipe.getId()) {
categories.get(iDisplayCategory).add(iRecipe);
found = true;
}
}
if (found)
break;
}
}
if (found)
break;
}
}
}
categories.keySet().removeIf(f -> categories.get(f).isEmpty());
return categories;
}
public List<IDisplayCategory> getAdatapersForOutput(ItemStack stack) {
return null;
}
public List<IDisplayCategory> getAdaptersForOutput(Item item) {
return null;
}
public void RecipesLoaded(RecipeManager manager) {
recipeList.clear();
displayAdapters.clear();
REIRecipeManager.instance().recipeManager = manager;
RiftLoader.instance.getListeners(IREIPlugin.class).forEach(IREIPlugin::register);
}
public void displayRecipesFor(ItemStack stack) {
Map<IDisplayCategory, List<IRecipe>> recipes = REIRecipeManager.instance().getRecipesFor(stack);
if (recipes.isEmpty())
return;
RecipeGui gui = new RecipeGui(null, Minecraft.getInstance().currentScreen, recipes);
Minecraft.getInstance().displayGuiScreen(gui);
}
public void displayUsesFor(ItemStack stack) {
Map<IDisplayCategory, List<IRecipe>> recipes = REIRecipeManager.instance().getUsesFor(stack);
if (recipes.isEmpty())
return;
RecipeGui gui = new RecipeGui(null, Minecraft.getInstance().currentScreen, recipes);
Minecraft.getInstance().displayGuiScreen(gui);
}
}
|