/
DyeableCauldronRecipe.java
236 lines (204 loc) · 7.24 KB
/
DyeableCauldronRecipe.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
package knightminer.inspirations.library.recipe.cauldron.special;
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.contents.ICauldronContents;
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.recipes.InspirationsRecipes;
import net.minecraft.data.IFinishedRecipe;
import net.minecraft.fluid.Fluids;
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.util.ResourceLocation;
import net.minecraft.util.SoundEvents;
import net.minecraft.world.World;
import slimeknights.mantle.util.JsonHelper;
import javax.annotation.Nullable;
import java.util.function.BiFunction;
/**
* Shared logic between dyeing and clearing dye
*/
public abstract class DyeableCauldronRecipe implements ICauldronRecipe {
private final ResourceLocation id;
private final Ingredient ingredient;
/**
* Recipe to remove dye from an item
* @param id Recipe ID
* @param ingredient Ingredient for input
*/
public DyeableCauldronRecipe(ResourceLocation id, Ingredient ingredient) {
this.id = id;
this.ingredient = ingredient;
}
@Override
public boolean matches(ICauldronInventory inv, World worldIn) {
ItemStack stack = inv.getStack();
return inv.getLevel() > 0 && ingredient.test(stack) && matches(inv.getContents(), stack);
}
/**
* Logic to check that the given contents match after ingredient and level check
* @param contents Contents to check
* @param stack Item stack to check
* @return True if the recipe matches
*/
protected abstract boolean matches(ICauldronContents contents, ItemStack stack);
@Override
public void handleRecipe(IModifyableCauldronInventory inventory) {
// if the stack contains multiple items, update just one
ICauldronContents contents = inventory.getContents();
ItemStack stack = inventory.getStack();
if (stack.getCount() > 1) {
stack = stack.split(1);
inventory.giveStack(updateColor(contents, stack));
} else {
// if one, update the one
inventory.setStack(updateColor(contents, stack));
}
// remove a level of dye
inventory.addLevel(-1);
// play sound
inventory.playSound(SoundEvents.ENTITY_GENERIC_SPLASH);
}
/**
* Logic to update the item stack color
* @param contents Cauldron contents
* @param stack Stack to update
* @return Updated stack
*/
protected abstract ItemStack updateColor(ICauldronContents contents, ItemStack stack);
@Override
public ResourceLocation getId() {
return id;
}
/**
* Recipe to dye a dyeable
*/
public static class Dye extends DyeableCauldronRecipe {
public Dye(ResourceLocation id, Ingredient ingredient) {
super(id, ingredient);
}
@Override
protected boolean matches(ICauldronContents contents, ItemStack stack) {
return contents.get(CauldronContentTypes.COLOR).filter(color -> !Util.hasColor(stack) || Util.getColor(stack) != color).isPresent();
}
@Override
protected ItemStack updateColor(ICauldronContents contents, ItemStack stack) {
int color = contents.get(CauldronContentTypes.COLOR).orElse(-1);
return Util.setColor(stack, color);
}
@Override
public IRecipeSerializer<?> getSerializer() {
return InspirationsRecipes.dyeDyeableSerializer;
}
}
/**
* Recipe to clear a dyeable
*/
public static class Clear extends DyeableCauldronRecipe {
public Clear(ResourceLocation id, Ingredient ingredient) {
super(id, ingredient);
}
@Override
protected boolean matches(ICauldronContents contents, ItemStack stack) {
return contents.contains(CauldronContentTypes.FLUID, Fluids.WATER) && Util.hasColor(stack);
}
@Override
protected ItemStack updateColor(ICauldronContents contents, ItemStack stack) {
return Util.clearColor(stack);
}
@Override
public IRecipeSerializer<?> getSerializer() {
return InspirationsRecipes.clearDyeableSerializer;
}
}
/**
* Serializer for the recipe
*/
public static class Serializer extends RecipeSerializer<DyeableCauldronRecipe> {
private final BiFunction<ResourceLocation, Ingredient, DyeableCauldronRecipe> factory;
public Serializer(BiFunction<ResourceLocation, Ingredient, DyeableCauldronRecipe> factory) {
this.factory = factory;
}
@Override
public DyeableCauldronRecipe read(ResourceLocation id, JsonObject json) {
return factory.apply(id, Ingredient.deserialize(JsonHelper.getElement(json, "ingredient")));
}
@Nullable
@Override
public DyeableCauldronRecipe read(ResourceLocation id, PacketBuffer buffer) {
return factory.apply(id, Ingredient.read(buffer));
}
@Override
public void write(PacketBuffer buffer, DyeableCauldronRecipe recipe) {
recipe.ingredient.write(buffer);
}
}
/**
* Finished recipe for data gen
*/
public static class FinishedRecipe implements IFinishedRecipe {
private final ResourceLocation id;
private final Ingredient ingredient;
private final Serializer serializer;
private FinishedRecipe(ResourceLocation id, Ingredient ingredient, Serializer serializer) {
this.id = id;
this.ingredient = ingredient;
this.serializer = serializer;
}
/**
* Creates a recipe for a generic serializer
* @param id Recipe ID
* @param ingredient Recipe ingredient
* @param serializer Recipe serializer
* @return Recipe
*/
public static FinishedRecipe recipe(ResourceLocation id, Ingredient ingredient, Serializer serializer) {
return new FinishedRecipe(id, ingredient, serializer);
}
/**
* Creates a dyeing recipe
* @param id Recipe ID
* @param ingredient Recipe ingredient
* @return Recipe
*/
public static FinishedRecipe dye(ResourceLocation id, Ingredient ingredient) {
return new FinishedRecipe(id, ingredient, InspirationsRecipes.dyeDyeableSerializer);
}
/**
* Creates a dye clearing recipe
* @param id Recipe ID
* @param ingredient Recipe ingredient
* @return Recipe
*/
public static FinishedRecipe clear(ResourceLocation id, Ingredient ingredient) {
return new FinishedRecipe(id, ingredient, InspirationsRecipes.clearDyeableSerializer);
}
@Override
public void serialize(JsonObject json) {
json.add("ingredient", ingredient.serialize());
}
@Override
public ResourceLocation getID() {
return id;
}
@Override
public IRecipeSerializer<?> getSerializer() {
return serializer;
}
@Nullable
@Override
public JsonObject getAdvancementJson() {
return null;
}
@Nullable
@Override
public ResourceLocation getAdvancementID() {
return null;
}
}
}