/
Focus.java
161 lines (142 loc) · 4.67 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
package mezz.jei.gui;
import com.google.common.collect.ImmutableSet;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidContainerItem;
import mezz.jei.Internal;
import mezz.jei.api.IRecipeRegistry;
import mezz.jei.api.recipe.IRecipeCategory;
public class Focus {
public enum Mode {
INPUT, OUTPUT, NONE
}
private final ItemStack stack;
private final Fluid fluid;
@Nonnull
private Mode mode = Mode.NONE;
public static Focus create(Object focus) {
if (focus instanceof ItemStack) {
return new Focus((ItemStack) focus);
} else if (focus instanceof Fluid) {
return new Focus((Fluid) focus);
} else if (focus instanceof FluidStack) {
return new Focus(((FluidStack) focus).getFluid());
} else {
return new Focus();
}
}
public Focus() {
this.stack = null;
this.fluid = null;
}
public Focus(ItemStack stack) {
this.stack = stack;
Item item = stack.getItem();
if (item instanceof IFluidContainerItem) {
IFluidContainerItem fluidContainerItem = (IFluidContainerItem) item;
FluidStack fluidStack = fluidContainerItem.getFluid(stack);
this.fluid = fluidStack.getFluid();
} else if (FluidContainerRegistry.isFilledContainer(stack)) {
FluidStack fluidStack = FluidContainerRegistry.getFluidForFilledItem(stack);
this.fluid = fluidStack.getFluid();
} else {
this.fluid = null;
}
}
public Focus(Fluid fluid) {
this.stack = null;
this.fluid = fluid;
}
public Fluid getFluid() {
return fluid;
}
public ItemStack getStack() {
return stack;
}
public boolean isBlank() {
return stack == null && fluid == null;
}
public void setMode(@Nonnull Mode mode) {
this.mode = mode;
}
@Nonnull
public Mode getMode() {
return mode;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Focus)) {
return false;
}
Focus other = (Focus) obj;
return ItemStack.areItemStacksEqual(this.stack, other.getStack()) && fluid == other.fluid && mode == other.mode;
}
@Nonnull
public List<IRecipeCategory> getCategories() {
IRecipeRegistry recipeRegistry = Internal.getRecipeRegistry();
switch (mode) {
case INPUT: {
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);
}
}
case OUTPUT: {
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);
}
}
}
return recipeRegistry.getRecipeCategories();
}
@Nonnull
public List<Object> getRecipes(IRecipeCategory recipeCategory) {
IRecipeRegistry recipeRegistry = Internal.getRecipeRegistry();
switch (mode) {
case INPUT: {
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);
}
}
case OUTPUT: {
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);
}
}
}
return recipeRegistry.getRecipes(recipeCategory);
}
}