/
FillPotionCauldronRecipe.java
119 lines (104 loc) · 4.25 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
package knightminer.inspirations.library.recipe.cauldron.special;
import com.google.gson.JsonObject;
import knightminer.inspirations.library.Util;
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.ingredient.SizedIngredient;
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 net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.network.PacketBuffer;
import net.minecraft.potion.PotionUtils;
import net.minecraft.util.JSONUtils;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvents;
import net.minecraft.world.World;
import slimeknights.mantle.recipe.RecipeHelper;
import javax.annotation.Nullable;
import java.util.Objects;
/**
* Recipe to fill a potion bottle using a cauldron
*/
public class FillPotionCauldronRecipe implements ICauldronRecipe {
private final ResourceLocation id;
private final SizedIngredient bottle;
private final Item potionItem;
/**
* Creates a new potion fill recipe
* @param id Recipe ID
* @param bottle Potion bottle
* @param potionItem Item to output
*/
public FillPotionCauldronRecipe(ResourceLocation id, SizedIngredient bottle, Item potionItem) {
this.id = id;
this.potionItem = potionItem;
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) && bottle.test(stack);
}
@Override
public void handleRecipe(IModifyableCauldronInventory inv) {
inv.getContents().get(CauldronContentTypes.POTION).ifPresent(potion -> {
// give player potion, removing a bottle
int amount = bottle.getAmountNeeded();
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);
});
}
@Override
public ResourceLocation getId() {
return id;
}
@Override
public IRecipeSerializer<?> getSerializer() {
return RecipeSerializers.CAULDRON_FILL_POTION;
}
public static class Serializer extends RecipeSerializer<FillPotionCauldronRecipe> {
@Override
public FillPotionCauldronRecipe read(ResourceLocation id, JsonObject json) {
SizedIngredient bottle = SizedIngredient.deserialize(JSONUtils.getJsonObject(json, "bottle"));
Item potion = Util.deserializeItem(json, "potion");
return new FillPotionCauldronRecipe(id, bottle, potion);
}
@Nullable
@Override
public FillPotionCauldronRecipe read(ResourceLocation id, PacketBuffer buffer) {
SizedIngredient bottle = SizedIngredient.read(buffer);
Item potion = RecipeHelper.readItem(buffer);
return new FillPotionCauldronRecipe(id, bottle, potion);
}
@Override
public void write(PacketBuffer buffer, FillPotionCauldronRecipe recipe) {
recipe.bottle.write(buffer);
RecipeHelper.writeItem(buffer, recipe.potionItem);
}
}
/** Finished recipe for datagen */
public static class FinishedRecipe extends DynamicFinishedRecipe {
private final SizedIngredient bottle;
private final Item potionItem;
public FinishedRecipe(ResourceLocation id, SizedIngredient bottle, Item potionItem) {
super(id, RecipeSerializers.CAULDRON_FILL_POTION);
this.bottle = bottle;
this.potionItem = potionItem;
}
@Override
public void serialize(JsonObject json) {
json.add("bottle", bottle.serialize());
json.addProperty("potion", Objects.requireNonNull(potionItem.getRegistryName()).toString());
}
}
}