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

Fix custom ingredient serialization with allowEmpty #3389

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ public class CustomIngredientImpl extends Ingredient {
serializer -> DataResult.success(serializer.getIdentifier())
);

public static final Codec<CustomIngredient> ALLOW_EMPTY_INGREDIENT_CODECS = CODEC.dispatch(TYPE_KEY, CustomIngredient::getSerializer, serializer -> serializer.getCodec(true));
public static final Codec<CustomIngredient> DISALLOW_EMPTY_INGREDIENT_CODECS = CODEC.dispatch(TYPE_KEY, CustomIngredient::getSerializer, serializer -> serializer.getCodec(false));

public static void registerSerializer(CustomIngredientSerializer<?> serializer) {
Objects.requireNonNull(serializer.getIdentifier(), "CustomIngredientSerializer identifier may not be null.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package net.fabricmc.fabric.mixin.recipe.ingredient;

import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -25,6 +26,7 @@
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.Ingredient;
import net.minecraft.util.Identifier;
import net.minecraft.util.dynamic.Codecs;

import net.fabricmc.fabric.api.recipe.v1.ingredient.CustomIngredient;
import net.fabricmc.fabric.api.recipe.v1.ingredient.CustomIngredientSerializer;
Expand All @@ -35,9 +37,18 @@
public class IngredientMixin implements FabricIngredient {
@Inject(method = "createCodec", at = @At("RETURN"), cancellable = true)
private static void injectCodec(boolean allowEmpty, CallbackInfoReturnable<Codec<Ingredient>> cir) {
final Codec<CustomIngredient> customIngredientCodec = allowEmpty ? CustomIngredientImpl.ALLOW_EMPTY_INGREDIENT_CODECS : CustomIngredientImpl.DISALLOW_EMPTY_INGREDIENT_CODECS;
Codec<Ingredient> ingredientCodec = customIngredientCodec.xmap(CustomIngredient::toVanilla, FabricIngredient::getCustomIngredient);
cir.setReturnValue(CustomIngredientImpl.first(cir.getReturnValue(), ingredientCodec));
Technici4n marked this conversation as resolved.
Show resolved Hide resolved
Codec<CustomIngredient> customIngredientCodec = CustomIngredientImpl.CODEC.dispatch(
CustomIngredientImpl.TYPE_KEY,
CustomIngredient::getSerializer,
serializer -> serializer.getCodec(allowEmpty));

cir.setReturnValue(Codecs.either(customIngredientCodec, cir.getReturnValue()).xmap(
either -> either.map(CustomIngredient::toVanilla, ingredient -> ingredient),
ingredient -> {
CustomIngredient customIngredient = ingredient.getCustomIngredient();
return customIngredient == null ? Either.right(ingredient) : Either.left(customIngredient);
}
));
}

@Inject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;

import net.minecraft.item.Items;
Expand All @@ -31,7 +32,7 @@
import net.minecraft.util.Util;

import net.fabricmc.fabric.api.gametest.v1.FabricGameTest;
import net.fabricmc.fabric.impl.recipe.ingredient.builtin.AllIngredient;
import net.fabricmc.fabric.api.recipe.v1.ingredient.DefaultCustomIngredients;

public class SerializationTests {
/**
Expand Down Expand Up @@ -64,19 +65,28 @@
}

/**
* Check that we can serialise a custom ingredient.
* Check that we can serialise and deserialize a custom ingredient.
*/
@GameTest(templateName = FabricGameTest.EMPTY_STRUCTURE)
public void testCustomIngredientSerialization(TestContext context) {
String ingredientJson = """
{"ingredients":[{"item":"minecraft:stone"}],"fabric:type":"fabric:all"}
""".trim();
for (boolean allowEmpty : List.of(false, true)) {
String ingredientJson = """
{"ingredients":[{"item":"minecraft:stone"}],"fabric:type":"fabric:all"}
""".trim();

var ingredient = new AllIngredient(List.of(
Ingredient.ofItems(Items.STONE)
));
String json = ingredient.toVanilla().toJson(false).toString();
context.assertTrue(json.equals(ingredientJson), "Unexpected json: " + json);
var ingredient = DefaultCustomIngredients.all(

Check failure on line 77 in fabric-recipe-api-v1/src/testmod/java/net/fabricmc/fabric/test/recipe/ingredient/SerializationTests.java

View workflow job for this annotation

GitHub Actions / build (17-ubuntu)

Illegal code structure detected.
Technici4n marked this conversation as resolved.
Show resolved Hide resolved
Ingredient.ofItems(Items.STONE)
);
JsonElement json = ingredient.toJson(allowEmpty);
context.assertTrue(json.toString().equals(ingredientJson), "Unexpected json: " + json);
// Make sure that we can deserialize it
Codec<Ingredient> ingredientCodec = allowEmpty ? Ingredient.ALLOW_EMPTY_CODEC : Ingredient.DISALLOW_EMPTY_CODEC;
Ingredient deserialized = Util.getResult(
ingredientCodec.parse(JsonOps.INSTANCE, json), JsonParseException::new
);
context.assertTrue(deserialized.getCustomIngredient() != null, "Custom ingredient was not deserialized");
context.assertTrue(deserialized.getCustomIngredient().getSerializer() == ingredient.getCustomIngredient().getSerializer(), "Serializer did not match");
}

Check failure on line 89 in fabric-recipe-api-v1/src/testmod/java/net/fabricmc/fabric/test/recipe/ingredient/SerializationTests.java

View workflow job for this annotation

GitHub Actions / build (17-ubuntu)

missing blank line after block at same indentation level
context.complete();
}
}
Loading