blob: ef69b8428995e54df5462c7fcc62d0c5a8932214 (
plain) (
blame)
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
|
package lv.enes.mc.eris_alchemy;
import com.google.gson.*;
import jakarta.annotation.Nonnull;
import lv.enes.mc.eris_alchemy.utils.RecipeUtils;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.ShapedRecipe;
import java.lang.reflect.Type;
import java.util.List;
public record SimplifiedRecipe(ItemStack output, List<ItemStack> remainder, List<Ingredient> input) {
public SimplifiedRecipe(Recipe<?> recipe, RegistryAccess registryAccess) {
this(
RecipeUtils.getOutput(recipe, registryAccess),
List.of(), // TODO:
RecipeUtils.getIngredients(recipe).stream().filter(ingredient -> !ingredient.isEmpty()).toList()
);
}
static class Deserializer implements JsonDeserializer<SimplifiedRecipe> {
@Nonnull
@Override
public SimplifiedRecipe deserialize(
JsonElement jsonElement,
Type type,
JsonDeserializationContext jsonDeserializationContext
) throws JsonParseException {
if (!jsonElement.isJsonObject()) {
throw new JsonParseException("Recipe must be an object");
}
var obj = jsonElement.getAsJsonObject();
if (obj.get("output") == null || obj.get("input") == null) {
throw new JsonParseException("Recipe must have 'output' and 'input' fields");
}
var output = parseOutputOrRemainder(obj.get("output"));
var remainder = parseRemainders(obj.get("remainder"));
var input = parseInputs(obj.get("input"));
return new SimplifiedRecipe(output, remainder, input);
}
private List<Ingredient> parseInputs(JsonElement el) {
if (el.isJsonArray()) {
return el.getAsJsonArray().asList().stream().map(this::parseInput).map(Ingredient::of).toList();
}
return List.of(Ingredient.of(parseInput(el)));
}
private ItemStack parseInput(JsonElement el) {
if (el.isJsonObject()) {
return ShapedRecipe.itemStackFromJson(el.getAsJsonObject());
} else if (el.isJsonPrimitive() && el.getAsJsonPrimitive().isString()) {
var id = new ResourceLocation(el.getAsString());
return BuiltInRegistries.ITEM.get(id).getDefaultInstance();
} else {
throw new JsonParseException("Every recipe input should be an object or a string");
}
}
private ItemStack parseOutputOrRemainder(JsonElement el) {
if (el.isJsonObject()) {
return ShapedRecipe.itemStackFromJson(el.getAsJsonObject());
} else if (el.isJsonPrimitive() && el.getAsJsonPrimitive().isString()) {
var id = new ResourceLocation(el.getAsString());
return BuiltInRegistries.ITEM.get(id).getDefaultInstance();
} else {
throw new JsonParseException("Recipe's output or remainder must be an object or a string");
}
}
private List<ItemStack> parseRemainders(JsonElement el) {
if (el == null) {
return List.of();
} else if (el.isJsonArray()) {
return el.getAsJsonArray().asList().stream().map(this::parseOutputOrRemainder).toList();
} else {
return List.of(parseOutputOrRemainder(el));
}
}
}
}
|