/
CauldronTransformBuilder.java
216 lines (194 loc) · 6.8 KB
/
CauldronTransformBuilder.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
package knightminer.inspirations.library.recipe.cauldron.recipe;
import com.google.gson.JsonObject;
import knightminer.inspirations.library.recipe.cauldron.CauldronContentTypes;
import knightminer.inspirations.library.recipe.cauldron.CauldronIngredients;
import knightminer.inspirations.library.recipe.cauldron.contents.ICauldronContents;
import knightminer.inspirations.library.recipe.cauldron.ingredient.ICauldronIngredient;
import knightminer.inspirations.library.recipe.cauldron.util.LevelPredicate;
import knightminer.inspirations.library.recipe.cauldron.util.TemperaturePredicate;
import knightminer.inspirations.recipes.InspirationsRecipes;
import net.minecraft.advancements.Advancement;
import net.minecraft.data.IFinishedRecipe;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.potion.Potion;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import slimeknights.mantle.recipe.data.AbstractRecipeBuilder;
import javax.annotation.Nullable;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
/**
* Builds a recipe that converts one fluid in the cauldron to another over time
*/
public class CauldronTransformBuilder extends AbstractRecipeBuilder<CauldronTransformBuilder> {
private final ICauldronIngredient ingredient;
private final ICauldronContents output;
private final int time;
@Nullable
private LevelPredicate level;
private TemperaturePredicate temperature = TemperaturePredicate.ANY;
@Nullable
private SoundEvent sound = null;
private CauldronTransformBuilder(ICauldronIngredient ingredient, ICauldronContents output, int time) {
this.ingredient = ingredient;
this.output = output;
this.time = time;
}
/**
* Creates a new builder instance
* @param ingredient Input ingredient
* @param output Output contents
* @param time Recipe duration
* @return Builder instance
*/
public static CauldronTransformBuilder transform(ICauldronIngredient ingredient, ICauldronContents output, int time) {
if (time <= 0) {
throw new IllegalArgumentException("Time must be greater than zero");
}
return new CauldronTransformBuilder(ingredient, output, time);
}
/**
* Sets the required temperature
* @param temp Temperature
* @return Builder instance
*/
public CauldronTransformBuilder setTemperature(TemperaturePredicate temp) {
this.temperature = temp;
return this;
}
/* Levels */
/**
* Sets the minimum number of levels required to match
* @param min Minimum levels
* @return Builder instance
*/
public CauldronTransformBuilder minLevels(int min) {
this.level = LevelPredicate.min(min);
return this;
}
/**
* Sets the minimum number of levels required to match
* @param max Maximum levels
* @return Builder instance
*/
public CauldronTransformBuilder maxLevels(int max) {
this.level = LevelPredicate.max(max);
return this;
}
/**
* Sets the required number of levels to be a empty cauldron
* @return Builder instance
*/
public CauldronTransformBuilder matchEmpty() {
return maxLevels(0);
}
/**
* Sets the required number of levels to be a full cauldron
* @return Builder instance
*/
public CauldronTransformBuilder matchFull() {
return minLevels(ICauldronRecipe.MAX);
}
/**
* Sets the range of levels required to match
* @param min Minimum levels
* @param max Maximum levels
* @return Builder instance
*/
public CauldronTransformBuilder levelRange(int min, int max) {
this.level = LevelPredicate.range(min, max);
return this;
}
/**
* Sets the sound to play upon performing this recipe
* @param sound Recipe sound
* @return Sound played after recipe is done
*/
public CauldronTransformBuilder setSound(SoundEvent sound) {
this.sound = sound;
return this;
}
@Override
public void build(Consumer<IFinishedRecipe> consumer) {
// try fluid next
Optional<Fluid> fluid = output.get(CauldronContentTypes.FLUID);
if (fluid.isPresent()) {
build(consumer, Objects.requireNonNull(fluid.get().getRegistryName()));
return;
}
// try potion
Optional<Potion> potion = output.get(CauldronContentTypes.POTION);
if (potion.isPresent()) {
build(consumer, Objects.requireNonNull(potion.get().getRegistryName()));
return;
}
throw new IllegalStateException("Unable to create automatic recipe ID");
}
@Override
public void build(Consumer<IFinishedRecipe> consumer, ResourceLocation id) {
ResourceLocation advancementId = this.buildAdvancement(id, "cauldron");
consumer.accept(new Result(id, ingredient, level, temperature, output, time, sound, advancementBuilder, advancementId));
}
/**
* Result class
*/
private static class Result implements IFinishedRecipe {
private final ResourceLocation id;
private final ICauldronIngredient ingredient;
@Nullable
private final LevelPredicate level;
private final TemperaturePredicate temperature;
private final ICauldronContents output;
private final int time;
@Nullable
private final SoundEvent sound;
private final Advancement.Builder advancementBuilder;
private final ResourceLocation advancementId;
private Result(ResourceLocation id, ICauldronIngredient ingredient, @Nullable LevelPredicate level, TemperaturePredicate temperature, ICauldronContents output, int time, @Nullable SoundEvent sound, Advancement.Builder advancementBuilder, ResourceLocation advancementId) {
this.id = id;
this.ingredient = ingredient;
this.level = level;
this.temperature = temperature;
this.output = output;
this.time = time;
this.sound = sound;
this.advancementBuilder = advancementBuilder;
this.advancementId = advancementId;
}
@Override
public void serialize(JsonObject json) {
json.add("input", CauldronIngredients.toJson(ingredient));
if (level != null) {
json.add("level", level.toJson());
}
if (temperature != TemperaturePredicate.ANY) {
json.addProperty("temperature", temperature.getName());
}
json.add("output", CauldronContentTypes.toJson(output));
json.addProperty("time", time);
if (sound != null) {
json.addProperty("sound", Objects.requireNonNull(sound.getRegistryName()).toString());
}
}
@Override
public ResourceLocation getID() {
return id;
}
@Override
public IRecipeSerializer<?> getSerializer() {
return InspirationsRecipes.cauldronTransformSerializer;
}
@Nullable
@Override
public JsonObject getAdvancementJson() {
return advancementBuilder.serialize();
}
@Nullable
@Override
public ResourceLocation getAdvancementID() {
return advancementId;
}
}
}