summaryrefslogtreecommitdiff
path: root/src/main/java/lv/enes/mc/eris_alchemy/SimplifiedRecipe.java
diff options
context:
space:
mode:
authorGravatar Uko Kokņevičs2024-01-11 23:11:27 +0100
committerGravatar Uko Kokņevičs2024-01-11 23:11:27 +0100
commitbca6a74e9a17e04de419743850f66af96a6473cc (patch)
tree6f81c0ceb615b0a85810df89ea9acafe6112eba9 /src/main/java/lv/enes/mc/eris_alchemy/SimplifiedRecipe.java
parentMaking SheetsMixin more extensible (diff)
downloadmc-eris-alchemy-bca6a74e9a17e04de419743850f66af96a6473cc.tar.gz
mc-eris-alchemy-bca6a74e9a17e04de419743850f66af96a6473cc.tar.xz
mc-eris-alchemy-bca6a74e9a17e04de419743850f66af96a6473cc.zip
Move default emc values to JSON files in datapack
Diffstat (limited to 'src/main/java/lv/enes/mc/eris_alchemy/SimplifiedRecipe.java')
-rw-r--r--src/main/java/lv/enes/mc/eris_alchemy/SimplifiedRecipe.java89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/main/java/lv/enes/mc/eris_alchemy/SimplifiedRecipe.java b/src/main/java/lv/enes/mc/eris_alchemy/SimplifiedRecipe.java
new file mode 100644
index 0000000..ef69b84
--- /dev/null
+++ b/src/main/java/lv/enes/mc/eris_alchemy/SimplifiedRecipe.java
@@ -0,0 +1,89 @@
1package lv.enes.mc.eris_alchemy;
2
3import com.google.gson.*;
4import jakarta.annotation.Nonnull;
5import lv.enes.mc.eris_alchemy.utils.RecipeUtils;
6import net.minecraft.core.RegistryAccess;
7import net.minecraft.core.registries.BuiltInRegistries;
8import net.minecraft.resources.ResourceLocation;
9import net.minecraft.world.item.ItemStack;
10import net.minecraft.world.item.crafting.Ingredient;
11import net.minecraft.world.item.crafting.Recipe;
12import net.minecraft.world.item.crafting.ShapedRecipe;
13
14import java.lang.reflect.Type;
15import java.util.List;
16
17public record SimplifiedRecipe(ItemStack output, List<ItemStack> remainder, List<Ingredient> input) {
18 public SimplifiedRecipe(Recipe<?> recipe, RegistryAccess registryAccess) {
19 this(
20 RecipeUtils.getOutput(recipe, registryAccess),
21 List.of(), // TODO:
22 RecipeUtils.getIngredients(recipe).stream().filter(ingredient -> !ingredient.isEmpty()).toList()
23 );
24 }
25
26 static class Deserializer implements JsonDeserializer<SimplifiedRecipe> {
27 @Nonnull
28 @Override
29 public SimplifiedRecipe deserialize(
30 JsonElement jsonElement,
31 Type type,
32 JsonDeserializationContext jsonDeserializationContext
33 ) throws JsonParseException {
34 if (!jsonElement.isJsonObject()) {
35 throw new JsonParseException("Recipe must be an object");
36 }
37 var obj = jsonElement.getAsJsonObject();
38
39 if (obj.get("output") == null || obj.get("input") == null) {
40 throw new JsonParseException("Recipe must have 'output' and 'input' fields");
41 }
42
43 var output = parseOutputOrRemainder(obj.get("output"));
44 var remainder = parseRemainders(obj.get("remainder"));
45 var input = parseInputs(obj.get("input"));
46 return new SimplifiedRecipe(output, remainder, input);
47 }
48
49 private List<Ingredient> parseInputs(JsonElement el) {
50 if (el.isJsonArray()) {
51 return el.getAsJsonArray().asList().stream().map(this::parseInput).map(Ingredient::of).toList();
52 }
53
54 return List.of(Ingredient.of(parseInput(el)));
55 }
56
57 private ItemStack parseInput(JsonElement el) {
58 if (el.isJsonObject()) {
59 return ShapedRecipe.itemStackFromJson(el.getAsJsonObject());
60 } else if (el.isJsonPrimitive() && el.getAsJsonPrimitive().isString()) {
61 var id = new ResourceLocation(el.getAsString());
62 return BuiltInRegistries.ITEM.get(id).getDefaultInstance();
63 } else {
64 throw new JsonParseException("Every recipe input should be an object or a string");
65 }
66 }
67
68 private ItemStack parseOutputOrRemainder(JsonElement el) {
69 if (el.isJsonObject()) {
70 return ShapedRecipe.itemStackFromJson(el.getAsJsonObject());
71 } else if (el.isJsonPrimitive() && el.getAsJsonPrimitive().isString()) {
72 var id = new ResourceLocation(el.getAsString());
73 return BuiltInRegistries.ITEM.get(id).getDefaultInstance();
74 } else {
75 throw new JsonParseException("Recipe's output or remainder must be an object or a string");
76 }
77 }
78
79 private List<ItemStack> parseRemainders(JsonElement el) {
80 if (el == null) {
81 return List.of();
82 } else if (el.isJsonArray()) {
83 return el.getAsJsonArray().asList().stream().map(this::parseOutputOrRemainder).toList();
84 } else {
85 return List.of(parseOutputOrRemainder(el));
86 }
87 }
88 }
89}