package net.minecraft.network.protocol.game;

import net.minecraft.network.PacketListener;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.Registry;
import net.minecraft.world.item.crafting.RecipeSerializer;
import java.util.Iterator;
import java.io.IOException;
import net.minecraft.network.FriendlyByteBuf;
import com.google.common.collect.Lists;
import java.util.Collection;
import net.minecraft.world.item.crafting.Recipe;
import java.util.List;
import net.minecraft.network.protocol.Packet;

public class ClientboundUpdateRecipesPacket implements Packet<ClientGamePacketListener> {
    private List<Recipe<?>> recipes;
    
    public ClientboundUpdateRecipesPacket() {
    }
    
    public ClientboundUpdateRecipesPacket(final Collection<Recipe<?>> collection) {
        this.recipes = Lists.newArrayList(collection);
    }
    
    @Override
    public void handle(final ClientGamePacketListener nl) {
        nl.handleUpdateRecipes(this);
    }
    
    @Override
    public void read(final FriendlyByteBuf mg) throws IOException {
        this.recipes = Lists.newArrayList();
        for (int integer3 = mg.readVarInt(), integer4 = 0; integer4 < integer3; ++integer4) {
            this.recipes.add(fromNetwork(mg));
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf mg) throws IOException {
        mg.writeVarInt(this.recipes.size());
        for (final Recipe<?> bmu4 : this.recipes) {
            ClientboundUpdateRecipesPacket.<Recipe<?>>toNetwork(bmu4, mg);
        }
    }
    
    public List<Recipe<?>> getRecipes() {
        return this.recipes;
    }
    
    public static Recipe<?> fromNetwork(final FriendlyByteBuf mg) {
        final ResourceLocation uh2 = mg.readResourceLocation();
        final ResourceLocation uh3 = mg.readResourceLocation();
        final Object o;
        final Object obj;
        return Registry.RECIPE_SERIALIZER.getOptional(uh2).<Throwable>orElseThrow(() -> {
            new IllegalArgumentException("Unknown recipe serializer " + obj);
            return o;
        }).fromNetwork(uh3, mg);
    }
    
    public static <T extends Recipe<?>> void toNetwork(final T bmu, final FriendlyByteBuf mg) {
        mg.writeResourceLocation(Registry.RECIPE_SERIALIZER.getKey(bmu.getSerializer()));
        mg.writeResourceLocation(bmu.getId());
        bmu.getSerializer().toNetwork(mg, bmu);
    }
}
