package lv.enes.mc.eris_alchemy.utils; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonParseException; import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.TagKey; import net.minecraft.world.item.crafting.Ingredient; import java.lang.reflect.Type; import java.util.function.Supplier; public class IngredientProvider implements JsonDeserializer> { public static Supplier deserialize(JsonElement el) { if (el.isJsonObject()) { return () -> ItemUtils.ingredientFromJson(el.getAsJsonObject()).orElse(Ingredient.EMPTY); } else if (el.isJsonPrimitive() && el.getAsJsonPrimitive().isString()) { return deserialize(el.getAsString().strip()); } else { throw new JsonParseException("Every ingredient should be an object or a string"); } } public static Supplier deserialize(String str) { if (str.startsWith("#")) { var tag = TagKey.create(Registries.ITEM, new ResourceLocation(str.substring(1).strip())); return () -> Ingredient.of(tag); } else { return () -> Ingredient.of(ItemUtils.get(new ResourceLocation(str))); } } @Override public Supplier deserialize( JsonElement el, Type t, JsonDeserializationContext c ) { return IngredientProvider.deserialize(el); } }