/
Focus.java
194 lines (170 loc) · 5.9 KB
/
Focus.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
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.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.ItemStack;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidUtil;
public class Focus {
public enum Mode {
INPUT, OUTPUT, NONE
}
private final ItemStack stack;
private final Fluid fluid;
@Nonnull
private Mode mode = Mode.NONE;
private boolean allowsCheating = false;
public Focus() {
this.stack = null;
this.fluid = null;
}
public Focus(ItemStack stack) {
this.stack = stack;
this.fluid = getFluidFromItemStack(stack);
}
public Focus(Fluid fluid) {
this.stack = null;
this.fluid = fluid;
}
@Nullable
private static Fluid 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 fluid;
}
}
FluidStack fluidContained = FluidUtil.getFluidContained(stack);
if (fluidContained != null) {
Fluid fluid = fluidContained.getFluid();
if (fluid != null) {
return fluid;
}
}
return null;
}
public Fluid getFluid() {
return fluid;
}
public ItemStack getStack() {
return stack;
}
public void setMode(@Nonnull Mode mode) {
this.mode = mode;
}
@Nonnull
public Mode getMode() {
return mode;
}
public void setAllowsCheating() {
allowsCheating = true;
}
public boolean allowsCheating() {
return allowsCheating;
}
public boolean equalsFocus(@Nonnull Focus other) {
return ItemStack.areItemStacksEqual(this.stack, other.getStack()) && fluid == other.getFluid() && mode == other.getMode();
}
@Nonnull
public List<IRecipeCategory> getCategories() {
IRecipeRegistry recipeRegistry = Internal.getRuntime().getRecipeRegistry();
if (mode == Mode.INPUT) {
return getInputCategories(recipeRegistry);
} else if (mode == Mode.OUTPUT) {
return getOutputCategories(recipeRegistry);
} else {
return recipeRegistry.getRecipeCategories();
}
}
@Nonnull
private List<IRecipeCategory> getInputCategories(@Nonnull IRecipeRegistry recipeRegistry) {
if (stack != null && fluid != null) {
List<IRecipeCategory> categories = new ArrayList<>(recipeRegistry.getRecipeCategoriesWithInput(stack));
categories.addAll(recipeRegistry.getRecipeCategoriesWithInput(fluid));
return ImmutableSet.copyOf(categories).asList();
}
if (stack != null) {
return recipeRegistry.getRecipeCategoriesWithInput(stack);
} else {
return recipeRegistry.getRecipeCategoriesWithInput(fluid);
}
}
@Nonnull
private List<IRecipeCategory> getOutputCategories(@Nonnull IRecipeRegistry recipeRegistry) {
if (stack != null && fluid != null) {
List<IRecipeCategory> categories = new ArrayList<>(recipeRegistry.getRecipeCategoriesWithOutput(stack));
categories.addAll(recipeRegistry.getRecipeCategoriesWithOutput(fluid));
return ImmutableSet.copyOf(categories).asList();
}
if (stack != null) {
return recipeRegistry.getRecipeCategoriesWithOutput(stack);
} else {
return recipeRegistry.getRecipeCategoriesWithOutput(fluid);
}
}
@Nonnull
public List<Object> getRecipes(@Nonnull IRecipeCategory recipeCategory) {
IRecipeRegistry recipeRegistry = Internal.getRuntime().getRecipeRegistry();
if (mode == Mode.INPUT) {
return getInputRecipes(recipeRegistry, recipeCategory);
} else if (mode == 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 (stack != null && mode == Mode.INPUT) {
StackHelper stackHelper = Internal.getStackHelper();
ItemStack matchingStack = stackHelper.containsStack(craftingItems, stack);
if (matchingStack != null) {
return Collections.singletonList(matchingStack);
}
}
return craftingItems;
}
@Nonnull
private List<Object> getInputRecipes(@Nonnull IRecipeRegistry recipeRegistry, @Nonnull IRecipeCategory recipeCategory) {
if (stack != null && fluid != null) {
List<Object> recipes = new ArrayList<>(recipeRegistry.getRecipesWithInput(recipeCategory, stack));
recipes.addAll(recipeRegistry.getRecipesWithInput(recipeCategory, fluid));
return ImmutableSet.copyOf(recipes).asList();
}
if (stack != null) {
return recipeRegistry.getRecipesWithInput(recipeCategory, stack);
} else {
return recipeRegistry.getRecipesWithInput(recipeCategory, fluid);
}
}
@Nonnull
private List<Object> getOutputRecipes(@Nonnull IRecipeRegistry recipeRegistry, @Nonnull IRecipeCategory recipeCategory) {
if (stack != null && fluid != null) {
List<Object> recipes = new ArrayList<>(recipeRegistry.getRecipesWithOutput(recipeCategory, stack));
recipes.addAll(recipeRegistry.getRecipesWithOutput(recipeCategory, fluid));
return ImmutableSet.copyOf(recipes).asList();
}
if (stack != null) {
return recipeRegistry.getRecipesWithOutput(recipeCategory, stack);
} else {
return recipeRegistry.getRecipesWithOutput(recipeCategory, fluid);
}
}
}