minecraft-src/net/minecraft/world/item/crafting/SimpleCraftingRecipeSerializer.java
2025-07-04 01:41:11 +03:00

34 lines
1.2 KiB
Java

package net.minecraft.world.item.crafting;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
public class SimpleCraftingRecipeSerializer<T extends CraftingRecipe> implements RecipeSerializer<T> {
private final MapCodec<T> codec;
private final StreamCodec<RegistryFriendlyByteBuf, T> streamCodec;
public SimpleCraftingRecipeSerializer(SimpleCraftingRecipeSerializer.Factory<T> constructor) {
this.codec = RecordCodecBuilder.mapCodec(
instance -> instance.group(CraftingBookCategory.CODEC.fieldOf("category").orElse(CraftingBookCategory.MISC).forGetter(CraftingRecipe::category))
.apply(instance, constructor::create)
);
this.streamCodec = StreamCodec.composite(CraftingBookCategory.STREAM_CODEC, CraftingRecipe::category, constructor::create);
}
@Override
public MapCodec<T> codec() {
return this.codec;
}
@Override
public StreamCodec<RegistryFriendlyByteBuf, T> streamCodec() {
return this.streamCodec;
}
@FunctionalInterface
public interface Factory<T extends CraftingRecipe> {
T create(CraftingBookCategory craftingBookCategory);
}
}