/
BrewingCauldronRecipe.java
359 lines (325 loc) · 13.3 KB
/
BrewingCauldronRecipe.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package knightminer.inspirations.recipes.recipe.cauldron;
import com.google.gson.JsonObject;
import knightminer.inspirations.library.recipe.DynamicFinishedRecipe;
import knightminer.inspirations.library.recipe.RecipeSerializer;
import knightminer.inspirations.library.recipe.RecipeSerializers;
import knightminer.inspirations.library.recipe.cauldron.CauldronContentTypes;
import knightminer.inspirations.library.recipe.cauldron.inventory.ICauldronInventory;
import knightminer.inspirations.library.recipe.cauldron.inventory.IModifyableCauldronInventory;
import knightminer.inspirations.library.recipe.cauldron.recipe.ICauldronRecipe;
import knightminer.inspirations.library.recipe.cauldron.util.CauldronTemperature;
import knightminer.inspirations.library.recipe.cauldron.util.DisplayCauldronRecipe;
import knightminer.inspirations.library.recipe.cauldron.util.TemperaturePredicate;
import knightminer.inspirations.library.util.ReflectionUtil;
import knightminer.inspirations.tweaks.recipe.NormalBrewingRecipe;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.network.PacketBuffer;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionBrewing;
import net.minecraft.potion.PotionUtils;
import net.minecraft.potion.Potions;
import net.minecraft.util.JSONUtils;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvents;
import net.minecraft.world.World;
import net.minecraftforge.common.brewing.BrewingRecipe;
import net.minecraftforge.common.brewing.BrewingRecipeRegistry;
import net.minecraftforge.common.brewing.IBrewingRecipe;
import net.minecraftforge.common.brewing.VanillaBrewingRecipe;
import slimeknights.mantle.recipe.IMultiRecipe;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Shared logic for both potion brewing recipe types
*/
public abstract class BrewingCauldronRecipe implements ICauldronRecipe, IMultiRecipe<DisplayCauldronRecipe> {
private final ResourceLocation id;
private final boolean instant;
private List<DisplayCauldronRecipe> displayRecipes;
/**
* Creates a new recipe instance
* @param id Recipe ID
* @param instant If true, recipe produces potion. If false, recipe produces unfermented potion
*/
protected BrewingCauldronRecipe(ResourceLocation id, boolean instant) {
this.id = id;
this.instant = instant;
}
@Override
public boolean matches(ICauldronInventory inv, World worldIn) {
// must have liquid and not be boiling
if (inv.getLevel() == 0 || inv.getTemperature() != CauldronTemperature.BOILING) {
return false;
}
// must have an item, and if the cauldron has 2 levels we must have two of it
ItemStack stack = inv.getStack();
if (stack.isEmpty()) {
return false;
}
// find a matching predicate and we are good
return inv.getContents()
.get(CauldronContentTypes.POTION)
.filter(value -> getResult(value, stack) != Potions.EMPTY)
.isPresent();
}
@Override
public void handleRecipe(IModifyableCauldronInventory inventory) {
inventory.getContents().get(CauldronContentTypes.POTION).ifPresent(potion -> {
// find a mix
ItemStack stack = inventory.getStack();
Potion output = getResult(potion, stack);
if (output != Potions.EMPTY) {
// change the potion
inventory.setContents((instant ? CauldronContentTypes.POTION : CauldronContentTypes.UNFERMENTED_POTION).of(output));
// shrink the stack in hand and return the container
ItemStack container = stack.getContainerItem().copy();
inventory.shrinkStack(1);
inventory.setOrGiveStack(container);
// play sound
inventory.playSound(SoundEvents.ENTITY_GENERIC_SPLASH);
}
});
}
/**
* Gets the resulting potion for this combination, or empty if none
* @param potion Potion input
* @param stack Stack input
* @return Resulting potion, or empty if not recipe
*/
protected abstract Potion getResult(Potion potion, ItemStack stack);
@Override
public ResourceLocation getId() {
return id;
}
/**
* Makes a display brewing recipe from the given inputs
* @param input Potion input
* @param reagent Potion reagent
* @param output Potion output
* @return Recipe instance
*/
protected DisplayCauldronRecipe makeRecipe(Potion input, Ingredient reagent, Potion output) {
return DisplayCauldronRecipe.builder(3)
.setTemperature(TemperaturePredicate.BOILING)
.setItemInputs(Arrays.asList(reagent.getMatchingStacks()))
.setContentInputs(CauldronContentTypes.POTION.of(input))
.setContentOutput((instant ? CauldronContentTypes.POTION : CauldronContentTypes.UNFERMENTED_POTION).of(output))
.build();
}
/**
* Gets a list of recipes for display. Does not need to be cached
* @return List of display recipes
*/
protected abstract List<DisplayCauldronRecipe> getDisplayRecipes();
@Override
public List<DisplayCauldronRecipe> getRecipes() {
if (displayRecipes == null) {
displayRecipes = getDisplayRecipes();
}
return displayRecipes;
}
/** Brewing cauldron recipe for the vanilla {@link PotionBrewing} */
public static class Vanilla extends BrewingCauldronRecipe {
/** Last successful match among potion mixes */
private Object lastMix;
/**
* Creates a new recipe instance
* @param id Recipe ID
* @param instant If true, recipe produces potion. If false, recipe produces unfermented potion
*/
public Vanilla(ResourceLocation id, boolean instant) {
super(id, instant);
}
/**
* Checks if the given mix predicate matches
* @param mix Mix predicate to test
* @param potion Potion to test against the predicate
* @param stack Reagent stack to test
* @return True if it matches
*/
private static Potion tryMix(Object mix, Potion potion, ItemStack stack) {
if (ReflectionUtil.getMixPredicateInput(mix) == potion) {
Ingredient ingredient = ReflectionUtil.getMixPredicateReagent(mix);
if (ingredient != null && ingredient.test(stack)) {
Potion output = ReflectionUtil.getMixPredicateOutput(mix);
if (output != null) {
return output;
}
}
}
return Potions.EMPTY;
}
@Override
protected Potion getResult(Potion potion, ItemStack stack) {
// try last mix first
if (lastMix != null) {
Potion output = tryMix(lastMix, potion, stack);
if (output != Potions.EMPTY) {
return output;
}
}
// try to find a new predicate among the list
for (Object mix : PotionBrewing.POTION_TYPE_CONVERSIONS) {
Potion output = tryMix(mix, potion, stack);
if (output != Potions.EMPTY) {
lastMix = mix;
return output;
}
}
return Potions.EMPTY;
}
@Override
protected List<DisplayCauldronRecipe> getDisplayRecipes() {
// map all potion type conversions to a recipe if possible
return ((List<?>)PotionBrewing.POTION_TYPE_CONVERSIONS).stream().flatMap(mix -> {
Potion input = ReflectionUtil.getMixPredicateInput(mix);
Potion output = ReflectionUtil.getMixPredicateOutput(mix);
Ingredient reagent = ReflectionUtil.getMixPredicateReagent(mix);
// ensure the reflection worked and the recipe is valid
if (input != null && output != null && reagent != null && !reagent.hasNoMatchingItems()) {
return Stream.of(makeRecipe(input, reagent, output));
}
return Stream.empty();
}).collect(Collectors.toList());
}
@Override
public IRecipeSerializer<?> getSerializer() {
return RecipeSerializers.CAULDRON_POTION_BREWING;
}
}
/** Potion brewing logic using {@link BrewingRecipeRegistry} */
public static class Forge extends BrewingCauldronRecipe {
/** Function to make a stack from a potion */
private static final Function<Potion, ItemStack> POTION_ITEM_MAPPER = potion -> PotionUtils.addPotionToItemStack(new ItemStack(Items.POTION), potion);
/** Cached map of items for each potion */
private final Map<Potion,ItemStack> potionItemLookup = new IdentityHashMap<>();
/** Last successful match among potion mixes */
private IBrewingRecipe lastRecipe;
/**
* Creates a new recipe instance
* @param id Recipe ID
* @param instant If true, recipe produces potion. If false, recipe produces unfermented potion
*/
public Forge(ResourceLocation id, boolean instant) {
super(id, instant);
}
/**
* Trys a potion brewing recipe to get the potion output
* @param recipe Recipe to try
* @param potion Potion stack input
* @param stack Item stack input
* @return Potion result, or empty if no match
*/
private static Potion tryRecipe(IBrewingRecipe recipe, ItemStack potion, ItemStack stack) {
ItemStack outputStack = recipe.getOutput(potion, stack);
if (!outputStack.isEmpty()) {
return PotionUtils.getPotionFromItem(outputStack);
}
return Potions.EMPTY;
}
@Override
protected Potion getResult(Potion potion, ItemStack stack) {
// first, make a stack from the potion
ItemStack input = potionItemLookup.computeIfAbsent(potion, POTION_ITEM_MAPPER);
if (lastRecipe != null) {
Potion output = tryRecipe(lastRecipe, input, stack);
if (output != Potions.EMPTY) {
return output;
}
}
// try each brewing recipe in the registry
for (IBrewingRecipe recipe : BrewingRecipeRegistry.getRecipes()) {
// skip vanilla recipe, we handle that separately for efficiency
if (recipe instanceof VanillaBrewingRecipe) {
continue;
}
Potion output = tryRecipe(recipe, input, stack);
if (output != Potions.EMPTY) {
lastRecipe = recipe;
return output;
}
}
return Potions.EMPTY;
}
@Override
protected List<DisplayCauldronRecipe> getDisplayRecipes() {
List<IBrewingRecipe> recipes = BrewingRecipeRegistry.getRecipes();
return Stream.concat(
// try to handle the Forge one
recipes.stream()
.filter(r -> r instanceof BrewingRecipe)
.map(r -> (BrewingRecipe) r)
.filter(r -> r.getOutput().getItem() == Items.POTION)
.flatMap(recipe -> {
// output must be a potion
Potion output = PotionUtils.getPotionFromItem(recipe.getOutput());
if (output != Potions.EMPTY) {
// get all potion inputs and return recipes
return Arrays.stream(recipe.getInput().getMatchingStacks())
.filter(s -> s.getItem() == Items.POTION)
.map(PotionUtils::getPotionFromItem)
.filter(pot -> pot != Potions.EMPTY)
.map(input -> makeRecipe(input, recipe.getIngredient(), output));
}
return Stream.empty();
}),
// special case our own brewing class, the Forge one really sucks
recipes.stream().filter(r -> r instanceof NormalBrewingRecipe)
.map(r -> (NormalBrewingRecipe) r)
.filter(NormalBrewingRecipe::isEnabled)
.map(r -> makeRecipe(r.getStart(), r.getCatalyst(), r.getOutput()))
).collect(Collectors.toList());
}
@Override
public IRecipeSerializer<?> getSerializer() {
return RecipeSerializers.CAULDRON_FORGE_BREWING;
}
}
/** Recipe serializer */
public static class Serializer extends RecipeSerializer<BrewingCauldronRecipe> {
private final BiFunction<ResourceLocation, Boolean, BrewingCauldronRecipe> factory;
/**
* Creates a new serializer instance
* @param factory Recipe constructor
*/
public Serializer(BiFunction<ResourceLocation,Boolean,BrewingCauldronRecipe> factory) {
this.factory = factory;
}
@Override
public BrewingCauldronRecipe read(ResourceLocation id, JsonObject json) {
boolean instant = JSONUtils.getBoolean(json, "instant");
return factory.apply(id, instant);
}
@Nullable
@Override
public BrewingCauldronRecipe read(ResourceLocation id, PacketBuffer buffer) {
return factory.apply(id, buffer.readBoolean());
}
@Override
public void write(PacketBuffer buffer, BrewingCauldronRecipe recipe) {
buffer.writeBoolean(recipe.instant);
}
}
/** Finished recipe for datagen */
public static class FinishedRecipe extends DynamicFinishedRecipe {
private final boolean instant;
public FinishedRecipe(ResourceLocation id, Serializer serializer, boolean instant) {
super(id, serializer);
this.instant = instant;
}
@Override
public void serialize(JsonObject json) {
json.addProperty("instant", instant);
}
}
}