Skip to content

Commit

Permalink
Codec-related changes
Browse files Browse the repository at this point in the history
  • Loading branch information
apple502j committed Apr 12, 2024
1 parent 3dee2c1 commit 08bbc82
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
import java.util.ArrayList;
import java.util.List;

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

Expand Down Expand Up @@ -94,17 +91,6 @@ private NbtCompound getNbt() {
private static class Serializer implements CustomIngredientSerializer<CustomDataIngredient> {
private static final Identifier ID = new Identifier("fabric", "custom_data");

// Supports decoding the NBT as a string as well as the object.
private static final Codec<NbtCompound> NBT_CODEC = Codec.xor(
Codec.STRING, NbtCompound.CODEC
).flatXmap(either -> either.map(s -> {
try {
return DataResult.success(StringNbtReader.parse(s));
} catch (CommandSyntaxException e) {
return DataResult.error(e::getMessage);
}
}, DataResult::success), nbtCompound -> DataResult.success(Either.left(nbtCompound.asString())));

private static final MapCodec<CustomDataIngredient> ALLOW_EMPTY_CODEC = createCodec(Ingredient.ALLOW_EMPTY_CODEC);
private static final MapCodec<CustomDataIngredient> DISALLOW_EMPTY_CODEC = createCodec(Ingredient.DISALLOW_EMPTY_CODEC);

Expand All @@ -118,7 +104,7 @@ private static MapCodec<CustomDataIngredient> createCodec(Codec<Ingredient> ingr
return RecordCodecBuilder.mapCodec(instance ->
instance.group(
ingredientCodec.fieldOf("base").forGetter(CustomDataIngredient::getBase),
NBT_CODEC.fieldOf("nbt").forGetter(CustomDataIngredient::getNbt)
StringNbtReader.NBT_COMPOUND_CODEC.fieldOf("nbt").forGetter(CustomDataIngredient::getNbt)
).apply(instance, CustomDataIngredient::new)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
package net.fabricmc.fabric.impl.transfer;

import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.codecs.RecordCodecBuilder;

import net.minecraft.component.ComponentChanges;
import net.minecraft.component.ComponentMapImpl;
import net.minecraft.item.ItemStack;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
Expand All @@ -32,11 +35,13 @@
import net.fabricmc.fabric.impl.transfer.item.ItemVariantImpl;

public class VariantCodecs {
public static final Codec<ItemVariant> ITEM_CODEC = RecordCodecBuilder.create(instance -> instance.group(
// AIR is valid (for some reason), don't use ItemStack#ITEM_CODEC
private static final Codec<ItemVariant> UNVALIDATED_ITEM_CODEC = RecordCodecBuilder.create(instance -> instance.group(
Registries.ITEM.getEntryCodec().fieldOf("item").forGetter(ItemVariant::getRegistryEntry),
ComponentChanges.CODEC.optionalFieldOf("components", ComponentChanges.EMPTY).forGetter(ItemVariant::getComponents)
).apply(instance, ItemVariantImpl::of)
);
public static final Codec<ItemVariant> ITEM_CODEC = UNVALIDATED_ITEM_CODEC.validate(VariantCodecs::validateComponents);
public static final PacketCodec<RegistryByteBuf, ItemVariant> ITEM_PACKET_CODEC = PacketCodec.tuple(
PacketCodecs.registryEntry(RegistryKeys.ITEM), ItemVariant::getRegistryEntry,
ComponentChanges.PACKET_CODEC, ItemVariant::getComponents,
Expand All @@ -53,4 +58,8 @@ public class VariantCodecs {
ComponentChanges.PACKET_CODEC, FluidVariant::getComponents,
FluidVariantImpl::of
);

private static DataResult<ItemVariant> validateComponents(ItemVariant variant) {
return ItemStack.validateComponents(ComponentMapImpl.create(variant.getItem().getComponents(), variant.getComponents())).map(v -> variant);
}
}

0 comments on commit 08bbc82

Please sign in to comment.