package net.minecraft.world.item.crafting;

import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.level.ItemLike;
import net.minecraft.core.Registry;
import net.minecraft.world.item.Item;
import com.google.gson.JsonElement;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonObject;
import net.minecraft.core.NonNullList;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.Container;

public abstract class SingleItemRecipe implements Recipe<Container> {
    protected final Ingredient ingredient;
    protected final ItemStack result;
    private final RecipeType<?> type;
    private final RecipeSerializer<?> serializer;
    protected final ResourceLocation id;
    protected final String group;
    
    public SingleItemRecipe(final RecipeType<?> bgz, final RecipeSerializer<?> bgy, final ResourceLocation sm, final String string, final Ingredient bgt, final ItemStack ben) {
        this.type = bgz;
        this.serializer = bgy;
        this.id = sm;
        this.group = string;
        this.ingredient = bgt;
        this.result = ben;
    }
    
    @Override
    public RecipeType<?> getType() {
        return this.type;
    }
    
    @Override
    public RecipeSerializer<?> getSerializer() {
        return this.serializer;
    }
    
    @Override
    public ResourceLocation getId() {
        return this.id;
    }
    
    @Override
    public String getGroup() {
        return this.group;
    }
    
    @Override
    public ItemStack getResultItem() {
        return this.result;
    }
    
    @Override
    public NonNullList<Ingredient> getIngredients() {
        final NonNullList<Ingredient> fy2 = NonNullList.<Ingredient>create();
        fy2.add(this.ingredient);
        return fy2;
    }
    
    @Override
    public boolean canCraftInDimensions(final int integer1, final int integer2) {
        return true;
    }
    
    @Override
    public ItemStack assemble(final Container aje) {
        return this.result.copy();
    }
    
    public static class Serializer<T extends SingleItemRecipe> implements RecipeSerializer<T> {
        final SingleItemMaker<T> factory;
        
        protected Serializer(final SingleItemMaker<T> a) {
            this.factory = a;
        }
        
        @Override
        public T fromJson(final ResourceLocation sm, final JsonObject jsonObject) {
            final String string4 = GsonHelper.getAsString(jsonObject, "group", "");
            Ingredient bgt5;
            if (GsonHelper.isArrayNode(jsonObject, "ingredient")) {
                bgt5 = Ingredient.fromJson(GsonHelper.getAsJsonArray(jsonObject, "ingredient"));
            }
            else {
                bgt5 = Ingredient.fromJson(GsonHelper.getAsJsonObject(jsonObject, "ingredient"));
            }
            final String string5 = GsonHelper.getAsString(jsonObject, "result");
            final int integer7 = GsonHelper.getAsInt(jsonObject, "count");
            final ItemStack ben8 = new ItemStack(Registry.ITEM.get(new ResourceLocation(string5)), integer7);
            return this.factory.create(sm, string4, bgt5, ben8);
        }
        
        @Override
        public T fromNetwork(final ResourceLocation sm, final FriendlyByteBuf kv) {
            final String string4 = kv.readUtf(32767);
            final Ingredient bgt5 = Ingredient.fromNetwork(kv);
            final ItemStack ben6 = kv.readItem();
            return this.factory.create(sm, string4, bgt5, ben6);
        }
        
        @Override
        public void toNetwork(final FriendlyByteBuf kv, final T bhh) {
            kv.writeUtf(bhh.group);
            bhh.ingredient.toNetwork(kv);
            kv.writeItem(bhh.result);
        }
        
        interface SingleItemMaker<T extends SingleItemRecipe> {
            T create(final ResourceLocation sm, final String string, final Ingredient bgt, final ItemStack ben);
        }
    }
}
