/
MixCauldronDyeRecipe.java
239 lines (210 loc) · 7.46 KB
/
MixCauldronDyeRecipe.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
package knightminer.inspirations.recipes.recipe.cauldron;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
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.EmptyCauldronContents;
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.DyeColor;
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.JSONUtils;
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.Arrays;
/**
* Cauldron recipe that takes a liquid container filled with dye and increases the cauldron liquid
*/
public class MixCauldronDyeRecipe implements ICauldronRecipe {
private final ResourceLocation id;
private final Ingredient ingredient;
@Nullable
private final Integer color;
/**
* Creates a new recipe instance
* @param id Recipe ID
* @param ingredient Input ingredient
* @param color Ingredient color
*/
public MixCauldronDyeRecipe(ResourceLocation id, Ingredient ingredient, @Nullable Integer color) {
this.id = id;
this.ingredient = ingredient;
this.color = color;
}
@Override
public boolean matches(ICauldronInventory inv, World worldIn) {
// must have space
int level = inv.getLevel();
if (level == MAX) {
return false;
}
// must have color, must match ingredient
ICauldronContents contents = inv.getContents();
return (level == 0 || contents.contains(CauldronContentTypes.FLUID, Fluids.WATER) || contents.contains(CauldronContentTypes.COLOR))
&& ingredient.test(inv.getStack());
}
@Override
public void handleRecipe(IModifyableCauldronInventory inv) {
// can set the color to null to read color from NBT
int newColor;
if (this.color == null) {
newColor = Util.getColor(inv.getStack());
} else {
newColor = this.color;
}
// if water, set the color directly
ICauldronContents contents = inv.getContents();
ItemStack container = inv.getStack().getContainerItem();
if (contents == EmptyCauldronContents.INSTANCE || contents.contains(CauldronContentTypes.FLUID, Fluids.WATER)) {
// update dye stack and return container
inv.shrinkStack(1);
inv.giveStack(container.copy());
// mix in a neutral grey for the water, its not a free dye dupe
int[] existing = new int[inv.getLevel()];
Arrays.fill(existing, 0x808080);
// update contents
inv.addLevel(1);
inv.setContents(CauldronContentTypes.COLOR.of(DyeCauldronWaterRecipe.addColors(newColor, existing)));
// play sound
inv.playSound(SoundEvents.ITEM_BOTTLE_EMPTY);
} else {
contents.get(CauldronContentTypes.COLOR).ifPresent(color -> {
// update dye stack and return container
inv.shrinkStack(1);
inv.giveStack(container.copy());
// add one copy of the existing color for each level
int[] existing = new int[inv.getLevel()];
Arrays.fill(existing, color);
// set contents
inv.addLevel(1);
inv.setContents(CauldronContentTypes.COLOR.of(DyeCauldronWaterRecipe.addColors(newColor, existing)));
// play sound
inv.playSound(SoundEvents.ITEM_BOTTLE_EMPTY);
});
}
}
@Override
public ItemStack getRecipeOutput() {
return ItemStack.EMPTY;
}
@Override
public ResourceLocation getId() {
return id;
}
@Override
public IRecipeSerializer<?> getSerializer() {
return InspirationsRecipes.mixCauldronDyeSerializer;
}
public static class Serializer extends RecipeSerializer<MixCauldronDyeRecipe> {
@SuppressWarnings("ConstantConditions")
@Override
public MixCauldronDyeRecipe read(ResourceLocation id, JsonObject json) {
Ingredient ingredient = Ingredient.deserialize(JsonHelper.getElement(json, "ingredient"));
// if color is defined, parse it
Integer color = null;
if (json.has("color")) {
// try dye color name first
String colorText = JSONUtils.getString(json, "color");
DyeColor dye = DyeColor.byTranslationKey(colorText, null);
if (dye != null) {
color = dye.getColorValue();
} else {
// hexadecimal color next
try {
color = Integer.parseInt(JSONUtils.getString(json, "color"), 16);
} catch (NumberFormatException e) {
throw new JsonSyntaxException("Invalid color string '" + colorText + "'");
}
}
}
return new MixCauldronDyeRecipe(id, ingredient, color);
}
@Nullable
@Override
public MixCauldronDyeRecipe read(ResourceLocation id, PacketBuffer buffer) {
Ingredient ingredient = Ingredient.read(buffer);
Integer color = null;
if (buffer.readBoolean()) {
color = buffer.readInt();
}
return new MixCauldronDyeRecipe(id, ingredient, color);
}
@Override
public void write(PacketBuffer buffer, MixCauldronDyeRecipe recipe) {
recipe.ingredient.write(buffer);
if (recipe.color == null) {
buffer.writeBoolean(false);
} else {
buffer.writeBoolean(true);
buffer.writeInt(recipe.color);
}
}
}
/**
* Finished recipe class for datagen
*/
public static class FinishedRecipe implements IFinishedRecipe {
private final ResourceLocation id;
private final Ingredient ingredient;
@Nullable
private final Integer color;
/**
* Creates a new recipe instance using the specified color
* @param id Recipe ID
* @param ingredient Recipe ingredient
* @param color Recipe color
*/
public FinishedRecipe(ResourceLocation id, Ingredient ingredient, Integer color) {
this.id = id;
this.ingredient = ingredient;
this.color = color;
}
/**
* Creates a new recipe instance using NBT dye color
* @param id Recipe ID
* @param ingredient Recipe ingredient
*/
public FinishedRecipe(ResourceLocation id, Ingredient ingredient) {
this.id = id;
this.ingredient = ingredient;
this.color = null;
}
@Override
public void serialize(JsonObject json) {
json.add("ingredient", ingredient.serialize());
if (color != null) {
json.addProperty("color", Integer.toHexString(color));
}
}
@Override
public ResourceLocation getID() {
return id;
}
@Override
public IRecipeSerializer<?> getSerializer() {
return InspirationsRecipes.mixCauldronDyeSerializer;
}
@Nullable
@Override
public JsonObject getAdvancementJson() {
return null;
}
@Nullable
@Override
public ResourceLocation getAdvancementID() {
return null;
}
}
}