-
Notifications
You must be signed in to change notification settings - Fork 18
/
AnvilRecipeBuilder.java
257 lines (225 loc) · 7.41 KB
/
AnvilRecipeBuilder.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package knightminer.inspirations.common.datagen;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.mojang.datafixers.util.Pair;
import knightminer.inspirations.library.Util;
import knightminer.inspirations.library.recipe.BlockIngredient;
import knightminer.inspirations.library.recipe.anvil.AnvilRecipe;
import net.minecraft.advancements.criterion.StatePropertiesPredicate;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.data.IFinishedRecipe;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.state.IProperty;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tags.Tag;
import net.minecraft.util.IItemProvider;
import net.minecraft.util.ResourceLocation;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class AnvilRecipeBuilder {
private final List<Ingredient> ingredients;
private final List<Pair<String, String>> properties;
private String group = "";
@Nullable
private final Block result;
private AnvilRecipeBuilder(@Nullable Block res) {
result = res;
ingredients = new ArrayList<>();
properties = new ArrayList<>();
}
public static AnvilRecipeBuilder copiesInput() {
return new AnvilRecipeBuilder(null);
}
public static AnvilRecipeBuilder places(@Nonnull Block block) {
return new AnvilRecipeBuilder(block);
}
public static AnvilRecipeBuilder smashes() {
return new AnvilRecipeBuilder(Blocks.AIR);
}
public AnvilRecipeBuilder group(String name) {
group = name;
return this;
}
public AnvilRecipeBuilder addIngredient(Block block) {
ingredients.add(new BlockIngredient.DirectBlockIngredient(block, StatePropertiesPredicate.EMPTY));
return this;
}
public AnvilRecipeBuilder addIngredient(Block block, StatePropertiesPredicate pred) {
ingredients.add(new BlockIngredient.DirectBlockIngredient(block, pred));
return this;
}
public AnvilRecipeBuilder addIngredient(Tag<Block> blockTag) {
ingredients.add(new BlockIngredient.TaggedBlockIngredient(blockTag, StatePropertiesPredicate.EMPTY));
return this;
}
public AnvilRecipeBuilder addIngredient(Tag<Block> blockTag, StatePropertiesPredicate pred) {
ingredients.add(new BlockIngredient.TaggedBlockIngredient(blockTag, pred));
return this;
}
public AnvilRecipeBuilder addIngredient(IItemProvider item) {
ingredients.add(Ingredient.fromItems(item));
return this;
}
public AnvilRecipeBuilder addIngredient(IItemProvider item, int quantity) {
for(int i = 0; i < quantity; i++) {
ingredients.add(Ingredient.fromItems(item));
}
return this;
}
public AnvilRecipeBuilder copiesProperty(IProperty<?> prop) {
properties.add(Pair.of(prop.getName(), AnvilRecipe.FROM_INPUT));
return this;
}
public <T extends Comparable<T>> AnvilRecipeBuilder setsProp(IProperty<T> prop, T value) {
properties.add(Pair.of(prop.getName(), value.toString()));
return this;
}
/**
* Use IProperty versions if possible to ensure validity!
*/
public AnvilRecipeBuilder copiesPropertyUnsafe(String prop) {
properties.add(Pair.of(prop, AnvilRecipe.FROM_INPUT));
return this;
}
/**
* Use IProperty versions if possible to ensure validity!
*/
public AnvilRecipeBuilder setsPropUnsafe(String prop) {
properties.add(Pair.of(prop, AnvilRecipe.FROM_INPUT));
return this;
}
// Shortcuts for common property setups.
public AnvilRecipeBuilder copiesStandardSlab() {
return this
.copiesProperty(BlockStateProperties.HALF)
.copiesProperty(BlockStateProperties.WATERLOGGED);
}
// Shortcuts for common property setups.
public AnvilRecipeBuilder copiesStandardStair() {
return this
.copiesProperty(BlockStateProperties.HALF)
.copiesProperty(BlockStateProperties.STAIRS_SHAPE)
.copiesProperty(BlockStateProperties.HORIZONTAL_FACING)
.copiesProperty(BlockStateProperties.WATERLOGGED);
}
public void build(Consumer<IFinishedRecipe> consumer, ResourceLocation id) {
if (ingredients.size() == 0) {
throw new IllegalStateException("Recipe must have at least one ingredient!");
}
if (result == Blocks.AIR) {
id = new ResourceLocation(id.getNamespace(), "anvil_smash_" + id.getPath());
} else if (!id.getPath().contains("anvil")) {
id = new ResourceLocation(id.getNamespace(), id.getPath() + "_from_anvil_smashing");
}
consumer.accept(new Finished(
id,
ingredients,
result,
properties,
group
));
}
public void buildInsp(Consumer<IFinishedRecipe> consumer, String id) {
build(consumer, Util.getResource(id));
}
public void buildVanilla(Consumer<IFinishedRecipe> consumer, String id) {
build(consumer, new ResourceLocation(id));
}
// Same name as the item.
public void build(Consumer<IFinishedRecipe> consumer) {
ResourceLocation id;
if (result == null) {
throw new IllegalStateException("Save location required for recipe which copies input block!");
} else if (result == Blocks.AIR) {
// Copy the first block input's ID.
id = null;
for(Ingredient ing: ingredients) {
if (ing instanceof BlockIngredient.DirectBlockIngredient) {
id = ((BlockIngredient.DirectBlockIngredient) ing).block.getRegistryName();
break;
} else if (ing instanceof BlockIngredient.TaggedBlockIngredient) {
id = ((BlockIngredient.TaggedBlockIngredient) ing).tag.getId();
break;
}
}
if (id == null) {
throw new IllegalStateException("Could not infer save location for smashing recipe!");
}
} else {
id = result.getRegistryName();
}
build(consumer, id);
}
private static class Finished implements IFinishedRecipe {
private final ResourceLocation id;
private final List<Ingredient> ingredients;
private final String group;
// If null, keep the existing block.
@Nullable
private final Block result;
// Properties to assign to the result
private final List<Pair<String, String>> properties;
private Finished(
ResourceLocation id,
List<Ingredient> ingredients,
@Nullable Block result,
List<Pair<String, String>> properties,
String group
) {
this.id = id;
this.ingredients = ingredients;
this.group = group;
this.result = result;
this.properties = properties;
}
@Override
public void serialize(@Nonnull JsonObject json) {
if (!group.isEmpty()) {
json.addProperty("group", group);
}
JsonObject result = new JsonObject();
json.add("result", result);
if (this.result == null) {
result.addProperty("block", AnvilRecipe.FROM_INPUT);
} else {
result.addProperty("block", this.result.getRegistryName().toString());
}
if (properties.size() > 0) {
JsonObject props = new JsonObject();
for(Pair<String, String> prop : properties) {
result.addProperty(prop.getFirst(), prop.getSecond());
}
result.add("properties", props);
}
JsonArray ingredients = new JsonArray();
for(Ingredient ingredient : this.ingredients) {
ingredients.add(ingredient.serialize());
}
json.add("ingredients", ingredients);
}
@Nonnull
@Override
public ResourceLocation getID() {
return id;
}
@Nonnull
@Override
public IRecipeSerializer<?> getSerializer() {
return AnvilRecipe.SERIALIZER;
}
// We can't add a recipe book to the anvil, so there's no advancement.
@Override
public JsonObject getAdvancementJson() {
return null;
}
@Override
public ResourceLocation getAdvancementID() {
return null;
}
}
}