/
MasterFocus.java
204 lines (179 loc) · 6.45 KB
/
MasterFocus.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package mezz.jei.gui;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import com.google.common.collect.ImmutableSet;
import mezz.jei.Internal;
import mezz.jei.api.IRecipeRegistry;
import mezz.jei.api.recipe.IFocus;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.util.StackHelper;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemPotion;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidUtil;
public class MasterFocus {
public static MasterFocus create(IFocus<?> focus) {
MasterFocus masterFocus;
Object value = focus.getValue();
if (value instanceof ItemStack) {
masterFocus = new MasterFocus((ItemStack) value);
} else if (value instanceof FluidStack) {
masterFocus = new MasterFocus((FluidStack) value);
} else {
masterFocus = new MasterFocus();
}
masterFocus.setMode(focus.getMode());
return masterFocus;
}
private final ItemStack itemStack;
private final FluidStack fluidStack;
@Nonnull
private IFocus.Mode mode = IFocus.Mode.NONE;
private boolean allowsCheating = false;
public MasterFocus() {
this.itemStack = null;
this.fluidStack = null;
}
public MasterFocus(ItemStack itemStack) {
this.itemStack = itemStack;
this.fluidStack = getFluidFromItemStack(itemStack);
}
public MasterFocus(FluidStack fluidStack) {
this.itemStack = null;
this.fluidStack = fluidStack;
}
@Nullable
private static FluidStack getFluidFromItemStack(@Nonnull ItemStack stack) {
Item item = stack.getItem();
if (item instanceof ItemBlock) {
Block block = ((ItemBlock) item).getBlock();
Fluid fluid = FluidRegistry.lookupFluidForBlock(block);
if (fluid != null) {
return new FluidStack(fluid, Fluid.BUCKET_VOLUME);
}
}
FluidStack fluidContained = FluidUtil.getFluidContained(stack);
if (fluidContained != null) {
return fluidContained;
}
return null;
}
public FluidStack getFluidStack() {
return fluidStack;
}
public ItemStack getItemStack() {
return itemStack;
}
public void setMode(@Nonnull IFocus.Mode mode) {
this.mode = mode;
}
@Nonnull
public IFocus.Mode getMode() {
return mode;
}
public void setAllowsCheating() {
allowsCheating = true;
}
public boolean allowsCheating() {
return allowsCheating;
}
public boolean equalsFocus(@Nonnull MasterFocus other) {
return ItemStack.areItemStacksEqual(this.itemStack, other.getItemStack()) && fluidStack == other.getFluidStack() && mode == other.getMode();
}
@Nonnull
public List<IRecipeCategory> getCategories() {
IRecipeRegistry recipeRegistry = Internal.getRuntime().getRecipeRegistry();
if (mode == IFocus.Mode.INPUT) {
return getInputCategories(recipeRegistry);
} else if (mode == IFocus.Mode.OUTPUT) {
return getOutputCategories(recipeRegistry);
} else {
return recipeRegistry.getRecipeCategories();
}
}
@Nonnull
private List<IRecipeCategory> getInputCategories(@Nonnull IRecipeRegistry recipeRegistry) {
if (itemStack != null && fluidStack != null) {
List<IRecipeCategory> categories = new ArrayList<>(recipeRegistry.getRecipeCategoriesWithInput(itemStack));
categories.addAll(recipeRegistry.getRecipeCategoriesWithInput(fluidStack));
return ImmutableSet.copyOf(categories).asList();
}
if (itemStack != null) {
return recipeRegistry.getRecipeCategoriesWithInput(itemStack);
} else {
return recipeRegistry.getRecipeCategoriesWithInput(fluidStack);
}
}
@Nonnull
private List<IRecipeCategory> getOutputCategories(@Nonnull IRecipeRegistry recipeRegistry) {
if (itemStack != null && fluidStack != null) {
List<IRecipeCategory> categories = new ArrayList<>(recipeRegistry.getRecipeCategoriesWithOutput(itemStack));
categories.addAll(recipeRegistry.getRecipeCategoriesWithOutput(fluidStack));
return ImmutableSet.copyOf(categories).asList();
}
if (itemStack != null) {
return recipeRegistry.getRecipeCategoriesWithOutput(itemStack);
} else {
return recipeRegistry.getRecipeCategoriesWithOutput(fluidStack);
}
}
@Nonnull
public List<Object> getRecipes(@Nonnull IRecipeCategory recipeCategory) {
IRecipeRegistry recipeRegistry = Internal.getRuntime().getRecipeRegistry();
if (mode == IFocus.Mode.INPUT) {
return getInputRecipes(recipeRegistry, recipeCategory);
} else if (mode == IFocus.Mode.OUTPUT) {
return getOutputRecipes(recipeRegistry, recipeCategory);
} else {
return recipeRegistry.getRecipes(recipeCategory);
}
}
@Nonnull
public Collection<ItemStack> getRecipeCategoryCraftingItems(@Nonnull IRecipeCategory recipeCategory) {
IRecipeRegistry recipeRegistry = Internal.getRuntime().getRecipeRegistry();
Collection<ItemStack> craftingItems = recipeRegistry.getCraftingItems(recipeCategory);
if (itemStack != null && mode == IFocus.Mode.INPUT) {
StackHelper stackHelper = Internal.getStackHelper();
ItemStack matchingStack = stackHelper.containsStack(craftingItems, itemStack);
if (matchingStack != null) {
return Collections.singletonList(matchingStack);
}
}
return craftingItems;
}
@Nonnull
private List<Object> getInputRecipes(@Nonnull IRecipeRegistry recipeRegistry, @Nonnull IRecipeCategory recipeCategory) {
if (itemStack != null && fluidStack != null) {
List<Object> recipes = new ArrayList<>(recipeRegistry.getRecipesWithInput(recipeCategory, itemStack));
recipes.addAll(recipeRegistry.getRecipesWithInput(recipeCategory, fluidStack));
return ImmutableSet.copyOf(recipes).asList();
}
if (itemStack != null) {
return recipeRegistry.getRecipesWithInput(recipeCategory, itemStack);
} else {
return recipeRegistry.getRecipesWithInput(recipeCategory, fluidStack);
}
}
@Nonnull
private List<Object> getOutputRecipes(@Nonnull IRecipeRegistry recipeRegistry, @Nonnull IRecipeCategory recipeCategory) {
if (itemStack != null && fluidStack != null) {
List<Object> recipes = new ArrayList<>(recipeRegistry.getRecipesWithOutput(recipeCategory, itemStack));
recipes.addAll(recipeRegistry.getRecipesWithOutput(recipeCategory, fluidStack));
return ImmutableSet.copyOf(recipes).asList();
}
if (itemStack != null) {
return recipeRegistry.getRecipesWithOutput(recipeCategory, itemStack);
} else {
return recipeRegistry.getRecipesWithOutput(recipeCategory, fluidStack);
}
}
}