Skip to content

Commit

Permalink
fix fabric custom recipe copy issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Yefancy committed Aug 3, 2023
1 parent a884afd commit f73a94a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item> tag, int amount) {
throw new AssertionError();
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -53,10 +54,31 @@ public boolean test(@Nullable ItemStack stack) {
return inner.test(stack);
}

@Override
public List<ItemStack> getMatchingStacks() {
return Arrays.stream(getItems()).toList();
}

@Override
public CustomIngredientSerializer<?> getSerializer() {
return Serializer.INSTANCE;
}

public TagKey<Item> 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<Item> tag, int amount) {
return new SizedIngredientImpl(tag, amount);
}
Expand All @@ -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<ItemStack> 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<Item> getTag() {
return vanilla.tag;
}
}


public static class Serializer implements CustomIngredientSerializer<CustomSizedIngredient> {
public static class Serializer implements CustomIngredientSerializer<SizedIngredientImpl> {

public static Serializer INSTANCE = new Serializer();

Expand All @@ -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());
Expand All @@ -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();
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SizedIngredientImpl> SERIALIZER = new IIngredientSerializer<>() {
@Override
public @NotNull SizedIngredientImpl parse(FriendlyByteBuf buffer) {
Expand Down

0 comments on commit f73a94a

Please sign in to comment.