From d7305ae1794895287972f6e8abb8a66faed5cf04 Mon Sep 17 00:00:00 2001 From: Abbie Date: Thu, 21 Nov 2024 15:04:35 +0000 Subject: [PATCH 1/5] add ingredient serialiser for list ingredients --- .../main/java/dev/emi/emi/VanillaPlugin.java | 3 + .../ListEmiIngredientSerializer.java | 60 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 xplat/src/main/java/dev/emi/emi/stack/serializer/ListEmiIngredientSerializer.java 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/stack/serializer/ListEmiIngredientSerializer.java b/xplat/src/main/java/dev/emi/emi/stack/serializer/ListEmiIngredientSerializer.java new file mode 100644 index 00000000..859eef4a --- /dev/null +++ b/xplat/src/main/java/dev/emi/emi/stack/serializer/ListEmiIngredientSerializer.java @@ -0,0 +1,60 @@ +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) { + if (!element.isJsonObject()) { + return EmiStack.EMPTY; + } + JsonObject json = element.getAsJsonObject(); + long amount = JsonHelper.getLong(json, "amount", 1); + float chance = JsonHelper.getFloat(json, "chance", 1); + JsonArray ingredientsArray = JsonHelper.getArray(json, "ingredients"); + List ingredients = new ArrayList<>(); + for (JsonElement ingredientElement : ingredientsArray) { + ingredients.add(EmiIngredientSerializers.deserialize(ingredientElement)); + } + ListEmiIngredient ingredient = new ListEmiIngredient(ingredients, amount); + if (chance != 1) { + ingredient.setChance(chance); + } + return ingredient; + } + + @Override + public JsonElement serialize(ListEmiIngredient stack) { + 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 (EmiStack innerStack : stack.getEmiStacks()) { + ingredients.add(EmiIngredientSerializers.serialize(innerStack)); + } + json.add("ingredients", ingredients); + return json; + } +} From d3c3fe694b8fe2c6e7ef7820fcb6f5d510187b96 Mon Sep 17 00:00:00 2001 From: Abbie Date: Mon, 23 Dec 2024 15:42:43 +0000 Subject: [PATCH 2/5] coerce deserialised list ingredients to tag or single ingredients if applicable --- .../emi/emi/stack/serializer/ListEmiIngredientSerializer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 859eef4a..780fc312 100644 --- a/xplat/src/main/java/dev/emi/emi/stack/serializer/ListEmiIngredientSerializer.java +++ b/xplat/src/main/java/dev/emi/emi/stack/serializer/ListEmiIngredientSerializer.java @@ -33,7 +33,7 @@ public EmiIngredient deserialize(JsonElement element) { for (JsonElement ingredientElement : ingredientsArray) { ingredients.add(EmiIngredientSerializers.deserialize(ingredientElement)); } - ListEmiIngredient ingredient = new ListEmiIngredient(ingredients, amount); + EmiIngredient ingredient = EmiIngredient.of(ingredients, amount); if (chance != 1) { ingredient.setChance(chance); } From 9c35df02a14fd0c5b850560081f708d71fc20f9d Mon Sep 17 00:00:00 2001 From: Abbie Date: Mon, 23 Dec 2024 16:02:29 +0000 Subject: [PATCH 3/5] deserialize plain arrays as list ingredients --- .../emi/registry/EmiIngredientSerializers.java | 2 ++ .../ListEmiIngredientSerializer.java | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) 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 index 780fc312..bc1f6c07 100644 --- a/xplat/src/main/java/dev/emi/emi/stack/serializer/ListEmiIngredientSerializer.java +++ b/xplat/src/main/java/dev/emi/emi/stack/serializer/ListEmiIngredientSerializer.java @@ -22,13 +22,21 @@ public String getType() { @Override public EmiIngredient deserialize(JsonElement element) { - if (!element.isJsonObject()) { + 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; } - JsonObject json = element.getAsJsonObject(); - long amount = JsonHelper.getLong(json, "amount", 1); - float chance = JsonHelper.getFloat(json, "chance", 1); - JsonArray ingredientsArray = JsonHelper.getArray(json, "ingredients"); List ingredients = new ArrayList<>(); for (JsonElement ingredientElement : ingredientsArray) { ingredients.add(EmiIngredientSerializers.deserialize(ingredientElement)); From 97d0bf91ff747e00cad99233e22c0cbc8c1c32ac Mon Sep 17 00:00:00 2001 From: Abbie Date: Mon, 23 Dec 2024 16:20:52 +0000 Subject: [PATCH 4/5] serialise list ingredients with 1 amount and 1 chance to simple arrays --- .../ListEmiIngredientSerializer.java | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) 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 index bc1f6c07..20d99965 100644 --- a/xplat/src/main/java/dev/emi/emi/stack/serializer/ListEmiIngredientSerializer.java +++ b/xplat/src/main/java/dev/emi/emi/stack/serializer/ListEmiIngredientSerializer.java @@ -50,19 +50,27 @@ public EmiIngredient deserialize(JsonElement element) { @Override public JsonElement serialize(ListEmiIngredient stack) { - 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 (EmiStack innerStack : stack.getEmiStacks()) { - ingredients.add(EmiIngredientSerializers.serialize(innerStack)); + if (stack.getAmount() == 1 && stack.getChance() == 1) { + JsonArray array = new JsonArray(); + for (EmiStack innerStack : stack.getEmiStacks()) { + array.add(EmiIngredientSerializers.serialize(innerStack)); + } + 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 (EmiStack innerStack : stack.getEmiStacks()) { + ingredients.add(EmiIngredientSerializers.serialize(innerStack)); + } + json.add("ingredients", ingredients); + return json; } - json.add("ingredients", ingredients); - return json; } } From 661fe8b78753836adb8a627f7dbc5c77dc9f5640 Mon Sep 17 00:00:00 2001 From: Abbie Date: Mon, 23 Dec 2024 16:23:57 +0000 Subject: [PATCH 5/5] serialise list ingredients using internal ingredient list rather than full stack list this can greatly reduce the serialised size of list ingredients that include tag ingredients --- .../java/dev/emi/emi/api/stack/ListEmiIngredient.java | 5 +++++ .../emi/stack/serializer/ListEmiIngredientSerializer.java | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) 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/stack/serializer/ListEmiIngredientSerializer.java b/xplat/src/main/java/dev/emi/emi/stack/serializer/ListEmiIngredientSerializer.java index 20d99965..84582a3a 100644 --- a/xplat/src/main/java/dev/emi/emi/stack/serializer/ListEmiIngredientSerializer.java +++ b/xplat/src/main/java/dev/emi/emi/stack/serializer/ListEmiIngredientSerializer.java @@ -52,8 +52,8 @@ public EmiIngredient deserialize(JsonElement element) { public JsonElement serialize(ListEmiIngredient stack) { if (stack.getAmount() == 1 && stack.getChance() == 1) { JsonArray array = new JsonArray(); - for (EmiStack innerStack : stack.getEmiStacks()) { - array.add(EmiIngredientSerializers.serialize(innerStack)); + for (EmiIngredient inner : stack.getIngredients()) { + array.add(EmiIngredientSerializers.serialize(inner)); } return array; } else { @@ -66,8 +66,8 @@ public JsonElement serialize(ListEmiIngredient stack) { json.addProperty("chance", stack.getChance()); } JsonArray ingredients = new JsonArray(); - for (EmiStack innerStack : stack.getEmiStacks()) { - ingredients.add(EmiIngredientSerializers.serialize(innerStack)); + for (EmiIngredient inner : stack.getIngredients()) { + ingredients.add(EmiIngredientSerializers.serialize(inner)); } json.add("ingredients", ingredients); return json;