/
FillPotionCauldronRecipe.java
126 lines (111 loc) · 4.38 KB
/
FillPotionCauldronRecipe.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
package knightminer.inspirations.library.recipe.cauldron.recipe;
import com.google.gson.JsonObject;
import knightminer.inspirations.library.Util;
import knightminer.inspirations.library.recipe.RecipeSerializer;
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.recipes.InspirationsRecipes;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
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.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.crafting.CraftingHelper;
import slimeknights.mantle.recipe.RecipeHelper;
import slimeknights.mantle.util.JsonHelper;
import javax.annotation.Nullable;
/**
* Recipe to fill a potion bottle using a cauldron
*/
public class FillPotionCauldronRecipe implements ICauldronRecipe {
private final ResourceLocation id;
private final Ingredient bottle;
private final int amount;
private final Item potionItem;
/**
* Creates a new potion fill recipe
* @param id Recipe ID
* @param bottle Potion bottle
* @param amount Number of the bottle needed and count of the output
* @param potionItem Item to output
*/
public FillPotionCauldronRecipe(ResourceLocation id, Ingredient bottle, int amount, Item potionItem) {
this.id = id;
this.potionItem = potionItem;
this.amount = amount;
this.bottle = bottle;
}
@Override
public boolean matches(ICauldronInventory inv, World world) {
// must have at least one level, contain any potion, and be using the correct item
ItemStack stack = inv.getStack();
return inv.getLevel() > 0 && inv.getContents().contains(CauldronContentTypes.POTION) && stack.getCount() >= amount && bottle.test(stack);
}
@Override
public void handleRecipe(IModifyableCauldronInventory inv) {
inv.getContents().get(CauldronContentTypes.POTION).ifPresent(potion -> {
// give player potion, removing a bottle
inv.shrinkStack(amount);
inv.setOrGiveStack(PotionUtils.addPotionToItemStack(new ItemStack(potionItem, amount), potion));
// update level
inv.addLevel(-1);
// play sound
inv.playSound(SoundEvents.ITEM_BOTTLE_FILL);
});
}
/**
* @deprecated use {@link #getCraftingResult(IInventory)}
*/
@Deprecated
@Override
public ItemStack getRecipeOutput() {
return new ItemStack(potionItem, amount);
}
@Override
public ItemStack getCraftingResult(ICauldronInventory inv) {
Potion potion = inv.getContents()
.get(CauldronContentTypes.POTION)
.orElse(Potions.EMPTY);
return PotionUtils.addPotionToItemStack(new ItemStack(potionItem, amount), potion);
}
@Override
public ResourceLocation getId() {
return id;
}
@Override
public IRecipeSerializer<?> getSerializer() {
return InspirationsRecipes.fillPotionSerializer;
}
public static class Serializer extends RecipeSerializer<FillPotionCauldronRecipe> {
@Override
public FillPotionCauldronRecipe read(ResourceLocation id, JsonObject json) {
Ingredient bottle = CraftingHelper.getIngredient(JsonHelper.getElement(json, "bottle"));
int amount = JSONUtils.getInt(json, "amount", 1);
Item potion = Util.deserializeItem(json, "potion");
return new FillPotionCauldronRecipe(id, bottle, amount, potion);
}
@Nullable
@Override
public FillPotionCauldronRecipe read(ResourceLocation id, PacketBuffer buffer) {
Ingredient bottle = Ingredient.read(buffer);
int amount = buffer.readVarInt();
Item potion = RecipeHelper.readItem(buffer);
return new FillPotionCauldronRecipe(id, bottle, amount, potion);
}
@Override
public void write(PacketBuffer buffer, FillPotionCauldronRecipe recipe) {
recipe.bottle.write(buffer);
buffer.writeVarInt(recipe.amount);
RecipeHelper.writeItem(buffer, recipe.potionItem);
}
}
}