diff options
Diffstat (limited to 'src/main/java/lv/enes/mc/eris_alchemy/utils/IngredientProvider.java')
| -rw-r--r-- | src/main/java/lv/enes/mc/eris_alchemy/utils/IngredientProvider.java | 44 |
1 files changed, 44 insertions, 0 deletions
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 new file mode 100644 index 0000000..e859114 --- /dev/null +++ b/src/main/java/lv/enes/mc/eris_alchemy/utils/IngredientProvider.java | |||
| @@ -0,0 +1,44 @@ | |||
| 1 | package lv.enes.mc.eris_alchemy.utils; | ||
| 2 | |||
| 3 | import com.google.gson.JsonDeserializationContext; | ||
| 4 | import com.google.gson.JsonDeserializer; | ||
| 5 | import com.google.gson.JsonElement; | ||
| 6 | import com.google.gson.JsonParseException; | ||
| 7 | import net.minecraft.core.registries.Registries; | ||
| 8 | import net.minecraft.resources.ResourceLocation; | ||
| 9 | import net.minecraft.tags.TagKey; | ||
| 10 | import net.minecraft.world.item.crafting.Ingredient; | ||
| 11 | import net.minecraft.world.item.crafting.ShapedRecipe; | ||
| 12 | |||
| 13 | import java.lang.reflect.Type; | ||
| 14 | import java.util.function.Supplier; | ||
| 15 | |||
| 16 | public class IngredientProvider implements JsonDeserializer<Supplier<Ingredient>> { | ||
| 17 | public static Supplier<Ingredient> deserialize(JsonElement el) { | ||
| 18 | if (el.isJsonObject()) { | ||
| 19 | return () -> Ingredient.of(ShapedRecipe.itemStackFromJson(el.getAsJsonObject())); | ||
| 20 | } else if (el.isJsonPrimitive() && el.getAsJsonPrimitive().isString()) { | ||
| 21 | return deserialize(el.getAsString().strip()); | ||
| 22 | } else { | ||
| 23 | throw new JsonParseException("Every ingredient should be an object or a string"); | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | public static Supplier<Ingredient> deserialize(String str) { | ||
| 28 | if (str.startsWith("#")) { | ||
| 29 | var tag = TagKey.create(Registries.ITEM, new ResourceLocation(str.substring(1).strip())); | ||
| 30 | return () -> Ingredient.of(tag); | ||
| 31 | } else { | ||
| 32 | return () -> Ingredient.of(ItemUtils.get(new ResourceLocation(str))); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | @Override | ||
| 37 | public Supplier<Ingredient> deserialize( | ||
| 38 | JsonElement el, | ||
| 39 | Type t, | ||
| 40 | JsonDeserializationContext c | ||
| 41 | ) { | ||
| 42 | return IngredientProvider.deserialize(el); | ||
| 43 | } | ||
| 44 | } | ||