diff --git a/xplat/src/main/java/dev/emi/emi/VanillaPlugin.java b/xplat/src/main/java/dev/emi/emi/VanillaPlugin.java index a4591db1..6e886078 100644 --- a/xplat/src/main/java/dev/emi/emi/VanillaPlugin.java +++ b/xplat/src/main/java/dev/emi/emi/VanillaPlugin.java @@ -47,6 +47,7 @@ import dev.emi.emi.api.stack.EmiStack; import dev.emi.emi.api.stack.FluidEmiStack; import dev.emi.emi.api.stack.ItemEmiStack; +import dev.emi.emi.api.stack.ListEmiIngredient; import dev.emi.emi.api.stack.TagEmiIngredient; import dev.emi.emi.api.widget.Bounds; import dev.emi.emi.api.widget.GeneratedSlotWidget; @@ -96,6 +97,7 @@ import dev.emi.emi.runtime.EmiReloadLog; import dev.emi.emi.stack.serializer.FluidEmiStackSerializer; import dev.emi.emi.stack.serializer.ItemEmiStackSerializer; +import dev.emi.emi.stack.serializer.ListEmiIngredientSerializer; import dev.emi.emi.stack.serializer.TagEmiIngredientSerializer; import net.minecraft.block.Block; import net.minecraft.block.BlockState; @@ -214,6 +216,7 @@ public void initialize(EmiInitRegistry registry) { registry.addIngredientSerializer(ItemEmiStack.class, new ItemEmiStackSerializer()); registry.addIngredientSerializer(FluidEmiStack.class, new FluidEmiStackSerializer()); registry.addIngredientSerializer(TagEmiIngredient.class, new TagEmiIngredientSerializer()); + registry.addIngredientSerializer(ListEmiIngredient.class, new ListEmiIngredientSerializer()); registry.addRegistryAdapter(EmiRegistryAdapter.simple(Item.class, EmiPort.getItemRegistry(), EmiStack::of)); registry.addRegistryAdapter(EmiRegistryAdapter.simple(Fluid.class, EmiPort.getFluidRegistry(), EmiStack::of)); diff --git a/xplat/src/main/java/dev/emi/emi/api/stack/ListEmiIngredient.java b/xplat/src/main/java/dev/emi/emi/api/stack/ListEmiIngredient.java index e81efd9c..9193611c 100644 --- a/xplat/src/main/java/dev/emi/emi/api/stack/ListEmiIngredient.java +++ b/xplat/src/main/java/dev/emi/emi/api/stack/ListEmiIngredient.java @@ -105,4 +105,9 @@ public List getTooltip() { tooltip.addAll(ingredients.get(item).copy().setAmount(amount).getTooltip()); return tooltip; } + + @ApiStatus.Internal + public List getIngredients() { + return ingredients; + } } \ No newline at end of file diff --git a/xplat/src/main/java/dev/emi/emi/registry/EmiIngredientSerializers.java b/xplat/src/main/java/dev/emi/emi/registry/EmiIngredientSerializers.java index 6c2bc4b9..af9b6f42 100644 --- a/xplat/src/main/java/dev/emi/emi/registry/EmiIngredientSerializers.java +++ b/xplat/src/main/java/dev/emi/emi/registry/EmiIngredientSerializers.java @@ -64,6 +64,8 @@ public static EmiIngredient deserialize(JsonElement element) { } } type = json.get("type").getAsString(); + } else if (element.isJsonArray()) { + type = "list"; } else { String[] split = element.getAsString().split(":"); type = split[0]; diff --git a/xplat/src/main/java/dev/emi/emi/stack/serializer/ListEmiIngredientSerializer.java b/xplat/src/main/java/dev/emi/emi/stack/serializer/ListEmiIngredientSerializer.java new file mode 100644 index 00000000..84582a3a --- /dev/null +++ b/xplat/src/main/java/dev/emi/emi/stack/serializer/ListEmiIngredientSerializer.java @@ -0,0 +1,76 @@ +package dev.emi.emi.stack.serializer; + +import net.minecraft.util.JsonHelper; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import dev.emi.emi.api.stack.EmiIngredient; +import dev.emi.emi.api.stack.EmiStack; +import dev.emi.emi.api.stack.ListEmiIngredient; +import dev.emi.emi.api.stack.serializer.EmiIngredientSerializer; +import dev.emi.emi.registry.EmiIngredientSerializers; + +import java.util.ArrayList; +import java.util.List; + +public class ListEmiIngredientSerializer implements EmiIngredientSerializer { + @Override + public String getType() { + return "list"; + } + + @Override + public EmiIngredient deserialize(JsonElement element) { + JsonArray ingredientsArray; + long amount; + float chance; + if (element.isJsonObject()) { + JsonObject json = element.getAsJsonObject(); + amount = JsonHelper.getLong(json, "amount", 1); + chance = JsonHelper.getFloat(json, "chance", 1); + ingredientsArray = JsonHelper.getArray(json, "ingredients"); + } else if (element.isJsonArray()) { + ingredientsArray = element.getAsJsonArray(); + amount = 1; + chance = 1; + } else { + return EmiStack.EMPTY; + } + List ingredients = new ArrayList<>(); + for (JsonElement ingredientElement : ingredientsArray) { + ingredients.add(EmiIngredientSerializers.deserialize(ingredientElement)); + } + EmiIngredient ingredient = EmiIngredient.of(ingredients, amount); + if (chance != 1) { + ingredient.setChance(chance); + } + return ingredient; + } + + @Override + public JsonElement serialize(ListEmiIngredient stack) { + if (stack.getAmount() == 1 && stack.getChance() == 1) { + JsonArray array = new JsonArray(); + for (EmiIngredient inner : stack.getIngredients()) { + array.add(EmiIngredientSerializers.serialize(inner)); + } + return array; + } else { + JsonObject json = new JsonObject(); + json.addProperty("type", getType()); + if (stack.getAmount() != 1) { + json.addProperty("amount", stack.getAmount()); + } + if (stack.getChance() != 1) { + json.addProperty("chance", stack.getChance()); + } + JsonArray ingredients = new JsonArray(); + for (EmiIngredient inner : stack.getIngredients()) { + ingredients.add(EmiIngredientSerializers.serialize(inner)); + } + json.add("ingredients", ingredients); + return json; + } + } +}