diff options
4 files changed, 38 insertions, 16 deletions
diff --git a/gradle.properties b/gradle.properties index fbfa732..a974dd8 100644 --- a/gradle.properties +++ b/gradle.properties | |||
| @@ -5,7 +5,7 @@ org.gradle.parallel = true | |||
| 5 | 5 | ||
| 6 | groupid = lv.enes.mc | 6 | groupid = lv.enes.mc |
| 7 | modid = eris_alchemy | 7 | modid = eris_alchemy |
| 8 | modVersion = 0.3.0 | 8 | modVersion = 0.3.1-SNAPSHOT |
| 9 | 9 | ||
| 10 | loomPluginVersion = 1.8.5 | 10 | loomPluginVersion = 1.8.5 |
| 11 | 11 | ||
diff --git a/src/main/java/lv/enes/mc/eris_alchemy/recipe/SimplifiedRecipe.java b/src/main/java/lv/enes/mc/eris_alchemy/recipe/SimplifiedRecipe.java index b63e4c5..e1022b2 100644 --- a/src/main/java/lv/enes/mc/eris_alchemy/recipe/SimplifiedRecipe.java +++ b/src/main/java/lv/enes/mc/eris_alchemy/recipe/SimplifiedRecipe.java | |||
| @@ -16,14 +16,12 @@ import net.minecraft.core.registries.BuiltInRegistries; | |||
| 16 | import net.minecraft.resources.ResourceLocation; | 16 | import net.minecraft.resources.ResourceLocation; |
| 17 | import net.minecraft.world.item.Item; | 17 | import net.minecraft.world.item.Item; |
| 18 | import net.minecraft.world.item.ItemStack; | 18 | import net.minecraft.world.item.ItemStack; |
| 19 | import net.minecraft.world.item.Items; | ||
| 19 | import net.minecraft.world.item.crafting.Ingredient; | 20 | import net.minecraft.world.item.crafting.Ingredient; |
| 20 | import net.minecraft.world.item.crafting.Recipe; | 21 | import net.minecraft.world.item.crafting.Recipe; |
| 21 | import net.minecraft.world.item.crafting.ShapedRecipe; | ||
| 22 | 22 | ||
| 23 | import java.lang.reflect.Type; | 23 | import java.lang.reflect.Type; |
| 24 | import java.util.Arrays; | 24 | import java.util.*; |
| 25 | import java.util.Collection; | ||
| 26 | import java.util.List; | ||
| 27 | import java.util.function.Predicate; | 25 | import java.util.function.Predicate; |
| 28 | import java.util.function.Supplier; | 26 | import java.util.function.Supplier; |
| 29 | import java.util.stream.Stream; | 27 | import java.util.stream.Stream; |
| @@ -113,8 +111,9 @@ public record SimplifiedRecipe( | |||
| 113 | throw new JsonParseException("Recipe must have 'output' and 'input' fields"); | 111 | throw new JsonParseException("Recipe must have 'output' and 'input' fields"); |
| 114 | } | 112 | } |
| 115 | 113 | ||
| 116 | var output = parseOutputOrRemainder(obj.get("output")); | 114 | var output = parseOutputOrRemainder(obj.get("output")).orElseGet(Items.AIR::getDefaultInstance); |
| 117 | var remainder = parseRemainders(obj.get("remainder")); | 115 | var remainder = parseRemainders(obj.get("remainder")) |
| 116 | .orElseGet(() -> List.of(Items.AIR.getDefaultInstance())); | ||
| 118 | var input = parseInputs(obj.get("input")); | 117 | var input = parseInputs(obj.get("input")); |
| 119 | return new SimplifiedRecipe(output, remainder, input, false); | 118 | return new SimplifiedRecipe(output, remainder, input, false); |
| 120 | } | 119 | } |
| @@ -127,24 +126,33 @@ public record SimplifiedRecipe( | |||
| 127 | return List.of(IngredientProvider.deserialize(el)); | 126 | return List.of(IngredientProvider.deserialize(el)); |
| 128 | } | 127 | } |
| 129 | 128 | ||
| 130 | private ItemStack parseOutputOrRemainder(JsonElement el) { | 129 | private Optional<ItemStack> parseOutputOrRemainder(JsonElement el) { |
| 131 | if (el.isJsonObject()) { | 130 | if (el.isJsonObject()) { |
| 132 | return ShapedRecipe.itemStackFromJson(el.getAsJsonObject()); | 131 | return ItemUtils.itemStackFromJson(el.getAsJsonObject()); |
| 133 | } else if (el.isJsonPrimitive() && el.getAsJsonPrimitive().isString()) { | 132 | } else if (el.isJsonPrimitive() && el.getAsJsonPrimitive().isString()) { |
| 134 | var id = new ResourceLocation(el.getAsString()); | 133 | var id = new ResourceLocation(el.getAsString()); |
| 135 | return BuiltInRegistries.ITEM.get(id).getDefaultInstance(); | 134 | return Optional.of(BuiltInRegistries.ITEM.get(id).getDefaultInstance()); |
| 136 | } else { | 135 | } else { |
| 137 | throw new JsonParseException("Recipe's output or remainder must be an object or a string"); | 136 | throw new JsonParseException("Recipe's output or remainder must be an object or a string"); |
| 138 | } | 137 | } |
| 139 | } | 138 | } |
| 140 | 139 | ||
| 141 | private List<ItemStack> parseRemainders(JsonElement el) { | 140 | private Optional<List<ItemStack>> parseRemainders(JsonElement el) { |
| 142 | if (el == null) { | 141 | if (el == null) { |
| 143 | return List.of(); | 142 | return Optional.of(List.of()); |
| 144 | } else if (el.isJsonArray()) { | 143 | } else if (el.isJsonArray()) { |
| 145 | return el.getAsJsonArray().asList().stream().map(this::parseOutputOrRemainder).toList(); | 144 | var optList = el.getAsJsonArray().asList().stream().map(this::parseOutputOrRemainder).toList(); |
| 145 | var retList = new ArrayList<ItemStack>(); | ||
| 146 | for (var opt : optList) { | ||
| 147 | if (opt.isPresent()) { | ||
| 148 | retList.add(opt.get()); | ||
| 149 | } else { | ||
| 150 | return Optional.empty(); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | return Optional.of(retList); | ||
| 146 | } else { | 154 | } else { |
| 147 | return List.of(parseOutputOrRemainder(el)); | 155 | return parseOutputOrRemainder(el).map(List::of); |
| 148 | } | 156 | } |
| 149 | } | 157 | } |
| 150 | } | 158 | } |
diff --git a/src/main/java/lv/enes/mc/eris_alchemy/utils/IngredientProvider.java b/src/main/java/lv/enes/mc/eris_alchemy/utils/IngredientProvider.java index e859114..6390c88 100644 --- a/src/main/java/lv/enes/mc/eris_alchemy/utils/IngredientProvider.java +++ b/src/main/java/lv/enes/mc/eris_alchemy/utils/IngredientProvider.java | |||
| @@ -8,7 +8,6 @@ import net.minecraft.core.registries.Registries; | |||
| 8 | import net.minecraft.resources.ResourceLocation; | 8 | import net.minecraft.resources.ResourceLocation; |
| 9 | import net.minecraft.tags.TagKey; | 9 | import net.minecraft.tags.TagKey; |
| 10 | import net.minecraft.world.item.crafting.Ingredient; | 10 | import net.minecraft.world.item.crafting.Ingredient; |
| 11 | import net.minecraft.world.item.crafting.ShapedRecipe; | ||
| 12 | 11 | ||
| 13 | import java.lang.reflect.Type; | 12 | import java.lang.reflect.Type; |
| 14 | import java.util.function.Supplier; | 13 | import java.util.function.Supplier; |
| @@ -16,7 +15,7 @@ import java.util.function.Supplier; | |||
| 16 | public class IngredientProvider implements JsonDeserializer<Supplier<Ingredient>> { | 15 | public class IngredientProvider implements JsonDeserializer<Supplier<Ingredient>> { |
| 17 | public static Supplier<Ingredient> deserialize(JsonElement el) { | 16 | public static Supplier<Ingredient> deserialize(JsonElement el) { |
| 18 | if (el.isJsonObject()) { | 17 | if (el.isJsonObject()) { |
| 19 | return () -> Ingredient.of(ShapedRecipe.itemStackFromJson(el.getAsJsonObject())); | 18 | return () -> ItemUtils.itemStackFromJson(el.getAsJsonObject()).map(Ingredient::of).orElse(Ingredient.EMPTY); |
| 20 | } else if (el.isJsonPrimitive() && el.getAsJsonPrimitive().isString()) { | 19 | } else if (el.isJsonPrimitive() && el.getAsJsonPrimitive().isString()) { |
| 21 | return deserialize(el.getAsString().strip()); | 20 | return deserialize(el.getAsString().strip()); |
| 22 | } else { | 21 | } else { |
diff --git a/src/main/java/lv/enes/mc/eris_alchemy/utils/ItemUtils.java b/src/main/java/lv/enes/mc/eris_alchemy/utils/ItemUtils.java index 7ac7358..0801d11 100644 --- a/src/main/java/lv/enes/mc/eris_alchemy/utils/ItemUtils.java +++ b/src/main/java/lv/enes/mc/eris_alchemy/utils/ItemUtils.java | |||
| @@ -1,12 +1,18 @@ | |||
| 1 | package lv.enes.mc.eris_alchemy.utils; | 1 | package lv.enes.mc.eris_alchemy.utils; |
| 2 | 2 | ||
| 3 | import com.google.gson.JsonObject; | ||
| 4 | import com.google.gson.JsonSyntaxException; | ||
| 5 | import lv.enes.mc.eris_alchemy.ErisAlchemy; | ||
| 3 | import net.minecraft.core.Holder; | 6 | import net.minecraft.core.Holder; |
| 4 | import net.minecraft.core.registries.BuiltInRegistries; | 7 | import net.minecraft.core.registries.BuiltInRegistries; |
| 5 | import net.minecraft.resources.ResourceLocation; | 8 | import net.minecraft.resources.ResourceLocation; |
| 6 | import net.minecraft.world.item.Item; | 9 | import net.minecraft.world.item.Item; |
| 7 | import net.minecraft.world.item.ItemStack; | 10 | import net.minecraft.world.item.ItemStack; |
| 11 | import net.minecraft.world.item.crafting.ShapedRecipe; | ||
| 8 | import net.minecraft.world.level.ItemLike; | 12 | import net.minecraft.world.level.ItemLike; |
| 9 | 13 | ||
| 14 | import java.util.Optional; | ||
| 15 | |||
| 10 | public final class ItemUtils { | 16 | public final class ItemUtils { |
| 11 | public static Item get(ResourceLocation id) { | 17 | public static Item get(ResourceLocation id) { |
| 12 | return BuiltInRegistries.ITEM.get(id); | 18 | return BuiltInRegistries.ITEM.get(id); |
| @@ -24,5 +30,14 @@ public final class ItemUtils { | |||
| 24 | return getId(stack.getItem()); | 30 | return getId(stack.getItem()); |
| 25 | } | 31 | } |
| 26 | 32 | ||
| 33 | public static Optional<ItemStack> itemStackFromJson(JsonObject json) { | ||
| 34 | try { | ||
| 35 | return Optional.of(ShapedRecipe.itemStackFromJson(json)); | ||
| 36 | } catch (JsonSyntaxException ex) { | ||
| 37 | ErisAlchemy.LOGGER.error("Exception while trying to parse item stack from JSON", ex); | ||
| 38 | return Optional.empty(); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 27 | private ItemUtils() {} | 42 | private ItemUtils() {} |
| 28 | } | 43 | } |