diff --git a/common/src/main/java/com/gregtechceu/gtceu/api/capability/recipe/ItemRecipeCapability.java b/common/src/main/java/com/gregtechceu/gtceu/api/capability/recipe/ItemRecipeCapability.java index 3836bd6e32..5491f3b462 100644 --- a/common/src/main/java/com/gregtechceu/gtceu/api/capability/recipe/ItemRecipeCapability.java +++ b/common/src/main/java/com/gregtechceu/gtceu/api/capability/recipe/ItemRecipeCapability.java @@ -3,6 +3,7 @@ import com.gregtechceu.gtceu.api.recipe.content.ContentModifier; import com.gregtechceu.gtceu.api.recipe.content.SerializerIngredient; import com.gregtechceu.gtceu.api.recipe.ingredient.SizedIngredient; +import net.fabricmc.fabric.api.recipe.v1.ingredient.CustomIngredient; import net.minecraft.world.item.crafting.Ingredient; import java.util.Arrays; @@ -22,10 +23,7 @@ protected ItemRecipeCapability() { @Override public Ingredient copyInner(Ingredient content) { - if (content instanceof SizedIngredient sizedIngredient) { - return SizedIngredient.copy(sizedIngredient); - } - return SizedIngredient.create(content, 1); + return SizedIngredient.copy(content); } @Override diff --git a/common/src/main/java/com/gregtechceu/gtceu/api/recipe/ingredient/SizedIngredient.java b/common/src/main/java/com/gregtechceu/gtceu/api/recipe/ingredient/SizedIngredient.java index 5e15fa18aa..638c4a68d0 100644 --- a/common/src/main/java/com/gregtechceu/gtceu/api/recipe/ingredient/SizedIngredient.java +++ b/common/src/main/java/com/gregtechceu/gtceu/api/recipe/ingredient/SizedIngredient.java @@ -59,6 +59,11 @@ public static SizedIngredient create(Ingredient inner, int amount) { throw new AssertionError(); } + @ExpectPlatform + public static SizedIngredient create(Ingredient inner) { + throw new AssertionError(); + } + @ExpectPlatform public static SizedIngredient create(TagKey tag, int amount) { throw new AssertionError(); @@ -69,10 +74,13 @@ public static SizedIngredient create(String tag, int amount) { throw new AssertionError(); } - public static SizedIngredient copy(SizedIngredient ingredient) { - var copied = SizedIngredient.create(ingredient.inner, ingredient.amount); - copied.tag = ingredient.tag; - return copied; + public static SizedIngredient copy(Ingredient ingredient) { + if (ingredient instanceof SizedIngredient sizedIngredient) { + var copied = SizedIngredient.create(sizedIngredient.inner, sizedIngredient.amount); + copied.tag = sizedIngredient.tag; + return copied; + } + return SizedIngredient.create(ingredient); } public int getAmount() { diff --git a/fabric/src/main/java/com/gregtechceu/gtceu/api/recipe/ingredient/fabric/SizedIngredientImpl.java b/fabric/src/main/java/com/gregtechceu/gtceu/api/recipe/ingredient/fabric/SizedIngredientImpl.java index 9718c9bdc6..d044a122e7 100644 --- a/fabric/src/main/java/com/gregtechceu/gtceu/api/recipe/ingredient/fabric/SizedIngredientImpl.java +++ b/fabric/src/main/java/com/gregtechceu/gtceu/api/recipe/ingredient/fabric/SizedIngredientImpl.java @@ -5,6 +5,7 @@ import net.fabricmc.fabric.api.recipe.v1.ingredient.CustomIngredient; import net.fabricmc.fabric.api.recipe.v1.ingredient.CustomIngredientSerializer; import net.fabricmc.fabric.api.recipe.v1.ingredient.FabricIngredient; +import net.fabricmc.fabric.impl.recipe.ingredient.CustomIngredientImpl; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.TagKey; @@ -21,7 +22,7 @@ * @date 2023/2/21 * @implNote SizedIngredientImpl */ -public class SizedIngredientImpl extends SizedIngredient implements FabricIngredient { +public class SizedIngredientImpl extends SizedIngredient implements FabricIngredient, CustomIngredient { protected SizedIngredientImpl(Ingredient inner, int amount) { super(inner, amount); @@ -53,10 +54,31 @@ public boolean test(@Nullable ItemStack stack) { return inner.test(stack); } + @Override + public List getMatchingStacks() { + return Arrays.stream(getItems()).toList(); + } + + @Override + public CustomIngredientSerializer getSerializer() { + return Serializer.INSTANCE; + } + + public TagKey getTag() { + return tag; + } + public static SizedIngredient create(Ingredient inner, int amount) { return new SizedIngredientImpl(inner, amount); } + public static SizedIngredient create(Ingredient inner) { + if (inner instanceof CustomIngredientImpl customIngredient && customIngredient.getCustomIngredient() instanceof SizedIngredient sizedIngredient) { + return SizedIngredient.copy(sizedIngredient); + } + return new SizedIngredientImpl(inner, 1); + } + public static SizedIngredient create(TagKey tag, int amount) { return new SizedIngredientImpl(tag, amount); } @@ -66,65 +88,11 @@ public static SizedIngredient create(String tag, int amount) { } @Override - public @Nullable CustomSizedIngredient getCustomIngredient() { - return new CustomSizedIngredient(this); + public @Nullable SizedIngredientImpl getCustomIngredient() { + return this; } - public static SizedIngredient fromJson(JsonObject json) { - return Serializer.INSTANCE.read(json).vanilla; - } - - public static class CustomSizedIngredient implements CustomIngredient { - - public final SizedIngredientImpl vanilla; - - public CustomSizedIngredient(SizedIngredientImpl vanilla) { - this.vanilla = vanilla; - } - - public CustomSizedIngredient(Ingredient inner, int amount) { - this.vanilla = new SizedIngredientImpl(inner, amount); - } - - public CustomSizedIngredient(String tag, int amount) { - this.vanilla = new SizedIngredientImpl(tag, amount); - } - - @Override - public boolean test(ItemStack stack) { - return vanilla.test(stack); - } - - @Override - public List getMatchingStacks() { - return Arrays.stream(vanilla.getItems()).toList(); - } - - @Override - public boolean requiresTesting() { - return true; - } - - @Override - public CustomIngredientSerializer getSerializer() { - return Serializer.INSTANCE; - } - - public int getAmount() { - return vanilla.amount; - } - - public Ingredient getInner() { - return vanilla.inner; - } - - public TagKey getTag() { - return vanilla.tag; - } - } - - - public static class Serializer implements CustomIngredientSerializer { + public static class Serializer implements CustomIngredientSerializer { public static Serializer INSTANCE = new Serializer(); @@ -134,18 +102,18 @@ public ResourceLocation getIdentifier() { } @Override - public CustomSizedIngredient read(JsonObject json) { + public SizedIngredientImpl read(JsonObject json) { int amount = json.get("count").getAsInt(); if (json.has("tag")) { - return new CustomSizedIngredient(json.get("tag").getAsString(), amount); + return new SizedIngredientImpl(json.get("tag").getAsString(), amount); } else { Ingredient inner = Ingredient.fromJson(json.get("ingredient")); - return new CustomSizedIngredient(inner, amount); + return new SizedIngredientImpl(inner, amount); } } @Override - public void write(JsonObject json, CustomSizedIngredient ingredient) { + public void write(JsonObject json, SizedIngredientImpl ingredient) { json.addProperty("count", ingredient.getAmount()); if (ingredient.getTag() != null) { json.addProperty("tag", ingredient.getTag().location().toString()); @@ -155,7 +123,7 @@ public void write(JsonObject json, CustomSizedIngredient ingredient) { } @Override - public CustomSizedIngredient read(FriendlyByteBuf buffer) { + public SizedIngredientImpl read(FriendlyByteBuf buffer) { int amount = buffer.readVarInt(); if (buffer.readBoolean()) { return new SizedIngredientImpl(buffer.readUtf(), amount).getCustomIngredient(); @@ -165,7 +133,7 @@ public CustomSizedIngredient read(FriendlyByteBuf buffer) { } @Override - public void write(FriendlyByteBuf buffer, CustomSizedIngredient ingredient) { + public void write(FriendlyByteBuf buffer, SizedIngredientImpl ingredient) { buffer.writeVarInt(ingredient.getAmount()); if (ingredient.getTag() != null) { buffer.writeBoolean(true); diff --git a/forge/src/main/java/com/gregtechceu/gtceu/api/recipe/ingredient/forge/SizedIngredientImpl.java b/forge/src/main/java/com/gregtechceu/gtceu/api/recipe/ingredient/forge/SizedIngredientImpl.java index ac64871464..e3e03609de 100644 --- a/forge/src/main/java/com/gregtechceu/gtceu/api/recipe/ingredient/forge/SizedIngredientImpl.java +++ b/forge/src/main/java/com/gregtechceu/gtceu/api/recipe/ingredient/forge/SizedIngredientImpl.java @@ -61,6 +61,10 @@ public static SizedIngredient fromJson(JsonObject json) { return SERIALIZER.parse(json); } + public static SizedIngredient create(Ingredient inner) { + return new SizedIngredientImpl(inner, 1); + } + public static final IIngredientSerializer SERIALIZER = new IIngredientSerializer<>() { @Override public @NotNull SizedIngredientImpl parse(FriendlyByteBuf buffer) {