Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ingredient serialiser for list ingredients #792

Open
wants to merge 5 commits into
base: 1.21
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions xplat/src/main/java/dev/emi/emi/VanillaPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,9 @@ public List<TooltipComponent> getTooltip() {
tooltip.addAll(ingredients.get(item).copy().setAmount(amount).getTooltip());
return tooltip;
}

@ApiStatus.Internal
public List<? extends EmiIngredient> getIngredients() {
return ingredients;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ListEmiIngredient> {
@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<EmiIngredient> 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;
}
}
}
Loading