/
DisplayCauldronRecipe.java
325 lines (285 loc) · 9.65 KB
/
DisplayCauldronRecipe.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package knightminer.inspirations.library.recipe.cauldron.util;
import knightminer.inspirations.Inspirations;
import knightminer.inspirations.library.recipe.cauldron.CauldronContentTypes;
import knightminer.inspirations.library.recipe.cauldron.contents.EmptyCauldronContents;
import knightminer.inspirations.library.recipe.cauldron.contents.ICauldronContents;
import knightminer.inspirations.library.recipe.cauldron.recipe.ICauldronRecipe;
import knightminer.inspirations.library.recipe.cauldron.recipe.ICauldronRecipeDisplay;
import net.minecraft.fluid.Fluids;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.util.IItemProvider;
import net.minecraft.util.LazyValue;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.fluids.FluidAttributes;
import net.minecraftforge.fluids.FluidStack;
import slimeknights.mantle.recipe.ICustomOutputRecipe;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Helper class to build a JEI display recipe for use in {@link slimeknights.mantle.recipe.IMultiRecipe}
* TODO: make mantle IMultiRecipe not require IRecipe
*/
public class DisplayCauldronRecipe implements ICauldronRecipeDisplay, ICustomOutputRecipe<IInventory> {
private static final ResourceLocation ID = Inspirations.getResource("dynamic_display");
/** Lazy getter for water */
public static final LazyValue<List<ICauldronContents>> WATER_CONTENTS = new LazyValue<>(() -> Collections.singletonList(CauldronContentTypes.FLUID.of(Fluids.WATER)));
public static final LazyValue<List<FluidStack>> WATER_FLUID = new LazyValue<>(() -> Collections.singletonList(new FluidStack(Fluids.WATER, FluidAttributes.BUCKET_VOLUME)));
// inputs
private final List<ItemStack> itemInputs;
private final List<ICauldronContents> contentInputs;
private final List<FluidStack> fluidInputs;
private final int levelInput;
// outputs
private final ItemStack itemOutput;
private final ICauldronContents contentOutputs;
private final FluidStack fluidOutput;
private final int levelOutput;
// misc
private final TemperaturePredicate temperature;
private final int time;
/** Constructor, use the builder to create */
private DisplayCauldronRecipe(List<ItemStack> itemInputs, List<ICauldronContents> contentInputs, List<FluidStack> fluidInputs, int levelInput,
ItemStack itemOutput, ICauldronContents contentOutputs, FluidStack fluidOutput, int levelOutput, TemperaturePredicate temperature, int time) {
this.fluidInputs = fluidInputs;
this.levelInput = levelInput;
this.itemInputs = itemInputs;
this.contentInputs = contentInputs;
this.fluidOutput = fluidOutput;
this.levelOutput = levelOutput;
this.itemOutput = itemOutput;
this.contentOutputs = contentOutputs;
this.temperature = temperature;
this.time = time;
}
/**
* Creates a new builder using the given levels
* @param levelInput Input level
* @param levelOutput Output level
* @return Builder instance
*/
public static Builder builder(int levelInput, int levelOutput) {
return new Builder(levelInput, levelOutput);
}
/**
* Creates a new builder for an unchanging level
* @param level Level
* @return Builder instance
*/
public static Builder builder(int level) {
return builder(level, level);
}
/**
* Gets an optional fluidstack from the given contents
* @param contents Contents instance
* @return Fluid stack if its a fluid contents
*/
public static Optional<FluidStack> getFluid(ICauldronContents contents) {
return contents.get(CauldronContentTypes.FLUID).map(fluid -> new FluidStack(fluid, FluidAttributes.BUCKET_VOLUME));
}
/* Input */
@Override
public List<ItemStack> getItemInputs() {
return itemInputs;
}
@Override
public List<ICauldronContents> getContentInputs() {
return contentInputs;
}
@Override
public List<FluidStack> getFluidInputs() {
return fluidInputs;
}
@Override
public int getLevelInput() {
return levelInput;
}
/* Outputs */
@Override
public ItemStack getItemOutput() {
return itemOutput;
}
@Override
public ICauldronContents getContentOutput() {
return contentOutputs;
}
@Override
public FluidStack getFluidOutput() {
return fluidOutput;
}
@Override
public int getLevelOutput() {
return levelOutput;
}
/* Misc */
@Override
public TemperaturePredicate getTemperature() {
return temperature;
}
@Override
public int getTime() {
return time;
}
/* IRecipe methods */
/** @deprecated unsupported method */
@Deprecated
@Override
public boolean matches(IInventory inv, World worldIn) {
return false;
}
/** @deprecated unsupported method */
@Deprecated
@Override
public ResourceLocation getId() {
return ID;
}
/** @deprecated unsupported method */
@Deprecated
@Override
public IRecipeSerializer<?> getSerializer() {
throw new UnsupportedOperationException();
}
/** @deprecated unsupported method */
@Deprecated
@Override
public IRecipeType<?> getType() {
throw new UnsupportedOperationException();
}
/** Builder class */
public static class Builder {
private final int levelInput;
private final int levelOutput;
private List<ItemStack> itemInputs = Collections.emptyList();
private List<ICauldronContents> contentInputs = Collections.emptyList();
private ItemStack itemOutput = ItemStack.EMPTY;
private ICauldronContents contentOutputs = EmptyCauldronContents.INSTANCE;
private TemperaturePredicate temperature = TemperaturePredicate.ANY;
private int time = -1;
private Builder(int levelInput, int levelOutput) {
this.levelInput = MathHelper.clamp(levelInput, 0, ICauldronRecipe.MAX);
this.levelOutput = MathHelper.clamp(levelOutput, 0, ICauldronRecipe.MAX);
}
/* Input */
/**
* Sets item inputs
* @param input Input list
* @return Builder instance
*/
public Builder setItemInputs(List<ItemStack> input) {
this.itemInputs = input;
return this;
}
/**
* Sets item inputs
* @param input Input stack
* @return Builder instance
*/
public Builder setItemInputs(ItemStack input) {
return setItemInputs(Collections.singletonList(input));
}
/**
* Sets item inputs
* @param input Input item
* @return Builder instance
*/
public Builder setItemInputs(IItemProvider input) {
return setItemInputs(new ItemStack(input));
}
/**
* Sets the cauldron content inputs
* @param input Content list
* @return Builder instance
*/
public Builder setContentInputs(List<ICauldronContents> input) {
if (levelInput != 0) {
this.contentInputs = input;
}
return this;
}
/**
* Sets the cauldron content inputs
* @param input Content input
* @return Builder instance
*/
public Builder setContentInputs(ICauldronContents input) {
return setContentInputs(Collections.singletonList(input));
}
/* Output */
/**
* Sets the output item
* @param output Output stack
* @return Builder instance
*/
public Builder setItemOutput(ItemStack output) {
this.itemOutput = output;
return this;
}
/**
* Sets the output item
* @param output Output item
* @return Builder instance
*/
public Builder setItemOutput(IItemProvider output) {
return setItemOutput(new ItemStack(output));
}
/**
* Sets the content output
* @param output Content output
* @return Builder instance
*/
public Builder setContentOutput(ICauldronContents output) {
if (levelOutput != 0) {
this.contentOutputs = output;
}
return this;
}
/* Misc */
/**
* Sets the recipe temperature
* @param temperature Temperature predicate
* @return Builder instance
*/
public Builder setTemperature(TemperaturePredicate temperature) {
this.temperature = temperature;
return this;
}
/**
* Sets the recipe time
* @param time Recipe time in ticks
* @return Builder instance
*/
public Builder setTime(int time) {
this.time = time;
return this;
}
/**
* Builds the final display recipe
* @return Display recipe
*/
public DisplayCauldronRecipe build() {
if (levelInput != 0 && contentInputs.isEmpty()) {
throw new IllegalStateException("Invalid recipe, must have at least one input for level greater than 0");
}
if (levelOutput != 0 && contentOutputs == EmptyCauldronContents.INSTANCE) {
throw new IllegalStateException("Invalid recipe, must have at least one output for level greater than 0");
}
// gather fluid inputs and outputs
List<FluidStack> fluidInputs = contentInputs.stream()
.flatMap(contents -> getFluid(contents).map(Stream::of).orElseGet(Stream::empty))
.collect(Collectors.toList());
if (fluidInputs.size() != contentInputs.size()) {
fluidInputs = Collections.emptyList();
}
FluidStack fluidOutput = getFluid(contentOutputs).orElse(FluidStack.EMPTY);
// return recipe
return new DisplayCauldronRecipe(itemInputs, contentInputs, fluidInputs, levelInput, itemOutput, contentOutputs, fluidOutput, levelOutput, temperature, time);
}
}
}