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 Codecs for FluidVariant and ResourceAmount<FluidVariant> #3419

Open
wants to merge 6 commits into
base: 1.20.2
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@

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.fabricmc.fabric.api.transfer.v1.storage.base.ResourceAmount;
import net.minecraft.registry.Registries;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -43,7 +51,19 @@
@ApiStatus.Experimental
@ApiStatus.NonExtendable
public interface FluidVariant extends TransferVariant<Fluid> {
/**
Codec<FluidVariant> 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<ResourceAmount<FluidVariant>> 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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Pair<ResourceAmount<FluidVariant>, JsonElement>> result = FluidVariant.AMOUNT_CODEC.decode(JsonOps.INSTANCE, json);
context.assertTrue(result.result().isPresent(), "Couldn't decode JSON");

ResourceAmount<FluidVariant> decoded = result.result().get().getFirst();
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();
}
}
3 changes: 2 additions & 1 deletion fabric-transfer-api-v1/src/testmod/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down