From 30a0603da115a11b9795e90c8bc08c71f52edf5b Mon Sep 17 00:00:00 2001 From: Reoseah Date: Sun, 12 Nov 2023 18:40:32 +0100 Subject: [PATCH 1/6] add `Codec`s for `FluidVariant` and `ResourceAmount` --- .../fabricmc/fabric/api/transfer/v1/fluid/FluidVariant.java | 6 ++++++ .../fabric/api/transfer/v1/storage/base/ResourceAmount.java | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/fluid/FluidVariant.java b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/fluid/FluidVariant.java index a43e4d79e0..ca5d137ff7 100644 --- a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/fluid/FluidVariant.java +++ b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/fluid/FluidVariant.java @@ -43,6 +43,12 @@ @ApiStatus.Experimental @ApiStatus.NonExtendable public interface FluidVariant extends TransferVariant { + public static final Codec CODEC = RecordCodecBuilder.create(instance -> + instance.group( + Registries.FLUID.getCodec().fieldOf("fluid").forGetter(FluidVariant::getFluid), + NbtCompound.CODEC.optionalFieldOf("nbt").forGetter(variant -> Optional.ofNullable(variant.getNbt())) + ).apply(instance, (fluid, optionalNbt) -> FluidVariant.of(fluid, optionalNbt.orElse(null)))); + /** * Retrieve a blank FluidVariant. */ diff --git a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/storage/base/ResourceAmount.java b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/storage/base/ResourceAmount.java index f7e2b02dd8..eb82e81ede 100644 --- a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/storage/base/ResourceAmount.java +++ b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/storage/base/ResourceAmount.java @@ -27,4 +27,9 @@ */ @ApiStatus.Experimental public record ResourceAmount (T resource, long amount) { + public static final Codec> FLUID_VARIANT_CODEC = RecordCodecBuilder.create(instance -> + instance.group( + FLUID_VARIANT_CODEC.fieldOf("variant").forGetter(ResourceAmount::resource), + Codec.LONG.fieldOf("amount").forGetter(ResourceAmount::amount) + ).apply(instance, ResourceAmount::new)); } From b50642df04df68da036dacbd9b25306293ccfa27 Mon Sep 17 00:00:00 2001 From: Reoseah Date: Sun, 12 Nov 2023 20:28:39 +0100 Subject: [PATCH 2/6] fix compilation errors.. --- .../fabric/api/transfer/v1/fluid/FluidVariant.java | 7 +++++++ .../api/transfer/v1/storage/base/ResourceAmount.java | 5 ++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/fluid/FluidVariant.java b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/fluid/FluidVariant.java index ca5d137ff7..763236f687 100644 --- a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/fluid/FluidVariant.java +++ b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/fluid/FluidVariant.java @@ -16,6 +16,13 @@ package net.fabricmc.fabric.api.transfer.v1.fluid; +import java.util.Optional; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; + +import net.minecraft.registry.Registries; + import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; diff --git a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/storage/base/ResourceAmount.java b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/storage/base/ResourceAmount.java index eb82e81ede..e168b7874c 100644 --- a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/storage/base/ResourceAmount.java +++ b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/storage/base/ResourceAmount.java @@ -16,7 +16,10 @@ package net.fabricmc.fabric.api.transfer.v1.storage.base; +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; import org.jetbrains.annotations.ApiStatus; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; /** * An immutable object storing both a resource and an amount, provided for convenience. @@ -29,7 +32,7 @@ public record ResourceAmount (T resource, long amount) { public static final Codec> FLUID_VARIANT_CODEC = RecordCodecBuilder.create(instance -> instance.group( - FLUID_VARIANT_CODEC.fieldOf("variant").forGetter(ResourceAmount::resource), + FluidVariant.CODEC.fieldOf("variant").forGetter(ResourceAmount::resource), Codec.LONG.fieldOf("amount").forGetter(ResourceAmount::amount) ).apply(instance, ResourceAmount::new)); } From ba0603917d51f93fcfe4c7bb4e70c039d87601d5 Mon Sep 17 00:00:00 2001 From: Reoseah Date: Sun, 12 Nov 2023 22:21:45 +0100 Subject: [PATCH 3/6] add test for `ResourceAmount.FLUID_VARIANT_CODEC`, which also uses and so tests `FluidVariant.CODEC` --- .../test/transfer/gametests/CodecsTest.java | 46 +++++++++++++++++++ .../src/testmod/resources/fabric.mod.json | 3 +- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 fabric-transfer-api-v1/src/testmod/java/net/fabricmc/fabric/test/transfer/gametests/CodecsTest.java diff --git a/fabric-transfer-api-v1/src/testmod/java/net/fabricmc/fabric/test/transfer/gametests/CodecsTest.java b/fabric-transfer-api-v1/src/testmod/java/net/fabricmc/fabric/test/transfer/gametests/CodecsTest.java new file mode 100644 index 0000000000..55057b638a --- /dev/null +++ b/fabric-transfer-api-v1/src/testmod/java/net/fabricmc/fabric/test/transfer/gametests/CodecsTest.java @@ -0,0 +1,46 @@ +package net.fabricmc.fabric.test.transfer.gametests; + +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import com.mojang.datafixers.util.Pair; +import com.mojang.serialization.DataResult; +import com.mojang.serialization.JsonOps; + +import net.fabricmc.fabric.api.transfer.v1.storage.base.ResourceAmount; + +import net.minecraft.fluid.Fluids; +import net.minecraft.test.GameTest; +import net.minecraft.test.TestContext; + +import net.fabricmc.fabric.api.gametest.v1.FabricGameTest; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; + +public class CodecsTest { + @GameTest(templateName = FabricGameTest.EMPTY_STRUCTURE) + public void testDecode(TestContext context) { + String input = """ + { + "variant": { + "fluid": "minecraft:water", + "nbt": { + "test": 42 + } + }, + "amount": 81000 + } + """; + + JsonElement json = JsonParser.parseString(input); + + DataResult, JsonElement>> result = ResourceAmount.FLUID_VARIANT_CODEC.decode(JsonOps.INSTANCE, json); + context.assertTrue(result.result().isPresent(), "Couldn't decode JSON"); + + ResourceAmount decoded = result.result().get().getFirst(); + context.assertTrue(decoded.resource().getFluid() == Fluids.WATER, "Incorrectly decoded fluid"); + context.assertTrue(decoded.resource().getNbt() != null, "Incorrectly decoded variant NBT"); + context.assertTrue(!decoded.resource().getNbt().isEmpty(), "Incorrectly decoded variant NBT"); + context.assertTrue(decoded.amount() == 81000, "Incorrectly decoded resource amount"); + + context.complete(); + } +} diff --git a/fabric-transfer-api-v1/src/testmod/resources/fabric.mod.json b/fabric-transfer-api-v1/src/testmod/resources/fabric.mod.json index 23b69c62ac..6337e4974c 100644 --- a/fabric-transfer-api-v1/src/testmod/resources/fabric.mod.json +++ b/fabric-transfer-api-v1/src/testmod/resources/fabric.mod.json @@ -18,7 +18,8 @@ ], "fabric-gametest": [ "net.fabricmc.fabric.test.transfer.gametests.VanillaStorageTests", - "net.fabricmc.fabric.test.transfer.gametests.WorldDependentAttributesTest" + "net.fabricmc.fabric.test.transfer.gametests.WorldDependentAttributesTest", + "net.fabricmc.fabric.test.transfer.gametests.CodecsTest" ] }, "mixins": [ From 27ffc31d60cfb5b1cbe50c9124fb7f2bd718e7c2 Mon Sep 17 00:00:00 2001 From: Reoseah Date: Sun, 12 Nov 2023 22:32:31 +0100 Subject: [PATCH 4/6] improve error messages --- .../fabric/test/transfer/gametests/CodecsTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fabric-transfer-api-v1/src/testmod/java/net/fabricmc/fabric/test/transfer/gametests/CodecsTest.java b/fabric-transfer-api-v1/src/testmod/java/net/fabricmc/fabric/test/transfer/gametests/CodecsTest.java index 55057b638a..5cce6d60b9 100644 --- a/fabric-transfer-api-v1/src/testmod/java/net/fabricmc/fabric/test/transfer/gametests/CodecsTest.java +++ b/fabric-transfer-api-v1/src/testmod/java/net/fabricmc/fabric/test/transfer/gametests/CodecsTest.java @@ -36,10 +36,10 @@ public void testDecode(TestContext context) { context.assertTrue(result.result().isPresent(), "Couldn't decode JSON"); ResourceAmount decoded = result.result().get().getFirst(); - context.assertTrue(decoded.resource().getFluid() == Fluids.WATER, "Incorrectly decoded fluid"); - context.assertTrue(decoded.resource().getNbt() != null, "Incorrectly decoded variant NBT"); - context.assertTrue(!decoded.resource().getNbt().isEmpty(), "Incorrectly decoded variant NBT"); - context.assertTrue(decoded.amount() == 81000, "Incorrectly decoded resource amount"); + context.assertTrue(decoded.resource().getFluid() == Fluids.WATER, "Fluid was not water"); + context.assertTrue(decoded.resource().getNbt() != null, "NBT was null"); + context.assertTrue(!decoded.resource().getNbt().isEmpty(), "NBT was empty"); + context.assertTrue(decoded.amount() == 81000, "Amount was not 81000"); context.complete(); } From 8d3de4fb70b1b3940d852fd6dc0f9c684e280a1b Mon Sep 17 00:00:00 2001 From: Reoseah Date: Fri, 17 Nov 2023 21:37:11 +0100 Subject: [PATCH 5/6] move `Codec>>` to `FluidVariant` --- .../fabric/api/transfer/v1/fluid/FluidVariant.java | 11 +++++++++-- .../api/transfer/v1/storage/base/ResourceAmount.java | 8 -------- .../fabric/test/transfer/gametests/CodecsTest.java | 2 +- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/fluid/FluidVariant.java b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/fluid/FluidVariant.java index 763236f687..003247ba02 100644 --- a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/fluid/FluidVariant.java +++ b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/fluid/FluidVariant.java @@ -21,6 +21,7 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.fabricmc.fabric.api.transfer.v1.storage.base.ResourceAmount; import net.minecraft.registry.Registries; import org.jetbrains.annotations.ApiStatus; @@ -50,13 +51,19 @@ @ApiStatus.Experimental @ApiStatus.NonExtendable public interface FluidVariant extends TransferVariant { - public static final Codec CODEC = RecordCodecBuilder.create(instance -> + Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( Registries.FLUID.getCodec().fieldOf("fluid").forGetter(FluidVariant::getFluid), NbtCompound.CODEC.optionalFieldOf("nbt").forGetter(variant -> Optional.ofNullable(variant.getNbt())) ).apply(instance, (fluid, optionalNbt) -> FluidVariant.of(fluid, optionalNbt.orElse(null)))); - /** + Codec> AMOUNT_CODEC = RecordCodecBuilder.create(instance -> + instance.group( + CODEC.fieldOf("variant").forGetter(ResourceAmount::resource), + Codec.LONG.fieldOf("amount").forGetter(ResourceAmount::amount) + ).apply(instance, ResourceAmount::new)); + + /** * Retrieve a blank FluidVariant. */ static FluidVariant blank() { diff --git a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/storage/base/ResourceAmount.java b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/storage/base/ResourceAmount.java index e168b7874c..f7e2b02dd8 100644 --- a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/storage/base/ResourceAmount.java +++ b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/storage/base/ResourceAmount.java @@ -16,10 +16,7 @@ package net.fabricmc.fabric.api.transfer.v1.storage.base; -import com.mojang.serialization.Codec; -import com.mojang.serialization.codecs.RecordCodecBuilder; import org.jetbrains.annotations.ApiStatus; -import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; /** * An immutable object storing both a resource and an amount, provided for convenience. @@ -30,9 +27,4 @@ */ @ApiStatus.Experimental public record ResourceAmount (T resource, long amount) { - public static final Codec> FLUID_VARIANT_CODEC = RecordCodecBuilder.create(instance -> - instance.group( - FluidVariant.CODEC.fieldOf("variant").forGetter(ResourceAmount::resource), - Codec.LONG.fieldOf("amount").forGetter(ResourceAmount::amount) - ).apply(instance, ResourceAmount::new)); } diff --git a/fabric-transfer-api-v1/src/testmod/java/net/fabricmc/fabric/test/transfer/gametests/CodecsTest.java b/fabric-transfer-api-v1/src/testmod/java/net/fabricmc/fabric/test/transfer/gametests/CodecsTest.java index 5cce6d60b9..6f03bd222c 100644 --- a/fabric-transfer-api-v1/src/testmod/java/net/fabricmc/fabric/test/transfer/gametests/CodecsTest.java +++ b/fabric-transfer-api-v1/src/testmod/java/net/fabricmc/fabric/test/transfer/gametests/CodecsTest.java @@ -32,7 +32,7 @@ public void testDecode(TestContext context) { JsonElement json = JsonParser.parseString(input); - DataResult, JsonElement>> result = ResourceAmount.FLUID_VARIANT_CODEC.decode(JsonOps.INSTANCE, json); + DataResult, JsonElement>> result = FluidVariant.AMOUNT_CODEC.decode(JsonOps.INSTANCE, json); context.assertTrue(result.result().isPresent(), "Couldn't decode JSON"); ResourceAmount decoded = result.result().get().getFirst(); From 49f45f9b2201c41f33de914feb20e805c0b0b2aa Mon Sep 17 00:00:00 2001 From: Reoseah Date: Fri, 17 Nov 2023 21:44:11 +0100 Subject: [PATCH 6/6] fix formatting --- .../fabric/api/transfer/v1/fluid/FluidVariant.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/fluid/FluidVariant.java b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/fluid/FluidVariant.java index 003247ba02..941c532f42 100644 --- a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/fluid/FluidVariant.java +++ b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/fluid/FluidVariant.java @@ -53,15 +53,15 @@ public interface FluidVariant extends TransferVariant { Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( - Registries.FLUID.getCodec().fieldOf("fluid").forGetter(FluidVariant::getFluid), - NbtCompound.CODEC.optionalFieldOf("nbt").forGetter(variant -> Optional.ofNullable(variant.getNbt())) - ).apply(instance, (fluid, optionalNbt) -> FluidVariant.of(fluid, optionalNbt.orElse(null)))); + Registries.FLUID.getCodec().fieldOf("fluid").forGetter(FluidVariant::getFluid), + NbtCompound.CODEC.optionalFieldOf("nbt").forGetter(variant -> Optional.ofNullable(variant.getNbt())) + ).apply(instance, (fluid, optionalNbt) -> FluidVariant.of(fluid, optionalNbt.orElse(null)))); Codec> AMOUNT_CODEC = RecordCodecBuilder.create(instance -> - instance.group( - CODEC.fieldOf("variant").forGetter(ResourceAmount::resource), - Codec.LONG.fieldOf("amount").forGetter(ResourceAmount::amount) - ).apply(instance, ResourceAmount::new)); + instance.group( + CODEC.fieldOf("variant").forGetter(ResourceAmount::resource), + Codec.LONG.fieldOf("amount").forGetter(ResourceAmount::amount) + ).apply(instance, ResourceAmount::new)); /** * Retrieve a blank FluidVariant.