diff options
Diffstat (limited to 'src/main/java/lv/enes/mc/eris_alchemy/recipe/BannedRecipe.java')
| -rw-r--r-- | src/main/java/lv/enes/mc/eris_alchemy/recipe/BannedRecipe.java | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/main/java/lv/enes/mc/eris_alchemy/recipe/BannedRecipe.java b/src/main/java/lv/enes/mc/eris_alchemy/recipe/BannedRecipe.java new file mode 100644 index 0000000..d94222c --- /dev/null +++ b/src/main/java/lv/enes/mc/eris_alchemy/recipe/BannedRecipe.java | |||
| @@ -0,0 +1,40 @@ | |||
| 1 | package lv.enes.mc.eris_alchemy.recipe; | ||
| 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 lv.enes.mc.eris_alchemy.utils.IngredientProvider; | ||
| 8 | import net.minecraft.world.item.crafting.Ingredient; | ||
| 9 | |||
| 10 | import java.lang.reflect.Type; | ||
| 11 | import java.util.function.Supplier; | ||
| 12 | |||
| 13 | public record BannedRecipe(Supplier<Ingredient> input, Supplier<Ingredient> output) { | ||
| 14 | public static BannedRecipe deserialize(JsonElement el) { | ||
| 15 | if (el.isJsonPrimitive() && el.getAsJsonPrimitive().isString()) { | ||
| 16 | return deserialize(el.getAsString()); | ||
| 17 | } else { | ||
| 18 | // TODO: Verbose object representation | ||
| 19 | throw new JsonParseException("Banned recipes should all be strings"); | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | public static BannedRecipe deserialize(String str) { | ||
| 24 | var split = str.indexOf("->"); | ||
| 25 | var input = IngredientProvider.deserialize(str.substring(0, split).trim()); | ||
| 26 | var output = IngredientProvider.deserialize(str.substring(split + 2).trim()); | ||
| 27 | return new BannedRecipe(input, output); | ||
| 28 | } | ||
| 29 | |||
| 30 | public static class Deserializer implements JsonDeserializer<BannedRecipe> { | ||
| 31 | @Override | ||
| 32 | public BannedRecipe deserialize( | ||
| 33 | JsonElement el, | ||
| 34 | Type t, | ||
| 35 | JsonDeserializationContext ctx | ||
| 36 | ) throws JsonParseException { | ||
| 37 | return BannedRecipe.deserialize(el); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | } | ||